-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
163 lines (148 loc) · 3.35 KB
/
rollup.config.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { readdirSync, statSync } from 'fs'
import babel from 'rollup-plugin-babel'
import commonjs from 'rollup-plugin-commonjs'
import del from 'rollup-plugin-delete'
import resolve from 'rollup-plugin-node-resolve'
import replace from 'rollup-plugin-replace'
import { terser } from 'rollup-plugin-terser'
import pkg from './package.json'
// Sets some constants
const env = process.env.NODE_ENV
const srcDir = 'src'
const EXTENSIONS = ['.js', '.json']
const CODES = [
'THIS_IS_UNDEFINED',
'MISSING_GLOBAL_NAME',
'CIRCULAR_DEPENDENCY',
]
const excludePaths = [
'.stories.js', // Storybook stories
]
function shouldIncludePath(path) {
for (const excludePath of excludePaths) {
if (path.includes(excludePath)) return false
}
return true
}
/**
* Returns files and subfolder as an array from a starting folder
*
* @param {String} dir Starting directory/folder
*/
const walkFolder = dir => {
var results = []
var list = readdirSync(dir)
list.forEach(function(file) {
file = `${dir}/${file}`
var stat = statSync(file)
if (stat && stat.isDirectory()) {
// Recurse into a subdirectory
results = results.concat(walkFolder(file))
} else {
// Is a file
results.push(file)
}
})
return results
}
/**
* Returns an object with chunks information (name and source)
*
* Output example:
* { 'atoms/buttons/Button': 'src/atoms/buttons/Button.js', ... }
*
* @param {String} URI Starting directory/folder
*/
const getChunks = URI =>
walkFolder(URI)
.filter(path => path.includes('.js') && shouldIncludePath(path))
.reduce(
(acc, current) => ({
...acc,
[current.replace('.js', '').replace(`${URI}/`, '')]: `${current}`,
}),
{}
)
/**
* Discards some warnings (defined in CODES)
*
* @param {Object} warning
*/
const discardWarning = warning => {
if (CODES.includes(warning.code)) {
return
}
console.error(warning) // eslint-disable-line no-console
}
// Setup common plugins
const commonPlugins = () => [
babel({
babelrc: false,
presets: [['@babel/preset-env', { modules: false }], '@babel/preset-react'],
extensions: EXTENSIONS,
exclude: 'node_modules/**',
}),
commonjs({
include: /node_modules/,
}),
replace({ 'process.env.NODE_ENV': JSON.stringify(env) }),
resolve({
extensions: EXTENSIONS,
preferBuiltins: false,
customResolveOptions: {
moduleDirectory: 'node_modules',
},
}),
]
// Actual Rollup configuration
export default [
// UMD
{
onwarn: discardWarning,
input: 'src/index.js',
output: {
esModule: false,
file: pkg.unpkg,
format: 'umd',
name: pkg.name,
exports: 'named',
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
},
plugins: [
del({
targets: 'umd',
}),
...commonPlugins(),
env === 'production' && terser(),
],
external: ['react', 'react-dom'],
},
// CJS and ESM
{
onwarn: discardWarning,
input: getChunks(srcDir),
output: [
{
dir: 'esm',
format: 'esm',
sourcemap: true,
},
{
dir: 'cjs',
format: 'cjs',
exports: 'named',
sourcemap: true,
},
],
plugins: [
del({
targets: ['cjs', 'esm'],
}),
...commonPlugins(),
],
external: ['react', 'react-dom'],
},
]