Skip to content

Commit 10b4db8

Browse files
fix: stuff
1 parent 49d5491 commit 10b4db8

File tree

8 files changed

+215
-554
lines changed

8 files changed

+215
-554
lines changed

package-lock.json

Lines changed: 34 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
},
5757
"dependencies": {
5858
"jest-worker": "^27.0.6",
59+
"merge-source-map": "^1.1.0",
5960
"p-limit": "^3.1.0",
6061
"schema-utils": "^3.1.1",
6162
"serialize-javascript": "^6.0.0",
@@ -91,7 +92,7 @@
9192
"standard-version": "^9.3.1",
9293
"typescript": "^4.3.5",
9394
"uglify-js": "^3.14.1",
94-
"webpack": "^5.48.0",
95+
"webpack": "^5.51.1",
9596
"worker-loader": "^3.0.8"
9697
},
9798
"keywords": [

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,11 @@ class TerserPlugin {
441441
inputSourceMap,
442442
// TODO rename to `minimizer` in the next major release
443443
minimizer: this.options.minify,
444-
minimizerOptions: { ...this.options.terserOptions },
444+
minimizerOptions: Array.isArray(this.options.terserOptions)
445+
? this.options.terserOptions.map((item) => {
446+
return { ...item };
447+
})
448+
: { ...this.options.terserOptions },
445449
extractComments: this.options.extractComments,
446450
};
447451

src/minify.js

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** @typedef {import("./index.js").InternalMinifyOptions} InternalMinifyOptions */
22
/** @typedef {import("./index.js").MinifyResult} MinifyResult */
3+
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
34

45
/**
56
* @param {InternalMinifyOptions} options
@@ -14,25 +15,108 @@ async function minify(options) {
1415
/**
1516
* @type {MinifyResult}
1617
*/
17-
const result = { code: options.input, map: options.inputSourceMap };
18+
const result = { code: options.input };
19+
20+
/**
21+
* @param {RawSourceMap | undefined} oldMap
22+
* @param {RawSourceMap | undefined} newMap
23+
* @returns {RawSourceMap | undefined}
24+
*/
25+
const mergeSourceMap = (oldMap, newMap) => {
26+
if (!oldMap) {
27+
return newMap;
28+
}
29+
30+
if (!newMap) {
31+
return oldMap;
32+
}
33+
34+
// eslint-disable-next-line global-require
35+
const sourceMap = require("source-map");
36+
const oldMapConsumer = new sourceMap.SourceMapConsumer(oldMap);
37+
const newMapConsumer = new sourceMap.SourceMapConsumer(newMap);
38+
const mergedMapGenerator = new sourceMap.SourceMapGenerator();
39+
40+
newMapConsumer.eachMapping((m) => {
41+
// pass when `originalLine` is null.
42+
// It occurs in case that the node does not have origin in original code.
43+
if (m.originalLine == null) {
44+
return;
45+
}
46+
47+
const origPosInOldMap = oldMapConsumer.originalPositionFor({
48+
line: m.originalLine,
49+
column: m.originalColumn,
50+
});
51+
52+
if (origPosInOldMap.source == null) {
53+
return;
54+
}
55+
56+
mergedMapGenerator.addMapping({
57+
original: {
58+
line: origPosInOldMap.line,
59+
column: origPosInOldMap.column,
60+
},
61+
generated: {
62+
line: m.generatedLine,
63+
column: m.generatedColumn,
64+
},
65+
source: origPosInOldMap.source,
66+
name: origPosInOldMap.name,
67+
});
68+
});
69+
70+
const consumers = [oldMapConsumer, newMapConsumer];
71+
72+
consumers.forEach((consumer) => {
73+
// @ts-ignore
74+
consumer.sources.forEach(
75+
/**
76+
* @param {string} sourceFile
77+
*/
78+
(sourceFile) => {
79+
// @ts-ignore
80+
// eslint-disable-next-line no-underscore-dangle
81+
mergedMapGenerator._sources.add(sourceFile);
82+
83+
const sourceContent = consumer.sourceContentFor(sourceFile);
84+
85+
if (sourceContent != null) {
86+
mergedMapGenerator.setSourceContent(sourceFile, sourceContent);
87+
}
88+
}
89+
);
90+
});
91+
92+
const merged = JSON.parse(mergedMapGenerator.toString());
93+
94+
merged.sourceRoot = oldMap.sourceRoot;
95+
merged.file = oldMap.file;
96+
97+
return merged;
98+
};
99+
100+
const { inputSourceMap } = options;
18101

19102
for (let i = 0; i <= minifyFns.length - 1; i++) {
20103
const minifyFn = minifyFns[i];
21104
const minifyOptions = Array.isArray(options.minimizerOptions)
22105
? options.minimizerOptions[i]
23106
: options.minimizerOptions;
107+
24108
// eslint-disable-next-line no-await-in-loop
25109
const minifyResult = await minifyFn(
26110
{ [options.name]: result.code },
27-
result.map,
111+
result.map || inputSourceMap,
28112
minifyOptions,
29113
options.extractComments
30114
);
31115

32116
result.code = minifyResult.code;
33117

34118
if (minifyResult.map) {
35-
result.map = minifyResult.map;
119+
result.map = mergeSourceMap(inputSourceMap, minifyResult.map);
36120
}
37121

38122
if (minifyResult.warnings && minifyResult.warnings.length > 0) {

0 commit comments

Comments
 (0)