Allow saving source maps during build

Summary: This makes it possible to save source maps to a separate folder so we can upload them from CI.

Reviewed By: nikoant

Differential Revision: D29521599

fbshipit-source-id: a137659092b7648858b97ecf5b5c60c88889517a
This commit is contained in:
Pascal Hartig
2021-07-05 06:04:20 -07:00
committed by Facebook GitHub Bot
parent d99b476bcb
commit 04616ad647
2 changed files with 57 additions and 4 deletions

View File

@@ -9,6 +9,7 @@
import Metro from 'metro';
import tmp from 'tmp';
import os from 'os';
import path from 'path';
import fs from 'fs-extra';
import {spawn} from 'promisify-child-process';
@@ -160,6 +161,16 @@ async function buildDefaultPlugins(defaultPlugins: InstalledPluginDetails[]) {
}
}
// TODO: Share this with the runBuild util in pkg-lib.
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));
}
}
const minifierConfig = {
minifierPath: require.resolve('metro-minify-terser'),
minifierConfig: {
@@ -179,7 +190,6 @@ async function compile(
entry: string,
) {
const out = path.join(buildFolder, 'bundle.js');
const sourceMapUrl = dev ? 'bundle.map' : undefined;
await Metro.runBuild(
{
reporter: {update: () => {}},
@@ -203,12 +213,16 @@ async function compile(
dev,
minify: !dev,
resetCache: !dev,
sourceMap: dev,
sourceMapUrl,
sourceMap: true,
sourceMapUrl: dev ? 'bundle.map' : undefined,
inlineSourceMap: false,
entry,
out,
},
);
if (!dev) {
stripSourceMapComment(out);
}
}
export async function compileRenderer(buildFolder: string) {
@@ -230,6 +244,33 @@ export async function compileRenderer(buildFolder: string) {
}
}
export async function moveSourceMaps(
buildFolder: string,
sourceMapFolder: string | undefined,
) {
console.log(`⚙️ Moving source maps...`);
const mainBundleMap = path.join(buildFolder, 'bundle.map');
const rendererBundleMap = path.join(staticDir, 'main.bundle.map');
if (sourceMapFolder) {
await fs.ensureDir(sourceMapFolder);
await fs.move(mainBundleMap, path.join(sourceMapFolder, 'bundle.map'), {
overwrite: true,
});
await fs.move(
rendererBundleMap,
path.join(sourceMapFolder, 'main.bundle.map'),
{overwrite: true},
);
console.log(`✅ Moved to ${sourceMapFolder}.`);
} else {
// If we don't move them out of the build folders, they'll get included in the ASAR
// which we don't want.
await fs.remove(mainBundleMap);
await fs.remove(rendererBundleMap);
console.log(`⏭ Removing source maps.`);
}
}
export async function compileMain() {
const out = path.join(staticDir, 'main.bundle.js');
process.env.FLIPPER_ELECTRON_VERSION =
@@ -259,10 +300,15 @@ export async function compileMain() {
out,
dev,
minify: !dev,
sourceMap: dev,
sourceMap: true,
sourceMapUrl: dev ? 'main.bundle.map' : undefined,
inlineSourceMap: false,
resetCache: !dev,
});
console.log('✅ Compiled main bundle.');
if (!dev) {
stripSourceMapComment(out);
}
} catch (err) {
die(err);
}