-
Notifications
You must be signed in to change notification settings - Fork 11
/
rollup.config.mjs
89 lines (83 loc) · 2.66 KB
/
rollup.config.mjs
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
import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
const input = './src/index.ts';
// please update the package.json to reflect any changes here
const bundles = createBundles({
// the `unpkg` UMD bundle (for usage without a bundler)
unpkg: {
format: 'umd',
targets: 'defaults',
output: { name: 'color2k' },
},
// legacy node bundle (note: this is currently used in jest-resolve)
main: {
format: 'cjs',
targets: 'node 4',
},
// the `module` bundle that should support older bundlers that used this entry
// point. (e.g. webpack 2)
module: {
format: 'es',
// `node 6` is a good target because it's closest to es2015 specified in the
// rollup spec: https://github.com/rollup/rollup/wiki/pkg.module
targets: 'node 6',
},
exports: {
// `node 12` is a good target because:
//
// this module entry point is used by several bundlers.
// namely: webpack, @rollup/plugin-node-resolve, wmr (via rollup)
//
// the lowest common denominator is **acorn 7** with ecmaVersion `2019`
// - @rollup/plugin-node-resolve supports as low as rollup 1.2
// - rollup 1.2 uses acorn 7 with ecmaVersion `2019` https://github.com/rollup/rollup/blob/v1.2.0/src/Module.ts#L105
// - webpack uses acorn 8 with ecmaVersion latest https://github.com/webpack/webpack/blob/v5.0.0/lib/javascript/JavascriptParser.js#L128
// - node 12 is the first version to completely support ES2019 https://node.green/#ES2019
//
// this module entry point is used by esbuild too.
// https://esbuild.github.io/api/#how-conditions-work
import: {
format: 'es',
targets: 'node 12',
extension: 'mjs',
},
require: {
format: 'cjs',
targets: 'node 12',
},
},
});
function createBundles(config, parents = []) {
if ('targets' in config) {
const { format, extension = 'js', output, targets } = config;
return [
{
input,
output: {
file: `./dist/index.${parents.join('.')}.${format}.${extension}`,
format,
sourcemap: true,
...output,
},
plugins: [
resolve({ extensions: ['.ts'], modulesOnly: true }),
babel({
babelrc: false,
configFile: false,
babelHelpers: 'bundled',
extensions: ['.ts'],
targets,
presets: [
['@babel/preset-env', { targets }],
'@babel/preset-typescript',
],
}),
],
},
];
}
return Object.entries(config).flatMap(([key, subConfig]) =>
createBundles(subConfig, [...parents, key])
);
}
export default bundles;