From 8df81d2dc0ad8b1b7dc6524feb42cf947878464e Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Mon, 21 Jun 2021 11:10:43 -0700 Subject: [PATCH] Generate source maps for releases Summary: Allows for optional generation of source maps while building plugins. Caveat: This will leave a broken `//# sourceMappingURL` comment at the bottom. If you set it to `null`, as the documentation suggests, you will instead get an inlined source map in addition to the written one. Reviewed By: nikoant Differential Revision: D29265385 fbshipit-source-id: 1e21e49d2516ecc5909b086e7797736b298b86ab --- desktop/pkg-lib/src/runBuild.ts | 27 ++++++++++++++++++++++----- desktop/scripts/build-plugin.ts | 8 +++++++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/desktop/pkg-lib/src/runBuild.ts b/desktop/pkg-lib/src/runBuild.ts index 4985bdfdf..72164bbef 100644 --- a/desktop/pkg-lib/src/runBuild.ts +++ b/desktop/pkg-lib/src/runBuild.ts @@ -33,7 +33,15 @@ async function getMetroDir() { return __dirname; } -export default async function bundlePlugin(pluginDir: string, dev: boolean) { +type Options = { + sourceMapPath?: string | undefined; +}; + +export default async function bundlePlugin( + pluginDir: string, + dev: boolean, + options?: Options, +) { const stat = await fs.lstat(pluginDir); if (!stat.isDirectory()) { throw new Error(`Plugin source ${pluginDir} is not a directory.`); @@ -49,7 +57,6 @@ export default async function bundlePlugin(pluginDir: string, dev: boolean) { const out = path.resolve(pluginDir, plugin.main); await fs.ensureDir(path.dirname(out)); - const sourceMapUrl = null; // inline source map const baseConfig = await Metro.loadConfig(); const config = Object.assign({}, baseConfig, { reporter: {update: () => {}}, @@ -89,13 +96,23 @@ export default async function bundlePlugin(pluginDir: string, dev: boolean) { }), ], }); + const sourceMapUrl = out.replace(/\.js$/, '.map'); await Metro.runBuild(config, { dev, - minify: !dev, - resetCache: false, - sourceMap: dev, + sourceMap: dev || !!options?.sourceMapPath, sourceMapUrl, + minify: !dev, + inlineSourceMap: dev, + resetCache: false, entry, out, }); + if ( + options?.sourceMapPath && + path.resolve(options.sourceMapPath) !== path.resolve(sourceMapUrl) + ) { + console.log(`Moving plugin sourcemap to ${options.sourceMapPath}`); + await fs.ensureDir(path.dirname(options.sourceMapPath)); + await fs.move(sourceMapUrl, options.sourceMapPath, {overwrite: true}); + } } diff --git a/desktop/scripts/build-plugin.ts b/desktop/scripts/build-plugin.ts index 449e27179..020fbcced 100644 --- a/desktop/scripts/build-plugin.ts +++ b/desktop/scripts/build-plugin.ts @@ -53,6 +53,11 @@ const argv = yargs type: 'string', alias: 'ou', }, + 'output-sourcemap': { + description: 'File path for the sourcemap to be written. Optional.', + type: 'string', + alias: 'os', + }, }) .help() .strict() @@ -65,9 +70,10 @@ async function buildPlugin() { const outputFileArg = argv.output; const outputUnpackedArg = argv['output-unpacked']; const minFlipperVersion = argv['min-flipper-version']; + const outputSourcemapArg = argv['output-sourcemap']; const packageJsonPath = path.join(pluginDir, 'package.json'); const packageJsonOverridePath = path.join(pluginDir, 'fb', 'package.json'); - await runBuild(pluginDir, false); + await runBuild(pluginDir, false, {sourceMapPath: outputSourcemapArg}); const checksum = await computePackageChecksum(pluginDir); if (previousChecksum !== checksum && argv.version) { console.log(`Plugin changed. Packaging new version ${argv.version}...`);