forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.js
149 lines (133 loc) · 4.07 KB
/
factory.js
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
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 isProd = env === 'production'
const isUmd = target === 'umd'
const isModule = target === 'module'
const packageName = pkg.name.replace('@upsilon/', '')
const input = `packages/${packageName}/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/${packageName}/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/${packageName}/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/${packageName}/${isProd ? pkg.umdMin : pkg.umd}`,
exports: 'named',
name: startCase(packageName).replace(/ /g, ''),
globals: pkg.umdGlobals,
},
external: Object.keys(pkg.umdGlobals || {}),
}
}
if (isModule) {
return {
plugins,
input,
output: [
{
file: `packages/${packageName}/${pkg.module}`,
format: 'es',
sourcemap: true,
},
{
file: `packages/${packageName}/${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}/`))
},
}
}
}
/**
* 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