/
rollup.config.base.js
71 lines (67 loc) · 2.18 KB
/
rollup.config.base.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
import path from 'path';
import process from 'process';
import fs from 'fs';
import jsx from 'acorn-jsx';
import { terser } from 'rollup-plugin-terser';
import tsPlugin from '@rollup/plugin-typescript';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
const cwd = process.cwd();
const resolvePath = _path => path.resolve(cwd, _path);
const camelCase = str => str.replace(/-([a-z])/g, g => g[1].toUpperCase());
const defineRollupConfig = (packageInfo, { external = [] } = {}) => {
const externalGlobals = {};
external.forEach(name => {
externalGlobals[name] = camelCase(name);
});
const config = {
input: fs.existsSync(resolvePath('./src/index.ts'))
? resolvePath('./src/index.ts')
: fs.existsSync(resolvePath('./src/index.tsx'))
? resolvePath('./src/index.tsx')
: resolvePath('./src/index.js'),
output: [
{
file: 'cjs/index.cjs',
format: 'cjs',
exports: 'auto',
sourcemap: true
},
{
file: 'esm/index.js',
format: 'es',
sourcemap: true
},
{
file: 'umd/index.js',
format: 'umd',
name: camelCase(packageInfo.name),
globals: {
react: 'React',
...externalGlobals
}
},
{
file: 'umd/index.min.js',
format: 'umd',
name: camelCase(packageInfo.name),
globals: {
react: 'React',
...externalGlobals
},
sourcemap: true,
plugins: [terser()]
}
],
plugins: [tsPlugin(), nodeResolve(), commonjs(), json()],
external: ['react', ...external],
acornInjectPlugins: [jsx()]
};
config.output = config.output.map(output => ({
...output,
file: resolvePath(output.file)
}));
return config;
};
export default defineRollupConfig;