1
1
const os = require ( "os" ) ;
2
2
3
- const { SourceMapConsumer } = require ( "source-map" ) ;
4
3
const { validate } = require ( "schema-utils" ) ;
5
- const serialize = require ( "serialize-javascript" ) ;
6
- const { Worker } = require ( "jest-worker" ) ;
7
4
8
5
const {
9
6
throttleAll,
@@ -24,12 +21,13 @@ const { minify: minifyWorker } = require("./minify");
24
21
/** @typedef {import("webpack").Compilation } Compilation */
25
22
/** @typedef {import("webpack").WebpackError } WebpackError */
26
23
/** @typedef {import("jest-worker").Worker } JestWorker */
27
- /** @typedef {import("source-map ").RawSourceMap } RawSourceMap */
24
+ /** @typedef {import("@jridgewell/trace-mapping ").EncodedSourceMap } RawSourceMap */
28
25
/** @typedef {import("webpack").Asset } Asset */
29
26
/** @typedef {import("postcss").ProcessOptions } ProcessOptions */
30
27
/** @typedef {import("postcss").Syntax } Syntax */
31
28
/** @typedef {import("postcss").Parser } Parser */
32
29
/** @typedef {import("postcss").Stringifier } Stringifier */
30
+ /** @typedef {import("@jridgewell/trace-mapping").TraceMap } TraceMap */
33
31
34
32
/**
35
33
* @typedef {Object } CssNanoOptions
@@ -153,6 +151,40 @@ const { minify: minifyWorker } = require("./minify");
153
151
154
152
const warningRegex = / \s .+ : + ( [ 0 - 9 ] + ) : + ( [ 0 - 9 ] + ) / ;
155
153
154
+ /**
155
+ * @template T
156
+ * @param fn {(function(): any) | undefined}
157
+ * @returns {function(): T }
158
+ */
159
+ const memoize = ( fn ) => {
160
+ let cache = false ;
161
+ /** @type {T } */
162
+ let result ;
163
+
164
+ return ( ) => {
165
+ if ( cache ) {
166
+ return result ;
167
+ }
168
+ result = /** @type {function(): any } */ ( fn ) ( ) ;
169
+ cache = true ;
170
+ // Allow to clean up memory for fn
171
+ // and all dependent resources
172
+ // eslint-disable-next-line no-undefined, no-param-reassign
173
+ fn = undefined ;
174
+
175
+ return result ;
176
+ } ;
177
+ } ;
178
+
179
+ const getSerializeJavascript = memoize ( ( ) =>
180
+ // eslint-disable-next-line global-require
181
+ require ( "serialize-javascript" )
182
+ ) ;
183
+ const getTraceMapping = memoize ( ( ) =>
184
+ // eslint-disable-next-line global-require
185
+ require ( "@jridgewell/trace-mapping" )
186
+ ) ;
187
+
156
188
/**
157
189
* @template [T=CssNanoOptionsExtended]
158
190
*/
@@ -215,7 +247,7 @@ class CssMinimizerPlugin {
215
247
* @param {Warning | WarningObject | string } warning
216
248
* @param {string } file
217
249
* @param {WarningsFilter } [warningsFilter]
218
- * @param {SourceMapConsumer } [sourceMap]
250
+ * @param {TraceMap } [sourceMap]
219
251
* @param {Compilation["requestShortener"] } [requestShortener]
220
252
* @returns {Error & { hideStack?: boolean, file?: string } | undefined }
221
253
*/
@@ -251,10 +283,12 @@ class CssMinimizerPlugin {
251
283
}
252
284
253
285
if ( line && column ) {
254
- const original = sourceMap . originalPositionFor ( {
255
- line,
256
- column,
257
- } ) ;
286
+ const original =
287
+ sourceMap &&
288
+ getTraceMapping ( ) . originalPositionFor ( sourceMap , {
289
+ line,
290
+ column,
291
+ } ) ;
258
292
259
293
if (
260
294
original &&
@@ -297,7 +331,7 @@ class CssMinimizerPlugin {
297
331
* @private
298
332
* @param {Error | ErrorObject | string } error
299
333
* @param {string } file
300
- * @param {SourceMapConsumer } [sourceMap]
334
+ * @param {TraceMap } [sourceMap]
301
335
* @param {Compilation["requestShortener"] } [requestShortener]
302
336
* @returns {Error }
303
337
*/
@@ -322,7 +356,8 @@ class CssMinimizerPlugin {
322
356
/** @type {ErrorObject & { line: number, column: number } } */ ( error ) ;
323
357
324
358
const original =
325
- sourceMap && sourceMap . originalPositionFor ( { line, column } ) ;
359
+ sourceMap &&
360
+ getTraceMapping ( ) . originalPositionFor ( sourceMap , { line, column } ) ;
326
361
327
362
if ( original && original . source && requestShortener ) {
328
363
builtError = new Error (
@@ -460,6 +495,9 @@ class CssMinimizerPlugin {
460
495
return initializedWorker ;
461
496
}
462
497
498
+ // eslint-disable-next-line global-require
499
+ const { Worker } = require ( "jest-worker" ) ;
500
+
463
501
initializedWorker = /** @type {MinimizerWorker<T> } */ (
464
502
new Worker ( require . resolve ( "./minify" ) , {
465
503
numWorkers : numberOfWorkers ,
@@ -535,7 +573,7 @@ class CssMinimizerPlugin {
535
573
536
574
try {
537
575
result = await ( getWorker
538
- ? getWorker ( ) . transform ( serialize ( options ) )
576
+ ? getWorker ( ) . transform ( getSerializeJavascript ( ) ( options ) )
539
577
: minifyWorker ( options ) ) ;
540
578
} catch ( error ) {
541
579
const hasSourceMap =
@@ -547,7 +585,7 @@ class CssMinimizerPlugin {
547
585
/** @type {any } */ ( error ) ,
548
586
name ,
549
587
hasSourceMap
550
- ? new SourceMapConsumer (
588
+ ? new ( getTraceMapping ( ) . TraceMap ) (
551
589
/** @type {RawSourceMap } */ ( inputSourceMap )
552
590
)
553
591
: // eslint-disable-next-line no-undefined
@@ -600,7 +638,7 @@ class CssMinimizerPlugin {
600
638
error ,
601
639
name ,
602
640
hasSourceMap
603
- ? new SourceMapConsumer (
641
+ ? new ( getTraceMapping ( ) . TraceMap ) (
604
642
/** @type {RawSourceMap } */ ( inputSourceMap )
605
643
)
606
644
: // eslint-disable-next-line no-undefined
@@ -622,7 +660,7 @@ class CssMinimizerPlugin {
622
660
name ,
623
661
this . options . warningsFilter ,
624
662
hasSourceMap
625
- ? new SourceMapConsumer (
663
+ ? new ( getTraceMapping ( ) . TraceMap ) (
626
664
/** @type {RawSourceMap } */ ( inputSourceMap )
627
665
)
628
666
: // eslint-disable-next-line no-undefined
0 commit comments