-4

I am trying to optimize a command-line Javascript tool with the google closure compiler.

I have the following Javascript source:

// main.js
console.log("test");
process.chdir("/");
console.log("arg: " + JSON.stringify(process.argv));

My flags file is the following (closure.conf):

--compilation_level=ADVANCED
--env CUSTOM
--language_in=ECMASCRIPT5_STRICT
--language_out=ECMASCRIPT5_STRICT
--warning_level=VERBOSE
--error_format=STANDARD
--strict_mode_input
--dependency_mode=NONE
--rewrite_polyfills=false
--module_resolution=NODE
--jscomp_off=checkVars
--package_json_entry_names es2015
--process_common_js_modules

I am compiling it with the command:

java -jar path/to/closure/compiler.jar --flagfile ./closure.conf --js main.js --js_output_file bundle.js

Compiling with the google closure compiler, I get this (in bundle.js):

'use strict';process.b("/");JSON.stringify(process.a);

As I can see, the closure compiler somehow thinks the process as an undefined, external class whose methods can be easily tuned/optimized.

But it is bad - process is an external resource provided by the node.js environment and its properties can be freely renamed. Furthermore, console can't be simply optimized out.

I think some way should exist, in which we can instruct for the closure compiler to know the command line node environment, and to treat its global entities as it should be.

The same should be done with similar external libraries. I am thinking on something like an "import" in Java, or the header files in C/C++.

How could I do this? As far I know, closure has its own API libraries, which are independent from the command line node API.

peterh
  • 11,875
  • 18
  • 85
  • 108

1 Answers1

1

Externs is what you want. AFAIK Closure Compiler's purpose is to make code smaller (for the browser), not faster.

Ben West
  • 4,398
  • 1
  • 16
  • 16
  • Thanks! It is only a minimal example, the actual code I want to optimize is far bigger. I also suspect, at least some speed improvement will also happen, but to measure it, first I had to compile my tool :-) Another possible reason is code obfuscation. – peterh May 17 '18 at 18:09