File size: 6,075 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
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 typescript from 'rollup-plugin-typescript2'
import { terser } from 'rollup-plugin-terser'
import { startCase } from 'lodash'
import Core from '../../packages/slate/package.json'
import History from '../../packages/slate-history/package.json'
import Hyperscript from '../../packages/slate-hyperscript/package.json'
import DOM from '../../packages/slate-dom/package.json'
import React from '../../packages/slate-react/package.json'
/**
* Return a Rollup configuration for a `pkg` with `env` and `target`.
*/
function configure(pkg, env, target) {
const isProd = env === 'production'
const isUmd = target === 'umd'
const isModule = target === 'module'
const isCommonJs = target === 'cjs'
const input = `packages/${pkg.name}/src/index.ts`
const deps = []
.concat(pkg.dependencies ? Object.keys(pkg.dependencies) : [])
.concat(pkg.peerDependencies ? Object.keys(pkg.peerDependencies) : [])
// Stop Rollup from warning about circular dependencies.
const onwarn = warning => {
if (warning.code !== 'CIRCULAR_DEPENDENCY') {
console.warn(`(!) ${warning.message}`) // eslint-disable-line no-console
}
}
const plugins = [
// Allow Rollup to resolve modules from `node_modules`, since it only
// resolves local modules by default.
resolve({
browser: true,
}),
typescript({
abortOnError: false,
tsconfig: `./packages/${pkg.name}/tsconfig.json`,
// COMPAT: Without this flag sometimes the declarations are not updated.
// clean: isProd ? true : false,
clean: true,
}),
// Allow Rollup to resolve CommonJS modules, since it only resolves ES2015
// modules by default.
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: {
'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({
runtimeHelpers: true,
include: [`packages/${pkg.name}/src/**`],
extensions: ['.js', '.ts', '.tsx'],
presets: [
'@babel/preset-typescript',
[
'@babel/preset-env',
isUmd
? { modules: false }
: {
exclude: [
'@babel/plugin-transform-regenerator',
'@babel/transform-async-to-generator',
],
modules: false,
targets: {
esmodules: isModule,
},
},
],
'@babel/preset-react',
],
plugins: [
[
'@babel/plugin-transform-runtime',
isUmd
? {}
: {
regenerator: false,
useESModules: isModule,
},
],
'@babel/plugin-proposal-class-properties',
],
}),
// 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 && terser(),
].filter(Boolean)
if (isUmd) {
return {
plugins,
input,
onwarn,
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 (isCommonJs) {
return {
plugins,
input,
onwarn,
output: [
{
file: `packages/${pkg.name}/${pkg.main}`,
format: 'cjs',
exports: 'named',
sourcemap: true,
},
],
// 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}/`))
},
}
}
if (isModule) {
return {
plugins,
input,
onwarn,
output: [
{
file: `packages/${pkg.name}/${pkg.module}`,
format: 'es',
sourcemap: true,
},
],
// 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`.
*/
function factory(pkg, options = {}) {
const isProd = process.env.NODE_ENV === 'production'
return [
configure(pkg, 'development', 'cjs', options),
configure(pkg, 'development', 'module', options),
isProd && configure(pkg, 'development', 'umd', options),
isProd && configure(pkg, 'production', 'umd', options),
].filter(Boolean)
}
/**
* Config.
*/
export default [
...factory(Core),
...factory(History),
...factory(Hyperscript),
...factory(DOM),
...factory(React),
]
|