terser-webpack-plugin
Advanced tools
Comparing version 3.1.0 to 4.0.0
@@ -5,2 +5,17 @@ # Changelog | ||
## [4.0.0](https://github.com/webpack-contrib/terser-webpack-plugin/compare/v3.1.0...v4.0.0) (2020-08-04) | ||
### ⚠ BREAKING CHANGES | ||
* the `warningsFilter` option was remove without replacement, | ||
* `terser` version is `5`, | ||
* returned value of the `minify` option was changes, only `code`/`map`/`extractedComments` are valid | ||
### Features | ||
* improved compatibility with webpack@5 | ||
## [3.1.0](https://github.com/webpack-contrib/terser-webpack-plugin/compare/v3.0.8...v3.1.0) (2020-08-03) | ||
@@ -7,0 +22,0 @@ |
@@ -36,4 +36,2 @@ "use strict"; | ||
const warningRegex = /\[.+:([0-9]+),([0-9]+)\]/; | ||
class TerserPlugin { | ||
@@ -49,3 +47,2 @@ constructor(options = {}) { | ||
test = /\.m?js(\?.*)?$/i, | ||
warningsFilter = () => true, | ||
extractComments = true, | ||
@@ -61,3 +58,2 @@ sourceMap, | ||
test, | ||
warningsFilter, | ||
extractComments, | ||
@@ -102,35 +98,2 @@ sourceMap, | ||
static buildWarning(warning, file, sourceMap, requestShortener, warningsFilter) { | ||
let warningMessage = warning; | ||
let locationMessage = ''; | ||
let source; | ||
if (sourceMap) { | ||
const match = warningRegex.exec(warning); | ||
if (match) { | ||
const line = +match[1]; | ||
const column = +match[2]; | ||
const original = sourceMap.originalPositionFor({ | ||
line, | ||
column | ||
}); | ||
if (original && original.source && original.source !== file && requestShortener) { | ||
({ | ||
source | ||
} = original); | ||
warningMessage = `${warningMessage.replace(warningRegex, '')}`; | ||
locationMessage = `[${requestShortener.shorten(original.source)}:${original.line},${original.column}]`; | ||
} | ||
} | ||
} | ||
if (warningsFilter && !warningsFilter(warning, file, source)) { | ||
return null; | ||
} | ||
return `Terser Plugin: ${warningMessage}${locationMessage}`; | ||
} | ||
static isWebpack4() { | ||
@@ -252,4 +215,3 @@ return _webpack.version[0] === '4'; | ||
error, | ||
map, | ||
warnings | ||
map | ||
} = taskResult; | ||
@@ -261,3 +223,3 @@ const { | ||
if ((error || warnings && warnings.length > 0) && inputSourceMap && TerserPlugin.isSourceMap(inputSourceMap)) { | ||
if (error && inputSourceMap && TerserPlugin.isSourceMap(inputSourceMap)) { | ||
sourceMap = new _sourceMap.SourceMapConsumer(inputSourceMap); | ||
@@ -334,13 +296,3 @@ } // Handling results | ||
minimized: true | ||
}); // Handling warnings | ||
if (warnings && warnings.length > 0) { | ||
warnings.forEach(warning => { | ||
const builtWarning = TerserPlugin.buildWarning(warning, name, sourceMap, new _RequestShortener.default(compiler.context), this.options.warningsFilter); | ||
if (builtWarning) { | ||
compilation.warnings.push(builtWarning); | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
@@ -393,8 +345,2 @@ | ||
task.assetSource = assetSource; | ||
task.cacheKeys = { | ||
terser: _package.default.version, | ||
// eslint-disable-next-line global-require | ||
'terser-webpack-plugin': require('../package.json').version, | ||
'terser-webpack-plugin-options': this.options | ||
}; | ||
} | ||
@@ -443,7 +389,3 @@ | ||
try { | ||
if (worker) { | ||
taskResult = await worker.transform((0, _serializeJavascript.default)(task)); | ||
} else { | ||
taskResult = (0, _minify.minify)(task); | ||
} | ||
taskResult = await (worker ? worker.transform((0, _serializeJavascript.default)(task)) : (0, _minify.minify)(task)); | ||
} catch (error) { | ||
@@ -549,8 +491,6 @@ taskResult = { | ||
const plugin = { | ||
name: this.constructor.name | ||
}; | ||
compiler.hooks.compilation.tap(plugin, compilation => { | ||
const pluginName = this.constructor.name; | ||
compiler.hooks.compilation.tap(pluginName, compilation => { | ||
if (this.options.sourceMap) { | ||
compilation.hooks.buildModule.tap(plugin, moduleArg => { | ||
compilation.hooks.buildModule.tap(pluginName, moduleArg => { | ||
// to get detailed location info about errors | ||
@@ -573,3 +513,3 @@ // eslint-disable-next-line no-param-reassign | ||
for (const template of [mainTemplate, chunkTemplate]) { | ||
template.hooks.hashForChunk.tap(plugin, hash => { | ||
template.hooks.hashForChunk.tap(pluginName, hash => { | ||
hash.update('TerserPlugin'); | ||
@@ -580,4 +520,7 @@ hash.update(data); | ||
compilation.hooks.optimizeChunkAssets.tapPromise(plugin, optimizeFn.bind(this, compilation)); | ||
compilation.hooks.optimizeChunkAssets.tapPromise(pluginName, optimizeFn.bind(this, compilation)); | ||
} else { | ||
// eslint-disable-next-line global-require | ||
const Compilation = require('webpack/lib/Compilation'); | ||
const hooks = _webpack.javascript.JavascriptModulesPlugin.getCompilationHooks(compilation); | ||
@@ -589,8 +532,11 @@ | ||
}); | ||
hooks.chunkHash.tap(plugin, (chunk, hash) => { | ||
hooks.chunkHash.tap(pluginName, (chunk, hash) => { | ||
hash.update('TerserPlugin'); | ||
hash.update(data); | ||
}); | ||
compilation.hooks.optimizeAssets.tapPromise(plugin, optimizeFn.bind(this, compilation)); | ||
compilation.hooks.statsPrinter.tap(plugin, stats => { | ||
compilation.hooks.processAssets.tapPromise({ | ||
name: pluginName, | ||
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE | ||
}, optimizeFn.bind(this, compilation)); | ||
compilation.hooks.statsPrinter.tap(pluginName, stats => { | ||
stats.hooks.print.for('asset.info.minimized').tap('terser-webpack-plugin', (minimized, { | ||
@@ -597,0 +543,0 @@ green, |
@@ -9,3 +9,2 @@ "use strict"; | ||
ecma, | ||
warnings, | ||
parse = {}, | ||
@@ -47,4 +46,3 @@ compress = {}, | ||
safari10, | ||
toplevel, | ||
warnings | ||
toplevel | ||
}); | ||
@@ -138,3 +136,3 @@ | ||
const minify = options => { | ||
async function minify(options) { | ||
const { | ||
@@ -164,18 +162,9 @@ name, | ||
terserOptions.output.comments = buildComments(options, terserOptions, extractedComments); | ||
const { | ||
error, | ||
map, | ||
code, | ||
warnings | ||
} = terserMinify({ | ||
const result = await terserMinify({ | ||
[name]: input | ||
}, terserOptions); | ||
return { | ||
error, | ||
map, | ||
code, | ||
warnings, | ||
return { ...result, | ||
extractedComments | ||
}; | ||
}; | ||
} | ||
@@ -187,9 +176,3 @@ function transform(options) { | ||
options = new Function('exports', 'require', 'module', '__filename', '__dirname', `'use strict'\nreturn ${options}`)(exports, require, module, __filename, __dirname); | ||
const result = minify(options); | ||
if (result.error) { | ||
throw result.error; | ||
} else { | ||
return result; | ||
} | ||
return minify(options); | ||
} | ||
@@ -196,0 +179,0 @@ |
@@ -164,8 +164,4 @@ { | ||
] | ||
}, | ||
"warningsFilter": { | ||
"description": "Allow to filter `terser` warnings.", | ||
"instanceof": "Function" | ||
} | ||
} | ||
} |
@@ -8,6 +8,2 @@ "use strict"; | ||
var _serializeJavascript = _interopRequireDefault(require("serialize-javascript")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
class Cache { | ||
@@ -26,3 +22,3 @@ // eslint-disable-next-line no-unused-vars | ||
// eslint-disable-next-line no-param-reassign | ||
task.cacheIdent = task.cacheIdent || `${task.name}|${(0, _serializeJavascript.default)(task.cacheKeys)}`; // eslint-disable-next-line no-param-reassign | ||
task.cacheIdent = task.cacheIdent || `${task.name}`; // eslint-disable-next-line no-param-reassign | ||
@@ -29,0 +25,0 @@ task.cacheETag = task.cacheETag || this.cache.getLazyHashedEtag(task.assetSource); |
{ | ||
"name": "terser-webpack-plugin", | ||
"version": "3.1.0", | ||
"version": "4.0.0", | ||
"description": "Terser plugin for webpack", | ||
@@ -51,3 +51,3 @@ "license": "MIT", | ||
"source-map": "^0.6.1", | ||
"terser": "^4.8.0", | ||
"terser": "^5.0.0", | ||
"webpack-sources": "^1.4.3" | ||
@@ -54,0 +54,0 @@ }, |
@@ -178,4 +178,5 @@ <div align="center"> | ||
'terser-webpack-plugin-options': this.options, // plugin options | ||
path: compiler.outputPath ? `${compiler.outputPath}/${file}` : file, // asset path | ||
hash: crypto.createHash('md4').update(input).digest('hex'), // source file hash | ||
nodeVersion: process.version, // Node.js version | ||
name: file, // asset path | ||
contentHash: crypto.createHash('md4').update(input).digest('hex'), // source file hash | ||
}); | ||
@@ -307,2 +308,3 @@ ``` | ||
new TerserPlugin({ | ||
// Can be async | ||
minify: (file, sourceMap) => { | ||
@@ -313,3 +315,3 @@ const extractedComments = []; | ||
const { error, map, code, warnings } = require('uglify-module') // Or require('./path/to/uglify-module') | ||
const { map, code } = require('uglify-module') // Or require('./path/to/uglify-module') | ||
.minify(file, { | ||
@@ -319,3 +321,3 @@ /* Your options for minification */ | ||
return { error, map, code, warnings, extractedComments }; | ||
return { map, code, extractedComments }; | ||
}, | ||
@@ -345,3 +347,2 @@ }), | ||
ecma: undefined, | ||
warnings: false, | ||
parse: {}, | ||
@@ -581,41 +582,2 @@ compress: {}, | ||
### `warningsFilter` | ||
Type: `Function<(warning, file, source) -> Boolean>` | ||
Default: `() => true` | ||
Allow to filter [terser](https://github.com/terser-js/terser) warnings. | ||
Return `true` to keep the warning, a falsy value (`false`/`null`/`undefined`) otherwise. | ||
> ⚠️ The `source` argument will contain `undefined` if you don't use source maps. | ||
**webpack.config.js** | ||
```js | ||
module.exports = { | ||
optimization: { | ||
minimize: true, | ||
minimizer: [ | ||
new TerserPlugin({ | ||
warningsFilter: (warning, file, source) => { | ||
if (/Dropping unreachable code/i.test(warning)) { | ||
return true; | ||
} | ||
if (/file\.js/i.test(file)) { | ||
return true; | ||
} | ||
if (/source\.js/i.test(source)) { | ||
return true; | ||
} | ||
return false; | ||
}, | ||
}), | ||
], | ||
}, | ||
}; | ||
``` | ||
## Examples | ||
@@ -622,0 +584,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67060
803
694
- Removedterser@4.8.1(transitive)
Updatedterser@^5.0.0