Skip to content

Commit

Permalink
Use rollup for browser builds
Browse files Browse the repository at this point in the history
  • Loading branch information
skovhus committed Jun 27, 2017
1 parent 9f9564a commit 49ddf7a
Show file tree
Hide file tree
Showing 5 changed files with 1,941 additions and 63 deletions.
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"ansi-styles": "^3.0.0",
"babel-core": "^6.23.1",
"babel-eslint": "^7.1.1",
"babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-syntax-trailing-function-commas": "^6.13.0",
"babel-plugin-transform-async-to-generator": "^6.16.0",
"babel-plugin-transform-es2015-destructuring": "^6.23.0",
Expand Down Expand Up @@ -53,6 +54,14 @@
"react-test-renderer": "15.4.2",
"regenerator-runtime": "^0.10.3",
"rimraf": "^2.5.4",
"rollup": "^0.41.6",
"rollup-plugin-babel": "^2.7.1",
"rollup-plugin-commonjs": "^8.0.2",
"rollup-plugin-flow": "^1.1.1",
"rollup-plugin-json": "^2.1.1",
"rollup-plugin-node-builtins": "^2.1.1",
"rollup-plugin-node-globals": "^1.1.0",
"rollup-plugin-node-resolve": "^3.0.0",
"slash": "^1.0.0",
"string-length": "^1.0.1",
"strip-ansi": "^3.0.1",
Expand Down
14 changes: 7 additions & 7 deletions packages/jest-haste-map/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -533,18 +533,18 @@ mime-types@^2.1.12, mime-types@~2.1.7:
dependencies:
mime-db "~1.27.0"

minimatch@^3.0.0, minimatch@^3.0.2:
version "3.0.3"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.3.tgz#2a4e4090b96b2db06a9d7df01055a62a77c9b774"
dependencies:
brace-expansion "^1.0.0"

minimatch@^3.0.4:
minimatch@^3.0.0, minimatch@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
dependencies:
brace-expansion "^1.1.7"

minimatch@^3.0.2:
version "3.0.3"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.3.tgz#2a4e4090b96b2db06a9d7df01055a62a77c9b774"
dependencies:
brace-expansion "^1.0.0"

[email protected]:
version "0.0.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
Expand Down
66 changes: 66 additions & 0 deletions scripts/browserBuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

const rollup = require('rollup').rollup;
const rollupResolve = require('rollup-plugin-node-resolve');
const rollupCommonjs = require('rollup-plugin-commonjs');
const rollupBuiltins = require('rollup-plugin-node-builtins');
const rollupGlobals = require('rollup-plugin-node-globals');
const rollupJson = require('rollup-plugin-json');
const rollupBabel = require('rollup-plugin-babel');
const rollupFlow = require('rollup-plugin-flow');

const babelEs5Options = Object.assign({}, {
babelrc: false,
exclude: 'node_modules/**',
plugins: [
'syntax-trailing-function-commas',
'transform-flow-strip-types',
'transform-es2015-destructuring',
'transform-es2015-parameters',
'transform-async-to-generator',
'transform-strict-mode',
'external-helpers',
'transform-runtime',
],
presets: [
[
'env',
{
modules: false,
},
],
],
runtimeHelpers: true,
});

function browserBuild(pkgName, entryPath, destination) {
return rollup({
entry: entryPath,
onwarn: () => {},
plugins: [
rollupFlow(),
rollupJson(),
rollupCommonjs(),
rollupBabel(babelEs5Options),
rollupGlobals(),
rollupBuiltins(),
rollupResolve(),
],
useStrict: false,
}).then(bundle => {
return bundle.write({
dest: destination,
format: 'umd',
moduleName: pkgName,
});
});
}

module.exports = browserBuild;
42 changes: 21 additions & 21 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const chalk = require('chalk');
const micromatch = require('micromatch');
const stringLength = require('string-length');
const getPackages = require('./_getPackages');
const browserBuild = require('./browserBuild');

const OK = chalk.reset.inverse.bold.green(' DONE ');
const SRC_DIR = 'src';
Expand All @@ -40,12 +41,6 @@ const babelNodeOptions = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '..', '.babelrc'), 'utf8')
);
babelNodeOptions.babelrc = false;
const babelEs5Options = Object.assign(
{},
babelNodeOptions,
{presets: 'env'},
{plugins: [...babelNodeOptions.plugins, 'transform-runtime']}
);

const adjustToTerminalWidth = str => {
const columns = process.stdout.columns || 80;
Expand All @@ -70,7 +65,7 @@ function getBuildPath(file, buildFolder) {
return path.resolve(pkgBuildPath, relativeToSrcPath);
}

function buildPackage(p) {
function buildNodePackage(p) {
const srcDir = path.resolve(p, SRC_DIR);
const pattern = path.resolve(srcDir, '**/*');
const files = glob.sync(pattern, {nodir: true});
Expand All @@ -81,29 +76,30 @@ function buildPackage(p) {
process.stdout.write(`${OK}\n`);
}

function buildFile(file, silent) {
buildFileFor(file, silent, 'node');
function buildBrowserPackage(p) {
const srcDir = path.resolve(p, SRC_DIR);
const pkgJsonPath = path.resolve(p, 'package.json');

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');
browserBuild(
p.split('/').pop(),
path.resolve(srcDir, 'index.js'),
path.resolve(p, browser)
).then(() => {
process.stdout.write(adjustToTerminalWidth(`${path.basename(p)}\n`));
process.stdout.write(`${OK}\n`);
});
}
}

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;
function buildFile(file, silent) {
const destPath = getBuildPath(file, BUILD_DIR);

mkdirp.sync(path.dirname(destPath));
if (micromatch.isMatch(file, IGNORE_PATTERN)) {
Expand All @@ -125,7 +121,7 @@ function buildFileFor(file, silent, env) {
'\n'
);
} else {
const transformed = babel.transformFileSync(file, babelOptions).code;
const transformed = babel.transformFileSync(file, babelNodeOptions).code;
fs.writeFileSync(destPath, transformed);
silent ||
process.stdout.write(
Expand All @@ -143,7 +139,11 @@ const files = process.argv.slice(2);
if (files.length) {
files.forEach(buildFile);
} else {
const packages = getPackages();
process.stdout.write(chalk.inverse(' Building packages \n'));
getPackages().forEach(buildPackage);
packages.forEach(buildNodePackage);
process.stdout.write('\n');

process.stdout.write(chalk.inverse(' Building browser packages \n'));
packages.forEach(buildBrowserPackage);
}
Loading

0 comments on commit 49ddf7a

Please sign in to comment.