-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
190 lines (175 loc) · 5.07 KB
/
rollup.config.mjs
File metadata and controls
190 lines (175 loc) · 5.07 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
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
189
190
import typescript from '@rollup/plugin-typescript'
import resolve from '@rollup/plugin-node-resolve'
import { babel } from '@rollup/plugin-babel'
import commonjs from '@rollup/plugin-commonjs'
import styles from 'rollup-plugin-styles'
import terser from '@rollup/plugin-terser'
import { exec } from 'child_process'
import { visualizer } from 'rollup-plugin-visualizer'
const isWatchMode = process.env.ROLLUP_WATCH === 'true'
const onSuccessCallback = process.env.ON_SUCCESS
const external = [
/@babel\/runtime/,
'react',
'react-dom',
'react-compiler-runtime',
'classnames',
'prop-types',
'@ariakit/react',
'aria-hidden',
'dayjs',
'dayjs/plugin/localizedFormat',
'react-focus-lock',
'react-keyed-flatten-children',
'use-callback-ref',
'tslib',
]
const basePlugins = [
resolve({
extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
}),
commonjs(),
babel({
babelHelpers: 'runtime',
extensions: ['.js', '.jsx', '.ts', '.tsx'],
exclude: 'node_modules/**',
}),
]
const baseStylesConfig = {
autoModules: /\.module\.css$/,
modules: {
mode: 'local',
generateScopedName: '[hash:8]',
},
mode: 'extract',
url: { inline: true },
}
const baseTypescriptConfig = {
tsconfig: './tsconfig.dist.json',
}
// Plugin to run a command after successful build
function onSuccess(command) {
return {
name: 'on-success',
closeBundle() {
if (command) {
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${error}`)
return
}
if (stdout) console.log(stdout)
if (stderr) console.error(stderr)
})
}
},
}
}
// Build configurations for es/, lib/, and dist/ folders
function createConfig({
format,
outputDir,
outputFile,
preserveModules,
withDeclarations,
withMinification,
withVisualizer,
onSuccessCommand,
}) {
return {
input: 'src/index.ts',
output: {
...(outputFile ? { file: outputFile } : { dir: outputDir }),
format,
sourcemap: true,
...(preserveModules && {
preserveModules: true,
preserveModulesRoot: 'src',
assetFileNames: '[name][extname]',
}),
...(format === 'cjs' && { exports: 'named' }),
},
external,
plugins: [
...basePlugins,
styles({
...baseStylesConfig,
...(withMinification && { minimize: true }),
}),
...(withDeclarations
? [
typescript({
...baseTypescriptConfig,
noForceEmit: true,
compilerOptions: {
outDir: outputDir || 'dist',
declaration: true,
declarationMap: false,
emitDeclarationOnly: true,
},
}),
]
: []),
...(withMinification ? [terser()] : []),
...(withVisualizer ? [visualizer({ gzipSize: true })] : []),
...(onSuccessCommand ? [onSuccess(onSuccessCommand)] : []),
],
}
}
// ESM unbundled build (es/ folder)
const es = createConfig({
format: 'esm',
outputDir: 'es',
preserveModules: true,
withVisualizer: true,
})
// CJS unbundled build with TypeScript declarations (lib/ folder)
const lib = createConfig({
format: 'cjs',
outputDir: 'lib',
preserveModules: true,
withDeclarations: true,
...(isWatchMode && onSuccessCallback && { onSuccessCommand: onSuccessCallback }),
})
// Generate dist/index.js proxy file
const distIndex = {
input: 'src/index.ts',
external: () => true,
output: {
file: 'dist/index.js',
format: 'cjs',
},
plugins: [
{
name: 'write-dist-index',
generateBundle(_, bundle) {
for (const fileName in bundle) {
delete bundle[fileName]
}
this.emitFile({
type: 'asset',
fileName: 'index.js',
source: `'use strict'
if (process.env.NODE_ENV === 'production') {
module.exports = require('./reactist.cjs.production.min.js')
} else {
module.exports = require('./reactist.cjs.development.js')
}
`,
})
},
},
],
}
// Bundled CJS development build (dist/ folder)
const distDev = createConfig({
format: 'cjs',
outputFile: 'dist/reactist.cjs.development.js',
})
// Bundled CJS production build - minified (dist/ folder)
const distProd = createConfig({
format: 'cjs',
outputFile: 'dist/reactist.cjs.production.min.js',
withMinification: true,
})
export default isWatchMode ? [es, lib, distIndex] : [es, lib, distDev, distProd]