-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.base.config.js
75 lines (73 loc) · 2.17 KB
/
rollup.base.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
import replace from '@rollup/plugin-replace';
import resolve from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
import analyzer from 'rollup-plugin-analyzer';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';
export default (
{
cwd,
pkg,
entry = './src/index.ts',
external = [],
libraryName,
minify = false,
extensions = ['js', 'jsx', 'mjs', 'ts', 'tsx'],
},
sourcemap = true,
) => {
const rollupESMConfig = {
input: `${cwd}/${entry}`,
output: [
{
file: pkg.module,
format: 'esm',
exports: 'named',
sourcemap: sourcemap,
},
],
external,
plugins: [
typescript({ tsconfig: `${cwd}/tsconfig.json` }),
// Some libraries (such as React) needs to be in production mode, or you risk have unusable code
replace({
'process.env.NODE_ENV': JSON.stringify('production'),
}),
// Tells the compiler the preference of modules when importing a library.
resolve({
mainFields: ['module', 'main'], // Since the target of this is ESM, we will prioritise modules, and then main
extensions: extensions, // List of extensions of files to be included
}),
minify && terser({ module: true, output: { comments: false } }), // If minify is true, the code will be minified
analyzer({ summaryOnly: true }), // Useful plugin to check what is using most of the space in the bundle
],
};
const rollupUMDConfig = {
input: `${cwd}/${entry}`,
output: [
{
file: pkg.main,
format: 'umd',
name: libraryName,
exports: 'named',
sourcemap: sourcemap,
},
],
external,
plugins: [
typescript({ lib: ['es5', 'es6', 'dom'], target: 'es5' }),
replace({
'process.env.NODE_ENV': JSON.stringify('production'),
}),
resolve({
mainFields: ['main'],
modulesOnly: true,
extensions: extensions,
}),
commonjs(),
minify && terser({ output: { comments: false } }),
analyzer({ summaryOnly: true }),
],
};
return [rollupESMConfig, rollupUMDConfig];
};