terser-webpack-plugin
Advanced tools
Comparing version 3.0.8 to 3.1.0
@@ -5,2 +5,15 @@ # Changelog | ||
## [3.1.0](https://github.com/webpack-contrib/terser-webpack-plugin/compare/v3.0.8...v3.1.0) (2020-08-03) | ||
### Features | ||
* show minimized assets in stats for webpack@5 ([#289](https://github.com/webpack-contrib/terser-webpack-plugin/issues/289)) ([d59eae2](https://github.com/webpack-contrib/terser-webpack-plugin/commit/d59eae2108ba43f3f7ce6c83195a8438634a9b55)) | ||
### Bug Fixes | ||
* compatibility cache feature with webpack@5 ([5d2bd29](https://github.com/webpack-contrib/terser-webpack-plugin/commit/5d2bd29403f614296795664a81e437712469bc05)) | ||
* skip double compression for child compilation ([37cc813](https://github.com/webpack-contrib/terser-webpack-plugin/commit/37cc8132df50ad7b374ec2606df1d0e202a579d1)) | ||
### [3.0.8](https://github.com/webpack-contrib/terser-webpack-plugin/compare/v3.0.7...v3.0.8) (2020-07-27) | ||
@@ -7,0 +20,0 @@ |
@@ -78,10 +78,2 @@ "use strict"; | ||
static buildSourceMap(inputSourceMap) { | ||
if (!inputSourceMap || !TerserPlugin.isSourceMap(inputSourceMap)) { | ||
return null; | ||
} | ||
return new _sourceMap.SourceMapConsumer(inputSourceMap); | ||
} | ||
static buildError(error, file, sourceMap, requestShortener) { | ||
@@ -141,13 +133,2 @@ if (error.line) { | ||
static removeQueryString(filename) { | ||
let targetFilename = filename; | ||
const queryStringIdx = targetFilename.indexOf('?'); | ||
if (queryStringIdx >= 0) { | ||
targetFilename = targetFilename.substr(0, queryStringIdx); | ||
} | ||
return targetFilename; | ||
} | ||
static isWebpack4() { | ||
@@ -164,6 +145,50 @@ return _webpack.version[0] === '4'; | ||
return parallel === true ? cpus.length - 1 : Math.min(Number(parallel) || 0, cpus.length - 1); | ||
} // eslint-disable-next-line consistent-return | ||
static getAsset(compilation, name) { | ||
// New API | ||
if (compilation.getAsset) { | ||
return compilation.getAsset(name); | ||
} | ||
if (compilation.assets[name]) { | ||
return { | ||
name, | ||
source: compilation.assets[name], | ||
info: {} | ||
}; | ||
} | ||
} | ||
*taskGenerator(compiler, compilation, allExtractedComments, file) { | ||
const assetSource = compilation.assets[file]; | ||
static emitAsset(compilation, name, source, assetInfo) { | ||
// New API | ||
if (compilation.emitAsset) { | ||
compilation.emitAsset(name, source, assetInfo); | ||
} // eslint-disable-next-line no-param-reassign | ||
compilation.assets[name] = source; | ||
} | ||
static updateAsset(compilation, name, newSource, assetInfo) { | ||
// New API | ||
if (compilation.updateAsset) { | ||
compilation.updateAsset(name, newSource, assetInfo); | ||
} // eslint-disable-next-line no-param-reassign | ||
compilation.assets[name] = newSource; | ||
} | ||
*taskGenerator(compiler, compilation, allExtractedComments, name) { | ||
const { | ||
info, | ||
source: assetSource | ||
} = TerserPlugin.getAsset(compilation, name); // Skip double minimize assets from child compilation | ||
if (info.minimized) { | ||
yield false; | ||
} | ||
let input; | ||
@@ -184,3 +209,3 @@ let inputSourceMap; // TODO refactor after drop webpack@4, webpack@5 always has `sourceAndMap` on sources | ||
inputSourceMap = map; | ||
compilation.warnings.push(new Error(`${file} contains invalid source map`)); | ||
compilation.warnings.push(new Error(`${name} contains invalid source map`)); | ||
} | ||
@@ -203,3 +228,3 @@ } | ||
let query = ''; | ||
let filename = file; | ||
let filename = name; | ||
const querySplit = filename.indexOf('?'); | ||
@@ -236,4 +261,4 @@ | ||
if (error || warnings && warnings.length > 0) { | ||
sourceMap = TerserPlugin.buildSourceMap(inputSourceMap); | ||
if ((error || warnings && warnings.length > 0) && inputSourceMap && TerserPlugin.isSourceMap(inputSourceMap)) { | ||
sourceMap = new _sourceMap.SourceMapConsumer(inputSourceMap); | ||
} // Handling results | ||
@@ -244,3 +269,3 @@ // Error case: add errors, and go to next file | ||
if (error) { | ||
compilation.errors.push(TerserPlugin.buildError(error, file, sourceMap, new _RequestShortener.default(compiler.context))); | ||
compilation.errors.push(TerserPlugin.buildError(error, name, sourceMap, new _RequestShortener.default(compiler.context))); | ||
return; | ||
@@ -261,3 +286,3 @@ } | ||
if (map) { | ||
outputSource = new _webpackSources.SourceMapSource(code, file, map, input, inputSourceMap, true); | ||
outputSource = new _webpackSources.SourceMapSource(code, name, map, input, inputSourceMap, true); | ||
} else { | ||
@@ -272,3 +297,3 @@ outputSource = new _webpackSources.RawSource(code); | ||
if (hasBannerForExtractedComments) { | ||
banner = this.options.extractComments.banner || `For license information please see ${_path.default.relative(_path.default.dirname(file), commentsFilename).replace(/\\/g, '/')}`; | ||
banner = this.options.extractComments.banner || `For license information please see ${_path.default.relative(_path.default.dirname(name), commentsFilename).replace(/\\/g, '/')}`; | ||
@@ -299,6 +324,6 @@ if (typeof banner === 'function') { | ||
const previousExtractedComments = compilation.assets[commentsFilename]; | ||
const previousExtractedComments = TerserPlugin.getAsset(compilation, commentsFilename); | ||
if (previousExtractedComments) { | ||
const previousExtractedCommentsSource = previousExtractedComments.source(); // Restore original comments and re-add them | ||
const previousExtractedCommentsSource = previousExtractedComments.source.source(); // Restore original comments and re-add them | ||
@@ -309,11 +334,11 @@ previousExtractedCommentsSource.replace(/\n$/, '').split('\n\n').forEach(comment => { | ||
} | ||
} // Updating assets | ||
// eslint-disable-next-line no-param-reassign | ||
} | ||
TerserPlugin.updateAsset(compilation, name, outputSource, { ...info, | ||
minimized: true | ||
}); // Handling warnings | ||
compilation.assets[file] = outputSource; // Handling warnings | ||
if (warnings && warnings.length > 0) { | ||
warnings.forEach(warning => { | ||
const builtWarning = TerserPlugin.buildWarning(warning, file, sourceMap, new _RequestShortener.default(compiler.context), this.options.warningsFilter); | ||
const builtWarning = TerserPlugin.buildWarning(warning, name, sourceMap, new _RequestShortener.default(compiler.context), this.options.warningsFilter); | ||
@@ -328,3 +353,3 @@ if (builtWarning) { | ||
const task = { | ||
file, | ||
name, | ||
input, | ||
@@ -365,6 +390,6 @@ inputSourceMap, | ||
nodeVersion: process.version, | ||
filename: file, | ||
name, | ||
contentHash: digest.substr(0, hashDigestLength) | ||
}; | ||
task.cacheKeys = this.options.cacheKeys(defaultCacheKeys, file); | ||
task.cacheKeys = this.options.cacheKeys(defaultCacheKeys, name); | ||
} | ||
@@ -435,3 +460,3 @@ } else { | ||
if (cache.isEnabled() && !taskResult.error) { | ||
taskResult = await cache.store(task, taskResult).then(() => taskResult, () => taskResult); | ||
await cache.store(task, taskResult); | ||
} | ||
@@ -443,3 +468,3 @@ | ||
scheduledTasks.push(limit(() => { | ||
scheduledTasks.push(limit(async () => { | ||
const task = getTaskForAsset(assetName).next().value; | ||
@@ -453,3 +478,17 @@ | ||
if (cache.isEnabled()) { | ||
return cache.get(task).then(taskResult => task.callback(taskResult), () => enqueue(task)); | ||
let taskResult; | ||
try { | ||
taskResult = await cache.get(task); | ||
} catch (ignoreError) { | ||
return enqueue(task); | ||
} // Webpack@5 return `undefined` when cache is not found | ||
if (!taskResult) { | ||
return enqueue(task); | ||
} | ||
task.callback(taskResult); | ||
return Promise.resolve(); | ||
} | ||
@@ -461,9 +500,7 @@ | ||
return Promise.all(scheduledTasks).then(() => { | ||
if (worker) { | ||
return worker.end(); | ||
} | ||
await Promise.all(scheduledTasks); | ||
return Promise.resolve(); | ||
}); | ||
if (worker) { | ||
await worker.end(); | ||
} | ||
} | ||
@@ -514,5 +551,4 @@ | ||
Object.keys(allExtractedComments).forEach(commentsFilename => { | ||
const extractedComments = Array.from(allExtractedComments[commentsFilename]).sort().join('\n\n'); // eslint-disable-next-line no-param-reassign | ||
compilation.assets[commentsFilename] = new _webpackSources.RawSource(`${extractedComments}\n`); | ||
const extractedComments = Array.from(allExtractedComments[commentsFilename]).sort().join('\n\n'); | ||
TerserPlugin.emitAsset(compilation, commentsFilename, new _webpackSources.RawSource(`${extractedComments}\n`)); | ||
}); | ||
@@ -564,2 +600,9 @@ return Promise.resolve(); | ||
compilation.hooks.optimizeAssets.tapPromise(plugin, optimizeFn.bind(this, compilation)); | ||
compilation.hooks.statsPrinter.tap(plugin, stats => { | ||
stats.hooks.print.for('asset.info.minimized').tap('terser-webpack-plugin', (minimized, { | ||
green, | ||
formatFlag | ||
}) => // eslint-disable-next-line no-undefined | ||
minimized ? green(formatFlag('minimized')) : undefined); | ||
}); | ||
} | ||
@@ -566,0 +609,0 @@ }); |
@@ -138,3 +138,3 @@ "use strict"; | ||
const { | ||
file, | ||
name, | ||
input, | ||
@@ -147,3 +147,3 @@ inputSourceMap, | ||
return minifyFn({ | ||
[file]: input | ||
[name]: input | ||
}, inputSourceMap); | ||
@@ -169,3 +169,3 @@ } // Copy terser options | ||
} = terserMinify({ | ||
[file]: input | ||
[name]: input | ||
}, terserOptions); | ||
@@ -172,0 +172,0 @@ return { |
@@ -33,11 +33,12 @@ "use strict"; | ||
get(task) { | ||
async get(task) { | ||
// eslint-disable-next-line no-param-reassign | ||
task.cacheIdent = task.cacheIdent || (0, _serializeJavascript.default)(task.cacheKeys); | ||
return _cacache.default.get(this.cacheDir, task.cacheIdent).then(({ | ||
const { | ||
data | ||
}) => JSON.parse(data)); | ||
} = await _cacache.default.get(this.cacheDir, task.cacheIdent); | ||
return JSON.parse(data); | ||
} | ||
store(task, data) { | ||
async store(task, data) { | ||
return _cacache.default.put(this.cacheDir, task.cacheIdent, JSON.stringify(data)); | ||
@@ -44,0 +45,0 @@ } |
@@ -8,71 +8,27 @@ "use strict"; | ||
var _getLazyHashedEtag = _interopRequireDefault(require("webpack/lib/cache/getLazyHashedEtag")); | ||
var _serializeJavascript = _interopRequireDefault(require("serialize-javascript")); | ||
var _webpack = require("webpack"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// eslint-disable-next-line import/extensions,import/no-unresolved | ||
class Cache { | ||
// eslint-disable-next-line no-unused-vars | ||
constructor(compilation, ignored) { | ||
this.compilation = compilation; | ||
} | ||
this.cache = compilation.getCache('TerserWebpackPlugin'); | ||
} // eslint-disable-next-line class-methods-use-this | ||
isEnabled() { | ||
return Boolean(this.compilation.cache); | ||
return true; | ||
} | ||
createCacheIdent(task) { | ||
const { | ||
outputOptions: { | ||
hashSalt, | ||
hashDigest, | ||
hashDigestLength, | ||
hashFunction | ||
} | ||
} = this.compilation; | ||
const hash = _webpack.util.createHash(hashFunction); | ||
if (hashSalt) { | ||
hash.update(hashSalt); | ||
} | ||
hash.update((0, _serializeJavascript.default)(task.cacheKeys)); | ||
const digest = hash.digest(hashDigest); | ||
const cacheKeys = digest.substr(0, hashDigestLength); | ||
return `${this.compilation.compilerPath}/TerserWebpackPlugin/${cacheKeys}/${task.file}`; | ||
} | ||
get(task) { | ||
async get(task) { | ||
// eslint-disable-next-line no-param-reassign | ||
task.cacheIdent = task.cacheIdent || this.createCacheIdent(task); // 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.cacheETag = task.cacheETag || (0, _getLazyHashedEtag.default)(task.assetSource); | ||
return new Promise((resolve, reject) => { | ||
this.compilation.cache.get(task.cacheIdent, task.cacheETag, (err, result) => { | ||
if (err) { | ||
reject(err); | ||
} else if (result) { | ||
resolve(result); | ||
} else { | ||
reject(); | ||
} | ||
}); | ||
}); | ||
task.cacheETag = task.cacheETag || this.cache.getLazyHashedEtag(task.assetSource); | ||
return this.cache.getPromise(task.cacheIdent, task.cacheETag); | ||
} | ||
store(task, data) { | ||
return new Promise((resolve, reject) => { | ||
this.compilation.cache.store(task.cacheIdent, task.cacheETag, data, err => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(data); | ||
} | ||
}); | ||
}); | ||
async store(task, data) { | ||
return this.cache.storePromise(task.cacheIdent, task.cacheETag, data); | ||
} | ||
@@ -79,0 +35,0 @@ |
{ | ||
"name": "terser-webpack-plugin", | ||
"version": "3.0.8", | ||
"version": "3.1.0", | ||
"description": "Terser plugin for webpack", | ||
@@ -46,3 +46,3 @@ "license": "MIT", | ||
"find-cache-dir": "^3.3.1", | ||
"jest-worker": "^26.1.0", | ||
"jest-worker": "^26.2.1", | ||
"p-limit": "^3.0.2", | ||
@@ -63,3 +63,3 @@ "schema-utils": "^2.6.6", | ||
"@webpack-contrib/eslint-config-webpack": "^3.0.0", | ||
"babel-jest": "^26.1.0", | ||
"babel-jest": "^26.2.1", | ||
"copy-webpack-plugin": "^6.0.3", | ||
@@ -74,3 +74,3 @@ "cross-env": "^7.0.2", | ||
"husky": "^4.2.5", | ||
"jest": "^26.0.1", | ||
"jest": "^26.2.1", | ||
"lint-staged": "^10.2.11", | ||
@@ -82,4 +82,4 @@ "memfs": "^3.2.0", | ||
"uglify-js": "^3.10.0", | ||
"webpack": "^4.44.0", | ||
"worker-loader": "^2.0.0" | ||
"webpack": "^4.44.1", | ||
"worker-loader": "^3.0.0" | ||
}, | ||
@@ -86,0 +86,0 @@ "keywords": [ |
69690
871
+ Addedcaniuse-lite@1.0.30001637(transitive)
- Removedcaniuse-lite@1.0.30001638(transitive)
Updatedjest-worker@^26.2.1