forked from egoist/bili
/
index.ts
731 lines (667 loc) · 19.8 KB
/
index.ts
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
import './polyfills'
import path from 'path'
import colors from 'chalk'
import prettyBytes from 'pretty-bytes'
import formatTime from 'pretty-ms'
import textTable from 'text-table'
import resolveFrom from 'resolve-from'
import boxen from 'boxen'
import stringWidth from 'string-width'
import {
rollup,
watch,
Plugin as RollupPlugin,
ModuleFormat as RollupFormat
} from 'rollup'
import merge from 'lodash/merge'
import waterfall from 'p-waterfall'
import spinner from './spinner'
import logger from './logger'
import progressPlugin from './plugins/progress'
import nodeResolvePlugin from './plugins/node-resolve'
import configLoader from './config-loader'
import isExternal from './utils/is-external'
import getBanner from './utils/get-banner'
import {
Options,
Config,
NormalizedConfig,
Format,
ConfigEntryObject,
Env,
ConfigOutput,
RunContext,
RollupConfig,
Task
} from './types'
// Make rollup-plugin-vue use basename in component.__file instead of absolute path
// TODO: PR to rollup-plugin-vue to allow this as an API option
process.env.BUILD = 'production'
interface RunOptions {
write?: boolean
watch?: boolean
concurrent?: boolean
}
interface RollupConfigInput {
source: {
input: string[] | ConfigEntryObject
files: string[]
hasVue: boolean
hasTs: boolean
}
title: string
format: Format
context: RunContext
assets: Assets
config: NormalizedConfig
}
type PluginFactory = (opts: any) => RollupPlugin
type GetPlugin = (name: string) => PluginFactory | Promise<PluginFactory>
export class Bundler {
rootDir: string
config: NormalizedConfig
configPath?: string
pkg: {
path?: string
data?: any
}
bundles: Set<Assets>
constructor(config: Config, public options: Options = {}) {
logger.setOptions({ logLevel: options.logLevel })
this.rootDir = path.resolve(options.rootDir || '.')
this.pkg = configLoader.loadSync({
files: ['package.json'],
cwd: this.rootDir
})
if (!this.pkg.data) {
this.pkg.data = {}
}
if (/\.mjs$/.test(this.pkg.data.module || this.pkg.data.main)) {
logger.warn(
`Bili no longer use .mjs extension for esm bundle, you should use .js instead!`
)
}
const userConfig =
options.configFile === false
? {}
: configLoader.loadSync({
files:
typeof options.configFile === 'string'
? [options.configFile]
: [
'bili.config.js',
'bili.config.ts',
'.bilirc.js',
'.bilirc.ts',
'package.json'
],
cwd: this.rootDir,
packageKey: 'bili'
})
if (userConfig.path) {
logger.debug(`Using config file:`, userConfig.path)
this.configPath = userConfig.path
}
this.config = this.normalizeConfig(
config,
userConfig.data || {}
) as NormalizedConfig
this.bundles = new Set()
}
normalizeConfig(config: Config, userConfig: Config) {
const result = merge({}, userConfig, config, {
input: config.input || userConfig.input || 'src/index.js',
output: merge({}, userConfig.output, config.output),
plugins: merge({}, userConfig.plugins, config.plugins),
babel: merge(
{
asyncToPromises: true
},
userConfig.babel,
config.babel
),
externals: [
...(Array.isArray(userConfig.externals)
? userConfig.externals
: [userConfig.externals]),
...(Array.isArray(config.externals)
? config.externals
: [config.externals])
]
})
result.output.dir = path.resolve(result.output.dir || 'dist')
return result
}
async createRollupConfig({
source,
format,
title,
context,
assets,
config
}: RollupConfigInput): Promise<RollupConfig> {
// Always minify if config.minify is truthy
// Otherwise infer by format
const minify =
config.output.minify === undefined
? format.endsWith('-min')
: config.output.minify
let minPlaceholder = ''
let rollupFormat: RollupFormat
if (format.endsWith('-min')) {
rollupFormat = format.replace(/-min$/, '') as RollupFormat
minPlaceholder = '.min'
} else {
rollupFormat = format as RollupFormat
}
// UMD format should always bundle node_modules
const bundleNodeModules =
rollupFormat === 'umd' ||
rollupFormat === 'iife' ||
config.bundleNodeModules
// rollup-plugin-typescript2 < v0.26 needs the `objectHashIgnoreUnknownHack`
// option to be enabled to correctly handle async plugins, but it's no
// longer needed (and causes a warning) if the user has a more recent
// version installed. [1] if the plugin is installed, detect the version
// and enable/disable the option accordingly.
//
// [1] https://github.com/egoist/bili/issues/305
const getObjectHashIgnoreUnknownHack = (): boolean => {
try {
const { version } = this.localRequire(
'rollup-plugin-typescript2/package.json'
)
const semver = require('semver')
return semver.lt(version, '0.26.0')
} catch (e) {
return true
}
}
const pluginsOptions: { [key: string]: any } = {
progress:
config.plugins.progress !== false &&
merge(
{
title
},
config.plugins.progress
),
json: config.plugins.json !== false && merge({}, config.plugins.json),
hashbang:
config.plugins.hashbang !== false && merge({}, config.plugins.hashbang),
'node-resolve':
config.plugins['node-resolve'] !== false &&
merge(
{},
{
rootDir: this.rootDir,
bundleNodeModules,
externals: config.externals,
browser: config.output.target === 'browser'
},
config.plugins['node-resolve']
),
postcss:
config.plugins.postcss !== false &&
merge(
{
extract: config.output.extractCSS !== false
},
config.plugins.postcss
),
vue:
(source.hasVue || config.plugins.vue) &&
merge(
{
css: false
},
config.plugins.vue
),
typescript2:
(source.hasTs || config.plugins.typescript2) &&
merge(
{
objectHashIgnoreUnknownHack: getObjectHashIgnoreUnknownHack(),
tsconfigOverride: {
compilerOptions: {
module: 'esnext'
}
}
},
config.plugins.typescript2
),
babel:
config.plugins.babel !== false &&
merge(
{
exclude: 'node_modules/**',
extensions: ['.js', '.jsx', '.mjs', '.ts', '.tsx', '.vue'],
babelrc: config.babel.babelrc,
configFile: config.babel.configFile,
presetOptions: config.babel
},
config.plugins.babel
),
buble:
(config.plugins.buble || config.babel.minimal) &&
merge(
{
exclude: 'node_modules/**',
include: '**/*.{js,mjs,jsx,ts,tsx,vue}',
transforms: {
modules: false,
dangerousForOf: true,
dangerousTaggedTemplateString: true
}
},
config.plugins.buble
),
commonjs:
config.plugins.commonjs !== false &&
merge({}, config.plugins.commonjs, {
// `ignore` is required to allow dynamic require
// See: https://github.com/rollup/rollup-plugin-commonjs/blob/4a22147456b1092dd565074dc33a63121675102a/src/index.js#L32
ignore: (name: string) => {
const { commonjs } = config.plugins
if (commonjs && commonjs.ignore && commonjs.ignore(name)) {
return true
}
return isExternal(config.externals, name)
}
})
}
const env = Object.assign({}, config.env)
// drop process.env.NODE_ENV from umd/iife
if (
['umd', 'umd-min', 'iife', 'iife-min'].includes(format) &&
env.NODE_ENV === undefined
) {
env.NODE_ENV = minify ? 'production' : 'development'
}
pluginsOptions.replace = {
...Object.keys(env).reduce((res: Env, name) => {
res[`process.env.${name}`] = JSON.stringify(env[name])
return res
}, {}),
...config.plugins.replace
}
if (Object.keys(pluginsOptions.replace).length === 0) {
pluginsOptions.replace = false
}
const banner = getBanner(config.banner, this.pkg.data)
if (minify) {
const terserOptions = config.plugins.terser || {}
pluginsOptions.terser = {
...terserOptions,
output: {
...terserOptions.output,
// Add banner (if there is)
preamble: banner
}
}
}
for (const name of Object.keys(config.plugins)) {
if (pluginsOptions[name] === undefined) {
Object.assign(pluginsOptions, { [name]: config.plugins[name] })
}
}
const getPlugin: GetPlugin = (name: string) => {
if (config.resolvePlugins && config.resolvePlugins[name]) {
return config.resolvePlugins[name]
}
// TODO: upgrade all plugins to @rollup scope
const isLegacyBuiltIn = require('../package').dependencies[
`rollup-plugin-${name}`
]
const isBuiltIn = require('../package').dependencies[
`@rollup/plugin-${name}`
]
const plugin =
name === 'babel'
? import('./plugins/babel').then(res => res.default)
: name === 'node-resolve'
? nodeResolvePlugin
: name === 'progress'
? progressPlugin
: name.startsWith('@rollup/')
? this.localRequire(name)
: isLegacyBuiltIn
? require(`rollup-plugin-${name}`)
: isBuiltIn
? require(`@rollup/plugin-${name}`)
: this.localRequire(`rollup-plugin-${name}`)
if (name === 'terser') {
return plugin.terser
}
return plugin.default || plugin
}
const plugins = await Promise.all(
Object.keys(pluginsOptions)
.filter(name => pluginsOptions[name])
.map(async name => {
const options =
pluginsOptions[name] === true ? {} : pluginsOptions[name]
const plugin = await getPlugin(name)
return plugin(options)
})
)
if (logger.isDebug) {
for (const name of Object.keys(pluginsOptions)) {
if (pluginsOptions[name]) {
logger.debug(colors.dim(format), `Using plugin: ${name}`)
}
}
}
// Add bundle to out assets Map
// So that we can log the stats when all builds completed
// Make sure this is the last plugin!
let startTime: number
let endTime: number
plugins.push({
name: 'record-bundle',
generateBundle(outputOptions, _assets) {
const EXTS = [
outputOptions.entryFileNames
? path.extname(outputOptions.entryFileNames)
: '.js',
'.css'
]
for (const fileName of Object.keys(_assets)) {
if (EXTS.some(ext => fileName.endsWith(ext))) {
const file: any = _assets[fileName]
const absolute =
outputOptions.dir && path.resolve(outputOptions.dir, fileName)
if (absolute) {
const relative = path.relative(process.cwd(), absolute)
assets.set(relative, {
absolute,
get source() {
return file.isAsset ? file.source.toString() : file.code
}
})
}
}
}
},
buildStart() {
startTime = Date.now()
},
buildEnd() {
endTime = Date.now()
},
async writeBundle() {
await printAssets(
assets,
`${title.replace('Bundle', 'Bundled')} ${colors.dim(
`(${formatTime(endTime - startTime)})`
)}`
)
}
})
const defaultFileName = getDefaultFileName(rollupFormat)
const getFileName = config.output.fileName || defaultFileName
const fileNameTemplate =
typeof getFileName === 'function'
? getFileName({ format: rollupFormat, minify }, defaultFileName)
: getFileName
const fileName = fileNameTemplate
.replace(/\[min\]/, minPlaceholder)
// The `[ext]` placeholder no longer makes sense
// Since we only output to `.js` now
// Probably remove it in the future
.replace(/\[ext\]/, '.js')
return {
inputConfig: {
input: source.input,
plugins,
external: Object.keys(config.globals || {}).filter(
v => !/^[\.\/]/.test(v)
),
onwarn(warning) {
if (typeof warning === 'string') {
return logger.warn(warning)
}
const code = (warning.code || '').toLowerCase()
if (code === 'mixed_exports' || code === 'missing_global_name') {
return
}
let message = warning.message
if (code === 'unresolved_import' && warning.source) {
if (format !== 'umd' || context.unresolved.has(warning.source)) {
return
}
context.unresolved.add(warning.source)
message = `${warning.source} is treated as external dependency`
}
logger.warn(
`${colors.yellow(`${code}`)}${colors.dim(':')} ${message}`
)
}
},
outputConfig: {
globals: config.globals,
format: rollupFormat,
dir: path.resolve(config.output.dir || 'dist'),
entryFileNames: fileName,
name: config.output.moduleName,
banner,
sourcemap:
typeof config.output.sourceMap === 'boolean'
? config.output.sourceMap
: minify,
sourcemapExcludeSources: config.output.sourceMapExcludeSources
}
}
}
async run(options: RunOptions = {}) {
const context: RunContext = {
unresolved: new Set()
}
const tasks: Task[] = []
let { input } = this.config
if (!Array.isArray(input)) {
input = [input || 'src/index.js']
}
if (Array.isArray(input) && input.length === 0) {
input = ['src/index.js']
}
const getMeta = (files: string[]) => {
return {
hasVue: files.some(file => file.endsWith('.vue')),
hasTs: files.some(file => /\.tsx?$/.test(file))
}
}
const normalizeInputValue = (input: string[] | ConfigEntryObject) => {
if (Array.isArray(input)) {
return input.map(
v => `./${path.relative(this.rootDir, this.resolveRootDir(v))}`
)
}
return Object.keys(input).reduce(
(res: ConfigEntryObject, entryName: string) => {
res[entryName] = `./${path.relative(
this.rootDir,
this.resolveRootDir(input[entryName])
)}`
return res
},
{}
)
}
const sources = input.map(v => {
if (typeof v === 'string') {
const files = v.split(',')
return {
files,
input: normalizeInputValue(files),
...getMeta(files)
}
}
const files = Object.values(v)
return {
files,
input: normalizeInputValue(v),
...getMeta(files)
}
})
let { format, target } = this.config.output
if (Array.isArray(format)) {
if (format.length === 0) {
format = ['cjs']
}
} else if (typeof format === 'string') {
format = format.split(',') as Format[]
} else {
format = ['cjs']
}
const formats = format
for (const source of sources) {
for (const format of formats) {
let title = `Bundle ${source.files.join(', ')} in ${format} format`
if (target) {
title += ` for target ${target}`
}
tasks.push({
title,
getConfig: async (context, task) => {
const assets: Assets = new Map()
this.bundles.add(assets)
const config = this.config.extendConfig
? this.config.extendConfig(merge({}, this.config), {
input: source.input,
format
})
: this.config
const rollupConfig = await this.createRollupConfig({
source,
format,
title: task.title,
context,
assets,
config
})
return this.config.extendRollupConfig
? this.config.extendRollupConfig(rollupConfig)
: rollupConfig
}
})
}
}
if (options.watch) {
const configs = await Promise.all(
tasks.map(async task => {
const { inputConfig, outputConfig } = await task.getConfig(
context,
task
)
return {
...inputConfig,
output: outputConfig,
watch: {}
}
})
)
const watcher = watch(configs)
watcher.on('event', e => {
if (e.code === 'ERROR') {
logger.error(e.error.message)
}
})
} else {
try {
if (options.concurrent) {
await Promise.all(
tasks.map(task => {
return this.build(task, context, options.write)
})
)
} else {
await waterfall(
tasks.map(task => () => {
return this.build(task, context, options.write)
}),
context
)
}
} catch (err) {
spinner.stop()
throw err
}
}
return this
}
async build(task: Task, context: RunContext, write?: boolean) {
try {
const { inputConfig, outputConfig } = await task.getConfig(context, task)
const bundle = await rollup(inputConfig)
if (write) {
await bundle.write(outputConfig)
} else {
await bundle.generate(outputConfig)
}
} catch (err) {
err.rollup = true
logger.error(task.title.replace('Bundle', 'Failed to bundle'))
if (err.message.includes('You must supply output.name for UMD bundles')) {
err.code = 'require_module_name'
err.message = `You must supply output.moduleName option or use --module-name <name> flag for UMD bundles`
}
throw err
}
}
handleError(err: any) {
if (err.stack) {
console.error()
console.error(colors.bold(colors.red('Stack Trace:')))
console.error(colors.dim(err.stack))
}
}
resolveRootDir(...args: string[]) {
return path.resolve(this.rootDir, ...args)
}
localRequire(
name: string,
{ silent, cwd }: { silent?: boolean; cwd?: string } = {}
) {
cwd = cwd || this.rootDir
const resolved = silent
? resolveFrom.silent(cwd, name)
: resolveFrom(cwd, name)
return resolved && require(resolved)
}
getBundle(index: number) {
return [...this.bundles][index]
}
}
interface Asset {
absolute: string
source: string
}
type Assets = Map<string, Asset>
async function printAssets(assets: Assets, title: string) {
const gzipSize = await import('gzip-size').then(res => res.default)
const table = await Promise.all(
[...assets.keys()].map(async relative => {
const asset = assets.get(relative) as Asset
const size = asset.source.length
return [
colors.green(relative),
prettyBytes(size),
prettyBytes(await gzipSize(asset.source))
]
})
)
table.unshift(['File', 'Size', 'Gzipped'].map(v => colors.dim(v)))
logger.success(title)
logger.log(
boxen(
textTable(table, {
stringLength: stringWidth
})
)
)
}
function getDefaultFileName(format: RollupFormat) {
return format === 'cjs' ? `[name][min][ext]` : `[name].[format][min][ext]`
}
export { Config, NormalizedConfig, Options, ConfigOutput }