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
This commit is contained in:
Pascal Hartig
2021-06-21 11:10:43 -07:00
committed by Facebook GitHub Bot
parent 640e06f130
commit 8df81d2dc0
2 changed files with 29 additions and 6 deletions

View File

@@ -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});
}
}

View File

@@ -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}...`);