How to use the metro.runBuild function in metro

To help you get started, we’ve selected a few metro examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github facebook / flipper / static / compilePlugins.js View on Github external
const out = path.join(pluginCache, fileName);
  const result = Object.assign({}, packageJSON, {rootDir, name, entry, out});
  // check if plugin needs to be compiled
  const rootDirCtime = await mostRecentlyChanged(rootDir);
  if (
    !options.force &&
    fs.existsSync(out) &&
    rootDirCtime < fs.lstatSync(out).ctime
  ) {
    // eslint-disable-next-line no-console
    console.log(`🥫  Using cached version of ${name}...`);
    return result;
  } else {
    console.log(`⚙️  Compiling ${name}...`); // eslint-disable-line no-console
    try {
      await Metro.runBuild(
        {
          reporter: {update: () => {}},
          projectRoot: rootDir,
          watchFolders: [__dirname, rootDir],
          serializer: {
            getRunModuleStatement: moduleID =>
              `module.exports = global.__r(${moduleID}).default;`,
            createModuleIdFactory,
          },
          transformer: {
            babelTransformerPath: path.join(
              __dirname,
              'transforms',
              'index.js',
            ),
          },
github rafeca / metro-sample-app / scripts / build.js View on Github external
'use strict';

const path = require('path');
const metro = require('metro');

const ROOT = path.resolve(path.join(__dirname, '..'));

metro.runBuild({
  dev: false,
  entry: path.join(ROOT, 'src', 'index.js'),
  optimize: true,
  out: path.join(ROOT, 'build', 'bundle.js'),
  projectRoots: [path.join(ROOT, 'src')],
  config: {
    getTransformModulePath: () => require.resolve('metro/src/defaultTransform'),
  }
}).then(() => {
  console.log('Production bundle created!');
}).catch(err => {
  console.log(err);
});
github facebook / metro / packages / metro-visualizer / src / middleware / routes.js View on Github external
router.use('/bundle.js', async (req, res, next) => {
  const status = 'Bundling visualizer app';

  const options = {
    dev: true,
    entry: './src/app/index.js',
    minify: false,
    platform: 'web',
  };

  const config = await metro.loadConfig({
    config: require.resolve('./build-utils/metro.config.js'),
  });

  await metro
    .runBuild(config, options)
    .then((val: {code: string, map: string}) => {
      terminal.status(`${status}... serving`);

      res.writeHead(200, {'Content-Type': 'text/javascript'});
      res.write(val.code);
      res.end();

      terminal.status(`${status}, done.`);
      terminal.persistStatus();
    })
    .catch(error => {
      terminal.log(error);
      terminal.status(`${status}, failed.`);
      terminal.persistStatus();
    });