UNPKG

1.74 kBJavaScriptView Raw
1'use strict'
2
3/**
4 * 文件说明:
5 * ----------------------------------------
6 * 创建用户: leo
7 * 创建日期 2019/1/3
8 */
9
10const {rollup, watch} = require('rollup')
11const signale = require('signale')
12const getRollupConfig = require('./get-rollup-config')
13const normalizeBundleOpts = require('./normalize-bundle-opts')
14
15async function build({entry, opts, isFork}) {
16 const {cwd, type, bundleOpts, targetDir} = opts
17 const rollupConfigs = getRollupConfig({
18 cwd,
19 type,
20 entry,
21 targetDir,
22 bundleOpts: normalizeBundleOpts(entry, bundleOpts),
23 })
24
25 for (const rollupConfig of rollupConfigs) {
26 if (opts.watch) {
27 const watcher = watch([
28 {
29 ...rollupConfig,
30 watch: {},
31 },
32 ])
33 watcher.on('event', (event) => {
34 if (event.error) {
35 signale.error(`[${cwd}: ${type}]: \n`, event.error)
36 } else {
37 signale.info(`[${cwd}: ${type}] file changed`)
38 }
39 })
40 process.once('SIGINT', () => {
41 watcher.close()
42 })
43 } else {
44 const {output, ...input} = rollupConfig
45 const bundle = await rollup(input)
46 await bundle.write(output)
47 if (isFork) {
48 process.exit(0)
49 }
50 }
51 }
52}
53
54module.exports = async function(opts, isFork) {
55 if (Array.isArray(opts.entry)) {
56 const {entry: entries} = opts
57 for (const entry of entries) {
58 await build({entry, opts, isFork})
59 }
60 } else {
61 await build({entry: opts.entry, opts, isFork})
62 }
63}