This repository has been archived by the owner on Jun 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
151 lines (136 loc) · 3.63 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
import babel from 'rollup-plugin-babel'
import commonjs from 'rollup-plugin-commonjs'
import nodeResolve from 'rollup-plugin-node-resolve'
import replace from 'rollup-plugin-replace'
import { sizeSnapshot } from 'rollup-plugin-size-snapshot'
import sourcemaps from 'rollup-plugin-sourcemaps'
import { terser } from 'rollup-plugin-terser'
import json from '@rollup/plugin-json'
import pkg from './package.json'
const inputs = ['./compiled/config.js']
const knownDependencyNames = {
'react-dom': 'ReactDOM',
'react': 'React'
}
const kebabToPascal = (kebab) => {
const pascal = kebab
.split('-')
.map((str) => {
if (str.length > 0) {
return str[0].toUpperCase() + str.slice(1)
} else {
return ''
}
})
.join('')
console.warn(
`Guessing the window.[name] for package "${kebab}" is "${pascal}"\nIf not, add\n '${kebab}': '[name]',\nto knownDependencyNames in rollup.config.js`
)
return pascal
}
const getGlobals = (bundleType) =>
['UMD_DEV', 'UMD_PROD'].includes(bundleType)
? Object.keys(pkg.peerDependencies || {}).reduce(
(dependencyNameMap, npmDependency) => ({
...dependencyNameMap,
[npmDependency]:
knownDependencyNames[npmDependency] || kebabToPascal(npmDependency)
}),
{}
)
: {}
const getExternal = (bundleType) => {
const peerDependencies = Object.keys(pkg.peerDependencies || {})
const dependencies = Object.keys(pkg.dependencies)
const makeExternalPredicate = (externals) => {
if (externals.length === 0) {
return () => false
} else {
const pattern = new RegExp(`^(${externals.join('|')})($|/)`)
return (id) => pattern.test(id)
}
}
switch (bundleType) {
case 'CJS':
case 'ES':
return makeExternalPredicate([...peerDependencies, ...dependencies])
default:
return makeExternalPredicate(peerDependencies)
}
}
const isProduction = (bundleType) => bundleType.endsWith('_PROD')
const getPlugins = (bundleType) => [
nodeResolve(),
commonjs({
include: 'node_modules/**'
}),
babel({
babelrc: false,
exclude: 'node_modules/**',
presets: [['@babel/env', { loose: true, modules: false }], '@babel/react'],
plugins: ['@babel/transform-runtime'],
runtimeHelpers: true
}),
json(),
replace({
'process.env.NODE_ENV': isProduction(bundleType)
? '"production"'
: '"development"'
}),
sourcemaps(),
sizeSnapshot(),
isProduction(bundleType) &&
terser({
output: { comments: false },
compress: {
keep_infinity: true,
pure_getters: true
},
warnings: true,
ecma: 5,
toplevel: false
})
]
const getCjsConfig = (input, bundleType) => ({
input,
external: getExternal(bundleType),
output: {
file: `dist/ainsley-config-starter.cjs.js`,
format: 'cjs',
sourcemap: true
},
plugins: getPlugins(bundleType)
})
const getEsConfig = (input) => ({
input,
external: getExternal('ES'),
output: {
file: pkg.module,
format: 'es',
sourcemap: true
},
plugins: getPlugins('ES')
})
const getUmdConfig = (input, bundleType) => ({
input,
external: getExternal(bundleType),
output: {
file: `dist/ainsley-config-starter.umd.${
isProduction(bundleType) ? 'production' : 'development'
}.js`,
format: 'umd',
globals: getGlobals(bundleType),
name: 'AinsleyConfigStarter',
sourcemap: true
},
plugins: getPlugins(bundleType)
})
export default inputs
.map((input) => [
getCjsConfig(input, 'CJS'),
getEsConfig(input),
...(pkg.browser
? [getUmdConfig(input, 'UMD_DEV'), getUmdConfig(input, 'UMD_PROD')]
: [])
])
.flat()