Search code examples
es6-modulesgoogle-closure-compiler

Closure compiler generates very long variable names in output


I am using the Closure compiler to compile a JavaScript file together with a couple of very simple ES6 modules.

demo.js

import {add} from "../modules/adder.js";
import {subtract} from "../modules/subtractor.js";
console.log(add(6, 5));
console.log(subtract(6, 5));

adder.js

export function add(num1, num2) {
    return num1 + num2;
}

subtractor.js

export function subtract(num1, num2) {
    return num1 - num2;
}

The compiler output JavaScript file is a large file (larger than the inputs) and contains very long variable names such as the following example:

add$$module$Users$mysername$IdeaProjects$myappdir$web$events$es$modules$adder

Is this normal?

I am not concerned about the file size, but if this code is used in production, it is unnecessarily exposing my development machine file structure, including my username.


Solution

  • There is a command line option --js_module_root that can be used to specify the path prefixes to be removed from ES6 modules. If used, the variable names will be shorter and will go only up to that path.