mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-06 12:01:40 +08:00
We plan to enable source maps for release builds behind a GN flag. For this to work rollup needs to consume the source maps already generated by tsc and merge it with its own source map. Rollup can't do this natively so this CL adds a plugin that can. R=jacktfranklin@chromium.org Bug: 1430396 Change-Id: Ibb38c7892326907431e54b7c7dbd7b912b59ccb9 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/4397707 Reviewed-by: Jack Franklin <jacktfranklin@chromium.org> Commit-Queue: Simon Zünd <szuend@chromium.org>
57 lines
1.9 KiB
JavaScript
57 lines
1.9 KiB
JavaScript
import fs from 'fs';
|
|
import util from 'util';
|
|
import { createFilter } from '@rollup/pluginutils';
|
|
import { resolveSourceMap, resolveSources } from 'source-map-resolve';
|
|
|
|
const promisifiedResolveSourceMap = util.promisify(resolveSourceMap);
|
|
const promisifiedResolveSources = util.promisify(resolveSources);
|
|
function sourcemaps({ include, exclude, readFile = fs.readFile, } = {}) {
|
|
const filter = createFilter(include, exclude);
|
|
const promisifiedReadFile = util.promisify(readFile);
|
|
return {
|
|
name: 'sourcemaps',
|
|
async load(id) {
|
|
if (!filter(id)) {
|
|
return null;
|
|
}
|
|
let code;
|
|
try {
|
|
code = (await promisifiedReadFile(id)).toString();
|
|
}
|
|
catch (_a) {
|
|
// Failed reading file, let the next plugin deal with it
|
|
return null;
|
|
}
|
|
let map;
|
|
try {
|
|
const result = await promisifiedResolveSourceMap(code, id, readFile);
|
|
// The code contained no sourceMappingURL,
|
|
if (result === null) {
|
|
return code;
|
|
}
|
|
map = result.map;
|
|
}
|
|
catch (_b) {
|
|
// Failed resolving source map, just return the source
|
|
return code;
|
|
}
|
|
// Resolve sources if they're not included
|
|
if (map.sourcesContent === undefined) {
|
|
try {
|
|
const { sourcesContent } = await promisifiedResolveSources(map, id, readFile);
|
|
if (sourcesContent.every(util.isString)) {
|
|
map.sourcesContent = sourcesContent;
|
|
}
|
|
}
|
|
catch (_c) {
|
|
// Ignore
|
|
}
|
|
}
|
|
return { code, map };
|
|
},
|
|
};
|
|
}
|
|
|
|
export default sourcemaps;
|
|
//# sourceMappingURL=index.js.map
|