-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
78 lines (72 loc) · 2.02 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
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import nodeResolve from '@rollup/plugin-node-resolve'
import shebang from 'rollup-plugin-add-shebang'
import babel from 'rollup-plugin-babel'
import pkg from './package.json'
const bundleTarget = process.env.BUILD_BUNDLE
const dependencies = [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
'core-js',
'fs',
'lib/snippets',
'path',
]
const isArrayLike = (x) => x != null && typeof x[Symbol.iterator] === 'function'
const isNil = (...xs) => xs.some((x) => x == null)
const isObjectLike = (x) => x != null && typeof x === 'object'
const isSameType = (x, y) => !isNil(x, y) && x.constructor === y.constructor
const sanitizeBundle = ({ type, ...rest }) => rest
const removeRelativePath = (dep) => dep.replace(/^(\.{1,2}\/)+/, '')
const external = (id) =>
dependencies
.map((dep) => removeRelativePath(id).startsWith(dep))
.some(Boolean)
const presetBundleDefaults = (defaults) => (opts) =>
Object.entries({ ...defaults, ...opts }).reduce(
(acc, [k, v]) => ({
...acc,
[k]:
isObjectLike(v) && isSameType(v, defaults[k])
? isArrayLike(v)
? [...defaults[k], ...v]
: { ...defaults[k], ...v }
: v,
}),
{}
)
const bundle = presetBundleDefaults({
input: 'src/index.js',
output: { exports: 'named', indent: false },
plugins: [
nodeResolve(),
commonjs(),
json(),
shebang({
include: `lib/${pkg.name}.js`,
}),
],
treeshake: true,
})
const bundles = [
bundle({
external,
output: { format: 'cjs', file: `lib/${pkg.name}.js` },
plugins: [babel({ runtimeHelpers: true })],
type: 'cjs',
}),
{
external,
input: 'src/parser.js',
output: { format: 'cjs', file: '.tmp/parser.js' },
plugins: [json()],
type: 'snippets',
},
]
export default (() =>
[
...(bundleTarget != null
? bundles.filter((x) => x.type === bundleTarget)
: bundles),
].map(sanitizeBundle))()