Skip to content

Commit

Permalink
Add support for building es2015 versions of packages
Browse files Browse the repository at this point in the history
  • Loading branch information
skovhus committed Apr 29, 2017
1 parent 0817d64 commit 4d2e564
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/integration_tests/transform/*/node_modules
/node_modules
/packages/*/build/
/packages/*/build-es5/
/packages/*/coverage/
/packages/*/node_modules/
/website/build
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"typescript": "^2.2.2"
},
"scripts": {
"build-clean": "rm -rf ./packages/*/build",
"build-clean": "rm -rf ./packages/*/build ./packages/*/build-es5",
"build": "node ./scripts/build.js",
"clean-all": "rm -rf ./node_modules && rm -rf ./packages/*/node_modules && rm -rf ./integration_tests/*/*/node_modules && yarn run build-clean",
"danger": "node ./danger/node_modules/.bin/danger",
Expand All @@ -73,6 +73,7 @@
"modulePathIgnorePatterns": [
"examples/.*",
"packages/.*/build",
"packages/.*/build-es5",
"packages/jest-runtime/src/__tests__/test_root.*",
"website/.*",
"integration_tests/runtime-internal-module-registry/__mocks__"
Expand Down Expand Up @@ -104,7 +105,8 @@
"/examples/",
"/integration_tests/.*/__tests__",
"\\.snap$",
"/packages/.*/build"
"/packages/.*/build",
"/packages/.*/build-es5"
],
"testMatch": [
"**/*-test.js"
Expand Down
56 changes: 45 additions & 11 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,29 @@
* node ./scripts/build.js /users/123/jest/packages/jest-111/src/111.js
*/

const babel = require('babel-core');
const chalk = require('chalk');
const fs = require('fs');
const getPackages = require('./_getPackages');
const glob = require('glob');
const micromatch = require('micromatch');
const mkdirp = require('mkdirp');
const path = require('path');

const babel = require('babel-core');
const chalk = require('chalk');
const micromatch = require('micromatch');

const getPackages = require('./_getPackages');

const SRC_DIR = 'src';
const BUILD_DIR = 'build';
const BUILD_ES5_DIR = 'build-es5';
const JS_FILES_PATTERN = '**/*.js';
const IGNORE_PATTERN = '**/__tests__/**';
const PACKAGES_DIR = path.resolve(__dirname, '../packages');

const babelOptions = JSON.parse(
const babelNodeOptions = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '..', '.babelrc'), 'utf8')
);
babelOptions.babelrc = false;
babelNodeOptions.babelrc = false;
const babelEs5Options = Object.assign({}, babelNodeOptions, {presets: 'env'});

const fixedWidth = str => {
const WIDTH = 80;
Expand All @@ -48,6 +53,18 @@ const fixedWidth = str => {
return strs.slice(0, -1).concat(lastString).join('\n');
};

function getPackageName(file) {
return path.relative(PACKAGES_DIR, file).split(path.sep)[0];
}

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

function buildPackage(p) {
const srcDir = path.resolve(p, SRC_DIR);
const pattern = path.resolve(srcDir, '**/*');
Expand All @@ -60,11 +77,28 @@ function buildPackage(p) {
}

function buildFile(file, silent) {
const packageName = path.relative(PACKAGES_DIR, file).split(path.sep)[0];
const packageSrcPath = path.resolve(PACKAGES_DIR, packageName, 'src');
const packageBuildPath = path.resolve(PACKAGES_DIR, packageName, 'build');
const relativeToSrcPath = path.relative(packageSrcPath, file);
const destPath = path.resolve(packageBuildPath, relativeToSrcPath);
buildFileFor(file, silent, 'node');

const pkgJsonPath = path.resolve(
PACKAGES_DIR,
getPackageName(file),
'package.json'
);
const {browser} = require(pkgJsonPath);
if (browser) {
if (browser.indexOf(BUILD_ES5_DIR) !== 0) {
throw new Error(
`browser field for ${pkgJsonPath} should start with "${BUILD_ES5_DIR}"`
);
}
buildFileFor(file, silent, 'es5');
}
}

function buildFileFor(file, silent, env) {
const buildDir = env === 'es5' ? BUILD_ES5_DIR : BUILD_DIR;
const destPath = getBuildPath(file, buildDir);
const babelOptions = env === 'es5' ? babelEs5Options : babelNodeOptions;

mkdirp.sync(path.dirname(destPath));
if (micromatch.isMatch(file, IGNORE_PATTERN)) {
Expand Down

0 comments on commit 4d2e564

Please sign in to comment.