diff --git a/README.md b/README.md index 721f2d9..de04377 100644 --- a/README.md +++ b/README.md @@ -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; @@ -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; diff --git a/package-lock.json b/package-lock.json index c3fc9d0..9c77262 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,10 +9,10 @@ "version": "5.3.1", "license": "MIT", "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": { @@ -2978,6 +2978,28 @@ "node": ">=8" } }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.0.7", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.0.7.tgz", + "integrity": "sha512-8cXDaBBHOr2pQ7j77Y6Vp5VDT2sIqWyWQ56TjEq4ih/a4iST3dItRe8Q9fp0rrIl9DoKhWQtUQz/YpOxLkXbNA==", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.4.13", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.13.tgz", + "integrity": "sha512-GryiOJmNcWbovBxTfZSF71V/mXbgcV3MewDe3kIMCLyIh5e7SKAeUZs+rMnJ8jkMolZ/4/VsdBmMrw3l+VdZ3w==" + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.13", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.13.tgz", + "integrity": "sha512-o1xbKhp9qnIAoHJSWd6KlCZfqslL4valSF81H8ImioOAxluWYWOpWkpyktY2vnt4tbrX9XYaxovq6cgowaJp2w==", + "dependencies": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + }, "node_modules/@nicolo-ribaudo/chokidar-2": { "version": "2.1.8-no-fsevents.3", "resolved": "https://registry.npmjs.org/@nicolo-ribaudo/chokidar-2/-/chokidar-2-2.1.8-no-fsevents.3.tgz", @@ -15310,6 +15332,25 @@ } } }, + "@jridgewell/resolve-uri": { + "version": "3.0.7", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.0.7.tgz", + "integrity": "sha512-8cXDaBBHOr2pQ7j77Y6Vp5VDT2sIqWyWQ56TjEq4ih/a4iST3dItRe8Q9fp0rrIl9DoKhWQtUQz/YpOxLkXbNA==" + }, + "@jridgewell/sourcemap-codec": { + "version": "1.4.13", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.13.tgz", + "integrity": "sha512-GryiOJmNcWbovBxTfZSF71V/mXbgcV3MewDe3kIMCLyIh5e7SKAeUZs+rMnJ8jkMolZ/4/VsdBmMrw3l+VdZ3w==" + }, + "@jridgewell/trace-mapping": { + "version": "0.3.13", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.13.tgz", + "integrity": "sha512-o1xbKhp9qnIAoHJSWd6KlCZfqslL4valSF81H8ImioOAxluWYWOpWkpyktY2vnt4tbrX9XYaxovq6cgowaJp2w==", + "requires": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + }, "@nicolo-ribaudo/chokidar-2": { "version": "2.1.8-no-fsevents.3", "resolved": "https://registry.npmjs.org/@nicolo-ribaudo/chokidar-2/-/chokidar-2-2.1.8-no-fsevents.3.tgz", diff --git a/package.json b/package.json index 1d0718e..7ae396c 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.js b/src/index.js index 729e35f..30627eb 100644 --- a/src/index.js +++ b/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"); @@ -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 */ @@ -64,7 +64,7 @@ const { minify } = require("./minify"); /** * @typedef {Object} MinimizedResult * @property {string} code - * @property {RawSourceMap} [map] + * @property {SourceMapInput} [map] * @property {Array} [errors] * @property {Array} [warnings] * @property {Array} [extractedComments] @@ -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} minifyOptions * @param {ExtractCommentsOptions | undefined} extractComments * @returns {Promise} @@ -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, options: MinimizerOptions }} minimizer */ @@ -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 && @@ -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} */ @@ -254,7 +254,7 @@ class TerserPlugin { if (error.line) { const original = sourceMap && - sourceMap.originalPositionFor({ + originalPositionFor(sourceMap, { line: error.line, column: error.col, }); @@ -438,7 +438,7 @@ class TerserPlugin { if (!output) { let input; - /** @type {RawSourceMap | undefined} */ + /** @type {SourceMapInput | undefined} */ let inputSourceMap; const { source: sourceFromInputSource, map } = @@ -453,7 +453,7 @@ class TerserPlugin { (new Error(`${name} contains invalid source map`)) ); } else { - inputSourceMap = /** @type {RawSourceMap} */ (map); + inputSourceMap = /** @type {SourceMapInput} */ (map); } } @@ -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, @@ -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, @@ -588,7 +588,7 @@ class TerserPlugin { name, output.map, input, - /** @type {RawSourceMap} */ (inputSourceMap), + /** @type {SourceMapInput} */ (inputSourceMap), true ); } else { diff --git a/src/utils.js b/src/utils.js index e181f93..e454467 100644 --- a/src/utils.js +++ b/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 */ @@ -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} @@ -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, }; } @@ -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} @@ -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} */ @@ -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} */ diff --git a/test/TerserPlugin.test.js b/test/TerserPlugin.test.js index 4a4d532..697f7f5 100644 --- a/test/TerserPlugin.test.js +++ b/test/TerserPlugin.test.js @@ -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"; @@ -551,7 +551,7 @@ describe("TerserPlugin", () => { TerserPlugin.buildError( errorWithLineAndCol, "test.js", - new SourceMapConsumer(rawSourceMap), + new TraceMap(rawSourceMap), // eslint-disable-next-line no-undefined undefined ) @@ -567,7 +567,7 @@ describe("TerserPlugin", () => { TerserPlugin.buildError( otherErrorWithLineAndCol, "test.js", - new SourceMapConsumer(rawSourceMap), + new TraceMap(rawSourceMap), new RequestShortener("/example.com/www/js/") ) ).toMatchSnapshot(); diff --git a/types/index.d.ts b/types/index.d.ts index 7adb303..e1b5d49 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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 */ /** @@ -37,7 +37,7 @@ export = TerserPlugin; /** * @typedef {Object} MinimizedResult * @property {string} code - * @property {RawSourceMap} [map] + * @property {SourceMapInput} [map] * @property {Array} [errors] * @property {Array} [warnings] * @property {Array} [extractedComments] @@ -65,7 +65,7 @@ export = TerserPlugin; * @template T * @callback BasicMinimizerImplementation * @param {Input} input - * @param {RawSourceMap | undefined} sourceMap + * @param {SourceMapInput | undefined} sourceMap * @param {MinimizerOptions} minifyOptions * @param {ExtractCommentsOptions | undefined} extractComments * @returns {Promise} @@ -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, options: MinimizerOptions }} minimizer */ @@ -131,7 +131,7 @@ declare class TerserPlugin { * @private * @param {any} error * @param {string} file - * @param {SourceMapConsumer} [sourceMap] + * @param {TraceMap} [sourceMap] * @param {Compilation["requestShortener"]} [requestShortener] * @returns {Error} */ @@ -190,7 +190,7 @@ declare namespace TerserPlugin { TerserECMA, TerserOptions, JestWorker, - RawSourceMap, + SourceMapInput, Rule, Rules, ExtractCommentsFunction, @@ -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 = ( @@ -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; @@ -295,7 +295,7 @@ type PredefinedOptions = { type MinimizerOptions = PredefinedOptions & InferDefaultType; type BasicMinimizerImplementation = ( input: Input, - sourceMap: RawSourceMap | undefined, + sourceMap: SourceMapInput | undefined, minifyOptions: MinimizerOptions, extractComments: ExtractCommentsOptions | undefined ) => Promise; @@ -307,7 +307,7 @@ type MinimizerImplementation = BasicMinimizerImplementation & type InternalOptions = { name: string; input: string; - inputSourceMap: RawSourceMap | undefined; + inputSourceMap: SourceMapInput | undefined; extractComments: ExtractCommentsOptions | undefined; minimizer: { implementation: MinimizerImplementation; diff --git a/types/utils.d.ts b/types/utils.d.ts index 141b5ac..4ee0eff 100644 --- a/types/utils.d.ts +++ b/types/utils.d.ts @@ -1,5 +1,5 @@ export type Task = () => Promise; -export type RawSourceMap = import("source-map").RawSourceMap; +export type SourceMapInput = import("@jridgewell/trace-mapping").SourceMapInput; export type TerserFormatOptions = import("terser").FormatOptions; export type TerserOptions = import("terser").MinifyOptions; export type TerserECMA = import("terser").ECMA; @@ -28,14 +28,14 @@ export type ExtractedComments = Array; export function throttleAll(limit: number, tasks: Task[]): Promise; /** * @param {Input} input - * @param {RawSourceMap | undefined} sourceMap + * @param {SourceMapInput | undefined} sourceMap * @param {PredefinedOptions & CustomOptions} minimizerOptions * @param {ExtractCommentsOptions | undefined} extractComments * @return {Promise} */ export function terserMinify( input: Input, - sourceMap: RawSourceMap | undefined, + sourceMap: SourceMapInput | undefined, minimizerOptions: PredefinedOptions & CustomOptions, extractComments: ExtractCommentsOptions | undefined ): Promise; @@ -47,14 +47,14 @@ export namespace terserMinify { } /** * @param {Input} input - * @param {RawSourceMap | undefined} sourceMap + * @param {SourceMapInput | undefined} sourceMap * @param {PredefinedOptions & CustomOptions} minimizerOptions * @param {ExtractCommentsOptions | undefined} extractComments * @return {Promise} */ export function uglifyJsMinify( input: Input, - sourceMap: RawSourceMap | undefined, + sourceMap: SourceMapInput | undefined, minimizerOptions: PredefinedOptions & CustomOptions, extractComments: ExtractCommentsOptions | undefined ): Promise; @@ -66,13 +66,13 @@ export namespace uglifyJsMinify { } /** * @param {Input} input - * @param {RawSourceMap | undefined} sourceMap + * @param {SourceMapInput | undefined} sourceMap * @param {PredefinedOptions & CustomOptions} minimizerOptions * @return {Promise} */ export function swcMinify( input: Input, - sourceMap: RawSourceMap | undefined, + sourceMap: SourceMapInput | undefined, minimizerOptions: PredefinedOptions & CustomOptions ): Promise; export namespace swcMinify { @@ -83,13 +83,13 @@ export namespace swcMinify { } /** * @param {Input} input - * @param {RawSourceMap | undefined} sourceMap + * @param {SourceMapInput | undefined} sourceMap * @param {PredefinedOptions & CustomOptions} minimizerOptions * @return {Promise} */ export function esbuildMinify( input: Input, - sourceMap: RawSourceMap | undefined, + sourceMap: SourceMapInput | undefined, minimizerOptions: PredefinedOptions & CustomOptions ): Promise; export namespace esbuildMinify {