项目原始demo,不改动
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
Це архівний репозитарій. Ви можете переглядати і клонувати файли, але не можете робити пуш або відкривати питання/запити.

4 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. [![npm][npm]][npm-url]
  2. [![node][node]][node-url]
  3. [![deps][deps]][deps-url]
  4. [![test][test]][test-url]
  5. [![coverage][cover]][cover-url]
  6. [![chat][chat]][chat-url]
  7. <div align="center">
  8. <a href="https://github.com/webpack/webpack">
  9. <img width="200" height="200"
  10. src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon.svg">
  11. </a>
  12. <h1>UglifyJS Webpack Plugin</h1>
  13. <p>This plugin uses <a href="https://github.com/mishoo/UglifyJS2/tree/harmony">UglifyJS v3 </a><a href="https://npmjs.com/package/uglify-es">(`uglify-es`)</a> to minify your JavaScript</p>
  14. </div>
  15. > ℹ️ `webpack < v4.0.0` currently contains [`v0.4.6`](https://github.com/webpack-contrib/uglifyjs-webpack-plugin/tree/version-0.4) of this plugin under `webpack.optimize.UglifyJsPlugin` as an alias. For usage of the latest version (`v1.0.0`), please follow the instructions below. Aliasing `v1.0.0` as `webpack.optimize.UglifyJsPlugin` is scheduled for `webpack v4.0.0`
  16. <h2 align="center">Install</h2>
  17. ```bash
  18. npm i -D uglifyjs-webpack-plugin
  19. ```
  20. <h2 align="center">Usage</h2>
  21. **webpack.config.js**
  22. ```js
  23. const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  24. module.exports = {
  25. //...
  26. optimization: {
  27. minimizer: [
  28. new UglifyJsPlugin()
  29. ]
  30. }
  31. }
  32. ```
  33. <h2 align="center">Options</h2>
  34. |Name|Type|Default|Description|
  35. |:--:|:--:|:-----:|:----------|
  36. |**`test`**|`{RegExp\|Array<RegExp>}`| <code>/\\.js$/i</code>|Test to match files against|
  37. |**`include`**|`{RegExp\|Array<RegExp>}`|`undefined`|Files to `include`|
  38. |**`exclude`**|`{RegExp\|Array<RegExp>}`|`undefined`|Files to `exclude`|
  39. |**`cache`**|`{Boolean\|String}`|`false`|Enable file caching|
  40. |**`cacheKeys`**|`{Function(defaultCacheKeys, file) -> {Object}}`|`defaultCacheKeys => defaultCacheKeys`|Allows you to override default cache keys|
  41. |**`parallel`**|`{Boolean\|Number}`|`false`|Use multi-process parallel running to improve the build speed|
  42. |**`sourceMap`**|`{Boolean}`|`false`|Use source maps to map error message locations to modules (This slows down the compilation) ⚠️ **`cheap-source-map` options don't work with this plugin**|
  43. |**`minify`**|`{Function}`|`undefined`|Allows you to override default minify function|
  44. |**`uglifyOptions`**|`{Object}`|[`{...defaults}`](https://github.com/webpack-contrib/uglifyjs-webpack-plugin/tree/master#uglifyoptions)|`uglify` [Options](https://github.com/mishoo/UglifyJS2/tree/harmony#minify-options)|
  45. |**`extractComments`**|`{Boolean\|RegExp\|Function<(node, comment) -> {Boolean\|Object}>}`|`false`|Whether comments shall be extracted to a separate file, (see [details](https://github.com/webpack/webpack/commit/71933e979e51c533b432658d5e37917f9e71595a) (`webpack >= 2.3.0`)|
  46. |**`warningsFilter`**|`{Function(source) -> {Boolean}}`|`() => true`|Allow to filter uglify warnings|
  47. ### `test`
  48. **webpack.config.js**
  49. ```js
  50. [
  51. new UglifyJsPlugin({
  52. test: /\.js($|\?)/i
  53. })
  54. ]
  55. ```
  56. ### `include`
  57. **webpack.config.js**
  58. ```js
  59. [
  60. new UglifyJsPlugin({
  61. include: /\/includes/
  62. })
  63. ]
  64. ```
  65. ### `exclude`
  66. **webpack.config.js**
  67. ```js
  68. [
  69. new UglifyJsPlugin({
  70. exclude: /\/excludes/
  71. })
  72. ]
  73. ```
  74. ### `cache`
  75. If you use your own `minify` function please read the `minify` section for cache invalidation correctly.
  76. #### `{Boolean}`
  77. **webpack.config.js**
  78. ```js
  79. [
  80. new UglifyJsPlugin({
  81. cache: true
  82. })
  83. ]
  84. ```
  85. Enable file caching.
  86. Default path to cache directory: `node_modules/.cache/uglifyjs-webpack-plugin`.
  87. #### `{String}`
  88. **webpack.config.js**
  89. ```js
  90. [
  91. new UglifyJsPlugin({
  92. cache: 'path/to/cache'
  93. })
  94. ]
  95. ```
  96. Path to cache directory.
  97. ### `cacheKeys`
  98. **webpack.config.js**
  99. ```js
  100. [
  101. new UglifyJsPlugin({
  102. cache: true,
  103. cacheKeys: (defaultCacheKeys, file) => {
  104. defaultCacheKeys.myCacheKey = 'myCacheKeyValue';
  105. return defaultCacheKeys;
  106. },
  107. })
  108. ]
  109. ```
  110. Allows you to override default cache keys.
  111. Default keys:
  112. ```js
  113. {
  114. 'uglify-es': versions.uglify, // uglify version
  115. 'uglifyjs-webpack-plugin': versions.plugin, // plugin version
  116. 'uglifyjs-webpack-plugin-options': this.options, // plugin options
  117. path: compiler.outputPath ? `${compiler.outputPath}/${file}` : file, // asset path
  118. hash: crypto.createHash('md4').update(input).digest('hex'), // source file hash
  119. }
  120. ```
  121. ### `parallel`
  122. #### `{Boolean}`
  123. **webpack.config.js**
  124. ```js
  125. [
  126. new UglifyJsPlugin({
  127. parallel: true
  128. })
  129. ]
  130. ```
  131. Enable parallelization.
  132. Default number of concurrent runs: `os.cpus().length - 1`.
  133. #### `{Number}`
  134. **webpack.config.js**
  135. ```js
  136. [
  137. new UglifyJsPlugin({
  138. parallel: 4
  139. })
  140. ]
  141. ```
  142. Number of concurrent runs.
  143. > ℹ️ Parallelization can speedup your build significantly and is therefore **highly recommended**
  144. ### `sourceMap`
  145. If you use your own `minify` function please read the `minify` section for handling source maps correctly.
  146. **webpack.config.js**
  147. ```js
  148. [
  149. new UglifyJsPlugin({
  150. sourceMap: true
  151. })
  152. ]
  153. ```
  154. > ⚠️ **`cheap-source-map` options don't work with this plugin**
  155. ### `minify`
  156. > ⚠️ **Always use `require` inside `minify` function when `parallel` option enabled**
  157. **webpack.config.js**
  158. ```js
  159. [
  160. new UglifyJsPlugin({
  161. minify(file, sourceMap) {
  162. const extractedComments = [];
  163. // Custom logic for extract comments
  164. const { error, map, code, warnings } = require('uglify-module') // Or require('./path/to/uglify-module')
  165. .minify(
  166. file,
  167. { /* Your options for minification */ },
  168. );
  169. return { error, map, code, warnings, extractedComments };
  170. }
  171. })
  172. ]
  173. ```
  174. By default plugin uses `uglify-es` package.
  175. Examples:
  176. #### `uglify-js`
  177. ```bash
  178. npm i -D uglify-js
  179. ```
  180. **webpack.config.js**
  181. ```js
  182. [
  183. new UglifyJsPlugin({
  184. // Uncomment lines below for cache invalidation correctly
  185. // cache: true,
  186. // cacheKeys(defaultCacheKeys) {
  187. // return Object.assign(
  188. // {},
  189. // defaultCacheKeys,
  190. // { 'uglify-js': require('uglify-js/package.json').version },
  191. // );
  192. // },
  193. minify(file, sourceMap) {
  194. // https://github.com/mishoo/UglifyJS2#minify-options
  195. const uglifyJsOptions = { /* your `uglify-js` package options */ };
  196. if (sourceMap) {
  197. uglifyJsOptions.sourceMap = {
  198. content: sourceMap,
  199. };
  200. }
  201. return require('uglify-js').minify(file, uglifyJsOptions);
  202. }
  203. })
  204. ]
  205. ```
  206. #### `terser`
  207. ```bash
  208. npm i -D terser
  209. ```
  210. **webpack.config.js**
  211. ```js
  212. [
  213. new UglifyJsPlugin({
  214. // Uncomment lines below for cache invalidation correctly
  215. // cache: true,
  216. // cacheKeys(defaultCacheKeys) {
  217. // return Object.assign(
  218. // {},
  219. // defaultCacheKeys,
  220. // { terser: require('terser/package.json').version },
  221. // );
  222. // },
  223. minify(file, sourceMap) {
  224. // https://github.com/fabiosantoscode/terser#minify-options
  225. const terserOptions = { /* your `terser` package options */ };
  226. if (sourceMap) {
  227. terserOption.sourceMap = {
  228. content: sourceMap,
  229. };
  230. }
  231. return require('terser').minify(file, terserOptions);
  232. }
  233. })
  234. ]
  235. ```
  236. ### [`uglifyOptions`](https://github.com/mishoo/UglifyJS2/tree/harmony#minify-options)
  237. |Name|Type|Default|Description|
  238. |:--:|:--:|:-----:|:----------|
  239. |**`ecma`**|`{Number}`|`undefined`|Supported ECMAScript Version (`5`, `6`, `7` or `8`). Affects `parse`, `compress` && `output` options|
  240. |**`warnings`**|`{Boolean}`|`false`|Display Warnings|
  241. |**[`parse`](https://github.com/mishoo/UglifyJS2/tree/harmony#parse-options)**|`{Object}`|`{}`|Additional Parse Options|
  242. |**[`compress`](https://github.com/mishoo/UglifyJS2/tree/harmony#compress-options)**|`{Boolean\|Object}`|`true`|Additional Compress Options|
  243. |**[`mangle`](https://github.com/mishoo/UglifyJS2/tree/harmony#mangle-options)**|`{Boolean\|Object}`|`{inline: false}`|Enable Name Mangling (See [Mangle Properties](https://github.com/mishoo/UglifyJS2/tree/harmony#mangle-properties-options) for advanced setups, use with ⚠️)|
  244. |**[`output`](https://github.com/mishoo/UglifyJS2/tree/harmony#output-options)**|`{Object}`|`{comments: extractComments ? false : /^\**!\|@preserve\|@license\|@cc_on/,}`|Additional Output Options (The defaults are optimized for best compression)|
  245. |**`toplevel`**|`{Boolean}`|`false`|Enable top level variable and function name mangling and to drop unused variables and functions|
  246. |**`nameCache`**|`{Object}`|`null`|Enable cache of mangled variable and property names across multiple invocations|
  247. |**`ie8`**|`{Boolean}`|`false`|Enable IE8 Support|
  248. |**`keep_classnames`**|`{Boolean}`|`undefined`|Enable prevent discarding or mangling of class names|
  249. |**`keep_fnames`**|`{Boolean}`|`false`| Enable prevent discarding or mangling of function names. Useful for code relying on `Function.prototype.name`. If the top level minify option `keep_classnames` is `undefined` it will be overriden with the value of the top level minify option `keep_fnames`|
  250. |**`safari10`**|`{Boolean}`|`false`|Enable work around Safari 10/11 bugs in loop scoping and `await`|
  251. **webpack.config.js**
  252. ```js
  253. [
  254. new UglifyJsPlugin({
  255. uglifyOptions: {
  256. ecma: 8,
  257. warnings: false,
  258. parse: {...options},
  259. compress: {...options},
  260. mangle: {
  261. ...options,
  262. properties: {
  263. // mangle property options
  264. }
  265. },
  266. output: {
  267. comments: false,
  268. beautify: false,
  269. ...options
  270. },
  271. toplevel: false,
  272. nameCache: null,
  273. ie8: false,
  274. keep_classnames: undefined,
  275. keep_fnames: false,
  276. safari10: false,
  277. }
  278. })
  279. ]
  280. ```
  281. ### `extractComments`
  282. #### `{Boolean}`
  283. All comments that normally would be preserved by the `comments` option will be moved to a separate file. If the original file is named `foo.js`, then the comments will be stored to `foo.js.LICENSE`.
  284. #### `{RegExp|String}` or `{Function<(node, comment) -> {Boolean}>}`
  285. All comments that match the given expression (resp. are evaluated to `true` by the function) will be extracted to the separate file. The `comments` option specifies whether the comment will be preserved, i.e. it is possible to preserve some comments (e.g. annotations) while extracting others or even preserving comments that have been extracted.
  286. #### `{Object}`
  287. |Name|Type|Default|Description|
  288. |:--:|:--:|:-----:|:----------|
  289. |**`condition`**|`{Regex\|Function}`|``|Regular Expression or function (see previous point)|
  290. |**`filename`**|`{String\|Function}`|`${file}.LICENSE`|The file where the extracted comments will be stored. Can be either a `{String}` or a `{Function<(string) -> {String}>}`, which will be given the original filename. Default is to append the suffix `.LICENSE` to the original filename|
  291. |**`banner`**|`{Boolean\|String\|Function}`|`/*! For license information please see ${filename}.js.LICENSE */`|The banner text that points to the extracted file and will be added on top of the original file. Can be `false` (no banner), a `{String}`, or a `{Function<(string) -> {String}` that will be called with the filename where extracted comments have been stored. Will be wrapped into comment|
  292. ### `warningsFilter`
  293. **webpack.config.js**
  294. ```js
  295. [
  296. new UglifyJsPlugin({
  297. warningsFilter: (src) => true
  298. })
  299. ]
  300. ```
  301. <h2 align="center">Maintainers</h2>
  302. <table>
  303. <tbody>
  304. <tr>
  305. <td align="center">
  306. <a href="https://github.com/hulkish">
  307. <img width="150" height="150" src="https://github.com/hulkish.png?size=150">
  308. </br>
  309. Steven Hargrove
  310. </a>
  311. </td>
  312. <td align="center">
  313. <a href="https://github.com/bebraw">
  314. <img width="150" height="150" src="https://github.com/bebraw.png?v=3&s=150">
  315. </br>
  316. Juho Vepsäläinen
  317. </a>
  318. </td>
  319. <td align="center">
  320. <a href="https://github.com/d3viant0ne">
  321. <img width="150" height="150" src="https://github.com/d3viant0ne.png?v=3&s=150">
  322. </br>
  323. Joshua Wiens
  324. </a>
  325. </td>
  326. <td align="center">
  327. <a href="https://github.com/michael-ciniawsky">
  328. <img width="150" height="150" src="https://github.com/michael-ciniawsky.png?v=3&s=150">
  329. </br>
  330. Michael Ciniawsky
  331. </a>
  332. </td>
  333. <td align="center">
  334. <a href="https://github.com/evilebottnawi">
  335. <img width="150" height="150" src="https://github.com/evilebottnawi.png?v=3&s=150">
  336. </br>
  337. Alexander Krasnoyarov
  338. </a>
  339. </td>
  340. </tr>
  341. <tbody>
  342. </table>
  343. [npm]: https://img.shields.io/npm/v/uglifyjs-webpack-plugin.svg
  344. [npm-url]: https://npmjs.com/package/uglifyjs-webpack-plugin
  345. [node]: https://img.shields.io/node/v/uglifyjs-webpack-plugin.svg
  346. [node-url]: https://nodejs.org
  347. [deps]: https://david-dm.org/webpack-contrib/uglifyjs-webpack-plugin.svg
  348. [deps-url]: https://david-dm.org/webpack-contrib/uglifyjs-webpack-plugin
  349. [test]: https://img.shields.io/circleci/project/github/webpack-contrib/uglifyjs-webpack-plugin.svg
  350. [test-url]: https://circleci.com/gh/webpack-contrib/uglifyjs-webpack-plugin
  351. [cover]: https://codecov.io/gh/webpack-contrib/uglifyjs-webpack-plugin/branch/master/graph/badge.svg
  352. [cover-url]: https://codecov.io/gh/webpack-contrib/uglifyjs-webpack-plugin
  353. [chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
  354. [chat-url]: https://gitter.im/webpack/webpack