返回介绍

ClosureWebpackPlugin

发布于 2019-05-27 04:54:31 字数 3228 浏览 1067 评论 0 收藏 0

npm version

Closure-Compiler is a full optimizing compiler and transpiler. It offers unmatched optimizations, provides type checking and can easily target transpilation to different versions of ECMASCRIPT.

Note: This plugin is a very early beta and currently uses a custom build of closure-compiler while neccessary changes are integrated back into the main compiler repository. Only the java version of closure-compiler is currently supported.

Usage example

const ClosureCompilerPlugin = require('closure-webpack-plugin');

new ClosureCompilerPlugin({mode: 'STANDARD'}, {
  // compiler flags here
  //
  // for debuging help, try these:
  //
  // formatting: 'PRETTY_PRINT'
  // debug: true
})

Options

  • mode - STANDARD (default) or AGGRESSIVE_BUNDLE. Controls how the plugin utilizes the compiler.
    In STANDARD mode, closure-compiler is used as a direct replacement for other minifiers as well as most Babel transformations.
    In AGGRESSIVE_BUNDLE mode, the compiler performs additional optimizations of modules to produce a much smaller file, but is not compatible with all input modules.

Compiler Flags

The plugin controls certain compiler flags. The following flags should not be used in any mode:

  • module_resolution - A custom resolution mode for webpack is utilized instead of the standard NODE or BROWSER options.
  • output_wrapper - The output wrapper is automatically added by either webpack or the plugin
  • dependency_mode - Controlled by the plugin mode.

Aggressive Bundle Mode

In this mode, the compiler rewrites CommonJS modules and hoists require calls. Some modules are not compatible with this type of rewritting. In particular, hoisting will cause the following code to execute out of order:

const foo = require('foo');
addPolyfillToFoo(foo);
const bar = require('bar');

Aggressive Bundle Mode utilizes a custom runtime in which modules within a chunk file are all included in the same scope. This avoids the cost of small modules.

In Aggressive Bundle Mode, a file can only appear in a single output chunk. Use the Commons Chunk Plugin to split duplicated files into a single output chunk.

Tips for Use

  • Don't use babel - closure-compiler is also a transpiler. If you need features not yet supported by closure-compiler, have babel only target those features.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文