1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-31 19:01:54 +02:00

refactor rollup, prettier and benchmark configuration

This commit is contained in:
Ian Storm Taylor
2018-02-06 19:41:03 -08:00
parent 3339d088e1
commit 83ec966113
11 changed files with 769 additions and 300 deletions

View File

@@ -1,8 +1,8 @@
/* eslint-disable no-console */
import chalk from 'chalk'
import baseline from '../tmp/benchmark-baseline'
import comparison from '../tmp/benchmark-comparison'
import baseline from '../../tmp/benchmark-baseline'
import comparison from '../../tmp/benchmark-comparison'
/**
* Constants.

View File

@@ -1,113 +1,103 @@
import path from 'path'
import { cloneDeep } from 'lodash'
import alias from 'rollup-plugin-alias'
import resolve from 'rollup-plugin-node-resolve'
import babel from 'rollup-plugin-babel'
import builtins from 'rollup-plugin-node-builtins'
import commonjs from 'rollup-plugin-commonjs'
import globals from 'rollup-plugin-node-globals'
import json from 'rollup-plugin-json'
import replace from 'rollup-plugin-replace'
import babel from 'rollup-plugin-babel'
import uglify from 'rollup-plugin-uglify'
import resolve from 'rollup-plugin-node-resolve'
import sourcemaps from 'rollup-plugin-sourcemaps'
import uglify from 'rollup-plugin-uglify'
import pkg from '../../package.json'
const environment = process.env.NODE_ENV || 'development'
/**
* Return a Rollup configuration for the examples with `env`.
*
* @param {String} env
* @return {Object}
*/
const configurations = []
function configure(env) {
const isDev = env === 'development'
const isProd = env === 'production'
const output = {
umd: pkg.umd,
umdMin: pkg.umdMin,
return {
input: 'examples/index.js',
output: {
file: isProd ? pkg.umdMin : pkg.umd,
name: 'slate-examples',
format: 'umd',
exports: 'named',
sourcemap: isDev,
},
watch: {
include: ['examples/**', 'packages/*/lib/*.es.js'],
},
plugins: [
// Allow Rollup to resolve modules from `node_modules`, since it only
// resolves local modules by default.
resolve({
browser: true,
}),
// Allow Rollup to resolve CommonJS modules, since it only resolves ES2015
// modules by default.
commonjs({
exclude: ['examples/**'],
// HACK: Sometimes the CommonJS plugin can't identify named exports, so
// we have to manually specify named exports here for them to work.
// https://github.com/rollup/rollup-plugin-commonjs#custom-named-exports
namedExports: {
esrever: ['reverse'],
immutable: [
'List',
'Map',
'Record',
'OrderedSet',
'Set',
'Stack',
'is',
],
'react-dom': ['findDOMNode'],
'react-dom/server': ['renderToStaticMarkup'],
},
}),
// Convert JSON imports to ES6 modules.
json(),
// Replace `process.env.NODE_ENV` with its value, which enables some
// modules like React and Slate to use their production variant.
replace({
'process.env.NODE_ENV': JSON.stringify(env),
}),
// Register Node.js builtins for browserify compatibility.
builtins(),
// Use Babel to transpile the result, limiting it to the source code.
babel({
include: ['examples/**'],
}),
// Register Node.js globals for browserify compatibility.
globals(),
// Only minify the output in production, since it is very slow.
isProd && uglify(),
// Only add sourcemaps in development.
isDev && sourcemaps(),
].filter(Boolean),
}
}
const umdConfig = {
input: 'examples/index.js',
output: {
file: output.umd,
name: 'slate-examples',
format: 'umd',
exports: 'named',
sourcemap: environment === 'development',
},
plugins: [
// Force rollup to use the browser variants of `debug` and `react-dom/server`
// The main variant of `debug` relies on Node.js globals, while the main
// variant of `react-dom/server` relies on Node.js's Stream.
alias({
debug: path.resolve(__dirname, 'node_modules/debug/src/browser'),
'react-dom/server': path.resolve(
__dirname,
'node_modules/react-dom/cjs/react-dom-server.browser.production.min'
),
}),
/**
* Export.
*
* @type {Array}
*/
// Allow rollup to resolve modules that are npm dependencies
// (by default, it can only resolve local modules).
resolve(),
// Allow rollup to resolve npm dependencies that are CommonJS
// (by default, it can only handle ES2015 syntax).
commonjs({
exclude: ['examples/**'],
// The CommonJS plugin sometimes cannot correctly identify named
// exports of CommonJS modules, so we manually specify here to
// hint that e.g. `import { List } from 'immutable'` is a reference
// to a valid named export.
namedExports: {
esrever: ['reverse'],
immutable: [
'List',
'Map',
'Record',
'OrderedSet',
'Set',
'Stack',
'is',
],
'react-dom': ['findDOMNode'],
'react-dom/server': ['renderToStaticMarkup'],
},
}),
// Convert JSON imports to ES6 modules.
json(),
// Replace `process.env.NODE_ENV` with its value -- needed for
// some modules like React to use their production variant (and
// one place within Slate itself).
replace({
'process.env.NODE_ENV': JSON.stringify(environment),
}),
// Use babel to transpile the result -- limit to package src
// to prevent babel from trying to transpile npm dependencies.
babel({
include: ['examples/**'],
}),
],
// Limit rollup's file watching to example src files and the
// built output of packages -- helps keep it from watching
// too much and choking.
watch: {
include: ['examples/**', 'packages/*/lib/*.es.js'],
},
}
if (environment === 'production') {
// Only build the minified UMD variant in production --
// it makes each rebuild take substantially longer.
const umdConfigMin = cloneDeep(umdConfig)
umdConfigMin.output.file = output.umdMin
umdConfigMin.plugins.push(uglify())
configurations.push(umdConfigMin)
} else {
// In development, add the sourcemaps plugin so they
// are emitted alongside the dist file.
umdConfig.plugins.push(sourcemaps())
// Only build the unminified variant in development --
// it serves no purpose in production.
configurations.push(umdConfig)
}
export default configurations
export default [
configure('development'),
process.env.NODE_ENV === 'production' && configure('production'),
].filter(Boolean)

View File

@@ -1,170 +0,0 @@
import path from 'path'
import { startCase, cloneDeep } from 'lodash'
import alias from 'rollup-plugin-alias'
import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
import babel from 'rollup-plugin-babel'
import replace from 'rollup-plugin-replace'
import uglify from 'rollup-plugin-uglify'
const environment = process.env.NODE_ENV || 'development'
export default pkg => {
const pkgName = pkg.name
const output = {
cjs: pkg.main,
es: pkg.module,
umd: pkg.umd,
umdMin: pkg.umdMin,
}
const umdGlobals = pkg.umdGlobals
// Generate list of external dependencies from package.json
let dependencies = []
if (pkg.dependencies) {
dependencies = dependencies.concat(Object.keys(pkg.dependencies))
}
if (pkg.peerDependencies) {
dependencies = dependencies.concat(Object.keys(pkg.peerDependencies))
}
// Consider a dependency external if:
// 1. It is directly located in the package.json dependencies/peerDependencies (e.g. `react`), or
// 2. It is part of a package.json dependency (e.g. `lodash/omit`)
// External dependencies are expected to be present at runtime (rather than being bundled into
// our built dist).
const isExternalDependency = id =>
!!dependencies.find(dep => dep === id || id.startsWith(`${dep}/`))
// UMD build for browsers
const umdConfig = {
input: `packages/${pkgName}/src/index.js`,
output: {
file: `packages/${pkgName}/${output.umd}`,
format: 'umd',
exports: 'named',
// For a package name such as `slate-react`, the UMD name
// should be SlateReact.
name: startCase(pkgName).replace(/ /g, ''),
// Some packages contain `umdGlobals` in their package.json, which
// indicates external dependencies that should be treated as globals
// rather than bundled into our dist, such as Immutable and React.
globals: umdGlobals,
},
// `external` tells rollup to treat the umdGlobals as external (and
// thus skip bundling them).
external: Object.keys(umdGlobals || {}),
plugins: [
// Force rollup to use the browser variant of `debug`.
// The main variant of `debug` relies on Node.js globals.
alias({
debug: path.resolve(__dirname, 'node_modules/debug/src/browser'),
}),
// Allow rollup to resolve modules that are npm dependencies
// (by default, it can only resolve local modules).
resolve(),
// Allow rollup to resolve npm dependencies that are CommonJS
// (by default, it can only handle ES2015 syntax).
commonjs({
exclude: [`packages/${pkgName}/src/**`],
// The CommonJS plugin sometimes cannot correctly identify named
// exports of CommonJS modules, so we manually specify here to
// hint that e.g. `import { List } from 'immutable'` is a reference
// to a valid named export.
namedExports: {
esrever: ['reverse'],
immutable: [
'List',
'Map',
'Record',
'OrderedSet',
'Set',
'Stack',
'is',
],
'react-dom': ['findDOMNode'],
'react-dom/server': ['renderToStaticMarkup'],
},
}),
// Replace `process.env.NODE_ENV` with its value -- needed for
// some modules like React to use their production variant (and
// one place within Slate itself).
replace({
'process.env.NODE_ENV': JSON.stringify('production'),
}),
// Use babel to transpile the result -- limit to package src
// to prevent babel from trying to transpile npm dependencies.
babel({
include: [`packages/${pkgName}/src/**`],
}),
],
}
// Additional UMD minified build based off of the unminified config
const umdConfigMin = cloneDeep(umdConfig)
umdConfigMin.output.file = `packages/${pkgName}/${output.umdMin}`
umdConfigMin.plugins.push(uglify())
// CommonJS (for Node) and ES module (for bundlers) build.
const moduleConfig = {
input: `packages/${pkgName}/src/index.js`,
output: [
{
file: `packages/${pkgName}/${output.es}`,
format: 'es',
sourcemap: environment === 'development',
},
{
file: `packages/${pkgName}/${output.cjs}`,
format: 'cjs',
exports: 'named',
},
],
external: isExternalDependency,
plugins: [
// Force rollup to use the browser variant of `debug`.
// The main variant of `debug` relies on Node.js globals.
alias({
debug: path.resolve(__dirname, 'node_modules/debug/src/browser'),
}),
// Allow rollup to resolve modules that are npm dependencies
// (by default, it can only resolve local modules).
resolve(),
// Replace `process.env.NODE_ENV` with its value -- needed for
// some modules like React to use their production variant (and
// one place within Slate itself).
replace({
'process.env.NODE_ENV': JSON.stringify(environment),
}),
// Use babel to transpile the result -- limit to package src
// to prevent babel from trying to transpile npm dependencies.
babel({
include: [`packages/${pkgName}/src/**`],
}),
],
}
const configurations = [moduleConfig]
if (environment === 'production') {
// In development, we only build the module version to
// reduce rebuild times. In production, we add the
// configs for the UMD variants here.
configurations.push(umdConfig, umdConfigMin)
}
return configurations
}

148
support/rollup/packages.js Normal file
View File

@@ -0,0 +1,148 @@
import babel from 'rollup-plugin-babel'
import builtins from 'rollup-plugin-node-builtins'
import commonjs from 'rollup-plugin-commonjs'
import globals from 'rollup-plugin-node-globals'
import json from 'rollup-plugin-json'
import replace from 'rollup-plugin-replace'
import resolve from 'rollup-plugin-node-resolve'
import uglify from 'rollup-plugin-uglify'
import { startCase } from 'lodash'
/**
* Return a Rollup configuration for a `pkg` with `env` and `target`.
*
* @param {Object} pkg
* @param {String} env
* @param {String} format
* @return {Object}
*/
function configure(pkg, env, target) {
const isDev = env === 'development'
const isProd = env === 'production'
const isUmd = target === 'umd'
const isModule = target === 'module'
const input = `packages/${pkg.name}/src/index.js`
const deps = []
.concat(pkg.dependencies ? Object.keys(pkg.dependencies) : [])
.concat(pkg.peerDependencies ? Object.keys(pkg.peerDependencies) : [])
const plugins = [
// Allow Rollup to resolve modules from `node_modules`, since it only
// resolves local modules by default.
resolve({
browser: true,
}),
// Allow Rollup to resolve CommonJS modules, since it only resolves ES2015
// modules by default.
isUmd &&
commonjs({
exclude: [`packages/${pkg.name}/src/**`],
// HACK: Sometimes the CommonJS plugin can't identify named exports, so
// we have to manually specify named exports here for them to work.
// https://github.com/rollup/rollup-plugin-commonjs#custom-named-exports
namedExports: {
esrever: ['reverse'],
immutable: [
'List',
'Map',
'Record',
'OrderedSet',
'Set',
'Stack',
'is',
],
'react-dom': ['findDOMNode'],
'react-dom/server': ['renderToStaticMarkup'],
},
}),
// Convert JSON imports to ES6 modules.
json(),
// Replace `process.env.NODE_ENV` with its value, which enables some modules
// like React and Slate to use their production variant.
replace({
'process.env.NODE_ENV': JSON.stringify(env),
}),
// Register Node.js builtins for browserify compatibility.
builtins(),
// Use Babel to transpile the result, limiting it to the source code.
babel({
include: [`packages/${pkg.name}/src/**`],
}),
// Register Node.js globals for browserify compatibility.
globals(),
// Only minify the output in production, since it is very slow. And only
// for UMD builds, since modules will be bundled by the consumer.
isUmd && isProd && uglify(),
].filter(Boolean)
if (isUmd) {
return {
plugins,
input,
output: {
format: 'umd',
file: `packages/${pkg.name}/${isProd ? pkg.umdMin : pkg.umd}`,
exports: 'named',
name: startCase(pkg.name).replace(/ /g, ''),
globals: pkg.umdGlobals,
},
external: Object.keys(pkg.umdGlobals || {}),
}
}
if (isModule) {
return {
plugins,
input,
output: [
{
file: `packages/${pkg.name}/${pkg.module}`,
format: 'es',
sourcemap: isDev,
},
{
file: `packages/${pkg.name}/${pkg.main}`,
format: 'cjs',
exports: 'named',
},
],
// We need to explicitly state which modules are external, meaning that
// they are present at runtime. In the case of non-UMD configs, this means
// all non-Slate packages.
external: id => {
return !!deps.find(dep => dep === id || id.startsWith(`${dep}/`))
},
}
}
}
/**
* Return a Rollup configuration for a `pkg`.
*
* @return {Array}
*/
function factory(pkg) {
const isProd = process.env.NODE_ENV === 'production'
return [
configure(pkg, 'development', 'module'),
isProd && configure(pkg, 'development', 'umd'),
isProd && configure(pkg, 'production', 'umd'),
].filter(Boolean)
}
/**
* Export.
*
* @type {Function}
*/
export default factory