1

I'm trying to achieve following use case:

  • my codebase is split to 3 JavaScript files: A, B and C
  • files B and C both depends on file A (meaning they are e.g. calling methods declared in A)
  • all files are using ES2015 (ES6) code

I'm using Google Closure Compiler to transpile them to ES5 and minify them. Which works fine with following configuration of NPM module google-closure-compiler:

default: {
            files: {
              "A.min.js" : "A.js",
              "B.min.js" : "B.js",
              "C.min.js" : "C.js",
            },
            options: {
                compilation_level: "SIMPLE",
                language_in: "ES6_STRICT",
                language_out: "ES5_STRICT"
            }
        }

But I'd like to optimize it. The configuration above causes that Closure Compiler 'injects' the same polyfill to all 3 files where needed (e.g. if A and C are using WeakMap, then both A.min and C.min contain polyfill for WeakMap).

My question is, is there a way to force the compiler to put common polyfills in only A? Something like if B or C are using WeakMap, then if A is using WeakMap, put polyfill to A. If A is not using WeakMap, put polyfills to B and C as usual?

Jeremy
  • 1
  • 85
  • 340
  • 366
ladar
  • 5,858
  • 3
  • 26
  • 38

1 Answers1

1

The best option is to use the code splitting functionality of the compiler to have a single compilation. The polyfills should only be injected into the base file then. See How do I split my javascript into modules using Google's Closure Compiler? for examples.

If you want to keep using separate compilations, there are two non-publicized flags that can help:

Community
  • 1
  • 1
Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57