UNPKG

7.68 kBMarkdownView Raw
1# closure-webpack-plugin
2
3[![npm version](https://badge.fury.io/js/closure-webpack-plugin.svg)](https://badge.fury.io/js/closure-webpack-plugin)
4
5This plugin supports the use of Google's Closure Tools with webpack.
6
7**Note: This is the webpack 4 branch.**
8
9[Closure-Compiler](https://developers.google.com/closure/compiler/) is a full optimizing compiler and transpiler.
10It offers unmatched optimizations, provides type checking and can easily target transpilation to different versions of ECMASCRIPT.
11
12[Closure-Library](https://developers.google.com/closure/library/) is a utility library designed for full compatibility
13with Closure-Compiler.
14
15## Older Versions
16
17For webpack 3 support, see https://github.com/webpack-contrib/closure-webpack-plugin/tree/webpack-3
18
19## Install
20
21You must install both the google-closure-compiler package as well as the closure-webpack-plugin.
22
23```
24npm install --save-dev closure-webpack-plugin google-closure-compiler
25```
26
27## Usage example
28
29```js
30const ClosurePlugin = require('closure-webpack-plugin');
31
32module.exports = {
33 optimization: {
34 minimizer: [
35 new ClosurePlugin({mode: 'STANDARD'}, {
36 // compiler flags here
37 //
38 // for debugging help, try these:
39 //
40 // formatting: 'PRETTY_PRINT'
41 // debug: true,
42 // renaming: false
43 })
44 ]
45 }
46};
47```
48
49## Options
50
51 * **platform** - `native`, `java` or `javascript`. Controls which version to use of closure-compiler.
52 By default the plugin will attempt to automatically choose the fastest option available.
53 - `JAVASCRIPT` does not require the JVM to be installed. Not all flags are supported.
54 - `JAVA` utilizes the jvm. Utilizes multiple threads for parsing and results in faster compilation for large builds.
55 - `NATIVE` only available on linux or MacOS. Faster compilation times without requiring a JVM.
56 * **mode** - `STANDARD` (default) or `AGGRESSIVE_BUNDLE`. Controls how the plugin utilizes the compiler.
57 - `STANDARD` mode, closure-compiler is used as a direct replacement for other minifiers as well as most Babel transformations.
58 - `AGGRESSIVE_BUNDLE` mode, the compiler performs additional optimizations of modules to produce a much smaller file
59 * **childCompilations** - boolean or function. Defaults to `false`.
60 In order to decrease build times, this plugin by default only operates on the main compilation.
61 Plugins such as extract-text-plugin and html-webpack-plugin run as child compilations and
62 usually do not need transpilation or minification. You can enable this for all child compilations
63 by setting this option to `true`. For specific control, the option can be set to a function which
64 will be passed a compilation object.
65 Example: `function(compilation) { return /html-webpack/.test(compilation.name); }`.
66 * **output** - An object with either `filename` or `chunkfilename` properties. Used to override the
67 output file naming for a particular compilation. See https://webpack.js.org/configuration/output/
68 for details.
69 * **test** - An optional string or regular expression to determine whether a chunk is included in the compilation
70 * **extraCommandArgs** - Optional string or Array of strings to pass to the google-closure-compiler plugin.
71 Can be used to pass flags to the java process.
72
73## Compiler Flags
74
75The plugin controls several compiler flags. The following flags should not be used in any mode:
76
77 * module_resolution
78 * output_wrapper
79 * dependency_mode
80 * create_source_map
81 * module
82 * entry_point
83
84## Aggressive Bundle Mode
85
86In this mode, the compiler rewrites CommonJS modules and hoists require calls. Some modules are not compatible with this type of rewriting. In particular, hoisting will cause the following code to execute out of order:
87
88```js
89const foo = require('foo');
90addPolyfillToFoo(foo);
91const bar = require('bar');
92```
93
94Aggressive Bundle Mode utilizes a custom runtime in which modules within a chunk file are all included in the same scope.
95This avoids [the cost of small modules](https://nolanlawson.com/2016/08/15/the-cost-of-small-modules/).
96
97In Aggressive Bundle Mode, a file can only appear in a single output chunk. Use the [Split Chunks Plugin](https://webpack.js.org/plugins/split-chunks-plugin/)
98to split duplicated files into a single output chunk. If a module is utilized by more than one chunk, the
99plugin will move it up to the first common parent to prevent code duplication.
100
101The [concatenatedModules optimization](https://webpack.js.org/configuration/optimization/#optimization-concatenatemodules)
102is not compatible with this mode since Closure-Compiler performs an equivalent optimization).
103The plugin will emit a warning if this optimization is not disabled.
104
105## Multiple Output Languages
106
107You can add the plugin multiple times. This easily allows you to target multiple output languages.
108Use `ECMASCRIPT_2015` for modern browsers and `ECMASCRIPT5` for older browsers.
109
110Use the `output` option to change the filenames of specific plugin instances.
111
112Use `<script type="module" src="es6_out_path.js">` to target modern browsers and
113`<script nomodule src="es5_out_path.js">` for older browsers.
114
115See the [es5 and es6 output demo](https://github.com/webpack-contrib/closure-webpack-plugin/tree/master/demo/es5-and-es6)
116for an example.
117
118## Other tips for Use
119 * Don't use babel at the same time - closure-compiler is also a transpiler.
120 If you need [features not yet supported](https://github.com/google/closure-compiler/wiki/ECMAScript6) by closure-compiler, have babel
121 only target those features. Closure Compiler can transpile async/await - you don't need babel for that functionality either.
122
123# Closure Library Plugin
124In order for webpack to recognize `goog.require`, `goog.provide`, `goog.module` and related primitives,
125a separate plugin is shipped.
126
127```js
128const ClosurePlugin = require('closure-webpack-plugin');
129
130module.exports = {
131 plugins: [
132 new ClosurePlugin.LibraryPlugin({
133 closureLibraryBase: require.resolve(
134 'google-closure-library/closure/goog/base'
135 ),
136 deps: [
137 require.resolve('google-closure-library/closure/goog/deps'),
138 './public/deps.js',
139 ],
140 })
141 ]
142};
143```
144The plugin adds extra functionality to support using Closure Library without Closure Compiler.
145This is typically used during development mode. When the webpack mode is `production`,
146only dependency information is provided to webpack as Closure Compiler will natively recognize
147the Closure Library primitives.
148
149The Closure Library Plugin is only compatible with the `AGGRESSIVE_BUNDLE` mode of the Closure-Compiler
150webpack plugin.
151
152## Options
153
154 * **closureLibraryBase** - (optional) string. Path to the base.js file in Closure-Library.
155 * **deps** - (optional) string or Array. Closures style dependency mappings. Typically generated by the
156 [depswriter.py script](https://developers.google.com/closure/library/docs/depswriter) included with Closure-Library.
157 * **extraDeps** - (optional) Object. Mapping of namespace to file path for closure-library provided namespaces.
158
159
160<h2 align="center">Maintainers</h2>
161
162<table>
163 <tbody>
164 <tr>
165 <td align="center">
166 <a href="https://github.com/ChadKillingsworth">
167 <img width="150" alt="" height="150" src="https://avatars.githubusercontent.com/u/1247639?v=3">
168 </br>
169 Chad Killingsworth
170 </a>
171 </td>
172 <td align="center">
173 <a href="https://github.com/d3viant0ne">
174 <img width="150" alt="" height="150" src="https://avatars.githubusercontent.com/u/8420490?v=3">
175 </br>
176 Joshua Wiens
177 </a>
178 </td>
179 </tr>
180 <tbody>
181</table>