Skip to content

Commit 6020a94

Browse files
authored
perf: replace source-map package with @jridgewell/trace-mapping (#489)
1 parent 66fdec8 commit 6020a94

File tree

8 files changed

+89
-48
lines changed

8 files changed

+89
-48
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ type minify = (
220220
input: {
221221
[file: string]: string;
222222
},
223-
sourceMap: import("source-map").RawSourceMap | undefined,
223+
sourceMap: import("@jridgewell/trace-mapping").SourceMapInput | undefined,
224224
minifyOptions: {
225225
module?: boolean | undefined;
226226
ecma?: import("terser").ECMA | undefined;
@@ -267,7 +267,7 @@ type minify = (
267267
| undefined
268268
) => Promise<{
269269
code: string;
270-
map?: import("source-map").RawSourceMap | undefined;
270+
map?: import("@jridgewell/trace-mapping").SourceMapInput | undefined;
271271
errors?: (string | Error)[] | undefined;
272272
warnings?: (string | Error)[] | undefined;
273273
extractedComments?: string[] | undefined;

package-lock.json

Lines changed: 42 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@
5555
}
5656
},
5757
"dependencies": {
58+
"@jridgewell/trace-mapping": "^0.3.7",
5859
"jest-worker": "^27.4.5",
5960
"schema-utils": "^3.1.1",
6061
"serialize-javascript": "^6.0.0",
61-
"source-map": "^0.6.1",
6262
"terser": "^5.7.2"
6363
},
6464
"devDependencies": {

src/index.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require("path");
22
const os = require("os");
33

4-
const { SourceMapConsumer } = require("source-map");
4+
const { TraceMap, originalPositionFor } = require("@jridgewell/trace-mapping");
55
const { validate } = require("schema-utils");
66
const serialize = require("serialize-javascript");
77
const { Worker } = require("jest-worker");
@@ -25,7 +25,7 @@ const { minify } = require("./minify");
2525
/** @typedef {import("./utils.js").TerserECMA} TerserECMA */
2626
/** @typedef {import("./utils.js").TerserOptions} TerserOptions */
2727
/** @typedef {import("jest-worker").Worker} JestWorker */
28-
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
28+
/** @typedef {import("@jridgewell/trace-mapping").SourceMapInput} SourceMapInput */
2929

3030
/** @typedef {RegExp | string} Rule */
3131

@@ -64,7 +64,7 @@ const { minify } = require("./minify");
6464
/**
6565
* @typedef {Object} MinimizedResult
6666
* @property {string} code
67-
* @property {RawSourceMap} [map]
67+
* @property {SourceMapInput} [map]
6868
* @property {Array<Error | string>} [errors]
6969
* @property {Array<Error | string>} [warnings]
7070
* @property {Array<string>} [extractedComments]
@@ -98,7 +98,7 @@ const { minify } = require("./minify");
9898
* @template T
9999
* @callback BasicMinimizerImplementation
100100
* @param {Input} input
101-
* @param {RawSourceMap | undefined} sourceMap
101+
* @param {SourceMapInput | undefined} sourceMap
102102
* @param {MinimizerOptions<T>} minifyOptions
103103
* @param {ExtractCommentsOptions | undefined} extractComments
104104
* @returns {Promise<MinimizedResult>}
@@ -119,7 +119,7 @@ const { minify } = require("./minify");
119119
* @typedef {Object} InternalOptions
120120
* @property {string} name
121121
* @property {string} input
122-
* @property {RawSourceMap | undefined} inputSourceMap
122+
* @property {SourceMapInput | undefined} inputSourceMap
123123
* @property {ExtractCommentsOptions | undefined} extractComments
124124
* @property {{ implementation: MinimizerImplementation<T>, options: MinimizerOptions<T> }} minimizer
125125
*/
@@ -199,8 +199,8 @@ class TerserPlugin {
199199
* @returns {boolean}
200200
*/
201201
static isSourceMap(input) {
202-
// All required options for `new SourceMapConsumer(...options)`
203-
// https://github.com/mozilla/source-map#new-sourcemapconsumerrawsourcemap
202+
// All required options for `new TraceMap(...options)`
203+
// https://github.com/jridgewell/trace-mapping#usage
204204
return Boolean(
205205
input &&
206206
input.version &&
@@ -234,7 +234,7 @@ class TerserPlugin {
234234
* @private
235235
* @param {any} error
236236
* @param {string} file
237-
* @param {SourceMapConsumer} [sourceMap]
237+
* @param {TraceMap} [sourceMap]
238238
* @param {Compilation["requestShortener"]} [requestShortener]
239239
* @returns {Error}
240240
*/
@@ -254,7 +254,7 @@ class TerserPlugin {
254254
if (error.line) {
255255
const original =
256256
sourceMap &&
257-
sourceMap.originalPositionFor({
257+
originalPositionFor(sourceMap, {
258258
line: error.line,
259259
column: error.col,
260260
});
@@ -438,7 +438,7 @@ class TerserPlugin {
438438

439439
if (!output) {
440440
let input;
441-
/** @type {RawSourceMap | undefined} */
441+
/** @type {SourceMapInput | undefined} */
442442
let inputSourceMap;
443443

444444
const { source: sourceFromInputSource, map } =
@@ -453,7 +453,7 @@ class TerserPlugin {
453453
(new Error(`${name} contains invalid source map`))
454454
);
455455
} else {
456-
inputSourceMap = /** @type {RawSourceMap} */ (map);
456+
inputSourceMap = /** @type {SourceMapInput} */ (map);
457457
}
458458
}
459459

@@ -507,8 +507,8 @@ class TerserPlugin {
507507
error,
508508
name,
509509
hasSourceMap
510-
? new SourceMapConsumer(
511-
/** @type {RawSourceMap} */ (inputSourceMap)
510+
? new TraceMap(
511+
/** @type {SourceMapInput} */ (inputSourceMap)
512512
)
513513
: // eslint-disable-next-line no-undefined
514514
undefined,
@@ -556,8 +556,8 @@ class TerserPlugin {
556556
item,
557557
name,
558558
hasSourceMap
559-
? new SourceMapConsumer(
560-
/** @type {RawSourceMap} */ (inputSourceMap)
559+
? new TraceMap(
560+
/** @type {SourceMapInput} */ (inputSourceMap)
561561
)
562562
: // eslint-disable-next-line no-undefined
563563
undefined,
@@ -588,7 +588,7 @@ class TerserPlugin {
588588
name,
589589
output.map,
590590
input,
591-
/** @type {RawSourceMap} */ (inputSourceMap),
591+
/** @type {SourceMapInput} */ (inputSourceMap),
592592
true
593593
);
594594
} else {

src/utils.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
1+
/** @typedef {import("@jridgewell/trace-mapping").SourceMapInput} SourceMapInput */
22
/** @typedef {import("terser").FormatOptions} TerserFormatOptions */
33
/** @typedef {import("terser").MinifyOptions} TerserOptions */
44
/** @typedef {import("terser").ECMA} TerserECMA */
@@ -80,7 +80,7 @@ function throttleAll(limit, tasks) {
8080
/* istanbul ignore next */
8181
/**
8282
* @param {Input} input
83-
* @param {RawSourceMap | undefined} sourceMap
83+
* @param {SourceMapInput | undefined} sourceMap
8484
* @param {PredefinedOptions & CustomOptions} minimizerOptions
8585
* @param {ExtractCommentsOptions | undefined} extractComments
8686
* @return {Promise<MinimizedResult>}
@@ -287,7 +287,7 @@ async function terserMinify(
287287
code: /** @type {string} **/ (result.code),
288288
// @ts-ignore
289289
// eslint-disable-next-line no-undefined
290-
map: result.map ? /** @type {RawSourceMap} **/ (result.map) : undefined,
290+
map: result.map ? /** @type {SourceMapInput} **/ (result.map) : undefined,
291291
extractedComments,
292292
};
293293
}
@@ -311,7 +311,7 @@ terserMinify.getMinimizerVersion = () => {
311311
/* istanbul ignore next */
312312
/**
313313
* @param {Input} input
314-
* @param {RawSourceMap | undefined} sourceMap
314+
* @param {SourceMapInput | undefined} sourceMap
315315
* @param {PredefinedOptions & CustomOptions} minimizerOptions
316316
* @param {ExtractCommentsOptions | undefined} extractComments
317317
* @return {Promise<MinimizedResult>}
@@ -529,7 +529,7 @@ uglifyJsMinify.getMinimizerVersion = () => {
529529
/* istanbul ignore next */
530530
/**
531531
* @param {Input} input
532-
* @param {RawSourceMap | undefined} sourceMap
532+
* @param {SourceMapInput | undefined} sourceMap
533533
* @param {PredefinedOptions & CustomOptions} minimizerOptions
534534
* @return {Promise<MinimizedResult>}
535535
*/
@@ -613,7 +613,7 @@ swcMinify.getMinimizerVersion = () => {
613613
/* istanbul ignore next */
614614
/**
615615
* @param {Input} input
616-
* @param {RawSourceMap | undefined} sourceMap
616+
* @param {SourceMapInput | undefined} sourceMap
617617
* @param {PredefinedOptions & CustomOptions} minimizerOptions
618618
* @return {Promise<MinimizedResult>}
619619
*/

test/TerserPlugin.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import crypto from "crypto";
22

33
import path from "path";
44

5-
import { SourceMapConsumer } from "source-map";
5+
import { TraceMap } from "@jridgewell/trace-mapping";
66
import CopyWebpackPlugin from "copy-webpack-plugin";
77
import RequestShortener from "webpack/lib/RequestShortener";
88
import { javascript, SourceMapDevToolPlugin, util } from "webpack";
@@ -551,7 +551,7 @@ describe("TerserPlugin", () => {
551551
TerserPlugin.buildError(
552552
errorWithLineAndCol,
553553
"test.js",
554-
new SourceMapConsumer(rawSourceMap),
554+
new TraceMap(rawSourceMap),
555555
// eslint-disable-next-line no-undefined
556556
undefined
557557
)
@@ -567,7 +567,7 @@ describe("TerserPlugin", () => {
567567
TerserPlugin.buildError(
568568
otherErrorWithLineAndCol,
569569
"test.js",
570-
new SourceMapConsumer(rawSourceMap),
570+
new TraceMap(rawSourceMap),
571571
new RequestShortener("/example.com/www/js/")
572572
)
573573
).toMatchSnapshot();

types/index.d.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export = TerserPlugin;
77
/** @typedef {import("./utils.js").TerserECMA} TerserECMA */
88
/** @typedef {import("./utils.js").TerserOptions} TerserOptions */
99
/** @typedef {import("jest-worker").Worker} JestWorker */
10-
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
10+
/** @typedef {import("@jridgewell/trace-mapping").SourceMapInput} SourceMapInput */
1111
/** @typedef {RegExp | string} Rule */
1212
/** @typedef {Rule[] | Rule} Rules */
1313
/**
@@ -37,7 +37,7 @@ export = TerserPlugin;
3737
/**
3838
* @typedef {Object} MinimizedResult
3939
* @property {string} code
40-
* @property {RawSourceMap} [map]
40+
* @property {SourceMapInput} [map]
4141
* @property {Array<Error | string>} [errors]
4242
* @property {Array<Error | string>} [warnings]
4343
* @property {Array<string>} [extractedComments]
@@ -65,7 +65,7 @@ export = TerserPlugin;
6565
* @template T
6666
* @callback BasicMinimizerImplementation
6767
* @param {Input} input
68-
* @param {RawSourceMap | undefined} sourceMap
68+
* @param {SourceMapInput | undefined} sourceMap
6969
* @param {MinimizerOptions<T>} minifyOptions
7070
* @param {ExtractCommentsOptions | undefined} extractComments
7171
* @returns {Promise<MinimizedResult>}
@@ -83,7 +83,7 @@ export = TerserPlugin;
8383
* @typedef {Object} InternalOptions
8484
* @property {string} name
8585
* @property {string} input
86-
* @property {RawSourceMap | undefined} inputSourceMap
86+
* @property {SourceMapInput | undefined} inputSourceMap
8787
* @property {ExtractCommentsOptions | undefined} extractComments
8888
* @property {{ implementation: MinimizerImplementation<T>, options: MinimizerOptions<T> }} minimizer
8989
*/
@@ -131,7 +131,7 @@ declare class TerserPlugin<T = import("terser").MinifyOptions> {
131131
* @private
132132
* @param {any} error
133133
* @param {string} file
134-
* @param {SourceMapConsumer} [sourceMap]
134+
* @param {TraceMap} [sourceMap]
135135
* @param {Compilation["requestShortener"]} [requestShortener]
136136
* @returns {Error}
137137
*/
@@ -190,7 +190,7 @@ declare namespace TerserPlugin {
190190
TerserECMA,
191191
TerserOptions,
192192
JestWorker,
193-
RawSourceMap,
193+
SourceMapInput,
194194
Rule,
195195
Rules,
196196
ExtractCommentsFunction,
@@ -244,7 +244,7 @@ type Asset = import("webpack").Asset;
244244
type TerserECMA = import("./utils.js").TerserECMA;
245245
type TerserOptions = import("./utils.js").TerserOptions;
246246
type JestWorker = import("jest-worker").Worker;
247-
type RawSourceMap = import("source-map").RawSourceMap;
247+
type SourceMapInput = import("@jridgewell/trace-mapping").SourceMapInput;
248248
type Rule = RegExp | string;
249249
type Rules = Rule[] | Rule;
250250
type ExtractCommentsFunction = (
@@ -276,7 +276,7 @@ type ExtractCommentsObject = {
276276
type ExtractCommentsOptions = ExtractCommentsCondition | ExtractCommentsObject;
277277
type MinimizedResult = {
278278
code: string;
279-
map?: import("source-map").RawSourceMap | undefined;
279+
map?: import("@jridgewell/trace-mapping").SourceMapInput | undefined;
280280
errors?: (string | Error)[] | undefined;
281281
warnings?: (string | Error)[] | undefined;
282282
extractedComments?: string[] | undefined;
@@ -295,7 +295,7 @@ type PredefinedOptions = {
295295
type MinimizerOptions<T> = PredefinedOptions & InferDefaultType<T>;
296296
type BasicMinimizerImplementation<T> = (
297297
input: Input,
298-
sourceMap: RawSourceMap | undefined,
298+
sourceMap: SourceMapInput | undefined,
299299
minifyOptions: MinimizerOptions<T>,
300300
extractComments: ExtractCommentsOptions | undefined
301301
) => Promise<MinimizedResult>;
@@ -307,7 +307,7 @@ type MinimizerImplementation<T> = BasicMinimizerImplementation<T> &
307307
type InternalOptions<T> = {
308308
name: string;
309309
input: string;
310-
inputSourceMap: RawSourceMap | undefined;
310+
inputSourceMap: SourceMapInput | undefined;
311311
extractComments: ExtractCommentsOptions | undefined;
312312
minimizer: {
313313
implementation: MinimizerImplementation<T>;

0 commit comments

Comments
 (0)