/
rollup.js
324 lines (296 loc) 路 9.07 KB
/
rollup.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"use strict"
const { rollup } = require("rollup")
const babel = require("rollup-plugin-babel")
const closure = require("rollup-plugin-closure-compiler-js")
const commonjs = require("rollup-plugin-commonjs")
const resolve = require("rollup-plugin-node-resolve")
const prettier = require("rollup-plugin-prettier")
const replace = require("rollup-plugin-replace")
const ora = require("ora")
// const stripBanner = require("rollup-plugin-strip-banner")
const chalk = require("chalk")
const path = require("path")
const fs = require("fs")
const argv = require("minimist")(process.argv.slice(2))
const Stats = require("./stats")
const sizes = require("./plugins/sizes")
const codeFrame = require("babel-code-frame")
const babelConfig = require("./babel.config")
const Modules = require("./modules")
const Bundles = require("./bundles")
const Packaging = require("./packaging")
const UMD_DEV = "UMD_DEV"
const UMD_PROD = "UMD_PROD"
const NODE_DEV = "NODE_DEV"
const NODE_PROD = "NODE_PROD"
// Errors in promises should be fatal.
let loggedErrors = new Set()
process.on("unhandledRejection", err => {
if (loggedErrors.has(err)) {
// No need to print it twice.
process.exit(1)
}
throw err
})
const { UNIVERSAL, NODE } = Bundles.bundleTypes
const forcePrettyOutput = argv.pretty
const shouldExtractErrors = argv["extract-errors"]
const closureOptions = {
compilationLevel: "SIMPLE",
languageIn: "ECMASCRIPT5_STRICT",
languageOut: "ECMASCRIPT5_STRICT",
env: "CUSTOM",
warningLevel: "QUIET",
applyInputSourceMaps: false,
useTypesForOptimization: false,
processCommonJsModules: false
}
function getBabelConfig(updateBabelOptions, bundleType, filename) {
return Object.assign({}, babelConfig({ modules: false }), {
babelrc: false,
exclude: "node_modules/**",
runtimeHelpers: true
})
}
function getRollupOutputOptions(outputPath, format, globals, globalName) {
return {
file: outputPath,
format,
globals,
interop: true, // might be turned off with Babel 7, please review
name: globalName,
sourcemap: false
}
}
function getFormat(bundleType) {
switch (bundleType) {
case UMD_DEV:
case UMD_PROD:
return `umd`
case NODE_DEV:
case NODE_PROD:
return `cjs`
}
}
function getFilename(bundle, bundleType) {
const filename = bundle.label || bundle.entry
switch (bundleType) {
case NODE_DEV:
case UMD_DEV:
return `${filename}.development.js`
case UMD_PROD:
case NODE_PROD:
return `${filename}.production.min.js`
}
}
function isProductionBundleType(bundleType) {
switch (bundleType) {
case UMD_DEV:
case NODE_DEV:
return false
case UMD_PROD:
case NODE_PROD:
return true
default:
throw new Error(`Unknown type: ${bundleType}`)
}
}
function getPlugins(
entry,
externals,
updateBabelOptions,
filename,
bundleType,
globalName,
moduleType,
modulesToStub
) {
// const findAndRecordErrorCodes = extractErrorCodes(errorCodeOpts)
const isProduction = isProductionBundleType(bundleType)
const isInGlobalScope = bundleType === UMD_DEV || bundleType === UMD_PROD
const shouldStayReadable = forcePrettyOutput
return [
// Use Node resolution mechanism.
resolve(),
// Compile to ES5.
babel(getBabelConfig(updateBabelOptions, bundleType)),
// Turn __DEV__ and process.env checks into constants.
replace({
"process.env.NODE_ENV": isProduction ? "'production'" : "'development'"
}),
// We still need CommonJS for external deps like object-assign.
commonjs(),
// Apply dead code elimination and/or minification.
isProduction &&
closure(
Object.assign({}, closureOptions, {
// Don't let it create global variables in the browser.
// https://github.com/facebook/react/issues/10909
assumeFunctionWrapper: !isInGlobalScope,
// Works because `google-closure-compiler-js` is forked in Yarn lockfile.
// We can remove this if GCC merges my PR:
// https://github.com/google/closure-compiler/pull/2707
// and then the compiled version is released via `google-closure-compiler-js`.
renaming: !shouldStayReadable
})
),
// Add the whitespace back if necessary.
shouldStayReadable && prettier(),
// TODO
// License and haste headers, top-level `if` blocks.
// {
// transformBundle(source) {
// return Wrappers.wrapBundle(
// source,
// bundleType,
// globalName,
// filename,
// moduleType
// )
// }
// },
// Record bundle size.
sizes({
getSize: (size, gzip) => {
const key = `${filename} (${bundleType})`
Stats.currentBuildResults.bundleSizes[key] = {
size,
gzip
}
}
})
].filter(Boolean)
}
function handleRollupWarning(warning) {
if (warning.code === "UNRESOLVED_IMPORT") {
console.error(warning.message)
process.exit(1)
}
if (warning.code === "UNUSED_EXTERNAL_IMPORT") {
const match = warning.message.match(/external module '([^']+)'/)
if (!match || typeof match[1] !== "string") {
throw new Error("Could not parse a Rollup warning. " + "Fix this method.")
}
const importSideEffects = Modules.getImportSideEffects()
const externalModule = match[1]
if (typeof importSideEffects[externalModule] !== "boolean") {
throw new Error(
'An external module "' +
externalModule +
'" is used in a DEV-only code path ' +
"but we do not know if it is safe to omit an unused require() to it in production. " +
"Please add it to the `importSideEffects` list in `scripts/rollup/modules.js`."
)
}
// Don't warn. We will remove side effectless require() in a later pass.
return
}
console.warn(warning.message || warning)
}
function handleRollupError(error) {
loggedErrors.add(error)
if (!error.code) {
console.error(error)
return
}
console.error(
`\x1b[31m-- ${error.code}${error.plugin ? ` (${error.plugin})` : ""} --`
)
console.error(error.message)
const { file, line, column } = error.loc
if (file) {
// This looks like an error from Rollup, e.g. missing export.
// We'll use the accurate line numbers provided by Rollup but
// use Babel code frame because it looks nicer.
const rawLines = fs.readFileSync(file, "utf-8")
// column + 1 is required due to rollup counting column start position from 0
// whereas babel-code-frame counts from 1
const frame = codeFrame(rawLines, line, column + 1, {
highlightCode: true
})
console.error(frame)
} else {
// This looks like an error from a plugin (e.g. Babel).
// In this case we'll resort to displaying the provided code frame
// because we can't be sure the reported location is accurate.
console.error(error.codeFrame)
}
}
async function build(bundle, bundleType) {
const filename = getFilename(bundle, bundleType)
const logKey =
chalk.white.bold(filename) + chalk.dim(` (${bundleType.toLowerCase()})`)
const format = getFormat(bundleType)
const packageName = Packaging.getPackageName(bundle.entry)
let resolvedEntry = require.resolve(bundle.entry)
const shouldBundleDependencies =
bundleType === UMD_DEV || bundleType === UMD_PROD
const peerGlobals = Modules.getPeerGlobals(
bundle.externals,
bundle.moduleType
)
let externals = Object.keys(peerGlobals)
if (!shouldBundleDependencies) {
const deps = Modules.getDependencies(bundleType, bundle.entry)
externals = externals.concat(deps)
}
const importSideEffects = Modules.getImportSideEffects()
const pureExternalModules = Object.keys(importSideEffects).filter(
module => !importSideEffects[module]
)
const rollupConfig = {
input: resolvedEntry,
treeshake: {
pureExternalModules
},
external(id) {
const containsThisModule = pkg => id === pkg || id.startsWith(pkg + "/")
const isProvidedByDependency = externals.some(containsThisModule)
if (!shouldBundleDependencies && isProvidedByDependency) {
return true
}
return !!peerGlobals[id]
},
onwarn: handleRollupWarning,
plugins: getPlugins(
bundle.entry,
externals,
bundle.babel,
filename,
bundleType,
bundle.global,
bundle.moduleType,
bundle.modulesToStub
)
}
const [mainOutputPath, ...otherOutputPaths] = Packaging.getBundleOutputPaths(
"UNIVERSAL",
filename,
packageName
)
const rollupOutputOptions = getRollupOutputOptions(
mainOutputPath,
format,
peerGlobals,
bundle.global
)
const spinner = ora(logKey).start()
try {
const result = await rollup(rollupConfig)
await result.write(rollupOutputOptions)
} catch (error) {
spinner.fail()
handleRollupError(error)
throw error
}
for (let i = 0; i < otherOutputPaths.length; i++) {
await asyncCopyTo(mainOutputPath, otherOutputPaths[i])
}
spinner.succeed()
}
module.exports = async function(bundle) {
await build(bundle, NODE_DEV)
await build(bundle, NODE_PROD)
// await build(bundle, UMD_DEV)
// await build(bundle, UMD_PROD)
}