closure-compiler
closure-compiler copied to clipboard
Incorrect JSC_UNDEFINED_VARIABLE when process_common_js_modules and dependency_mode=NONE
// a.js
import * as b from './b.js';
console.log(b.x);
// b.js
const x = /\/*/;
export { x };
java -jar compiler.jar \
--compilation_level=ADVANCED \
--dependency_mode NONE \
--process_common_js_modules \
--js ./b.js \
--js ./a.js
Expected output:
console.log(/\/*/);
Actual output:
./a.js:2:14: ERROR - [JSC_UNDEFINED_VARIABLE] variable x$$module$b is undeclared
2| console.log(b.x);
compiler.jar --version:
Closure Compiler (http://github.com/google/closure-compiler)
Version: v20210601
The following variants work as expected:
export const x = /\/*/;
const x = /.*/;
const x = /\//;
import { x } from './b.js';
I couldn't find proper documentation on this, but I think this is due to how CommonJS modules syntax works. Changing your example to the following works with
$ google-closure-compiler -O ADVANCED --dependency_mode NONE --process_common_js_modules --js ./b.js --js ./a.js
a.js:
const b = require('./b.js');
console.log(b.x);
b.js:
exports.x = /\/*/;
@ChadKillingsworth am I right in thinking that you cannot use ES module syntax when passing --process_common_js_modules to the compiler?