-
Notifications
You must be signed in to change notification settings - Fork 8
/
rollup-config.js
61 lines (55 loc) · 1.58 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
import typescript from 'rollup-plugin-typescript2';
import json from '@rollup/plugin-json';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import image from '@rollup/plugin-image';
const defaultExternal = id =>
!id.startsWith('\0') &&
!id.startsWith('~') &&
!id.startsWith('.') &&
!id.startsWith(process.platform === 'win32' ? process.cwd() : '/');
const createOutput = (dir = 'dist', defaultOpts) => {
const { external, output, plugins = [], filename } = defaultOpts;
const defaultPlugins = [
resolve({
mainFields: ['module', 'main'],
browser: true,
preferBuiltins: false,
}),
commonjs({
include: /\/node_modules\//,
}),
json(),
image(),
typescript({
typescript: require('typescript'),
rollupCommonJSResolveHack: true,
}),
];
const outputs = [
{
dir,
format: 'cjs',
chunkFileNames: filename ? `${filename}.js` : `[name].js`,
entryFileNames: filename ? `${filename}.js` : `[name].js`,
...output,
},
{
dir,
format: 'esm',
chunkFileNames: filename ? `${filename}.esm.js` : `[name].esm.js`,
entryFileNames: filename ? `${filename}.esm.js` : `[name].esm.js`,
...output,
},
];
return {
...defaultOpts,
external: external || defaultExternal,
plugins: defaultPlugins.filter(Boolean).concat(plugins),
output: outputs,
};
};
export function config(opts) {
const inputs = Array.isArray(opts) ? opts : [opts];
return inputs.map(({ dest: dir, ...opts }) => createOutput(dir, opts));
}