Skip to content

Commit 787b748

Browse files
fix: improve perf
1 parent 445e75b commit 787b748

10 files changed

+93
-59
lines changed

.cspell.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"colortable",
3838
"wagoid",
3939
"autocrlf",
40-
"codecov"
40+
"codecov",
41+
"jridgewell"
4142
],
4243

4344
"ignorePaths": [

package-lock.json

+15-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@
6969
}
7070
},
7171
"dependencies": {
72+
"@jridgewell/trace-mapping": "^0.3.18",
7273
"cssnano": "^6.0.1",
7374
"jest-worker": "^29.4.3",
7475
"postcss": "^8.4.23",
7576
"schema-utils": "^4.0.1",
76-
"serialize-javascript": "^6.0.1",
77-
"source-map": "^0.6.1"
77+
"serialize-javascript": "^6.0.1"
7878
},
7979
"devDependencies": {
8080
"@babel/cli": "^7.21.5",
@@ -113,6 +113,7 @@
113113
"prettier": "^2.8.8",
114114
"sass": "^1.62.1",
115115
"sass-loader": "^13.2.0",
116+
"source-map": "^0.6.1",
116117
"standard-version": "^9.5.0",
117118
"sugarss": "^4.0.1",
118119
"typescript": "^5.0.4",

src/index.js

+53-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
const os = require("os");
22

3-
const { SourceMapConsumer } = require("source-map");
43
const { validate } = require("schema-utils");
5-
const serialize = require("serialize-javascript");
6-
const { Worker } = require("jest-worker");
74

85
const {
96
throttleAll,
@@ -24,12 +21,13 @@ const { minify: minifyWorker } = require("./minify");
2421
/** @typedef {import("webpack").Compilation} Compilation */
2522
/** @typedef {import("webpack").WebpackError} WebpackError */
2623
/** @typedef {import("jest-worker").Worker} JestWorker */
27-
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
24+
/** @typedef {import("@jridgewell/trace-mapping").EncodedSourceMap} RawSourceMap */
2825
/** @typedef {import("webpack").Asset} Asset */
2926
/** @typedef {import("postcss").ProcessOptions} ProcessOptions */
3027
/** @typedef {import("postcss").Syntax} Syntax */
3128
/** @typedef {import("postcss").Parser} Parser */
3229
/** @typedef {import("postcss").Stringifier} Stringifier */
30+
/** @typedef {import("@jridgewell/trace-mapping").TraceMap} TraceMap */
3331

3432
/**
3533
* @typedef {Object} CssNanoOptions
@@ -153,6 +151,40 @@ const { minify: minifyWorker } = require("./minify");
153151

154152
const warningRegex = /\s.+:+([0-9]+):+([0-9]+)/;
155153

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+
156188
/**
157189
* @template [T=CssNanoOptionsExtended]
158190
*/
@@ -215,7 +247,7 @@ class CssMinimizerPlugin {
215247
* @param {Warning | WarningObject | string} warning
216248
* @param {string} file
217249
* @param {WarningsFilter} [warningsFilter]
218-
* @param {SourceMapConsumer} [sourceMap]
250+
* @param {TraceMap} [sourceMap]
219251
* @param {Compilation["requestShortener"]} [requestShortener]
220252
* @returns {Error & { hideStack?: boolean, file?: string } | undefined}
221253
*/
@@ -251,10 +283,12 @@ class CssMinimizerPlugin {
251283
}
252284

253285
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+
});
258292

259293
if (
260294
original &&
@@ -297,7 +331,7 @@ class CssMinimizerPlugin {
297331
* @private
298332
* @param {Error | ErrorObject | string} error
299333
* @param {string} file
300-
* @param {SourceMapConsumer} [sourceMap]
334+
* @param {TraceMap} [sourceMap]
301335
* @param {Compilation["requestShortener"]} [requestShortener]
302336
* @returns {Error}
303337
*/
@@ -322,7 +356,8 @@ class CssMinimizerPlugin {
322356
/** @type {ErrorObject & { line: number, column: number }} */ (error);
323357

324358
const original =
325-
sourceMap && sourceMap.originalPositionFor({ line, column });
359+
sourceMap &&
360+
getTraceMapping().originalPositionFor(sourceMap, { line, column });
326361

327362
if (original && original.source && requestShortener) {
328363
builtError = new Error(
@@ -460,6 +495,9 @@ class CssMinimizerPlugin {
460495
return initializedWorker;
461496
}
462497

498+
// eslint-disable-next-line global-require
499+
const { Worker } = require("jest-worker");
500+
463501
initializedWorker = /** @type {MinimizerWorker<T>} */ (
464502
new Worker(require.resolve("./minify"), {
465503
numWorkers: numberOfWorkers,
@@ -535,7 +573,7 @@ class CssMinimizerPlugin {
535573

536574
try {
537575
result = await (getWorker
538-
? getWorker().transform(serialize(options))
576+
? getWorker().transform(getSerializeJavascript()(options))
539577
: minifyWorker(options));
540578
} catch (error) {
541579
const hasSourceMap =
@@ -547,7 +585,7 @@ class CssMinimizerPlugin {
547585
/** @type {any} */ (error),
548586
name,
549587
hasSourceMap
550-
? new SourceMapConsumer(
588+
? new (getTraceMapping().TraceMap)(
551589
/** @type {RawSourceMap} */ (inputSourceMap)
552590
)
553591
: // eslint-disable-next-line no-undefined
@@ -600,7 +638,7 @@ class CssMinimizerPlugin {
600638
error,
601639
name,
602640
hasSourceMap
603-
? new SourceMapConsumer(
641+
? new (getTraceMapping().TraceMap)(
604642
/** @type {RawSourceMap} */ (inputSourceMap)
605643
)
606644
: // eslint-disable-next-line no-undefined
@@ -622,7 +660,7 @@ class CssMinimizerPlugin {
622660
name,
623661
this.options.warningsFilter,
624662
hasSourceMap
625-
? new SourceMapConsumer(
663+
? new (getTraceMapping().TraceMap)(
626664
/** @type {RawSourceMap} */ (inputSourceMap)
627665
)
628666
: // eslint-disable-next-line no-undefined

src/minify.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/** @typedef {import("./index.js").MinimizedResult} MinimizedResult */
2-
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
32
/** @typedef {import("./index.js").InternalResult} InternalResult */
43

54
/**

src/utils.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/** @typedef {import("./index.js").Input} Input */
2-
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
3-
/** @typedef {import("source-map").SourceMapGenerator} SourceMapGenerator */
2+
/** @typedef {import("@jridgewell/trace-mapping").EncodedSourceMap} RawSourceMap */
43
/** @typedef {import("./index.js").MinimizedResult} MinimizedResult */
54
/** @typedef {import("./index.js").CustomOptions} CustomOptions */
65
/** @typedef {import("postcss").ProcessOptions} ProcessOptions */
@@ -178,6 +177,7 @@ async function cssnanoMinify(
178177

179178
return {
180179
code: result.css,
180+
// @ts-ignore
181181
map: result.map
182182
? result.map.toJSON()
183183
: // eslint-disable-next-line no-undefined
@@ -206,7 +206,7 @@ async function cssoMinify(input, sourceMap, minimizerOptions) {
206206
return {
207207
code: result.css,
208208
map: result.map
209-
? /** @type {SourceMapGenerator & { toJSON(): RawSourceMap }} */
209+
? /** @type {any & { toJSON(): RawSourceMap }} */
210210
(result.map).toJSON()
211211
: // eslint-disable-next-line no-undefined
212212
undefined,
@@ -232,7 +232,7 @@ async function cleanCssMinify(input, sourceMap, minimizerOptions) {
232232

233233
const generatedSourceMap =
234234
result.sourceMap &&
235-
/** @type {SourceMapGenerator & { toJSON(): RawSourceMap }} */
235+
/** @type {any & { toJSON(): RawSourceMap }} */
236236
(result.sourceMap).toJSON();
237237

238238
// workaround for source maps on windows

test/CssMinimizerPlugin.test.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from "path";
22

3-
import { SourceMapConsumer } from "source-map";
3+
import { TraceMap } from "@jridgewell/trace-mapping";
44
import MiniCssExtractPlugin from "mini-css-extract-plugin";
55
import CopyPlugin from "copy-webpack-plugin";
66
import RequestShortener from "webpack/lib/RequestShortener";
@@ -152,7 +152,7 @@ describe("CssMinimizerPlugin", () => {
152152
CssMinimizerPlugin.buildError(
153153
errorWithLineAndCol,
154154
"test.css",
155-
new SourceMapConsumer(rawSourceMap)
155+
new TraceMap(rawSourceMap)
156156
)
157157
).toMatchSnapshot();
158158

@@ -166,7 +166,7 @@ describe("CssMinimizerPlugin", () => {
166166
CssMinimizerPlugin.buildError(
167167
otherErrorWithLineAndCol,
168168
"test.css",
169-
new SourceMapConsumer(rawSourceMap),
169+
new TraceMap(rawSourceMap),
170170
new RequestShortener("/example.com/www/js/")
171171
)
172172
).toMatchSnapshot();
@@ -193,7 +193,7 @@ describe("CssMinimizerPlugin", () => {
193193
"test.css",
194194
// eslint-disable-next-line no-undefined
195195
undefined,
196-
new SourceMapConsumer(rawSourceMap)
196+
new TraceMap(rawSourceMap)
197197
)
198198
).toMatchSnapshot();
199199
expect(
@@ -202,7 +202,7 @@ describe("CssMinimizerPlugin", () => {
202202
"test.css",
203203
// eslint-disable-next-line no-undefined
204204
undefined,
205-
new SourceMapConsumer(rawSourceMap),
205+
new TraceMap(rawSourceMap),
206206
new RequestShortener("/example.com/www/js/")
207207
)
208208
).toMatchSnapshot();
@@ -211,7 +211,7 @@ describe("CssMinimizerPlugin", () => {
211211
"Warning test.css:1:1",
212212
"test.css",
213213
() => true,
214-
new SourceMapConsumer(rawSourceMap),
214+
new TraceMap(rawSourceMap),
215215
new RequestShortener("/example.com/www/js/")
216216
)
217217
).toMatchSnapshot();
@@ -220,7 +220,7 @@ describe("CssMinimizerPlugin", () => {
220220
"Warning test.css:1:1",
221221
"test.css",
222222
() => false,
223-
new SourceMapConsumer(rawSourceMap),
223+
new TraceMap(rawSourceMap),
224224
new RequestShortener("/example.com/www/js/")
225225
)
226226
).toMatchSnapshot();
@@ -234,7 +234,7 @@ describe("CssMinimizerPlugin", () => {
234234
"test.css",
235235
// eslint-disable-next-line no-undefined
236236
undefined,
237-
new SourceMapConsumer(rawSourceMap),
237+
new TraceMap(rawSourceMap),
238238
new RequestShortener("/example.com/www/js/")
239239
)
240240
).toMatchSnapshot();

0 commit comments

Comments
 (0)