Strip source map URL from bundled plugins

Summary: Sandcastle embedded the box ID in the source map URL which caused it to invalidate the caches. It shouldn't be there to begin with but the Metro option doesn't work as it's documented.

Reviewed By: nikoant

Differential Revision: D29456824

fbshipit-source-id: 5d8c5f29e2b344d046c802693e4da68fda92b8db
This commit is contained in:
Pascal Hartig
2021-06-29 07:47:07 -07:00
committed by Facebook GitHub Bot
parent 2f47928524
commit 77612a3f7b

View File

@@ -37,6 +37,18 @@ type Options = {
sourceMapPath?: string | undefined;
};
// Metro erroneously adds source map comments to the bottom of the file
// which break checksums on CI environments where paths change and are generally
// undesired. We manually strip the comment here and write the file back.
async function stripSourceMapComment(out: string) {
const lines = (await fs.readFile(out, 'utf-8')).split(os.EOL);
const lastLine = lines[lines.length - 1];
if (lastLine.startsWith('//# sourceMappingURL=')) {
console.log(`Updating ${out} to remove sourceMapURL= comment.`);
await fs.writeFile(out, lines.slice(0, lines.length - 1).join(os.EOL));
}
}
export default async function bundlePlugin(
pluginDir: string,
dev: boolean,
@@ -97,9 +109,10 @@ export default async function bundlePlugin(
],
});
const sourceMapUrl = out.replace(/\.js$/, '.map');
const sourceMap = dev || !!options?.sourceMapPath;
await Metro.runBuild(config, {
dev,
sourceMap: dev || !!options?.sourceMapPath,
sourceMap,
sourceMapUrl,
minify: !dev,
inlineSourceMap: dev,
@@ -107,6 +120,9 @@ export default async function bundlePlugin(
entry,
out,
});
if (sourceMap && !dev) {
await stripSourceMapComment(out);
}
if (
options?.sourceMapPath &&
path.resolve(options.sourceMapPath) !== path.resolve(sourceMapUrl)