Refactor out strip source map fn

Summary: Remove the duplicate function and centralise it as nikoant suggested.

Reviewed By: fabiomassimo

Differential Revision: D29548480

fbshipit-source-id: 3e931cc88198415017c557c6b7c81cb35c3f22c9
This commit is contained in:
Pascal Hartig
2021-07-05 06:36:20 -07:00
committed by Facebook GitHub Bot
parent 04616ad647
commit 0e1f8e45ec
4 changed files with 30 additions and 24 deletions

View File

@@ -10,3 +10,4 @@
export {default as runBuild} from './runBuild'; export {default as runBuild} from './runBuild';
export {default as getWatchFolders} from './getWatchFolders'; export {default as getWatchFolders} from './getWatchFolders';
export {default as computePackageChecksum} from './computePackageChecksum'; export {default as computePackageChecksum} from './computePackageChecksum';
export {default as stripSourceMapComment} from './stripSourceMap';

View File

@@ -13,6 +13,7 @@ import path from 'path';
import fs from 'fs-extra'; import fs from 'fs-extra';
import {getInstalledPluginDetails} from 'flipper-plugin-lib'; import {getInstalledPluginDetails} from 'flipper-plugin-lib';
import {FileStore} from 'metro-cache'; import {FileStore} from 'metro-cache';
import stripSourceMapComment from './stripSourceMap';
import os from 'os'; import os from 'os';
let metroDir: string | undefined; let metroDir: string | undefined;
@@ -37,18 +38,6 @@ type Options = {
sourceMapPath?: string | undefined; 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( export default async function bundlePlugin(
pluginDir: string, pluginDir: string,
dev: boolean, dev: boolean,

View File

@@ -0,0 +1,23 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import os from 'os';
import fs from 'fs-extra';
// 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.
export default 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));
}
}

View File

@@ -9,11 +9,14 @@
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';
import {getWatchFolders, runBuild} from 'flipper-pkg-lib'; import {
getWatchFolders,
runBuild,
stripSourceMapComment,
} from 'flipper-pkg-lib';
import getAppWatchFolders from './get-app-watch-folders'; import getAppWatchFolders from './get-app-watch-folders';
import { import {
getSourcePlugins, getSourcePlugins,
@@ -161,16 +164,6 @@ 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: {