-
Notifications
You must be signed in to change notification settings - Fork 201
/
rollup.config.js
188 lines (179 loc) · 4.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
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
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
import pkg from './package.json' with { type: 'json' }
import nodeResolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import alias from '@rollup/plugin-alias'
import terser from '@rollup/plugin-terser'
import replace from '@rollup/plugin-replace'
import { optimizeLodashImports } from '@optimize-lodash/rollup-plugin'
import { visualizer } from 'rollup-plugin-visualizer'
import { babel } from '@rollup/plugin-babel'
const __dirname = dirname(fileURLToPath(import.meta.url))
const baseConfig = {
input: 'dist/esm-raw/index.js',
output: {
file: 'dist/contentful.cjs',
format: 'cjs',
},
plugins: [
optimizeLodashImports(),
replace({
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
__VERSION__: JSON.stringify(pkg.version),
}),
commonjs({
sourceMap: false,
transformMixedEsModules: true,
ignoreGlobal: true,
ignoreDynamicRequires: true,
requireReturnsDefault: 'auto',
}),
json(),
],
}
const esmConfig = {
input: 'dist/esm-raw/index.js',
output: {
dir: 'dist/esm', // Output directory
format: 'esm', // Output module format
preserveModules: true, // Preserve module structure
},
plugins: [
replace({
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
__VERSION__: JSON.stringify(pkg.version),
}),
],
}
const cjsConfig = {
...baseConfig,
output: {
...baseConfig.output,
},
plugins: [
...baseConfig.plugins,
nodeResolve({
preferBuiltins: true,
browser: false,
}),
babel({
babelHelpers: 'bundled',
// exclude: 'node_modules/**',
presets: [
[
'@babel/preset-env',
// Please note: This is set to Node.js v8 in order to satisfy ECMA2017 requirements
// However, we cannot ensure it will operate without issues on Node.js v8
{ targets: { node: 8 }, modules: false, bugfixes: true },
],
],
}),
alias({
entries: [
{
find: 'axios',
replacement: resolve(__dirname, './node_modules/axios/dist/node/axios.cjs'),
},
],
}),
],
}
const browserConfig = {
...baseConfig,
output: {
file: 'dist/contentful.browser.js',
format: 'iife',
name: 'contentful',
},
plugins: [
nodeResolve({
preferBuiltins: false,
browser: true,
}),
alias({
entries: [
{
find: 'axios',
replacement: resolve(__dirname, './node_modules/axios/dist/browser/axios.cjs'),
},
{
find: 'process',
replacement: resolve(__dirname, 'node_modules', 'process/browser'),
},
],
}),
...baseConfig.plugins,
babel({
babelHelpers: 'runtime',
presets: [
[
'@babel/preset-env',
{
targets: pkg.browserslist,
modules: false,
bugfixes: true,
},
],
],
plugins: [
[
'@babel/plugin-transform-runtime',
{
regenerator: true,
},
],
],
}),
],
}
const browserMinConfig = {
...browserConfig,
output: {
...browserConfig.output,
file: 'dist/contentful.browser.min.js',
},
plugins: [
...browserConfig.plugins,
terser({
// sourceMap: {
// content: 'inline',
// includeSources: true,
// url: 'inline'
// },
compress: {
passes: 5,
ecma: 2018,
drop_console: true,
drop_debugger: true,
sequences: true,
booleans: true,
loops: true,
unused: true,
evaluate: true,
if_return: true,
join_vars: true,
collapse_vars: true,
reduce_vars: true,
pure_getters: true,
pure_new: true,
keep_classnames: false,
keep_fnames: false,
keep_fargs: false,
keep_infinity: false,
},
format: {
comments: false, // Remove all comments
beautify: false, // Minify output
},
}),
visualizer({
emitFile: true,
filename: 'stats-browser-min.html',
}),
],
}
export default [esmConfig, cjsConfig, browserConfig, browserMinConfig]