-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
rollup.config.js
175 lines (162 loc) · 3.8 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
164
165
166
167
168
169
170
171
172
173
174
175
import path from 'path'
import nodeResolve from 'rollup-plugin-node-resolve'
import replace from 'rollup-plugin-replace'
import commonjs from 'rollup-plugin-commonjs'
import sourceMaps from 'rollup-plugin-sourcemaps'
import useBabel from 'rollup-plugin-babel'
import ttypescript from 'ttypescript'
import tsPlugin from 'rollup-plugin-typescript2'
import { terser } from 'rollup-plugin-terser'
import packageJson from './package.json'
// Import as CJS since writing Babel config in ES
// makes it unreadable by other tools (i.e. Storybook).
const babelConfig = require('./babel.config')
const BUILD_DIR = '.'
const getPath = (filepath) => {
return path.resolve(BUILD_DIR, filepath)
}
const { NODE_ENV: nodeEnv, TARGET: target } = process.env
const PRODUCTION = nodeEnv === 'production'
const __PROD__ = PRODUCTION ? 'true' : '""'
const input = packageJson.esnext
const external = (moduleName) => {
return !moduleName.startsWith('.') && !path.isAbsolute(moduleName)
}
const babel = (overrides = {}) => {
return useBabel({
...babelConfig,
...overrides,
extensions: ['.ts', '.tsx'],
})
}
const typescript = () => {
return tsPlugin({
clean: true,
// Disable type checking during the build
// to increase the build speed. Types must be
// checked during the local development.
// They may also be checked during type definition
// emitting (ttsc)
check: false,
tsconfigOverride: {
compilerOptions: {
declaration: false,
emitDeclarationOnly: false,
},
},
// Provide custom TypeScript instance so it can
// resolve path aliases (i.e. "@utils/").
typescript: ttypescript,
})
}
const resolve = (override = {}) => {
return nodeResolve({
mainFields: ['main', 'esnext'],
extensions: ['.ts', '.tsx'],
...override,
})
}
const warnOnMissingDependency = (message) => {
if (
message.code === 'UNRESOLVED_IMPORT' &&
message.source === '@atomic-layout/core'
) {
throw new Error(
`Could not resolve "@atomic-layout/core" module. Make sure it's installed.`,
)
}
}
// CommonJS module
const buildCjs = () => ({
input,
external,
output: {
file: getPath(packageJson.main),
format: 'cjs',
exports: 'named',
sourcemap: PRODUCTION,
},
plugins: [
resolve(),
typescript(),
replace({
__PROD__,
'process.env.NODE_ENV': JSON.stringify(nodeEnv),
}),
PRODUCTION && sourceMaps(),
PRODUCTION &&
terser({
ecma: 5,
sourcemap: true,
output: {
comments: false,
},
warnings: true,
toplevel: true,
}),
],
})
// UMD module
const buildUmd = () => ({
input,
external: ['react', 'styled-components'],
output: {
name: 'AtomicLayout',
format: 'umd',
exports: 'named',
file: getPath(packageJson['umd:main']),
globals: {
react: 'React',
'styled-components': 'styled',
},
},
plugins: [
resolve(),
typescript(),
replace({
__PROD__,
'process.env.NODE_ENV': JSON.stringify(nodeEnv),
}),
babel(),
commonjs(),
PRODUCTION && sourceMaps(),
PRODUCTION &&
terser({
sourcemap: true,
output: {
comments: false,
},
warnings: true,
ecma: 5,
toplevel: false,
}),
],
onwarn: warnOnMissingDependency,
})
// ECMAScript module
const buildEsm = () => ({
input,
external,
output: {
file: getPath(packageJson.module),
format: 'esm',
sourcemap: PRODUCTION,
},
plugins: [
resolve({
mainFields: ['esnext'],
}),
replace({
__PROD__,
}),
typescript(),
babel(),
PRODUCTION && sourceMaps(),
],
})
const buildTargets = {
cjs: buildCjs(),
umd: buildUmd(),
esm: buildEsm(),
}
export default target ? buildTargets[target] : Object.values(buildTargets)