Skip to content

Commit

Permalink
Merge pull request #104 from Financial-Times/matth/shared-rollup
Browse files Browse the repository at this point in the history
Shared rollup configuration
  • Loading branch information
i-like-robots authored Jul 24, 2018
2 parents d589cee + e4f721d commit d88b27e
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 156 deletions.
6 changes: 2 additions & 4 deletions components/x-increment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
"main": "dist/Increment.cjs.js",
"scripts": {
"postinstall": "npm run build",
"build": "rollup -c rollup.config.js",
"start": "rollup --watch -c rollup.config.js"
"build": "node rollup.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"browser": "dist/Increment.es5.js",
"module": "dist/Increment.esm.js",
"devDependencies": {
"@financial-times/x-rollup": "file:../../packages/x-rollup",
"rollup": "^0.57.1"
"@financial-times/x-rollup": "file:../../packages/x-rollup"
},
"dependencies": {
"@financial-times/x-engine": "file:../../packages/x-engine",
Expand Down
6 changes: 0 additions & 6 deletions components/x-increment/rollup.config.js

This file was deleted.

4 changes: 4 additions & 0 deletions components/x-increment/rollup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const xRollup = require('@financial-times/x-rollup');
const pkg = require('./package.json');

xRollup({ input: './src/Increment.jsx', pkg });
6 changes: 2 additions & 4 deletions components/x-interaction/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
"main": "dist/Interaction.cjs.js",
"scripts": {
"postinstall": "npm run build",
"build": "rollup -c rollup.config.js",
"start": "rollup --watch -c rollup.config.js"
"build": "node rollup.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"browser": "dist/Interaction.es5.js",
"module": "dist/Interaction.esm.js",
"devDependencies": {
"@financial-times/x-rollup": "file:../../packages/x-rollup",
"rollup": "^0.57.1"
"@financial-times/x-rollup": "file:../../packages/x-rollup"
},
"dependencies": {
"@financial-times/x-engine": "file:../../packages/x-engine",
Expand Down
12 changes: 0 additions & 12 deletions components/x-interaction/rollup.config.js

This file was deleted.

4 changes: 4 additions & 0 deletions components/x-interaction/rollup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const xRollup = require('@financial-times/x-rollup');
const pkg = require('./package.json');

xRollup({ input: './src/Interaction.jsx', pkg });
6 changes: 2 additions & 4 deletions components/x-styling-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
"private": true,
"scripts": {
"postinstall": "npm run build",
"build": "rollup -c rollup.config.js",
"start": "rollup --watch -c rollup.config.js"
"build": "node rollup.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@financial-times/x-rollup": "file:../../packages/x-rollup",
"node-sass": "^4.9.0",
"rollup": "^0.57.1"
"node-sass": "^4.9.0"
},
"dependencies": {
"@financial-times/x-engine": "file:../../packages/x-engine",
Expand Down
6 changes: 0 additions & 6 deletions components/x-styling-demo/rollup.config.js

This file was deleted.

4 changes: 4 additions & 0 deletions components/x-styling-demo/rollup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const xRollup = require('@financial-times/x-rollup');
const pkg = require('./package.json');

xRollup({ input: './src/Button.jsx', pkg });
6 changes: 2 additions & 4 deletions components/x-teaser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"types": "Props.d.ts",
"scripts": {
"postinstall": "npm run build",
"build": "rollup -c rollup.config.js",
"start": "rollup --watch -c rollup.config.js"
"build": "node rollup.js"
},
"keywords": [],
"author": "",
Expand All @@ -19,8 +18,7 @@
"dateformat": "^3.0.3"
},
"devDependencies": {
"@financial-times/x-rollup": "file:../../packages/x-rollup",
"rollup": "^0.57.1"
"@financial-times/x-rollup": "file:../../packages/x-rollup"
},
"engines": {
"node": ">= 6.0.0"
Expand Down
10 changes: 0 additions & 10 deletions components/x-teaser/rollup.config.js

This file was deleted.

4 changes: 4 additions & 0 deletions components/x-teaser/rollup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const xRollup = require('@financial-times/x-rollup');
const pkg = require('./package.json');

xRollup({ input: './src/Teaser.jsx', pkg });
110 changes: 14 additions & 96 deletions packages/x-rollup/index.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,19 @@
const babel = require('rollup-plugin-babel');
const commonjs = require('rollup-plugin-commonjs');
const postcss = require('rollup-plugin-postcss');
const path = require('path');
/* eslint no-console:off */

const resolvePlugin = (plugin) => Array.isArray(plugin)
? [require.resolve(plugin[0]), plugin[1]]
: require.resolve(plugin);
const rollup = require('rollup');
const rollupConfig = require('./src/rollup-config');

const babelOptions = (targets) => ({
include: '**/*.{js,jsx}',
plugins: [
['babel-plugin-transform-react-jsx', {
pragma: 'h',
useBuiltIns: true,
}],
['babel-plugin-transform-object-rest-spread', {
// although this is stage 4, we'd have to use babel 7 to get the version
// of preset-env that supports it :/
useBuiltIns: true,
}],
'babel-plugin-external-helpers',
['fast-async', {
compiler: {
noRuntime: true,
},
}],
].map(resolvePlugin),
module.exports = async (options) => {
const config = rollupConfig(options);

presets: targets && [
['babel-preset-env', {
targets,
modules: false,
exclude: ['transform-regenerator', 'transform-async-to-generator'],
}],
].map(resolvePlugin),
});

module.exports = ({input, pkg, external: extraExternal = []}) => {
const external = [
'@financial-times/x-engine',
...extraExternal,
];

const commonPlugin = commonjs({ extensions: ['.js', '.jsx'] });
const postcssPlugin = pkg.style && postcss({
extract: pkg.style,
modules: true,
use: [
['sass', {
includePaths: [path.resolve(process.cwd(), 'bower_components')]
}],
'stylus',
'less',
],
});

return [
{
input,
output: {
file: pkg.module,
format: 'es'
},
external,
plugins: [
babel(babelOptions({ node: 6 })),
postcssPlugin,
commonPlugin
].filter(Boolean),
},
{
input,
output: {
file: pkg.main,
format: 'cjs'
},
external,
plugins: [
babel(babelOptions({ node: 6 })),
postcssPlugin,
commonPlugin,
].filter(Boolean),
},
{
input,
output: {
file: pkg.browser,
format: 'cjs'
},
external,
plugins: [
babel(babelOptions({ browsers: ['ie 11'] })),
postcssPlugin,
commonPlugin,
].filter(Boolean),
for (const [input, output] of config) {
try {
console.log(`Bundling ${input.input}${output.file}…`);
const bundle = await rollup.rollup(input);
await bundle.write(output);
} catch (error) {
console.error(error);
process.exit(1);
}
];
}
};

module.exports.babelOptions = babelOptions;
4 changes: 1 addition & 3 deletions packages/x-rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
"babel-plugin-transform-react-jsx": "^6.24.1",
"babel-preset-env": "^1.7.0",
"fast-async": "^6.3.7",
"rollup": "^0.63.0",
"rollup-plugin-babel": "^3.0.7",
"rollup-plugin-commonjs": "^9.1.3",
"rollup-plugin-postcss": "^1.6.2"
},
"peerDependencies": {
"rollup": "^0.57.1"
}
}
7 changes: 0 additions & 7 deletions packages/x-rollup/rollup.template.js

This file was deleted.

43 changes: 43 additions & 0 deletions packages/x-rollup/src/babel-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module.exports = (targets = []) => ({
include: '**/*.{js,jsx}',
plugins: [
// this plugin is not React specific! It includes a general JSX parser and helper 🙄
[
require.resolve('babel-plugin-transform-react-jsx'),
{
pragma: 'h',
useBuiltIns: true
}
],
// Although this feature is at stage 4, we'd have to use babel 7 to get the version
// of preset-env that actually supports it 😖
[
require.resolve('babel-plugin-transform-object-rest-spread'),
{
useBuiltIns: true
}
],
// Instruct Babel to not include any internal helper declarations in the output
require.resolve('babel-plugin-external-helpers'),
// Implements async/await using syntax transformation rather than generators which require
// a huge runtime for browsers which do not natively support them.
[
require.resolve('fast-async'),
{
compiler: {
noRuntime: true
}
}
]
],
presets: [
[
require.resolve('babel-preset-env'),
{
targets,
modules: false,
exclude: ['transform-regenerator', 'transform-async-to-generator']
}
]
]
});
18 changes: 18 additions & 0 deletions packages/x-rollup/src/postcss-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const path = require('path');

module.exports = (style) => {
return {
extract: style,
modules: true,
use: [
[
'sass',
{
includePaths: [path.resolve(process.cwd(), 'bower_components')]
}
],
'stylus',
'less'
]
};
};
58 changes: 58 additions & 0 deletions packages/x-rollup/src/rollup-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const babel = require('rollup-plugin-babel');
const postcss = require('rollup-plugin-postcss');
const commonjs = require('rollup-plugin-commonjs');
const postcssConfig = require('./postcss-config');
const babelConfig = require('./babel-config');

module.exports = ({ input, pkg }) => {
// Don't bundle any dependencies
const external = Object.keys(pkg.dependencies);

const plugins = [
// Convert CommonJS modules to ESM so they can be included in the bundle
commonjs({ extensions: ['.js', '.jsx'] })
];

// Add support for CSS modules (and any required transpilation)
if (pkg.style) {
const config = postcssConfig(pkg.style);
plugins.push(postcss(config));
}

// Pairs of input and output options
return [
[
{
input,
external,
plugins: [babel(babelConfig({ node: 6 })), ...plugins]
},
{
file: pkg.module,
format: 'es'
}
],
[
{
input,
external,
plugins: [babel(babelConfig({ node: 6 })), ...plugins]
},
{
file: pkg.main,
format: 'cjs'
}
],
[
{
input,
external,
plugins: [babel(babelConfig({ browsers: ['ie 11'] })), ...plugins]
},
{
file: pkg.browser,
format: 'cjs'
}
]
];
};

0 comments on commit d88b27e

Please sign in to comment.