Skip to content

Commit b944353

Browse files
fix: compatibility with the format option for terser
1 parent a79ea67 commit b944353

File tree

5 files changed

+152
-23
lines changed

5 files changed

+152
-23
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ module.exports = {
234234
compress: {},
235235
mangle: true, // Note `mangle.properties` is `false` by default.
236236
module: false,
237+
// Deprecated
237238
output: null,
239+
format: null,
238240
toplevel: false,
239241
nameCache: null,
240242
ie8: false,
@@ -256,7 +258,7 @@ Default: `true`
256258
Whether comments shall be extracted to a separate file, (see [details](https://github.com/webpack/webpack/commit/71933e979e51c533b432658d5e37917f9e71595a)).
257259
By default extract only comments using `/^\**!|@preserve|@license|@cc_on/i` regexp condition and remove remaining comments.
258260
If the original file is named `foo.js`, then the comments will be stored to `foo.js.LICENSE.txt`.
259-
The `terserOptions.output.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.
261+
The `terserOptions.format.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.
260262

261263
#### `Boolean`
262264

@@ -479,7 +481,7 @@ module.exports = {
479481
minimizer: [
480482
new TerserPlugin({
481483
terserOptions: {
482-
output: {
484+
format: {
483485
comments: /@license/i,
484486
},
485487
},
@@ -503,7 +505,7 @@ module.exports = {
503505
minimizer: [
504506
new TerserPlugin({
505507
terserOptions: {
506-
output: {
508+
format: {
507509
comments: false,
508510
},
509511
},

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,17 @@ import { minify as minifyFn } from './minify';
6363
* @param {MinifyOptions} minifyOptions
6464
*/
6565

66+
/**
67+
* @typedef {ExtractCommentsCondition | ExtractCommentsObject} ExtractCommentsOptions
68+
*/
69+
6670
/**
6771
* @typedef {Object} TerserPluginOptions
6872
* @property {Rules} [test]
6973
* @property {Rules} [include]
7074
* @property {Rules} [exclude]
7175
* @property {MinifyOptions} [terserOptions]
72-
* @property {ExtractCommentsCondition | ExtractCommentsObject} [extractComments]
76+
* @property {ExtractCommentsOptions} [extractComments]
7377
* @property {boolean} [parallel]
7478
* @property {CustomMinifyFunction} [minify]
7579
*/

src/minify.js

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
const { minify: terserMinify } = require('terser');
22

3+
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
4+
/** @typedef {import("./index.js").ExtractCommentsOptions} ExtractCommentsOptions */
5+
/** @typedef {import("./index.js").CustomMinifyFunction} CustomMinifyFunction */
6+
/** @typedef {import("./index.js").MinifyOptions} MinifyOptions */
37
/** @typedef {import("terser").MinifyOptions} TerserMinifyOptions */
48
/** @typedef {import("terser").MinifyOutput} MinifyOutput */
59
/** @typedef {import("terser").FormatOptions} FormatOptions */
610
/** @typedef {import("terser").MangleOptions} MangleOptions */
7-
/** @typedef {import("source-map").RawSourceMap} SourceMapRawSourceMap */
811
/** @typedef {import("./index.js").ExtractCommentsFunction} ExtractCommentsFunction */
912
/** @typedef {import("./index.js").ExtractCommentsCondition} ExtractCommentsCondition */
1013

1114
/**
1215
* @typedef {Object} InternalMinifyOptions
1316
* @property {string} name
1417
* @property {string} input
15-
* @property {any} inputSourceMap
16-
* @property {any} extractComments
17-
* @property {any} minify
18-
* @property {any} minifyOptions
18+
* @property {RawSourceMap} inputSourceMap
19+
* @property {ExtractCommentsOptions} extractComments
20+
* @property {CustomMinifyFunction} minify
21+
* @property {MinifyOptions} minifyOptions
1922
*/
2023

2124
/**
@@ -27,7 +30,7 @@ const { minify: terserMinify } = require('terser');
2730
*/
2831

2932
/**
30-
* @typedef {TerserMinifyOptions & { mangle: MangleOptions, output: FormatOptions & { beautify: boolean }, sourceMap: undefined }} NormalizedTerserMinifyOptions
33+
* @typedef {TerserMinifyOptions & { sourceMap: undefined } & ({ output: FormatOptions & { beautify: boolean } } | { format: FormatOptions & { beautify: boolean } })} NormalizedTerserMinifyOptions
3134
*/
3235

3336
/**
@@ -43,14 +46,13 @@ function buildTerserOptions(terserOptions = {}) {
4346
: typeof terserOptions.mangle === 'boolean'
4447
? terserOptions.mangle
4548
: { ...terserOptions.mangle },
46-
// Deprecated
47-
output: {
48-
beautify: false,
49-
...terserOptions.output,
50-
},
5149
// Ignoring sourceMap from options
5250
// eslint-disable-next-line no-undefined
5351
sourceMap: undefined,
52+
// the `output` option is deprecated
53+
...(terserOptions.format
54+
? { format: { beautify: false, ...terserOptions.format } }
55+
: { output: { beautify: false, ...terserOptions.output } }),
5456
};
5557
}
5658

@@ -65,15 +67,22 @@ function isObject(value) {
6567
}
6668

6769
/**
68-
* @param {any} extractComments
70+
* @param {ExtractCommentsOptions} extractComments
6971
* @param {NormalizedTerserMinifyOptions} terserOptions
7072
* @param {ExtractedComments} extractedComments
7173
* @returns {ExtractCommentsFunction}
7274
*/
7375
function buildComments(extractComments, terserOptions, extractedComments) {
7476
/** @type {{ [index: string]: ExtractCommentsCondition }} */
7577
const condition = {};
76-
const { comments } = terserOptions.output;
78+
79+
let comments;
80+
81+
if (terserOptions.format) {
82+
({ comments } = terserOptions.format);
83+
} else if (terserOptions.output) {
84+
({ comments } = terserOptions.output);
85+
}
7786

7887
condition.preserve = typeof comments !== 'undefined' ? comments : false;
7988

@@ -86,7 +95,7 @@ function buildComments(extractComments, terserOptions, extractedComments) {
8695
condition.extract = extractComments;
8796
} else if (typeof extractComments === 'function') {
8897
condition.extract = extractComments;
89-
} else if (isObject(extractComments)) {
98+
} else if (extractComments && isObject(extractComments)) {
9099
condition.extract =
91100
typeof extractComments.condition === 'boolean' &&
92101
extractComments.condition
@@ -213,11 +222,19 @@ async function minify(options) {
213222
const extractedComments = [];
214223
const { extractComments } = options;
215224

216-
terserOptions.output.comments = buildComments(
217-
extractComments,
218-
terserOptions,
219-
extractedComments
220-
);
225+
if (terserOptions.output) {
226+
terserOptions.output.comments = buildComments(
227+
extractComments,
228+
terserOptions,
229+
extractedComments
230+
);
231+
} else if (terserOptions.format) {
232+
terserOptions.format.comments = buildComments(
233+
extractComments,
234+
terserOptions,
235+
extractedComments
236+
);
237+
}
221238

222239
const result = await terserMinify({ [name]: input }, terserOptions);
223240

test/__snapshots__/terserOptions-option.test.js.snap

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,76 @@ exports[`terserOptions option should match snapshot for the "ecma" option with t
322322

323323
exports[`terserOptions option should match snapshot for the "ecma" option with the "8" value: warnings 1`] = `Array []`;
324324

325+
exports[`terserOptions option should match snapshot for the "format.beautify" option with "true" value: assets 1`] = `
326+
Object {
327+
"main.js": "(() => {
328+
var r = {
329+
791: r => {
330+
r.exports = function() {
331+
console.log(7);
332+
};
333+
}
334+
}, o = {};
335+
!function t(e) {
336+
if (o[e]) return o[e].exports;
337+
var n = o[e] = {
338+
exports: {}
339+
};
340+
return r[e](n, n.exports, t), n.exports;
341+
}(791);
342+
})();",
343+
}
344+
`;
345+
346+
exports[`terserOptions option should match snapshot for the "format.beautify" option with "true" value: errors 1`] = `Array []`;
347+
348+
exports[`terserOptions option should match snapshot for the "format.beautify" option with "true" value: warnings 1`] = `Array []`;
349+
350+
exports[`terserOptions option should match snapshot for the "format.comments" option with the "true": assets 1`] = `
351+
Object {
352+
"main.js": "/******/(()=>{// webpackBootstrap
353+
/******/var r={
354+
/***/791:
355+
/***/r=>{r.exports=function(){console.log(7)}}
356+
/***/
357+
/******/},o={};
358+
/************************************************************************/
359+
/******/ // The module cache
360+
/******/
361+
/******/
362+
/************************************************************************/
363+
/******/ // startup
364+
/******/ // Load entry module
365+
/******/ // This entry module is referenced by other modules so it can't be inlined
366+
/******/!
367+
/******/
368+
/******/ // The require function
369+
/******/function t(e){
370+
/******/ // Check if module is in cache
371+
/******/if(o[e])
372+
/******/return o[e].exports;
373+
/******/
374+
/******/ // Create a new module (and put it into the cache)
375+
/******/var n=o[e]={
376+
/******/ // no module.id needed
377+
/******/ // no module.loaded needed
378+
/******/exports:{}
379+
/******/};
380+
/******/
381+
/******/ // Execute the module function
382+
/******/
383+
/******/
384+
/******/ // Return the exports of the module
385+
/******/return r[e](n,n.exports,t),n.exports;
386+
/******/}(791)})
387+
/******/();",
388+
}
389+
`;
390+
391+
exports[`terserOptions option should match snapshot for the "format.comments" option with the "true": errors 1`] = `Array []`;
392+
393+
exports[`terserOptions option should match snapshot for the "format.comments" option with the "true": warnings 1`] = `Array []`;
394+
325395
exports[`terserOptions option should match snapshot for the "ie8" option with the "false" value: assets 1`] = `
326396
Object {
327397
"main.js": "(()=>{var r={791:r=>{r.exports=function(){console.log(7)}}},o={};!function t(e){if(o[e])return o[e].exports;var n=o[e]={exports:{}};return r[e](n,n.exports,t),n.exports}(791)})();",

test/terserOptions-option.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,42 @@ describe('terserOptions option', () => {
351351
expect(getWarnings(stats)).toMatchSnapshot('warnings');
352352
});
353353

354+
it('should match snapshot for the "format.beautify" option with "true" value', async () => {
355+
const compiler = getCompiler();
356+
357+
new TerserPlugin({
358+
terserOptions: {
359+
format: {
360+
beautify: true,
361+
},
362+
},
363+
}).apply(compiler);
364+
365+
const stats = await compile(compiler);
366+
367+
expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
368+
expect(getErrors(stats)).toMatchSnapshot('errors');
369+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
370+
});
371+
372+
it('should match snapshot for the "format.comments" option with the "true"', async () => {
373+
const compiler = getCompiler();
374+
375+
new TerserPlugin({
376+
terserOptions: {
377+
format: {
378+
comments: true,
379+
},
380+
},
381+
}).apply(compiler);
382+
383+
const stats = await compile(compiler);
384+
385+
expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
386+
expect(getErrors(stats)).toMatchSnapshot('errors');
387+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
388+
});
389+
354390
it('should match snapshot for the "toplevel" option with the "false" value', async () => {
355391
const compiler = getCompiler();
356392

0 commit comments

Comments
 (0)