From 77612a3f7b9d9069cf9de6760b0958fc5e9990af Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Tue, 29 Jun 2021 07:47:07 -0700 Subject: [PATCH] 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 --- desktop/pkg-lib/src/runBuild.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/desktop/pkg-lib/src/runBuild.ts b/desktop/pkg-lib/src/runBuild.ts index 72164bbef..ff47871ce 100644 --- a/desktop/pkg-lib/src/runBuild.ts +++ b/desktop/pkg-lib/src/runBuild.ts @@ -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)