google / google/closure-compiler

$jscomp undefined when using skipNonTranspilationPasses (missing es6_runtime)

Open
#1,812 2 comments 1 reaction 1 assignee Claimed by @MatrixFrog View on GitHub
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

With classes (this is a part- ES6 module example, but it makes no difference):

``` javascript
goog.module("A");
goog.module.declareLegacyNamespace(); // #1778
class A {}
exports = A;

import A from "goog:A";
class B extends A {}
```

When compiling normally this works as expected, injecting the ES6 runtime and defining `$jscomp` correctly. [Debugger](https://closure-compiler-debugger.appspot.com/#input0%3Dgoog.module%28%2522A%2522%29%253B%250Agoog.module.declareLegacyNamespace%28%29%253B%2520%252F%252F%2520%25231778%250A%250Aclass%2520A%2520%257B%250A%257D%250A%250Aexports%2520%253D%2520A%253B%250A%26input1%3Dimport%2520A%2520from%2520%2522goog%253AA%2522%253B%250A%250Aclass%2520B%2520extends%2520A%2520%257B%250A%257D%250A%26conformanceConfig%26externs%26refasterjs-template%26includeDefaultExterns%3D1%26CLOSURE_PASS%3D1%26LANG_IN_IS_ES6%3D1%26PRETTY_PRINT%3D1%26TRANSPILE%3D1).

When compiling with `skipNonTranspilationPasses=true`, the `extends` clause leads to `$jscomp.inherits(...)` being written out, but `$jscomp` has not been defined (es6_runtime hasn't been injected), leading to a runtime error. [Debugger](https://closure-compiler-debugger.appspot.com/#input0%3Dgoog.module%28%2522A%2522%29%253B%250Agoog.module.declareLegacyNamespace%28%29%253B%2520%252F%252F%2520%25231778%250Aclass%2520A%2520%257B%257D%250Aexports%2520%253D%2520A%253B%250A%26input1%3Dimport%2520A%2520from%2520%2522goog%253AA%2522%253B%250Aclass%2520B%2520extends%2520A%2520%257B%257D%250A%26conformanceConfig%26externs%26refasterjs-template%26includeDefaultExterns%3D1%26CLOSURE_PASS%3D1%26LANG_IN_IS_ES6%3D1%26PRETTY_PRINT%3D1%26SKIP_NON_TRANSPILATION_PASSES%3D1%26TRANSPILE%3D1)

``` javascript
goog.require("A");
var B$$module$input1 = function(var_args) {
A.apply(this, arguments);
};
$jscomp.inherits(B$$module$input1, A);
```

With rewrite polyfills:

I've also found that if you have both `rewritePolyfills=true` and `skipNonTranspilationPasses=true`, the compiler injects the polyfills but without es6_runtime, with the same effect, `$jscomp` undefined error at runtime. I think this is wrong, and it should either force es6_runtime, or honour skipNonTranspilationPasses.

Background: I'm having fun with ES6 modules in Closure Compiler, but need a rapid development mode similar to WHITESPACE_ONLY but which also does ES6 module conversion. The closest I can find is transpiling (lang out ES3, which I mostly don't need for Chrome) but skipping non-transpilation passes (which, aside from this issue, works beautifully, giving me a build time many times faster even than SIMPLE_OPTIMIZATIONS with optimisations disabled).

I've looked in the code and could probably put together a PR, if given some pointers as to the desired approach.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.