Skip to content

Commit

Permalink
Resolve source map URL/path relative to the script
Browse files Browse the repository at this point in the history
  • Loading branch information
motiz88 committed Jul 2, 2021
1 parent 420f285 commit 8d8dd25
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,11 @@ describe('parseHookNames', () => {
// has a recursion breaker which falls back to the default behavior.
Error.prepareStackTrace = (error, trace) => {
return error.stack;
}
};

fetchMock.mockIf(/.+$/, request => {
const {resolve} = require('path');
const url = request.url;
if (url.endsWith('js.map')) {
// Source maps are relative URLs (e.g. "path/to/Exmaple.js" specifies "Exmaple.js.map").
const sourceMapURL = resolve(
__dirname,
'__source__',
'__compiled__',
'external',
url,
);
return Promise.resolve(requireText(sourceMapURL, 'utf8'));
} else {
return Promise.resolve(requireText(url, 'utf8'));
}
return Promise.resolve(requireText(request.url, 'utf8'));
});

// Mock out portion of browser API used by parseHookNames to initialize "source-map".
Expand Down
9 changes: 8 additions & 1 deletion packages/react-devtools-extensions/src/parseHookNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ function extractAndLoadSourceMaps(
}
} else {
for (let i = 0; i < sourceMappingURLs.length; i++) {
const fileName = ((hookSourceData.hookSource.fileName: any): string);
const sourceMappingURL = sourceMappingURLs[i];
const index = sourceMappingURL.indexOf('base64,');
if (index >= 0) {
Expand All @@ -211,7 +212,6 @@ function extractAndLoadSourceMaps(

// Hook source might be a URL like "https://4syus.csb.app/src/App.js"
// Parsed source map might be a partial path like "src/App.js"
const fileName = ((hookSourceData.hookSource.fileName: any): string);
const match = parsed.sources.find(
source =>
source === 'Inline Babel script' || fileName.includes(source),
Expand All @@ -235,6 +235,13 @@ function extractAndLoadSourceMaps(
if (!isValidUrl(url)) {
throw new Error(`Invalid source map URL "${url}"`);
}
} else if (!url.startsWith('/')) {
// Resolve paths relative to the location of the file name
const lastSlashIdx = fileName.lastIndexOf('/');
if (lastSlashIdx !== -1) {
const baseURL = fileName.slice(0, fileName.lastIndexOf('/'));
url = `${baseURL}/${url}`;
}
}

hookSourceData.sourceMapURL = url;
Expand Down

0 comments on commit 8d8dd25

Please sign in to comment.