-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrollup.config.js
100 lines (92 loc) · 2.9 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
import typescript from 'rollup-plugin-typescript2'
import json from 'rollup-plugin-json'
import yaml from 'rollup-plugin-yaml'
import {terser} from 'rollup-plugin-terser'
import autoExternal from 'rollup-plugin-auto-external'
import path from 'path'
import fs from 'fs'
import builtins from 'rollup-plugin-node-builtins'
import commonjs from 'rollup-plugin-commonjs'
import globals from 'rollup-plugin-node-globals'
import multiEntry from 'rollup-plugin-multi-entry'
import resolve from 'rollup-plugin-node-resolve'
const {LERNA_PACKAGE_NAME, NODE_ENV} = process.env
const PACKAGE_ROOT_PATH = process.cwd()
const OUTPUT_DIR = path.join(PACKAGE_ROOT_PATH, 'dist')
const PKG_JSON = JSON.parse(
fs.readFileSync(path.join(PACKAGE_ROOT_PATH, 'package.json')),
)
const IS_PROD = NODE_ENV === 'production'
// const packagesPath = `${LERNA_ROOT_PATH}/packages`
// const content = fs.readdirSync(packagesPath)
// const ALL_PACKAGES = content.map(p => JSON.parse(fs.readFileSync(`${packagesPath}/${p}/package.json`, 'utf8')).name)
const extensions = ['.js', '.jsx', '.ts', '.tsx']
const nodeBundles = ['', ['esm', 'cjs']] // node
const browserBundles = ['.browser', ['esm']] // browser
const nativeBundles = ['.mobile', ['esm']] // react-native
const entries = [nodeBundles]
if (!!PKG_JSON['browser']) {
entries.push(browserBundles)
}
if (!!PKG_JSON['react-native']) {
entries.push(nativeBundles)
}
const isBrowser = bundle => bundle === '.mobile'
export default entries
.filter(([bundle]) =>
fs.existsSync(path.join(PACKAGE_ROOT_PATH, `src/index${bundle}.ts`)),
)
.reduce(
(prev, [bundle, formats]) => [
...prev,
...formats.map(format => ({
plugins: [
yaml(),
json(),
typescript({
rollupCommonJSResolveHack: true,
clean: true,
tsconfigOverride: {
compilerOptions: {},
},
}),
builtins({
fs: true,
crypto: true,
}),
commonjs({
extensions,
}),
globals(),
multiEntry(),
resolve({
browser: true,
preferBuiltins: false,
mainFields: [
...(isBrowser(bundle) ? ['browser'] : []),
...['module', 'main'],
],
extensions,
}),
autoExternal({
builtins: !isBrowser(bundle),
dependencies: true,
peerDependencies: true,
packagePath: PACKAGE_ROOT_PATH,
}),
...(IS_PROD ? [terser()] : []),
],
input: path.join(PACKAGE_ROOT_PATH, `src/index${bundle}.ts`),
output: {
file: path.join(OUTPUT_DIR, `index${bundle}.${format}.js`),
name: LERNA_PACKAGE_NAME,
format,
sourcemap: IS_PROD ? false : 'inline',
amd: {
id: LERNA_PACKAGE_NAME,
},
},
})),
],
[],
)