Skip to content

Commit

Permalink
perf: replace source-map package with @jridgewell/trace-mapping (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Jun 1, 2022
1 parent 66fdec8 commit 6020a94
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 48 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -220,7 +220,7 @@ type minify = (
input: {
[file: string]: string;
},
sourceMap: import("source-map").RawSourceMap | undefined,
sourceMap: import("@jridgewell/trace-mapping").SourceMapInput | undefined,
minifyOptions: {
module?: boolean | undefined;
ecma?: import("terser").ECMA | undefined;
Expand Down Expand Up @@ -267,7 +267,7 @@ type minify = (
| undefined
) => Promise<{
code: string;
map?: import("source-map").RawSourceMap | undefined;
map?: import("@jridgewell/trace-mapping").SourceMapInput | undefined;
errors?: (string | Error)[] | undefined;
warnings?: (string | Error)[] | undefined;
extractedComments?: string[] | undefined;
Expand Down
43 changes: 42 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -55,10 +55,10 @@
}
},
"dependencies": {
"@jridgewell/trace-mapping": "^0.3.7",
"jest-worker": "^27.4.5",
"schema-utils": "^3.1.1",
"serialize-javascript": "^6.0.0",
"source-map": "^0.6.1",
"terser": "^5.7.2"
},
"devDependencies": {
Expand Down
32 changes: 16 additions & 16 deletions src/index.js
@@ -1,7 +1,7 @@
const path = require("path");
const os = require("os");

const { SourceMapConsumer } = require("source-map");
const { TraceMap, originalPositionFor } = require("@jridgewell/trace-mapping");
const { validate } = require("schema-utils");
const serialize = require("serialize-javascript");
const { Worker } = require("jest-worker");
Expand All @@ -25,7 +25,7 @@ const { minify } = require("./minify");
/** @typedef {import("./utils.js").TerserECMA} TerserECMA */
/** @typedef {import("./utils.js").TerserOptions} TerserOptions */
/** @typedef {import("jest-worker").Worker} JestWorker */
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
/** @typedef {import("@jridgewell/trace-mapping").SourceMapInput} SourceMapInput */

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

Expand Down Expand Up @@ -64,7 +64,7 @@ const { minify } = require("./minify");
/**
* @typedef {Object} MinimizedResult
* @property {string} code
* @property {RawSourceMap} [map]
* @property {SourceMapInput} [map]
* @property {Array<Error | string>} [errors]
* @property {Array<Error | string>} [warnings]
* @property {Array<string>} [extractedComments]
Expand Down Expand Up @@ -98,7 +98,7 @@ const { minify } = require("./minify");
* @template T
* @callback BasicMinimizerImplementation
* @param {Input} input
* @param {RawSourceMap | undefined} sourceMap
* @param {SourceMapInput | undefined} sourceMap
* @param {MinimizerOptions<T>} minifyOptions
* @param {ExtractCommentsOptions | undefined} extractComments
* @returns {Promise<MinimizedResult>}
Expand All @@ -119,7 +119,7 @@ const { minify } = require("./minify");
* @typedef {Object} InternalOptions
* @property {string} name
* @property {string} input
* @property {RawSourceMap | undefined} inputSourceMap
* @property {SourceMapInput | undefined} inputSourceMap
* @property {ExtractCommentsOptions | undefined} extractComments
* @property {{ implementation: MinimizerImplementation<T>, options: MinimizerOptions<T> }} minimizer
*/
Expand Down Expand Up @@ -199,8 +199,8 @@ class TerserPlugin {
* @returns {boolean}
*/
static isSourceMap(input) {
// All required options for `new SourceMapConsumer(...options)`
// https://github.com/mozilla/source-map#new-sourcemapconsumerrawsourcemap
// All required options for `new TraceMap(...options)`
// https://github.com/jridgewell/trace-mapping#usage
return Boolean(
input &&
input.version &&
Expand Down Expand Up @@ -234,7 +234,7 @@ class TerserPlugin {
* @private
* @param {any} error
* @param {string} file
* @param {SourceMapConsumer} [sourceMap]
* @param {TraceMap} [sourceMap]
* @param {Compilation["requestShortener"]} [requestShortener]
* @returns {Error}
*/
Expand All @@ -254,7 +254,7 @@ class TerserPlugin {
if (error.line) {
const original =
sourceMap &&
sourceMap.originalPositionFor({
originalPositionFor(sourceMap, {
line: error.line,
column: error.col,
});
Expand Down Expand Up @@ -438,7 +438,7 @@ class TerserPlugin {

if (!output) {
let input;
/** @type {RawSourceMap | undefined} */
/** @type {SourceMapInput | undefined} */
let inputSourceMap;

const { source: sourceFromInputSource, map } =
Expand All @@ -453,7 +453,7 @@ class TerserPlugin {
(new Error(`${name} contains invalid source map`))
);
} else {
inputSourceMap = /** @type {RawSourceMap} */ (map);
inputSourceMap = /** @type {SourceMapInput} */ (map);
}
}

Expand Down Expand Up @@ -507,8 +507,8 @@ class TerserPlugin {
error,
name,
hasSourceMap
? new SourceMapConsumer(
/** @type {RawSourceMap} */ (inputSourceMap)
? new TraceMap(
/** @type {SourceMapInput} */ (inputSourceMap)
)
: // eslint-disable-next-line no-undefined
undefined,
Expand Down Expand Up @@ -556,8 +556,8 @@ class TerserPlugin {
item,
name,
hasSourceMap
? new SourceMapConsumer(
/** @type {RawSourceMap} */ (inputSourceMap)
? new TraceMap(
/** @type {SourceMapInput} */ (inputSourceMap)
)
: // eslint-disable-next-line no-undefined
undefined,
Expand Down Expand Up @@ -588,7 +588,7 @@ class TerserPlugin {
name,
output.map,
input,
/** @type {RawSourceMap} */ (inputSourceMap),
/** @type {SourceMapInput} */ (inputSourceMap),
true
);
} else {
Expand Down
12 changes: 6 additions & 6 deletions src/utils.js
@@ -1,4 +1,4 @@
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
/** @typedef {import("@jridgewell/trace-mapping").SourceMapInput} SourceMapInput */
/** @typedef {import("terser").FormatOptions} TerserFormatOptions */
/** @typedef {import("terser").MinifyOptions} TerserOptions */
/** @typedef {import("terser").ECMA} TerserECMA */
Expand Down Expand Up @@ -80,7 +80,7 @@ function throttleAll(limit, tasks) {
/* istanbul ignore next */
/**
* @param {Input} input
* @param {RawSourceMap | undefined} sourceMap
* @param {SourceMapInput | undefined} sourceMap
* @param {PredefinedOptions & CustomOptions} minimizerOptions
* @param {ExtractCommentsOptions | undefined} extractComments
* @return {Promise<MinimizedResult>}
Expand Down Expand Up @@ -287,7 +287,7 @@ async function terserMinify(
code: /** @type {string} **/ (result.code),
// @ts-ignore
// eslint-disable-next-line no-undefined
map: result.map ? /** @type {RawSourceMap} **/ (result.map) : undefined,
map: result.map ? /** @type {SourceMapInput} **/ (result.map) : undefined,
extractedComments,
};
}
Expand All @@ -311,7 +311,7 @@ terserMinify.getMinimizerVersion = () => {
/* istanbul ignore next */
/**
* @param {Input} input
* @param {RawSourceMap | undefined} sourceMap
* @param {SourceMapInput | undefined} sourceMap
* @param {PredefinedOptions & CustomOptions} minimizerOptions
* @param {ExtractCommentsOptions | undefined} extractComments
* @return {Promise<MinimizedResult>}
Expand Down Expand Up @@ -529,7 +529,7 @@ uglifyJsMinify.getMinimizerVersion = () => {
/* istanbul ignore next */
/**
* @param {Input} input
* @param {RawSourceMap | undefined} sourceMap
* @param {SourceMapInput | undefined} sourceMap
* @param {PredefinedOptions & CustomOptions} minimizerOptions
* @return {Promise<MinimizedResult>}
*/
Expand Down Expand Up @@ -613,7 +613,7 @@ swcMinify.getMinimizerVersion = () => {
/* istanbul ignore next */
/**
* @param {Input} input
* @param {RawSourceMap | undefined} sourceMap
* @param {SourceMapInput | undefined} sourceMap
* @param {PredefinedOptions & CustomOptions} minimizerOptions
* @return {Promise<MinimizedResult>}
*/
Expand Down
6 changes: 3 additions & 3 deletions test/TerserPlugin.test.js
Expand Up @@ -2,7 +2,7 @@ import crypto from "crypto";

import path from "path";

import { SourceMapConsumer } from "source-map";
import { TraceMap } from "@jridgewell/trace-mapping";
import CopyWebpackPlugin from "copy-webpack-plugin";
import RequestShortener from "webpack/lib/RequestShortener";
import { javascript, SourceMapDevToolPlugin, util } from "webpack";
Expand Down Expand Up @@ -551,7 +551,7 @@ describe("TerserPlugin", () => {
TerserPlugin.buildError(
errorWithLineAndCol,
"test.js",
new SourceMapConsumer(rawSourceMap),
new TraceMap(rawSourceMap),
// eslint-disable-next-line no-undefined
undefined
)
Expand All @@ -567,7 +567,7 @@ describe("TerserPlugin", () => {
TerserPlugin.buildError(
otherErrorWithLineAndCol,
"test.js",
new SourceMapConsumer(rawSourceMap),
new TraceMap(rawSourceMap),
new RequestShortener("/example.com/www/js/")
)
).toMatchSnapshot();
Expand Down
20 changes: 10 additions & 10 deletions types/index.d.ts
Expand Up @@ -7,7 +7,7 @@ export = TerserPlugin;
/** @typedef {import("./utils.js").TerserECMA} TerserECMA */
/** @typedef {import("./utils.js").TerserOptions} TerserOptions */
/** @typedef {import("jest-worker").Worker} JestWorker */
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
/** @typedef {import("@jridgewell/trace-mapping").SourceMapInput} SourceMapInput */
/** @typedef {RegExp | string} Rule */
/** @typedef {Rule[] | Rule} Rules */
/**
Expand Down Expand Up @@ -37,7 +37,7 @@ export = TerserPlugin;
/**
* @typedef {Object} MinimizedResult
* @property {string} code
* @property {RawSourceMap} [map]
* @property {SourceMapInput} [map]
* @property {Array<Error | string>} [errors]
* @property {Array<Error | string>} [warnings]
* @property {Array<string>} [extractedComments]
Expand Down Expand Up @@ -65,7 +65,7 @@ export = TerserPlugin;
* @template T
* @callback BasicMinimizerImplementation
* @param {Input} input
* @param {RawSourceMap | undefined} sourceMap
* @param {SourceMapInput | undefined} sourceMap
* @param {MinimizerOptions<T>} minifyOptions
* @param {ExtractCommentsOptions | undefined} extractComments
* @returns {Promise<MinimizedResult>}
Expand All @@ -83,7 +83,7 @@ export = TerserPlugin;
* @typedef {Object} InternalOptions
* @property {string} name
* @property {string} input
* @property {RawSourceMap | undefined} inputSourceMap
* @property {SourceMapInput | undefined} inputSourceMap
* @property {ExtractCommentsOptions | undefined} extractComments
* @property {{ implementation: MinimizerImplementation<T>, options: MinimizerOptions<T> }} minimizer
*/
Expand Down Expand Up @@ -131,7 +131,7 @@ declare class TerserPlugin<T = import("terser").MinifyOptions> {
* @private
* @param {any} error
* @param {string} file
* @param {SourceMapConsumer} [sourceMap]
* @param {TraceMap} [sourceMap]
* @param {Compilation["requestShortener"]} [requestShortener]
* @returns {Error}
*/
Expand Down Expand Up @@ -190,7 +190,7 @@ declare namespace TerserPlugin {
TerserECMA,
TerserOptions,
JestWorker,
RawSourceMap,
SourceMapInput,
Rule,
Rules,
ExtractCommentsFunction,
Expand Down Expand Up @@ -244,7 +244,7 @@ type Asset = import("webpack").Asset;
type TerserECMA = import("./utils.js").TerserECMA;
type TerserOptions = import("./utils.js").TerserOptions;
type JestWorker = import("jest-worker").Worker;
type RawSourceMap = import("source-map").RawSourceMap;
type SourceMapInput = import("@jridgewell/trace-mapping").SourceMapInput;
type Rule = RegExp | string;
type Rules = Rule[] | Rule;
type ExtractCommentsFunction = (
Expand Down Expand Up @@ -276,7 +276,7 @@ type ExtractCommentsObject = {
type ExtractCommentsOptions = ExtractCommentsCondition | ExtractCommentsObject;
type MinimizedResult = {
code: string;
map?: import("source-map").RawSourceMap | undefined;
map?: import("@jridgewell/trace-mapping").SourceMapInput | undefined;
errors?: (string | Error)[] | undefined;
warnings?: (string | Error)[] | undefined;
extractedComments?: string[] | undefined;
Expand All @@ -295,7 +295,7 @@ type PredefinedOptions = {
type MinimizerOptions<T> = PredefinedOptions & InferDefaultType<T>;
type BasicMinimizerImplementation<T> = (
input: Input,
sourceMap: RawSourceMap | undefined,
sourceMap: SourceMapInput | undefined,
minifyOptions: MinimizerOptions<T>,
extractComments: ExtractCommentsOptions | undefined
) => Promise<MinimizedResult>;
Expand All @@ -307,7 +307,7 @@ type MinimizerImplementation<T> = BasicMinimizerImplementation<T> &
type InternalOptions<T> = {
name: string;
input: string;
inputSourceMap: RawSourceMap | undefined;
inputSourceMap: SourceMapInput | undefined;
extractComments: ExtractCommentsOptions | undefined;
minimizer: {
implementation: MinimizerImplementation<T>;
Expand Down

0 comments on commit 6020a94

Please sign in to comment.