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

@@ -26,6 +26,7 @@ import {
getVersionNumber, getVersionNumber,
genMercurialRevision, genMercurialRevision,
prepareDefaultPlugins, prepareDefaultPlugins,
moveSourceMaps,
} from './build-utils'; } from './build-utils';
import fetch from '@adobe/node-fetch-retry'; import fetch from '@adobe/node-fetch-retry';
import {getIcons, buildLocalIconPath, getIconURL} from '../app/src/utils/icons'; import {getIcons, buildLocalIconPath, getIconURL} from '../app/src/utils/icons';
@@ -93,6 +94,11 @@ const argv = yargs
'Directory with prepared list of default plugins which will be included into the Flipper distribution as "defaultPlugins" dir', 'Directory with prepared list of default plugins which will be included into the Flipper distribution as "defaultPlugins" dir',
type: 'string', type: 'string',
}, },
'source-map-dir': {
describe:
'Directory to write the main.bundle.map and bundle.map files for the main and render bundle sourcemaps, respectively',
type: 'string',
},
}) })
.help() .help()
.strict() .strict()
@@ -360,6 +366,7 @@ function downloadIcons(buildFolder: string) {
await copyStaticFolder(dir); await copyStaticFolder(dir);
await downloadIcons(dir); await downloadIcons(dir);
await compileRenderer(dir); await compileRenderer(dir);
await moveSourceMaps(dir, argv['source-map-dir']);
const versionNumber = getVersionNumber(argv.version); const versionNumber = getVersionNumber(argv.version);
const hgRevision = await genMercurialRevision(); const hgRevision = await genMercurialRevision();
await modifyPackageManifest(dir, versionNumber, hgRevision, argv.channel); await modifyPackageManifest(dir, versionNumber, hgRevision, argv.channel);

View File

@@ -9,6 +9,7 @@
import Metro from 'metro'; import Metro from 'metro';
import tmp from 'tmp'; import tmp from 'tmp';
import os from 'os';
import path from 'path'; import path from 'path';
import fs from 'fs-extra'; import fs from 'fs-extra';
import {spawn} from 'promisify-child-process'; 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 = { const minifierConfig = {
minifierPath: require.resolve('metro-minify-terser'), minifierPath: require.resolve('metro-minify-terser'),
minifierConfig: { minifierConfig: {
@@ -179,7 +190,6 @@ async function compile(
entry: string, entry: string,
) { ) {
const out = path.join(buildFolder, 'bundle.js'); const out = path.join(buildFolder, 'bundle.js');
const sourceMapUrl = dev ? 'bundle.map' : undefined;
await Metro.runBuild( await Metro.runBuild(
{ {
reporter: {update: () => {}}, reporter: {update: () => {}},
@@ -203,12 +213,16 @@ async function compile(
dev, dev,
minify: !dev, minify: !dev,
resetCache: !dev, resetCache: !dev,
sourceMap: dev, sourceMap: true,
sourceMapUrl, sourceMapUrl: dev ? 'bundle.map' : undefined,
inlineSourceMap: false,
entry, entry,
out, out,
}, },
); );
if (!dev) {
stripSourceMapComment(out);
}
} }
export async function compileRenderer(buildFolder: string) { 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() { export async function compileMain() {
const out = path.join(staticDir, 'main.bundle.js'); const out = path.join(staticDir, 'main.bundle.js');
process.env.FLIPPER_ELECTRON_VERSION = process.env.FLIPPER_ELECTRON_VERSION =
@@ -259,10 +300,15 @@ export async function compileMain() {
out, out,
dev, dev,
minify: !dev, minify: !dev,
sourceMap: dev, sourceMap: true,
sourceMapUrl: dev ? 'main.bundle.map' : undefined,
inlineSourceMap: false,
resetCache: !dev, resetCache: !dev,
}); });
console.log('✅ Compiled main bundle.'); console.log('✅ Compiled main bundle.');
if (!dev) {
stripSourceMapComment(out);
}
} catch (err) { } catch (err) {
die(err); die(err);
} }