-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
157 lines (149 loc) · 3.54 KB
/
rollup.config.mjs
File metadata and controls
157 lines (149 loc) · 3.54 KB
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
import { resolve, dirname } from 'path'
import { fileURLToPath } from 'url'
import alias from '@rollup/plugin-alias'
import pkg from './package.json' with { type: 'json' }
import nodeResolve from '@rollup/plugin-node-resolve'
import json from '@rollup/plugin-json'
import replace from '@rollup/plugin-replace'
import typescript from '@rollup/plugin-typescript'
import sourcemaps from 'rollup-plugin-sourcemaps2'
import commonjs from '@rollup/plugin-commonjs'
import nodePolyfills from 'rollup-plugin-polyfill-node'
import terser from '@rollup/plugin-terser'
const __dirname = dirname(fileURLToPath(import.meta.url))
const baseConfig = {
input: 'lib/index.ts',
plugins: [
nodeResolve(),
typescript({
tsconfig: './tsconfig.json',
declaration: false,
noEmitOnError: true,
}),
sourcemaps(),
replace({
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
__VERSION__: JSON.stringify(pkg.version),
}),
json(),
],
external: [/node_modules\/(?!tslib.*)/],
}
const esmConfig = {
...baseConfig,
output: {
dir: 'dist/esm',
format: 'esm',
preserveModules: true,
sourcemap: true,
entryFileNames: '[name].mjs',
chunkFileNames: '[name].mjs',
},
}
const cjsBundleConfig = {
...baseConfig,
output: {
file: 'dist/cjs/index.cjs',
format: 'cjs',
sourcemap: true,
interop: 'auto',
},
}
const typesConfig = {
...baseConfig,
output: {
dir: 'dist/types',
format: 'esm',
preserveModules: true,
sourcemap: true,
},
plugins: [
...baseConfig.plugins,
typescript({
tsconfig: './tsconfig.json',
outDir: 'dist/types',
declaration: true,
noEmitOnError: true,
emitDeclarationOnly: true,
}),
],
}
// Browser bundle for direct script inclusion
const scriptConfig = {
...baseConfig,
output: {
file: 'dist/browser/index.js',
format: 'iife',
name: 'contentfulManagement',
sourcemap: true,
},
plugins: [
...baseConfig.plugins,
nodeResolve({
browser: true,
preferBuiltins: false,
}),
alias({
entries: [
{
find: 'axios',
replacement: resolve(__dirname, './node_modules/axios/dist/browser/axios.cjs'),
},
{
find: 'process',
replacement: resolve(__dirname, 'node_modules', 'process/browser'),
},
],
}),
commonjs({
sourceMap: true,
transformMixedEsModules: true,
ignoreGlobal: true,
ignoreDynamicRequires: true,
requireReturnsDefault: 'auto',
}),
nodePolyfills({
include: ['util'],
}),
],
external: [], // Purposefully bundle all dependencies for browser usage
}
const scriptMinConfig = {
...scriptConfig,
output: {
...scriptConfig.output,
file: 'dist/browser/index.min.js',
},
plugins: [
...scriptConfig.plugins,
terser({
compress: {
passes: 5,
ecma: 2023,
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,
beautify: false,
},
}),
],
}
export default [esmConfig, cjsBundleConfig, typesConfig, scriptConfig, scriptMinConfig]