4

Google Closure Compiler addes "use strict" to result file when --language_out is set to ECMASCRIPT_2015

For some unknown reason, when setting the "--language_out ECMASCRIPT_2015" param, the program forces the use of strict mode and adds “use strict” at the beginning of the result file.

Neither "--jscomp_off es5Strict" no any undocumented params help. It looks like GCC in ECMASCRIPT_2015-compatible mode uses "strict mode" as default.

Anybody knows how to turn it off?

I use the last GCC version for java via bat-file command:

java -jar %USERPROFILE%\OneDrive\Portable\ClosureCompiler\cc.jar --js %1.%2 --js_output_file %1.min.%2 --language_in ECMASCRIPT_2015 --language_out ECMASCRIPT_2015 --jscomp_off es5Strict
Nisbor
  • 41
  • 1
  • Are you sure it's not related to `--language_in ECMASCRIPT_2015` rather? Notice that [ES6 module code is always strict](https://stackoverflow.com/a/29285330/1048572). – Bergi Nov 03 '19 at 15:52
  • May be it causes too. I need to explain: I began to use es6 structures in my code and GCC started to add to minified files $jscomp warning system code. Twice it caused me bugs in tested before code - only in minificated files. I suppose there should not be need testing both original and minified code separately... The "--jscomp_off es5Strict", the "--jscomp_off=*" don't work to me. Then I tried to turn on es6 mode - and faced to described problem that it suggests only strict mode in output file. I did not see that es6 requires mode to be strict - it is only an option... – Nisbor Nov 04 '19 at 17:40
  • In ES6 *modules*, it is enforced. In normal scripts, it's still opt-in for backward compat. Do you use `import` and `export` syntax? Then you use strict mode. – Bergi Nov 04 '19 at 18:31
  • Btw, what's the problem with using it? What sloppy mode features do you need? – Bergi Nov 04 '19 at 18:32
  • switched to use terser (( – Nisbor Nov 05 '19 at 13:33

2 Answers2

3

There is now an option for this, --emit_use_strict=false

Alon Zakai
  • 1,038
  • 5
  • 4
0

If you are developing and working with Google Closure Compiler, you can disable strict mode with the following code:

CompilerOptions compilerOptions = new CompilerOptions();
compilerOptions.setEmitUseStrict(false);

List<SourceFile> externs = CommandLineRunner.getDefaultExterns();
compiler.compile(externs, Lists.newArrayList(input), compilerOptions);
Elyas Hadizadeh
  • 3,289
  • 8
  • 40
  • 54