Skip to content

Commit 64f83a6

Browse files
author
Brian Vaughn
authored
Replace "source-map" library with "source-map-js" (#22126)
1 parent aa25824 commit 64f83a6

File tree

10 files changed

+32
-70
lines changed

10 files changed

+32
-70
lines changed

packages/react-devtools-extensions/build.js

-6
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,6 @@ const build = async (tempPath, manifestPath) => {
9393
STATIC_FILES.map(file => copy(join(__dirname, file), join(zipPath, file))),
9494
);
9595

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-
10296
const commit = getGitCommit();
10397
const dateString = new Date().toLocaleDateString();
10498
const manifest = JSON.parse(readFileSync(copiedManifestPath).toString());

packages/react-devtools-extensions/chrome/manifest.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
"main.html",
3333
"panel.html",
3434
"build/react_devtools_backend.js",
35-
"build/renderer.js",
36-
"mappings.wasm"
35+
"build/renderer.js"
3736
],
3837

3938
"background": {

packages/react-devtools-extensions/edge/manifest.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
"main.html",
3333
"panel.html",
3434
"build/react_devtools_backend.js",
35-
"build/renderer.js",
36-
"mappings.wasm"
35+
"build/renderer.js"
3736
],
3837

3938
"background": {

packages/react-devtools-extensions/firefox/manifest.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
"main.html",
3838
"panel.html",
3939
"build/react_devtools_backend.js",
40-
"build/renderer.js",
41-
"mappings.wasm"
40+
"build/renderer.js"
4241
],
4342

4443
"background": {

packages/react-devtools-extensions/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"rollup-plugin-babel": "^4.0.1",
6363
"rollup-plugin-commonjs": "^9.3.4",
6464
"rollup-plugin-node-resolve": "^2.1.1",
65-
"source-map": "^0.8.0-beta.0",
65+
"source-map-js": "^0.6.2",
6666
"sourcemap-codec": "^1.4.8",
6767
"style-loader": "^0.23.1",
6868
"webpack": "^4.43.0",

packages/react-devtools-extensions/src/SourceMapMetadataConsumer.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
MixedSourceMap,
1616
} from './SourceMapTypes';
1717
import type {HookMap} from './generateHookMap';
18-
import * as util from 'source-map/lib/util';
18+
import * as util from 'source-map-js/lib/util';
1919
import {decodeHookMap} from './generateHookMap';
2020
import {getHookNameForLocation} from './getHookNameForLocation';
2121

@@ -27,10 +27,9 @@ const REACT_SOURCES_EXTENSION_KEY = 'x_react_sources';
2727
const FB_SOURCES_EXTENSION_KEY = 'x_facebook_sources';
2828

2929
/**
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.
3433
*/
3534
function normalizeSourcePath(
3635
sourceInput: string,
@@ -41,6 +40,18 @@ function normalizeSourcePath(
4140

4241
// eslint-disable-next-line react-internal/no-primitive-constructors
4342
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;
4455
return util.computeSourceURL(sourceRoot, source);
4556
}
4657

packages/react-devtools-extensions/src/__tests__/parseHookNames-test.js

-23
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,6 @@ function requireText(path, encoding) {
2525
}
2626
}
2727

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-
4528
describe('parseHookNames', () => {
4629
let fetchMock;
4730
let inspectHooks;
@@ -57,9 +40,6 @@ describe('parseHookNames', () => {
5740
fetchMock = require('jest-fetch-mock');
5841
fetchMock.enableMocks();
5942

60-
// Mock out portion of browser API used by parseHookNames to initialize "source-map".
61-
global.chrome = chromeGlobal;
62-
6343
inspectHooks = require('react-debug-tools/src/ReactDebugHooks')
6444
.inspectHooks;
6545
parseHookNames = require('../parseHookNames/parseHookNames').parseHookNames;
@@ -908,9 +888,6 @@ describe('parseHookNames worker', () => {
908888
};
909889
});
910890

911-
// Mock out portion of browser API used by parseHookNames to initialize "source-map".
912-
global.chrome = chromeGlobal;
913-
914891
inspectHooks = require('react-debug-tools/src/ReactDebugHooks')
915892
.inspectHooks;
916893
parseHookNames = require('../parseHookNames').parseHookNames;

packages/react-devtools-extensions/src/parseHookNames/index.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* global chrome */
2-
31
/**
42
* Copyright (c) Facebook, Inc. and its affiliates.
53
*
@@ -15,14 +13,11 @@
1513
import WorkerizedParseHookNames from './parseHookNames.worker';
1614
import typeof * as ParseHookNamesModule from './parseHookNames';
1715

18-
// $FlowFixMe
19-
const wasmMappingsURL = chrome.extension.getURL('mappings.wasm');
20-
2116
const workerizedParseHookNames: ParseHookNamesModule = WorkerizedParseHookNames();
2217

2318
type ParseHookNames = $PropertyType<ParseHookNamesModule, 'parseHookNames'>;
2419

2520
export const parseHookNames: ParseHookNames = hooksTree =>
26-
workerizedParseHookNames.parseHookNames(hooksTree, wasmMappingsURL);
21+
workerizedParseHookNames.parseHookNames(hooksTree);
2722

2823
export const purgeCachedMetadata = workerizedParseHookNames.purgeCachedMetadata;

packages/react-devtools-extensions/src/parseHookNames/parseHookNames.js

+6-16
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import {parse} from '@babel/parser';
1111
import LRU from 'lru-cache';
12-
import {SourceMapConsumer} from 'source-map';
12+
import {SourceMapConsumer} from 'source-map-js';
1313
import {getHookName} from '../astUtils';
1414
import {areSourceMapsAppliedToErrors} from '../ErrorTester';
1515
import {__DEBUG__} from 'react-devtools-shared/src/constants';
@@ -107,7 +107,6 @@ const originalURLToMetadataCache: LRUCache<
107107

108108
export async function parseHookNames(
109109
hooksTree: HooksTree,
110-
wasmMappingsURL: string,
111110
): Thenable<HookNames | null> {
112111
const hooksList: Array<HooksNode> = [];
113112
flattenHooksList(hooksTree, hooksList);
@@ -167,9 +166,7 @@ export async function parseHookNames(
167166
}
168167

169168
return loadSourceFiles(locationKeyToHookSourceData)
170-
.then(() =>
171-
extractAndLoadSourceMaps(locationKeyToHookSourceData, wasmMappingsURL),
172-
)
169+
.then(() => extractAndLoadSourceMaps(locationKeyToHookSourceData))
173170
.then(() => parseSourceAST(locationKeyToHookSourceData))
174171
.then(() => updateLruCache(locationKeyToHookSourceData))
175172
.then(() => findHookNames(hooksList, locationKeyToHookSourceData));
@@ -191,7 +188,6 @@ function decodeBase64String(encoded: string): Object {
191188

192189
function extractAndLoadSourceMaps(
193190
locationKeyToHookSourceData: Map<string, HookSourceData>,
194-
wasmMappingsURL: string,
195191
): Promise<*> {
196192
// SourceMapConsumer.initialize() does nothing when running in Node (aka Jest)
197193
// because the wasm file is automatically read from the file system
@@ -202,8 +198,6 @@ function extractAndLoadSourceMaps(
202198
'extractAndLoadSourceMaps() Initializing source-map library ...',
203199
);
204200
}
205-
206-
SourceMapConsumer.initialize({'lib/mappings.wasm': wasmMappingsURL});
207201
}
208202

209203
// Deduplicate fetches, since there can be multiple location keys per source map.
@@ -259,11 +253,7 @@ function extractAndLoadSourceMaps(
259253
hookSourceData.metadataConsumer = new SourceMapMetadataConsumer(
260254
parsed,
261255
);
262-
setPromises.push(
263-
new SourceMapConsumer(parsed).then(sourceConsumer => {
264-
hookSourceData.sourceConsumer = sourceConsumer;
265-
}),
266-
);
256+
hookSourceData.sourceConsumer = new SourceMapConsumer(parsed);
267257
break;
268258
}
269259
} else {
@@ -299,10 +289,10 @@ function extractAndLoadSourceMaps(
299289
fetchFile(url).then(
300290
sourceMapContents => {
301291
const parsed = JSON.parse(sourceMapContents);
302-
return new SourceMapConsumer(parsed).then(sourceConsumer => ({
303-
sourceConsumer,
292+
return {
293+
sourceConsumer: new SourceMapConsumer(parsed),
304294
metadataConsumer: new SourceMapMetadataConsumer(parsed),
305-
}));
295+
};
306296
},
307297
// In this case, we fall back to the assumption that the source has no source map.
308298
// This might indicate an (unlikely) edge case that had no source map,

yarn.lock

+5-7
Original file line numberDiff line numberDiff line change
@@ -14168,6 +14168,11 @@ source-list-map@^2.0.0:
1416814168
resolved "https://registry.yarnpkg.com/source-list-map/-/source-list-map-2.0.1.tgz#3993bd873bfc48479cca9ea3a547835c7c154b34"
1416914169
integrity sha512-qnQ7gVMxGNxsiL4lEuJwe/To8UnK7fAnmbGEEH8RpLouuKbeEm0lhbQVFIrNSuB+G7tVrAlVsZgETT5nljf+Iw==
1417014170

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+
1417114176
source-map-resolve@^0.5.0:
1417214177
version "0.5.3"
1417314178
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:
1422314228
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
1422414229
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==
1422514230

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-
1423314231
sourcemap-codec@^1.4.1, sourcemap-codec@^1.4.4, sourcemap-codec@^1.4.8:
1423414232
version "1.4.8"
1423514233
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4"

0 commit comments

Comments
 (0)