1

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.

stepanian
  • 11,373
  • 8
  • 43
  • 63
  • 1
    Looks like a very simple way to avoid name collisions between the modules, and is a fine approach if the resulting file is expected to be minified afterwards anyway. About exposing the file structure of your machine: that's weird, I would expect the tool to somehow recognise the project root directory. – Bergi May 29 '21 at 22:01
  • @Bergi Yeah, that's what I thought, it's basically namespacing to avoid conflict with other global JavaScript names. But, the $Users$myusername part concerns me. – stepanian May 29 '21 at 22:05
  • First guess is that you're missing on a couple of flags and whether you use simple _vs_ advanced optimisations. Have a look at my [Q&A](https://stackoverflow.com/q/66018630/1244884) it may or may not help. – customcommander Jun 11 '21 at 11:01

1 Answers1

1

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.

stepanian
  • 11,373
  • 8
  • 43
  • 63