Split package "flipper-pkg" into two: lib and cli

Summary: "flipper-pkg" added ~2MB to Flipper disttributive size, because of heavy dependencies which are only required for CLI functionality. See size warning in diff D21068373 in this stack where I added pkg as dependency to flipper. Here I'm splitting it into library and CLI packages, so Flipper app will only reference the library.

Reviewed By: passy

Differential Revision: D21087336

fbshipit-source-id: d9d62f1e75a835d1c0fa78ff1addb0d9a761a9c7
This commit is contained in:
Anton Nikolaev
2020-04-17 09:58:24 -07:00
committed by Facebook GitHub Bot
parent 4395b19140
commit 21b79af5f2
21 changed files with 131 additions and 14 deletions

View File

@@ -0,0 +1,68 @@
/**
* 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 Metro from 'metro';
import getWatchFolders from './getWatchFolders';
function hash(string: string) {
let hash = 0;
if (string.length === 0) {
return hash;
}
let chr;
for (let i = 0; i < string.length; i++) {
chr = string.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0;
}
return hash;
}
const fileToIdMap = new Map();
const createModuleIdFactory = () => (filePath: string) => {
if (filePath === '__prelude__') {
return 0;
}
let id = fileToIdMap.get(filePath);
if (typeof id !== 'number') {
id = hash(filePath);
fileToIdMap.set(filePath, id);
}
return id;
};
export default async function runBuild(
inputDirectory: string,
entry: string,
out: string,
) {
const baseConfig = await Metro.loadConfig();
const config = Object.assign({}, baseConfig, {
reporter: {update: () => {}},
projectRoot: inputDirectory,
watchFolders: [inputDirectory, ...(await getWatchFolders(inputDirectory))],
serializer: {
...baseConfig.serializer,
getRunModuleStatement: (moduleID: string) =>
`module.exports = global.__r(${moduleID}).default;`,
createModuleIdFactory,
},
transformer: {
...baseConfig.transformer,
babelTransformerPath: require.resolve('flipper-babel-transformer'),
},
});
await Metro.runBuild(config, {
dev: false,
minify: false,
resetCache: false,
sourceMap: true,
entry,
out,
});
}