Skip to content

Commit 9fec3f2

Browse files
author
Brian Vaughn
authored
DevTools: Ignore multiple sourceMappingUrls for external source maps (#21871)
Added an edge case regression test and bugfix.
1 parent 14bac61 commit 9fec3f2

12 files changed

+198
-33
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import React, {useState} from 'react';
11+
12+
// ?sourceMappingURL=([^\s'"]+)/gm
13+
14+
export function Component() {
15+
const [count, setCount] = useState(0);
16+
17+
return (
18+
<div>
19+
<p>You clicked {count} times</p>
20+
<button onClick={() => setCount(count + 1)}>Click me</button>
21+
</div>
22+
);
23+
}

packages/react-devtools-extensions/src/__tests__/__source__/__compiled__/bundle/index.js

+19-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-devtools-extensions/src/__tests__/__source__/__compiled__/bundle/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-devtools-extensions/src/__tests__/__source__/__compiled__/external/ContainingStringSourceMappingURL.js

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-devtools-extensions/src/__tests__/__source__/__compiled__/external/ContainingStringSourceMappingURL.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-devtools-extensions/src/__tests__/__source__/__compiled__/external/index.js

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-devtools-extensions/src/__tests__/__source__/__compiled__/external/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-devtools-extensions/src/__tests__/__source__/__compiled__/inline/ContainingStringSourceMappingURL.js

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-devtools-extensions/src/__tests__/__source__/__compiled__/inline/index.js

+9-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-devtools-extensions/src/__tests__/__source__/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
export {Component as ComponentWithCustomHook} from './ComponentWithCustomHook';
1111
export {Component as ComponentWithExternalCustomHooks} from './ComponentWithExternalCustomHooks';
1212
export {Component as ComponentWithMultipleHooksPerLine} from './ComponentWithMultipleHooksPerLine';
13+
export {Component as ContainingStringSourceMappingURL} from './ContainingStringSourceMappingURL';
1314
export {Component as Example} from './Example';
1415
export {Component as InlineRequire} from './InlineRequire';
1516
import * as ToDoList from './ToDoList';

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

+37-8
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('parseHookNames', () => {
4848
};
4949

5050
fetchMock.mockIf(/.+$/, request => {
51-
return Promise.resolve(requireText(request.url, 'utf8'));
51+
return requireText(request.url, 'utf8');
5252
});
5353

5454
// Mock out portion of browser API used by parseHookNames to initialize "source-map".
@@ -80,8 +80,12 @@ describe('parseHookNames', () => {
8080
}
8181

8282
function requireText(path, encoding) {
83-
const {readFileSync} = require('fs');
84-
return readFileSync(path, encoding);
83+
const {existsSync, readFileSync} = require('fs');
84+
if (existsSync(path)) {
85+
return Promise.resolve(readFileSync(path, encoding));
86+
} else {
87+
return Promise.reject(`File not found "${path}"`);
88+
}
8589
}
8690

8791
async function getHookNamesForComponent(Component, props = {}) {
@@ -126,7 +130,7 @@ describe('parseHookNames', () => {
126130
if (request.url.endsWith('useCustom.js')) {
127131
throw Error(`Unexpected file request for "${request.url}"`);
128132
}
129-
return Promise.resolve(requireText(request.url, 'utf8'));
133+
return requireText(request.url, 'utf8');
130134
});
131135

132136
const hookNames = await getHookNamesForComponent(Component);
@@ -261,10 +265,10 @@ describe('parseHookNames', () => {
261265
await test(
262266
'./__source__/__compiled__/external/ComponentWithMultipleHooksPerLine',
263267
); // external source map
264-
// await test(
265-
// './__source__/__compiled__/bundle',
266-
// 'ComponentWithMultipleHooksPerLine',
267-
// ); // bundle source map
268+
await test(
269+
'./__source__/__compiled__/bundle',
270+
'ComponentWithMultipleHooksPerLine',
271+
); // bundle source map
268272
});
269273

270274
// TODO Inline require (e.g. require("react").useState()) isn't supported yet.
@@ -284,5 +288,30 @@ describe('parseHookNames', () => {
284288
await test('./__source__/__compiled__/external/InlineRequire'); // external source map
285289
await test('./__source__/__compiled__/bundle', 'InlineRequire'); // bundle source map
286290
});
291+
292+
it('should support sources that contain the string "sourceMappingURL="', async () => {
293+
async function test(path, name = 'Component') {
294+
const Component = require(path)[name];
295+
const hookNames = await getHookNamesForComponent(Component);
296+
expectHookNamesToEqual(hookNames, [
297+
'count', // useState()
298+
]);
299+
}
300+
301+
// We expect the inline sourceMappingURL to be invalid in this case; mute the warning.
302+
console.warn = () => {};
303+
304+
await test('./__source__/ContainingStringSourceMappingURL'); // original source (uncompiled)
305+
await test(
306+
'./__source__/__compiled__/inline/ContainingStringSourceMappingURL',
307+
); // inline source map
308+
await test(
309+
'./__source__/__compiled__/external/ContainingStringSourceMappingURL',
310+
); // external source map
311+
await test(
312+
'./__source__/__compiled__/bundle',
313+
'ContainingStringSourceMappingURL',
314+
); // bundle source map
315+
});
287316
});
288317
});

0 commit comments

Comments
 (0)