Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use @define inside ES6 modules #3642

Closed
ctjlewis opened this issue Jul 16, 2020 · 1 comment
Closed

Cannot use @define inside ES6 modules #3642

ctjlewis opened this issue Jul 16, 2020 · 1 comment

Comments

@ctjlewis
Copy link
Contributor

ctjlewis commented Jul 16, 2020

Wanted to make sure there was an issue open here regarding this - was unsure if this was a bug until I saw Chad's comments on this SO thread.

EDIT: I found a few closed issues on this after searching, but it seems like the solution is more of a workaround involving goog.module and goog.define, and I would still consider this to be an open issue (though please feel free to close this if you disagree).

Why it's important to fix

ES6 modules better adhere to DRY principles by allowing developers to export functions with a single keyword, instead of repeatedly redefining everything in module.exports at the bottom of the file. Having just rewritten a codebase into an ES6 module, I was sad to see the only thing preventing it from compiling was the use of a @define flag (the PRODUCTION flag for release builds, which I assume is the most common use case).

The annotation works just fine for CommonJS, but add an import statement and it will stop injecting the variable and display a warning. There's a ready-to-go example repository here, just run ./run.sh.

lib/index.js

/**
 * Should be overridden to 42 at compile-time with --define flag.
 * 
 * @define {number}
 */
const TEST_NUMBER = 0;

command

google-closure-compiler \
  -O ADVANCED \
  --process_common_js_modules \
  --module_resolution NODE \
  --js lib/**.js \
  --define TEST_NUMBER=42

output

WARNING - [JSC_UNKNOWN_DEFINE_WARNING] unknown @define variable TEST_NUMBER

0 error(s), 1 warning(s), 100.0% typed
console.log({a:0,b:"HELLO WORLD"});
@ctjlewis
Copy link
Contributor Author

ctjlewis commented Jul 16, 2020

Closing this because it's a duplicate of #1601 and #2872 and this workaround seems to at least get something to compile as expected. I updated the repo with a workaround.sh to use @shicks goog.define method and produce meaningful output, old test moved to ./test.sh. As others in the original issue said, @define and -D flags are good to be properly global and overwrite wherever, as the largest use case is likely just for PRODUCTION or RELEASE flags.

For now, replacing a compiler call like google-closure-compiler --define TEST_NUMBER=42 and source like:

/**
 * Should be overridden to 42 at compile-time with --define flag.
 * 
 * @define {number}
 */
const TEST_NUMBER = 0;

with google-closure-compiler --define Test.Testing.TestNumber=42 and source like:

/**
 * Should be overridden to 42 at compile-time with --define flag.
 * 
 * @define {number}
 */
const TEST_NUMBER = goog.define('Test.Testing.TestNumber', 0);

Will overwrite your variables at compile-time as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant