Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DevTools: Ignore multiple sourceMappingUrls for external source maps #21871

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import React, {useState} from 'react';

// ?sourceMappingURL=([^\s'"]+)/gm

export function Component() {
const [count, setCount] = useState(0);

return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
}

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

Large diffs are not rendered by default.

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

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

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

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
export {Component as ComponentWithCustomHook} from './ComponentWithCustomHook';
export {Component as ComponentWithExternalCustomHooks} from './ComponentWithExternalCustomHooks';
export {Component as ComponentWithMultipleHooksPerLine} from './ComponentWithMultipleHooksPerLine';
export {Component as ContainingStringSourceMappingURL} from './ContainingStringSourceMappingURL';
export {Component as Example} from './Example';
export {Component as InlineRequire} from './InlineRequire';
import * as ToDoList from './ToDoList';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('parseHookNames', () => {
};

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

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

function requireText(path, encoding) {
const {readFileSync} = require('fs');
return readFileSync(path, encoding);
const {existsSync, readFileSync} = require('fs');
if (existsSync(path)) {
return Promise.resolve(readFileSync(path, encoding));
} else {
return Promise.reject(`File not found "${path}"`);
}
}

async function getHookNamesForComponent(Component, props = {}) {
Expand Down Expand Up @@ -126,7 +130,7 @@ describe('parseHookNames', () => {
if (request.url.endsWith('useCustom.js')) {
throw Error(`Unexpected file request for "${request.url}"`);
}
return Promise.resolve(requireText(request.url, 'utf8'));
return requireText(request.url, 'utf8');
});

const hookNames = await getHookNamesForComponent(Component);
Expand Down Expand Up @@ -261,10 +265,10 @@ describe('parseHookNames', () => {
await test(
'./__source__/__compiled__/external/ComponentWithMultipleHooksPerLine',
); // external source map
// await test(
// './__source__/__compiled__/bundle',
// 'ComponentWithMultipleHooksPerLine',
// ); // bundle source map
await test(
'./__source__/__compiled__/bundle',
'ComponentWithMultipleHooksPerLine',
); // bundle source map
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was an oversight from a previous commit.

});

// TODO Inline require (e.g. require("react").useState()) isn't supported yet.
Expand All @@ -284,5 +288,30 @@ describe('parseHookNames', () => {
await test('./__source__/__compiled__/external/InlineRequire'); // external source map
await test('./__source__/__compiled__/bundle', 'InlineRequire'); // bundle source map
});

it('should support sources that contain the string "sourceMappingURL="', async () => {
async function test(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'count', // useState()
]);
}

// We expect the inline sourceMappingURL to be invalid in this case; mute the warning.
console.warn = () => {};

await test('./__source__/ContainingStringSourceMappingURL'); // original source (uncompiled)
await test(
'./__source__/__compiled__/inline/ContainingStringSourceMappingURL',
); // inline source map
await test(
'./__source__/__compiled__/external/ContainingStringSourceMappingURL',
); // external source map
await test(
'./__source__/__compiled__/bundle',
'ContainingStringSourceMappingURL',
); // bundle source map
});
});
});
Loading