Replace "source-map" library with "source-map-js" (#22126)
Brian Vaughn committed
Aug 19, 2021 at 11:25 UTC
64f83a6fd24ddfcc74607e0e55b3fc9fe0d3c9b3
10 files changed
+32
-70
packages/react-devtools-extensions/build.js
-6
@@ -93,12 +93,6 @@ const build = async (tempPath, manifestPath) => {
93
STATIC_FILES.map(file => copy(join(__dirname, file), join(zipPath, file))),
94
);
95
96
- // The "source-map" library requires this chunk of WASM to be bundled at runtime.
97
- await copy(
98
- join(__dirname, 'node_modules', 'source-map', 'lib', 'mappings.wasm'),
99
- join(zipPath, 'mappings.wasm'),
100
- );
101
-
96
const commit = getGitCommit();
97
const dateString = new Date().toLocaleDateString();
98
const manifest = JSON.parse(readFileSync(copiedManifestPath).toString());
packages/react-devtools-extensions/chrome/manifest.json
+1
-2
@@ -32,8 +32,7 @@
32
"main.html",
33
"panel.html",
34
"build/react_devtools_backend.js",
35
- "build/renderer.js",
36
- "mappings.wasm"
35
+ "build/renderer.js"
36
],
37
38
"background": {
packages/react-devtools-extensions/edge/manifest.json
+1
-2
@@ -32,8 +32,7 @@
32
"main.html",
33
"panel.html",
34
"build/react_devtools_backend.js",
35
- "build/renderer.js",
36
- "mappings.wasm"
35
+ "build/renderer.js"
36
],
37
38
"background": {
packages/react-devtools-extensions/firefox/manifest.json
+1
-2
@@ -37,8 +37,7 @@
37
"main.html",
38
"panel.html",
39
"build/react_devtools_backend.js",
40
- "build/renderer.js",
41
- "mappings.wasm"
40
+ "build/renderer.js"
41
],
42
43
"background": {
packages/react-devtools-extensions/package.json
+1
-1
@@ -62,7 +62,7 @@
62
"rollup-plugin-babel": "^4.0.1",
63
"rollup-plugin-commonjs": "^9.3.4",
64
"rollup-plugin-node-resolve": "^2.1.1",
65
- "source-map": "^0.8.0-beta.0",
65
+ "source-map-js": "^0.6.2",
66
"sourcemap-codec": "^1.4.8",
67
"style-loader": "^0.23.1",
68
"webpack": "^4.43.0",
packages/react-devtools-extensions/src/SourceMapMetadataConsumer.js
+16
-5
@@ -15,7 +15,7 @@ import type {
15
MixedSourceMap,
16
} from './SourceMapTypes';
17
import type {HookMap} from './generateHookMap';
18
-import * as util from 'source-map/lib/util';
18
+import * as util from 'source-map-js/lib/util';
19
import {decodeHookMap} from './generateHookMap';
20
import {getHookNameForLocation} from './getHookNameForLocation';
21
@@ -27,10 +27,9 @@ const REACT_SOURCES_EXTENSION_KEY = 'x_react_sources';
27
const FB_SOURCES_EXTENSION_KEY = 'x_facebook_sources';
28
29
/**
30
- * Extracted from the logic in source-map@0.8.0-beta.0's SourceMapConsumer.
31
- * By default, source names are normalized using the same logic that the
32
- * `source-map@0.8.0-beta.0` package uses internally. This is crucial for keeping the
33
- * sources list in sync with a `SourceMapConsumer` instance.
30
+ * Extracted from the logic in source-map-js@0.6.2's SourceMapConsumer.
31
+ * By default, source names are normalized using the same logic that the `source-map-js@0.6.2` package uses internally.
32
+ * This is crucial for keeping the sources list in sync with a `SourceMapConsumer` instance.
33
*/
34
function normalizeSourcePath(
35
sourceInput: string,
@@ -41,6 +40,18 @@ function normalizeSourcePath(
40
41
// eslint-disable-next-line react-internal/no-primitive-constructors
42
source = String(source);
43
+ // Some source maps produce relative source paths like "./foo.js" instead of
44
+ // "foo.js". Normalize these first so that future comparisons will succeed.
45
+ // See bugzil.la/1090768.
46
+ source = util.normalize(source);
47
+ // Always ensure that absolute sources are internally stored relative to
48
+ // the source root, if the source root is absolute. Not doing this would
49
+ // be particularly problematic when the source root is a prefix of the
50
+ // source (valid, but why??). See github issue #199 and bugzil.la/1188982.
51
+ source =
52
+ sourceRoot != null && util.isAbsolute(sourceRoot) && util.isAbsolute(source)
53
+ ? util.relative(sourceRoot, source)
54
+ : source;
55
return util.computeSourceURL(sourceRoot, source);
56
}
57
packages/react-devtools-extensions/src/__tests__/parseHookNames-test.js
-23
@@ -25,23 +25,6 @@ function requireText(path, encoding) {
25
}
26
}
27
28
-const chromeGlobal = {
29
- extension: {
30
- getURL: jest.fn((...args) => {
31
- const {join} = require('path');
32
- return join(
33
- __dirname,
34
- '..',
35
- '..',
36
- 'node_modules',
37
- 'source-map',
38
- 'lib',
39
- 'mappings.wasm',
40
- );
41
- }),
42
- },
43
-};
44
-
28
describe('parseHookNames', () => {
29
let fetchMock;
30
let inspectHooks;
@@ -57,9 +40,6 @@ describe('parseHookNames', () => {
40
fetchMock = require('jest-fetch-mock');
41
fetchMock.enableMocks();
42
60
- // Mock out portion of browser API used by parseHookNames to initialize "source-map".
61
- global.chrome = chromeGlobal;
62
-
43
inspectHooks = require('react-debug-tools/src/ReactDebugHooks')
44
.inspectHooks;
45
parseHookNames = require('../parseHookNames/parseHookNames').parseHookNames;
@@ -908,9 +888,6 @@ describe('parseHookNames worker', () => {
888
};
889
});
890
911
- // Mock out portion of browser API used by parseHookNames to initialize "source-map".
912
- global.chrome = chromeGlobal;
913
-
891
inspectHooks = require('react-debug-tools/src/ReactDebugHooks')
892
.inspectHooks;
893
parseHookNames = require('../parseHookNames').parseHookNames;
packages/react-devtools-extensions/src/parseHookNames/index.js
+1
-6
@@ -1,5 +1,3 @@
1
-/* global chrome */
2
-
1
/**
2
* Copyright (c) Facebook, Inc. and its affiliates.
3
*
@@ -15,14 +13,11 @@
13
import WorkerizedParseHookNames from './parseHookNames.worker';
14
import typeof * as ParseHookNamesModule from './parseHookNames';
15
18
-// $FlowFixMe
19
-const wasmMappingsURL = chrome.extension.getURL('mappings.wasm');
20
-
16
const workerizedParseHookNames: ParseHookNamesModule = WorkerizedParseHookNames();
17
18
type ParseHookNames = $PropertyType<ParseHookNamesModule, 'parseHookNames'>;
19
20
export const parseHookNames: ParseHookNames = hooksTree =>
26
- workerizedParseHookNames.parseHookNames(hooksTree, wasmMappingsURL);
21
+ workerizedParseHookNames.parseHookNames(hooksTree);
22
23
export const purgeCachedMetadata = workerizedParseHookNames.purgeCachedMetadata;
packages/react-devtools-extensions/src/parseHookNames/parseHookNames.js
+6
-16
@@ -9,7 +9,7 @@
9
10
import {parse} from '@babel/parser';
11
import LRU from 'lru-cache';
12
-import {SourceMapConsumer} from 'source-map';
12
+import {SourceMapConsumer} from 'source-map-js';
13
import {getHookName} from '../astUtils';
14
import {areSourceMapsAppliedToErrors} from '../ErrorTester';
15
import {__DEBUG__} from 'react-devtools-shared/src/constants';
@@ -107,7 +107,6 @@ const originalURLToMetadataCache: LRUCache<
107
108
export async function parseHookNames(
109
hooksTree: HooksTree,
110
- wasmMappingsURL: string,
110
): Thenable<HookNames | null> {
111
const hooksList: Array<HooksNode> = [];
112
flattenHooksList(hooksTree, hooksList);
@@ -167,9 +166,7 @@ export async function parseHookNames(
166
}
167
168
return loadSourceFiles(locationKeyToHookSourceData)
170
- .then(() =>
171
- extractAndLoadSourceMaps(locationKeyToHookSourceData, wasmMappingsURL),
172
- )
169
+ .then(() => extractAndLoadSourceMaps(locationKeyToHookSourceData))
170
.then(() => parseSourceAST(locationKeyToHookSourceData))
171
.then(() => updateLruCache(locationKeyToHookSourceData))
172
.then(() => findHookNames(hooksList, locationKeyToHookSourceData));
@@ -191,7 +188,6 @@ function decodeBase64String(encoded: string): Object {
188
189
function extractAndLoadSourceMaps(
190
locationKeyToHookSourceData: Map<string, HookSourceData>,
194
- wasmMappingsURL: string,
191
): Promise<*> {
192
// SourceMapConsumer.initialize() does nothing when running in Node (aka Jest)
193
// because the wasm file is automatically read from the file system
@@ -202,8 +198,6 @@ function extractAndLoadSourceMaps(
198
'extractAndLoadSourceMaps() Initializing source-map library ...',
199
);
200
}
205
-
206
- SourceMapConsumer.initialize({'lib/mappings.wasm': wasmMappingsURL});
201
}
202
203
// Deduplicate fetches, since there can be multiple location keys per source map.
@@ -259,11 +253,7 @@ function extractAndLoadSourceMaps(
253
hookSourceData.metadataConsumer = new SourceMapMetadataConsumer(
254
parsed,
255
);
262
- setPromises.push(
263
- new SourceMapConsumer(parsed).then(sourceConsumer => {
264
- hookSourceData.sourceConsumer = sourceConsumer;
265
- }),
266
- );
256
+ hookSourceData.sourceConsumer = new SourceMapConsumer(parsed);
257
break;
258
}
259
} else {
@@ -299,10 +289,10 @@ function extractAndLoadSourceMaps(
289
fetchFile(url).then(
290
sourceMapContents => {
291
const parsed = JSON.parse(sourceMapContents);
302
- return new SourceMapConsumer(parsed).then(sourceConsumer => ({
303
- sourceConsumer,
292
+ return {
293
+ sourceConsumer: new SourceMapConsumer(parsed),
294
metadataConsumer: new SourceMapMetadataConsumer(parsed),
305
- }));
295
+ };
296
},
297
// In this case, we fall back to the assumption that the source has no source map.
298
// This might indicate an (unlikely) edge case that had no source map,
yarn.lock
+5
-7
@@ -14168,6 +14168,11 @@ source-list-map@^2.0.0:
14168
resolved "https://registry.yarnpkg.com/source-list-map/-/source-list-map-2.0.1.tgz#3993bd873bfc48479cca9ea3a547835c7c154b34"
14169
integrity sha512-qnQ7gVMxGNxsiL4lEuJwe/To8UnK7fAnmbGEEH8RpLouuKbeEm0lhbQVFIrNSuB+G7tVrAlVsZgETT5nljf+Iw==
14170
14171
+source-map-js@^0.6.2:
14172
+ version "0.6.2"
14173
+ resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-0.6.2.tgz#0bb5de631b41cfbda6cfba8bd05a80efdfd2385e"
14174
+ integrity sha512-/3GptzWzu0+0MBQFrDKzw/DvvMTUORvgY6k6jd/VS6iCR4RDTKWH6v6WPwQoUO8667uQEf9Oe38DxAYWY5F/Ug==
14175
+
14176
source-map-resolve@^0.5.0:
14177
version "0.5.3"
14178
resolved "https://registry.yarnpkg.com/source-map-resolve/-/source-map-resolve-0.5.3.tgz#190866bece7553e1f8f267a2ee82c606b5509a1a"
@@ -14223,13 +14228,6 @@ source-map@^0.7.3:
14228
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
14229
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==
14230
14226
-source-map@^0.8.0-beta.0:
14227
- version "0.8.0-beta.0"
14228
- resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.8.0-beta.0.tgz#d4c1bb42c3f7ee925f005927ba10709e0d1d1f11"
14229
- integrity sha512-2ymg6oRBpebeZi9UUNsgQ89bhx01TcTkmNTGnNO88imTmbSgy4nfujrgVEFKWpMTEGA11EDkTt7mqObTPdigIA==
14230
- dependencies:
14231
- whatwg-url "^7.0.0"
14232
-
14231
sourcemap-codec@^1.4.1, sourcemap-codec@^1.4.4, sourcemap-codec@^1.4.8:
14232
version "1.4.8"
14233
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4"