-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
rollup.config.js
114 lines (109 loc) · 2.83 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
import path from 'path';
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import json from 'rollup-plugin-json';
import replace from 'rollup-plugin-replace';
import nodeResolve from 'rollup-plugin-node-resolve';
import sourceMaps from 'rollup-plugin-sourcemaps';
import { sizeSnapshot } from 'rollup-plugin-size-snapshot';
import { terser } from 'rollup-plugin-terser';
import pkg from './package.json';
/**
* based on https://github.com/palmerhq/tsdx/blob/master/src/createRollupConfig.ts
*/
const babelOptions = (format /* : 'cjs' | 'es' | 'umd' */) => ({
exclude: /node_modules/,
extensions: ['.ts', '.tsx', '.js', '.jsx', '.es', '.mjs', '.json'],
presets: [
[
'@babel/preset-env',
{
useBuiltIns: 'entry',
corejs: '3',
loose: true,
},
],
],
plugins: [
require.resolve('babel-plugin-annotate-pure-calls'),
require.resolve('babel-plugin-dev-expression'),
],
});
function createConfig(
format, //: 'cjs' | 'umd' | 'es',
env, //: 'development' | 'production',
opts //: { input: string; name: string; target: 'node' | 'browser' }
) {
return {
input: opts.input,
external: id => {
// external modules except local and absolute
return (
!(id.startsWith('./') || id.startsWith('../')) && !path.isAbsolute(id)
);
},
output: {
file: `dist/${pkg.name}.${format}.${env}.js`,
format,
freeze: false,
esModule: false,
treeshake: {
propertyReadSideEffects: false,
},
name: opts.name,
sourcemap: true,
// exports: 'named',
},
plugins: [
nodeResolve({
mainFields: ['module', 'jsnext', 'main'],
browser: format !== 'cjs',
}),
format === 'umd' &&
commonjs({
include: /\/node_modules\//,
}),
json(),
babel(babelOptions(format)),
replace({
'process.env.NODE_ENV': JSON.stringify(env),
}),
sourceMaps(),
sizeSnapshot({
printInfo: false,
}),
env === 'production' &&
terser({
sourcemap: true,
output: { comments: false },
compress: {
keep_infinity: true,
pure_getters: true,
// collapse_vars: false,
},
ecma: 5,
toplevel: format === 'es' || format === 'cjs',
warnings: true,
}),
],
};
}
export default [
createConfig('cjs', 'development', {
input: './out/index.js',
}),
createConfig('cjs', 'production', {
input: './out/index.js',
}),
createConfig('es', 'production', {
input: './out/index.js',
}),
createConfig('umd', 'development', {
input: './out/index.js',
name: 'TypesafeActions',
}),
createConfig('umd', 'production', {
input: './out/index.js',
name: 'TypesafeActions',
}),
];