1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | const {rollup, watch} = require('rollup')
|
11 | const signale = require('signale')
|
12 | const getRollupConfig = require('./get-rollup-config')
|
13 | const normalizeBundleOpts = require('./normalize-bundle-opts')
|
14 |
|
15 | async 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 |
|
54 | module.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 | }
|