Files
flipper/scripts/build-utils.js
Anton Nikolaev 18c259dc22 Typescriptify the main process code (1/N)
Summary:
As a first step I've configured bundling for the main process code using Metro. For now I haven't converted anything to ts, just made that possible.

The bundle is just produced into the "static" directory. To avoid too many changes I kept the "static" folder as it is, but probably non-static code should be moved from there.

Also installed modules from "node_modules" for the main process are not bundled to avoid potential issues with node native modules.

Reviewed By: mweststrate

Differential Revision: D19960982

fbshipit-source-id: efbd426254e2b37c913c5f5f75f042c50ccee2f3
2020-02-24 05:23:45 -08:00

160 lines
4.1 KiB
JavaScript

/**
* 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
*/
const Metro = require('../static/node_modules/metro');
const compilePlugins = require('../static/compilePlugins');
const tmp = require('tmp');
const path = require('path');
const fs = require('fs-extra');
const cp = require('promisify-child-process');
function die(err) {
console.error(err.stack);
process.exit(1);
}
function compileDefaultPlugins(defaultPluginDir, skipAll = false) {
return compilePlugins(
null,
skipAll
? []
: [
path.join(__dirname, '..', 'src', 'plugins'),
path.join(__dirname, '..', 'src', 'fb', 'plugins'),
],
defaultPluginDir,
{force: true, failSilently: false, recompileOnChanges: false},
)
.then(defaultPlugins =>
fs.writeFileSync(
path.join(defaultPluginDir, 'index.json'),
JSON.stringify(
defaultPlugins.map(({entry, rootDir, out, ...plugin}) => ({
...plugin,
out: path.parse(out).base,
})),
),
),
)
.catch(die);
}
function compile(buildFolder, entry) {
console.log(`⚙️ Compiling renderer bundle...`);
const projectRoots = path.join(__dirname, '..');
return Metro.runBuild(
{
reporter: {update: () => {}},
projectRoot: projectRoots,
watchFolders: [projectRoots],
serializer: {},
transformer: {
babelTransformerPath: path.join(
__dirname,
'..',
'static',
'transforms',
'index.js',
),
},
resolver: {
blacklistRE: /(\/|\\)(sonar|flipper|flipper-public)(\/|\\)(dist|doctor)(\/|\\)|(\.native\.js$)/,
},
},
{
dev: false,
minify: false,
resetCache: true,
sourceMap: true,
entry,
out: path.join(buildFolder, 'bundle.js'),
},
)
.then(() => console.log('✅ Compiled renderer bundle.'))
.catch(die);
}
async function compileMain() {
console.log(`⚙️ Compiling main bundle...`);
try {
const staticDir = path.resolve(__dirname, '..', 'static');
const config = Object.assign({}, await Metro.loadConfig(), {
reporter: {update: () => {}},
projectRoot: staticDir,
watchFolders: [staticDir],
transformer: {
babelTransformerPath: path.join(
__dirname,
'..',
'static',
'transforms',
'index.js',
),
},
resolver: {
sourceExts: ['tsx', 'ts', 'js'],
blacklistRE: /(\/|\\)(sonar|flipper|flipper-public)(\/|\\)(dist|doctor)(\/|\\)|(\.native\.js$)/,
},
});
await Metro.runBuild(config, {
platform: 'web',
entry: path.join(staticDir, 'main.js'),
out: path.join(staticDir, 'main.bundle.js'),
dev: false,
minify: false,
sourceMap: true,
resetCache: true,
});
console.log('✅ Compiled main bundle.');
} catch (err) {
die(err);
}
}
function buildFolder() {
// eslint-disable-next-line no-console
console.log('Creating build directory');
return new Promise((resolve, reject) => {
tmp.dir({prefix: 'flipper-build-'}, (err, buildFolder) => {
if (err) {
reject(err);
} else {
resolve(buildFolder);
}
});
}).catch(die);
}
function getVersionNumber() {
let {version} = require('../package.json');
const buildNumber = process.argv.join(' ').match(/--version=(\d+)/);
if (buildNumber && buildNumber.length > 0) {
version = [...version.split('.').slice(0, 2), buildNumber[1]].join('.');
}
return version;
}
// Asynchronously determine current mercurial revision as string or `null` in case of any error.
function genMercurialRevision() {
return cp
.spawn('hg', ['log', '-r', '.', '-T', '{node}'], {encoding: 'utf8'})
.catch(err => null)
.then(res => (res && res.stdout) || null);
}
module.exports = {
buildFolder,
compile,
compileMain,
die,
compileDefaultPlugins,
getVersionNumber,
genMercurialRevision,
};