closure-compiler icon indicating copy to clipboard operation
closure-compiler copied to clipboard

Incorrect JSC_UNDEFINED_VARIABLE when process_common_js_modules and dependency_mode=NONE

Open WearyMonkey opened this issue 4 years ago • 2 comments

// 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';

WearyMonkey avatar Aug 10 '21 07:08 WearyMonkey

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 = /\/*/;

lvelden avatar Aug 11 '21 14:08 lvelden

@ChadKillingsworth am I right in thinking that you cannot use ES module syntax when passing --process_common_js_modules to the compiler?

brad4d avatar Aug 11 '21 16:08 brad4d