terser-webpack-plugin
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -5,2 +5,15 @@ # Changelog | ||
## [2.1.0](https://github.com/webpack-contrib/terser-webpack-plugin/compare/v2.0.1...v2.1.0) (2019-09-16) | ||
### Bug Fixes | ||
* correct interpretation url for extracted comment file ([#157](https://github.com/webpack-contrib/terser-webpack-plugin/issues/157)) ([aba8ba7](https://github.com/webpack-contrib/terser-webpack-plugin/commit/aba8ba7)) | ||
### Features | ||
* emit warning when comment file conlict with an existing asset ([#156](https://github.com/webpack-contrib/terser-webpack-plugin/issues/156)) ([2b4d2a4](https://github.com/webpack-contrib/terser-webpack-plugin/commit/2b4d2a4)) | ||
* improve naming of extracted file with comments ([#154](https://github.com/webpack-contrib/terser-webpack-plugin/issues/154)) ([5fe3337](https://github.com/webpack-contrib/terser-webpack-plugin/commit/5fe3337)) | ||
### [2.0.1](https://github.com/webpack-contrib/terser-webpack-plugin/compare/v2.0.0...v2.0.1) (2019-09-06) | ||
@@ -24,2 +37,3 @@ | ||
* using the `extractComments.condition` option with `true` value extract only `some` comments | ||
* the `sourceMap` option now defaults to depending on the `devtool` Webpack option | ||
@@ -26,0 +40,0 @@ |
@@ -142,2 +142,18 @@ "use strict"; | ||
static removeQueryString(filename) { | ||
let targetFilename = filename; | ||
const queryStringIdx = targetFilename.indexOf('?'); | ||
if (queryStringIdx >= 0) { | ||
targetFilename = targetFilename.substr(0, queryStringIdx); | ||
} | ||
return targetFilename; | ||
} | ||
static hasAsset(commentFilename, assets) { | ||
const assetFilenames = Object.keys(assets).map(assetFilename => TerserPlugin.removeQueryString(assetFilename)); | ||
return assetFilenames.includes(TerserPlugin.removeQueryString(commentFilename)); | ||
} | ||
apply(compiler) { | ||
@@ -188,12 +204,35 @@ this.options.sourceMap = typeof this.options.sourceMap === 'undefined' ? compiler.options.devtool && /^((inline|hidden|nosources)-)?source-?map/.test(compiler.options.devtool) : Boolean(this.options.sourceMap); | ||
let commentsFile = false; | ||
let commentsFilename = false; | ||
if (this.options.extractComments) { | ||
commentsFile = this.options.extractComments.filename || `${file}.LICENSE`; | ||
commentsFilename = this.options.extractComments.filename || '[file].LICENSE[query]'; // Todo remove this in next major release | ||
if (typeof commentsFile === 'function') { | ||
commentsFile = commentsFile(file); | ||
if (typeof commentsFilename === 'function') { | ||
commentsFilename = commentsFilename.bind(null, file); | ||
} | ||
let query = ''; | ||
let filename = file; | ||
const querySplit = filename.indexOf('?'); | ||
if (querySplit >= 0) { | ||
query = filename.substr(querySplit); | ||
filename = filename.substr(0, querySplit); | ||
} | ||
const lastSlashIndex = filename.lastIndexOf('/'); | ||
const basename = lastSlashIndex === -1 ? filename : filename.substr(lastSlashIndex + 1); | ||
const data = { | ||
filename, | ||
basename, | ||
query | ||
}; | ||
commentsFilename = compilation.getPath(commentsFilename, data); | ||
} | ||
if (commentsFilename && TerserPlugin.hasAsset(commentsFilename, compilation.assets)) { | ||
// Todo make error and stop uglifing in next major release | ||
compilation.warnings.push(new Error(`The comment file "${TerserPlugin.removeQueryString(commentsFilename)}" conflicts with an existing asset, this may lead to code corruption, please use a different name`)); | ||
} | ||
const task = { | ||
@@ -203,3 +242,3 @@ file, | ||
inputSourceMap, | ||
commentsFile, | ||
commentsFilename, | ||
extractComments: this.options.extractComments, | ||
@@ -243,3 +282,3 @@ terserOptions: this.options.terserOptions, | ||
inputSourceMap, | ||
commentsFile | ||
commentsFilename | ||
} = tasks[index]; | ||
@@ -274,8 +313,8 @@ const { | ||
outputSource = new _webpackSources.RawSource(code); | ||
} // Write extracted comments to commentsFile | ||
} // Write extracted comments to commentsFilename | ||
if (commentsFile && extractedComments && extractedComments.length > 0) { | ||
if (commentsFile in compilation.assets) { | ||
const commentsFileSource = compilation.assets[commentsFile].source(); | ||
if (commentsFilename && extractedComments && extractedComments.length > 0) { | ||
if (commentsFilename in compilation.assets) { | ||
const commentsFileSource = compilation.assets[commentsFilename].source(); | ||
extractedComments = extractedComments.filter(comment => !commentsFileSource.includes(comment)); | ||
@@ -287,6 +326,6 @@ } | ||
if (this.options.extractComments.banner !== false) { | ||
let banner = this.options.extractComments.banner || `For license information please see ${_path.default.posix.basename(commentsFile)}`; | ||
let banner = this.options.extractComments.banner || `For license information please see ${_path.default.relative(_path.default.dirname(file), commentsFilename).replace(/\\/g, '/')}`; | ||
if (typeof banner === 'function') { | ||
banner = banner(commentsFile); | ||
banner = banner(commentsFilename); | ||
} | ||
@@ -301,14 +340,14 @@ | ||
if (commentsFile in compilation.assets) { | ||
if (commentsFilename in compilation.assets) { | ||
// commentsFile already exists, append new comments... | ||
if (compilation.assets[commentsFile] instanceof _webpackSources.ConcatSource) { | ||
compilation.assets[commentsFile].add('\n'); | ||
compilation.assets[commentsFile].add(commentsSource); | ||
if (compilation.assets[commentsFilename] instanceof _webpackSources.ConcatSource) { | ||
compilation.assets[commentsFilename].add('\n'); | ||
compilation.assets[commentsFilename].add(commentsSource); | ||
} else { | ||
// eslint-disable-next-line no-param-reassign | ||
compilation.assets[commentsFile] = new _webpackSources.ConcatSource(compilation.assets[commentsFile], '\n', commentsSource); | ||
compilation.assets[commentsFilename] = new _webpackSources.ConcatSource(compilation.assets[commentsFilename], '\n', commentsSource); | ||
} | ||
} else { | ||
// eslint-disable-next-line no-param-reassign | ||
compilation.assets[commentsFile] = commentsSource; | ||
compilation.assets[commentsFilename] = commentsSource; | ||
} | ||
@@ -315,0 +354,0 @@ } |
{ | ||
"name": "terser-webpack-plugin", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Terser plugin for webpack", | ||
@@ -46,9 +46,9 @@ "license": "MIT", | ||
"source-map": "^0.6.1", | ||
"terser": "^4.2.1", | ||
"terser": "^4.3.1", | ||
"webpack-sources": "^1.4.3" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.5.5", | ||
"@babel/core": "^7.5.5", | ||
"@babel/preset-env": "^7.5.5", | ||
"@babel/cli": "^7.6.0", | ||
"@babel/core": "^7.6.0", | ||
"@babel/preset-env": "^7.6.0", | ||
"@commitlint/cli": "^8.1.0", | ||
@@ -62,5 +62,5 @@ "@commitlint/config-conventional": "^8.1.0", | ||
"del": "^5.1.0", | ||
"del-cli": "^2.0.0", | ||
"del-cli": "^3.0.0", | ||
"eslint": "^6.3.0", | ||
"eslint-config-prettier": "^6.2.0", | ||
"eslint-config-prettier": "^6.3.0", | ||
"eslint-plugin-import": "^2.18.2", | ||
@@ -76,3 +76,3 @@ "husky": "^3.0.5", | ||
"uglify-js": "^3.6.0", | ||
"webpack": "^4.39.3" | ||
"webpack": "^4.40.1" | ||
}, | ||
@@ -79,0 +79,0 @@ "keywords": [ |
@@ -283,3 +283,3 @@ <div align="center"> | ||
Type: `Boolean` | ||
Default: `false` | ||
Default: `false` (see below for details around `devtool`) | ||
@@ -390,3 +390,3 @@ **Works only with `source-map`, `inline-source-map`, `hidden-source-map` and `nosources-source-map` values for the [`devtool`](https://webpack.js.org/configuration/devtool/) option.** | ||
Type: `Boolean|String|RegExp|Function<(node, comment) -> Boolean|Object>|Object` | ||
Default: `false` | ||
Default: `true` | ||
@@ -494,4 +494,7 @@ Whether comments shall be extracted to a separate file, (see [details](https://github.com/webpack/webpack/commit/71933e979e51c533b432658d5e37917f9e71595a)). | ||
condition: /^\**!|@preserve|@license|@cc_on/i, | ||
filename: (file) => { | ||
return `${file}.LICENSE`; | ||
filename: (file, fileData) => { | ||
// A file can contain a query string (for example when you have `output.filename: '[name].js?[chunkhash]'`) | ||
// You must consider this | ||
// The "fileData" argument contains object with "filename", "basename", "query" | ||
return file.replace(/\.(\w+)($|\?)/, '.$1.LICENSE$2'); | ||
}, | ||
@@ -524,4 +527,6 @@ banner: (licenseFile) => { | ||
condition: 'some', | ||
filename: (file) => { | ||
return `${file}.LICENSE`; | ||
filename: (file, fileData) => { | ||
// A file can contain a query string (for example when you have `output.filename: '[name].js?[chunkhash]'`) | ||
// You must consider this | ||
return file.replace(/\.(\w+)($|\?)/, '.$1.LICENSE$2'); | ||
}, | ||
@@ -541,4 +546,6 @@ banner: (licenseFile) => { | ||
Type: `String|Function<(string) -> String>` | ||
Default: `${file}.LICENSE` | ||
Default: `[file].LICENSE[query]` | ||
Available placeholders: `[file]`, `[query]` and `[filebase]`. | ||
The file where the extracted comments will be stored. | ||
@@ -587,4 +594,6 @@ Default is to append the suffix `.LICENSE` to the original filename. | ||
condition: true, | ||
filename: (file) => { | ||
return `${file}.LICENSE`; | ||
filename: (file, fileData) => { | ||
// A file can contain a query string (for example when you have `output.filename: '[name].js?[chunkhash]'`) | ||
// You must consider this | ||
return file.replace(/\.(\w+)($|\?)/, '.$1.LICENSE$2'); | ||
}, | ||
@@ -591,0 +600,0 @@ banner: (commentsFile) => { |
53920
731
753
Updatedterser@^4.3.1