1
1
/** @typedef {import("./index.js").InternalMinifyOptions } InternalMinifyOptions */
2
2
/** @typedef {import("./index.js").MinifyResult } MinifyResult */
3
+ /** @typedef {import("source-map").RawSourceMap } RawSourceMap */
3
4
4
5
/**
5
6
* @param {InternalMinifyOptions } options
@@ -14,25 +15,108 @@ async function minify(options) {
14
15
/**
15
16
* @type {MinifyResult }
16
17
*/
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 ;
18
101
19
102
for ( let i = 0 ; i <= minifyFns . length - 1 ; i ++ ) {
20
103
const minifyFn = minifyFns [ i ] ;
21
104
const minifyOptions = Array . isArray ( options . minimizerOptions )
22
105
? options . minimizerOptions [ i ]
23
106
: options . minimizerOptions ;
107
+
24
108
// eslint-disable-next-line no-await-in-loop
25
109
const minifyResult = await minifyFn (
26
110
{ [ options . name ] : result . code } ,
27
- result . map ,
111
+ result . map || inputSourceMap ,
28
112
minifyOptions ,
29
113
options . extractComments
30
114
) ;
31
115
32
116
result . code = minifyResult . code ;
33
117
34
118
if ( minifyResult . map ) {
35
- result . map = minifyResult . map ;
119
+ result . map = mergeSourceMap ( inputSourceMap , minifyResult . map ) ;
36
120
}
37
121
38
122
if ( minifyResult . warnings && minifyResult . warnings . length > 0 ) {
0 commit comments