forked from jxom/fannypack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.js
106 lines (97 loc) · 2.54 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
const babel = require('rollup-plugin-babel');
const resolve = require('rollup-plugin-node-resolve');
const replace = require('rollup-plugin-replace');
const commonjs = require('rollup-plugin-commonjs');
const json = require('rollup-plugin-json');
const { uglify } = require('rollup-plugin-uglify');
const ignore = require('rollup-plugin-ignore');
const pkg = require('./package.json');
const getModuleEntries = require('./scripts/get-module-entries');
const extensions = ['.ts', '.tsx', '.js', '.jsx', '.json'];
const makeExternalPredicate = externalArr => {
if (!externalArr.length) {
return () => false;
}
const pattern = new RegExp(`^(${externalArr.join('|')})($|/)`);
return id => pattern.test(id);
};
const getExternal = (umd, pkg) => {
const external = [...Object.keys(pkg.peerDependencies), 'prop-types'];
const allExternal = [...external, ...Object.keys(pkg.dependencies)];
return makeExternalPredicate(umd ? external : allExternal);
};
const commonPlugins = [
babel({
extensions,
exclude: ['node_modules/**']
}),
resolve({ extensions, preferBuiltins: false })
];
const getPlugins = umd =>
umd
? [
...commonPlugins,
commonjs({
include: /node_modules/,
namedExports: {
'./node_modules/react-is/index.js': ['isValidElementType', 'isElement', 'ForwardRef']
}
}),
ignore(['stream']),
uglify(),
replace({
'process.env.NODE_ENV': JSON.stringify('production')
}),
json()
]
: commonPlugins;
const getOutput = (umd, pkg) =>
umd
? {
name: 'Fannypack',
file: pkg.unpkg,
format: 'umd',
exports: 'named',
globals: {
fannypack: 'Fannypack',
react: 'React',
'react-dom': 'ReactDOM',
'prop-types': 'PropTypes'
}
}
: [
{
file: pkg.main,
format: 'cjs',
exports: 'named'
},
{
file: pkg.module,
format: 'es'
}
];
const createConfig = ({ umd, pkg, plugins = [], ...config }) => ({
external: getExternal(umd, pkg),
plugins: [...getPlugins(umd), ...plugins],
output: getOutput(umd, pkg),
...config
});
export default [
createConfig({
pkg,
experimentalCodeSplitting: true,
input: getModuleEntries(),
output: [
{
format: 'es',
dir: 'es'
},
{
format: 'cjs',
dir: 'lib',
exports: 'named'
}
]
}),
createConfig({ pkg, input: 'src/index.ts', umd: true })
];