Skip to content

Commit

Permalink
Add build script to react-native-babel-transformer
Browse files Browse the repository at this point in the history
Summary:
This diff adds build script and 'prepare' hook to `react-native-babel-transformer`. The script is effectively is a copy-paste from `react-native-codegen`, we'll deduplicate them in the future.

Changelog: [Internal] - Add build script to react-native-babel-transformer

bypass-github-export-checks

Differential Revision: https://internalfb.com/D47006627

fbshipit-source-id: 7b05d98759d8554f46e57684db4247368a78e0d4
  • Loading branch information
Dmytro Rykun authored and facebook-github-bot committed Jul 6, 2023
1 parent a631b0e commit 6516541
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 2 deletions.
12 changes: 12 additions & 0 deletions packages/react-native-babel-transformer/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"plugins": [
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-transform-async-to-generator",
"@babel/plugin-transform-destructuring",
"@babel/plugin-transform-flow-strip-types",
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-nullish-coalescing-operator",
"@babel/plugin-proposal-optional-chaining"
]
}
8 changes: 8 additions & 0 deletions packages/react-native-babel-transformer/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"arrowParens": "avoid",
"bracketSameLine": true,
"bracketSpacing": false,
"requirePragma": true,
"singleQuote": true,
"trailingComma": "all"
}
26 changes: 24 additions & 2 deletions packages/react-native-babel-transformer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
"url": "[email protected]:facebook/metro.git"
},
"scripts": {
"prepare-release": "test -d build && rm -rf src.real && mv src src.real && mv build src",
"cleanup-release": "test ! -e build && mv src build && mv src.real src"
"prepare-release": "test -d lib && mv src src.real && mv lib src",
"cleanup-release": "test ! -e lib && mv src lib && mv src.real src && rimraf lib",
"build-clean": "rimraf build && rimraf src.real",
"build": " node scripts/build.js --verbose",
"prepare": "yarn run build-clean && yarn run build && yarn run prepare-release",
"postpublish": "yarn run cleanup-release"
},
"keywords": [
"transformer",
Expand All @@ -24,6 +28,24 @@
"metro-react-native-babel-preset": "0.76.7",
"nullthrows": "^1.1.1"
},
"devDependencies": {
"@babel/plugin-proposal-class-properties": "^7.18.0",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.0",
"@babel/plugin-proposal-object-rest-spread": "^7.20.0",
"@babel/plugin-proposal-optional-chaining": "^7.20.0",
"@babel/plugin-syntax-dynamic-import": "^7.8.0",
"@babel/plugin-transform-async-to-generator": "^7.20.0",
"@babel/plugin-transform-destructuring": "^7.20.0",
"@babel/plugin-transform-flow-strip-types": "^7.20.0",
"@babel/preset-env": "^7.20.0",
"chalk": "^4.0.0",
"glob": "^7.1.1",
"invariant": "^2.2.4",
"micromatch": "^4.0.4",
"mkdirp": "^0.5.1",
"prettier": "2.8.8",
"rimraf": "^3.0.2"
},
"peerDependencies": {
"@babel/core": "*"
},
Expand Down
114 changes: 114 additions & 0 deletions packages/react-native-babel-transformer/scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

/**
* script to build (transpile) files.
*
* Based off of the build script from Metro, and tweaked to run in just one
* package instead of in a monorepo. Just run `build.js` and the JS files in
* `src/` will be built in `lib/`, and the original source files will be copied
* over as `Example.js.flow`, so consumers of this module can still make use of
* type checking.
*
* Call this script with the `--verbose` flag to show the full output of this
* script.
*/

'use strict';

const babel = require('@babel/core');
const chalk = require('chalk');
const fs = require('fs');
const glob = require('glob');
const micromatch = require('micromatch');
const mkdirp = require('mkdirp');
const path = require('path');
const prettier = require('prettier');
const prettierConfig = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '..', '.prettierrc'), 'utf8'),
);

const SRC_DIR = 'src';
const BUILD_DIR = 'lib';
const JS_FILES_PATTERN = '**/*.js';
const IGNORE_PATTERN = '**/__tests__/**';
const PACKAGE_DIR = path.resolve(__dirname, '../');

const fixedWidth = str => {
const WIDTH = 80;
const strs = str.match(new RegExp(`(.{1,${WIDTH}})`, 'g')) || [str];
let lastString = strs[strs.length - 1];
if (lastString.length < WIDTH) {
lastString += Array(WIDTH - lastString.length).join(chalk.dim('.'));
}
return strs.slice(0, -1).concat(lastString).join('\n');
};

function getBuildPath(file, buildFolder) {
const pkgSrcPath = path.resolve(PACKAGE_DIR, SRC_DIR);
const pkgBuildPath = path.resolve(PACKAGE_DIR, BUILD_DIR);
const relativeToSrcPath = path.relative(pkgSrcPath, file);
return path.resolve(pkgBuildPath, relativeToSrcPath);
}

function buildFile(file, silent) {
const destPath = getBuildPath(file, BUILD_DIR);

mkdirp.sync(path.dirname(destPath));
if (micromatch.isMatch(file, IGNORE_PATTERN)) {
silent ||
process.stdout.write(
chalk.dim(' \u2022 ') +
path.relative(PACKAGE_DIR, file) +
' (ignore)\n',
);
} else if (!micromatch.isMatch(file, JS_FILES_PATTERN)) {
fs.createReadStream(file).pipe(fs.createWriteStream(destPath));
silent ||
process.stdout.write(
chalk.red(' \u2022 ') +
path.relative(PACKAGE_DIR, file) +
chalk.red(' \u21D2 ') +
path.relative(PACKAGE_DIR, destPath) +
' (copy)' +
'\n',
);
} else {
const transformed = prettier.format(
babel.transformFileSync(file, {}).code,
{
...prettierConfig,
parser: 'babel',
},
);
fs.writeFileSync(destPath, transformed);
const source = fs.readFileSync(file).toString('utf-8');
if (/@flow/.test(source)) {
fs.createReadStream(file).pipe(fs.createWriteStream(destPath + '.flow'));
}
silent ||
process.stdout.write(
chalk.green(' \u2022 ') +
path.relative(PACKAGE_DIR, file) +
chalk.green(' \u21D2 ') +
path.relative(PACKAGE_DIR, destPath) +
'\n',
);
}
}

const srcDir = path.resolve(__dirname, '..', SRC_DIR);
const pattern = path.resolve(srcDir, '**/*');
const files = glob.sync(pattern, {nodir: true});

process.stdout.write(fixedWidth(`${path.basename(PACKAGE_DIR)}\n`));

files.forEach(file => buildFile(file, !process.argv.includes('--verbose')));

process.stdout.write(`[ ${chalk.green('OK')} ]\n`);

0 comments on commit 6516541

Please sign in to comment.