-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.js
192 lines (171 loc) · 3.67 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { dirname } from 'path'
import commonjs from 'rollup-plugin-commonjs'
import resolve from 'rollup-plugin-node-resolve'
import { terser } from 'rollup-plugin-terser'
import pkg from './package.json'
// `npm run build` -> `production` is true
// `npm run dev` -> `production` is false
const production = ! process.env.ROLLUP_WATCH
let name = pkg.name
if (name.slice(0, 1) === '@') {
name = name.split('/')[1]
if (! name) {
throw new TypeError('pkg.name invalid')
}
}
name = parseName(name)
const targetDir = dirname(pkg.main)
const deps = pkg.dependencies
const peerDeps = pkg.peerDependencies
const banner = `
/**
* ${pkg.name}
* ${pkg.description}
*
* @version ${pkg.version}
* @author ${pkg.author}
* @license ${pkg.license}
* @link ${pkg.homepage}
*/
`.trimLeft()
const uglifyOpts = {
mangle: true,
compress: {
unused: false,
sequences: true,
dead_code: true,
conditionals: true,
booleans: true,
if_return: true,
join_vars: true,
drop_console: false,
drop_debugger: false,
typeofs: false,
},
output: {
preamble: banner,
},
}
const globals = {
'rxjs/operators': 'rxjs.operators',
'rxjs/websocket': 'rxjs.websocket',
}
const external = [
'rxjs', 'rxjs/operators', 'rxjs/websocket', 'rxjs/ajax',
]
const nodeModule = [
'fs', 'path', 'util', 'os',
]
if (deps && Object.keys(deps).length) {
for (const depName of Object.keys(deps)) {
external.push(depName)
}
}
if (peerDeps && Object.keys(peerDeps).length) {
for (const depName of Object.keys(peerDeps)) {
external.push(depName)
}
}
const config = [
// CommonJS (for Node) and ES module (for bundlers) build.
{
external: external.concat(nodeModule),
input: pkg.module,
output: [
{
banner,
format: 'es',
file: pkg.es2015,
},
{ file: pkg.main,
amd: { id: name },
banner,
format: 'cjs',
globals,
name,
},
],
},
]
if (production) {
config.push(
// esm minify
{
external: external.concat(nodeModule),
input: pkg.module,
plugins: [ terser(uglifyOpts) ],
output: {
banner,
file: parseName(pkg.es2015) + '.min.js',
format: 'es',
sourcemap: true,
},
},
)
}
if (pkg.browser) {
config.push(
// umd bundle min
{
external: nodeModule,
input: pkg.module,
plugins: [
resolve({
browser: true,
jsnext: true,
main: true,
}),
commonjs(),
production && terser(uglifyOpts),
],
output: {
amd: { id: name },
banner,
file: `${targetDir}/${name}.umd.min.js`,
format: 'umd',
globals,
name,
sourcemap: production ? true : false,
},
},
)
}
if (pkg.bin) {
const shebang = `#!/usr/bin/env node\n\n${banner}`
for (const binPath of Object.values(pkg.bin)) {
if (! binPath) {
continue
}
const binSrcPath = binPath.includes('dist/') ? binPath : `./dist/${binPath}`
config.push({
external: external.concat(nodeModule),
input: binSrcPath,
output: [
{
file: binPath,
banner: shebang,
format: 'cjs',
globals,
},
],
})
}
}
// remove pkg.name extension if exists
function parseName(name) {
if (typeof name === 'string' && name) {
const arr = name.split('.')
const len = arr.length
if (len > 2) {
return arr.slice(0, -1).join('.')
}
else if (len === 2 || len === 1) {
return arr[0]
}
}
else {
throw new TypeError('name invalid')
}
return name
}
export default config