【问题标题】:Shared Polyfills in Google Closure CompilerGoogle Closure Compiler 中的共享 Polyfill
【发布时间】:2017-10-04 01:56:49
【问题描述】:

我正在尝试实现以下用例:

  • 我的代码库被拆分为 3 个 JavaScript 文件:A、B 和 C
  • 文件 B 和 C 都依赖于文件 A(这意味着它们是例如调用在 A 中声明的方法)
  • 所有文件都使用 ES2015 (ES6) 代码

我正在使用 Google Closure Compiler 将它们转换为 ES5 并缩小它们。与以下 NPM 模块 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"
            }
        }

但我想优化它。上面的配置导致 Closure Compiler 在需要的地方向所有 3 个文件“注入”相同的 polyfill(例如,如果 A 和 C 使用 Wea​​kMap,则 A.min 和 C.min 都包含 WeakMap 的 polyfill)。

我的问题是,有没有办法强制编译器只将常见的 polyfill 放在 A 中?比如如果 B 或 C 使用 Wea​​kMap,那么如果 A 使用 Wea​​kMap,则将 polyfill 放入 A。如果 A 未使用 Wea​​kMap,则像往常一样将 polyfill 放入 B 和 C?

【问题讨论】:

    标签: javascript google-closure-compiler transpiler


    【解决方案1】:

    最好的选择是使用编译器的代码拆分功能进行一次编译。 polyfills 应该只被注入到基础文件中。有关示例,请参阅How do I split my javascript into modules using Google's Closure Compiler?

    如果您想继续使用单独的编译,有两个未公开的标志可以提供帮助:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      • 2011-05-12
      • 1970-01-01
      • 2019-09-27
      • 2016-08-15
      • 2010-12-14
      • 2012-04-21
      相关资源
      最近更新 更多