-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrollup-build.ts
61 lines (55 loc) · 1.54 KB
/
rollup-build.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
import { vfilePlugin } from './rollup-plugin-vfile'
import { rollup } from 'rollup'
import { files } from './files'
import { cssStringPlugin } from './rollup-plugin-css'
// see below for details on these options
const inputOptions = {
input: './index', // resolved by our plugin
plugins: [vfilePlugin(files), cssStringPlugin()],
// 不需要 tree shaking
treeshake: false,
}
// you can create multiple outputs from the same input to generate e.g.
// different formats like CommonJS and ESM
const outputOptionsList = [
{
file: 'bundle.js',
format: 'umd', //es
},
]
export async function rollupBuild(callback) {
if (!files['./index']) {
return
}
let bundle
try {
// create a bundle
// @ts-ignore
bundle = await rollup(inputOptions)
// an array of file names this bundle depends on
const output = await generateOutputs(bundle)
callback(output[0].code)
} catch (error) {
// do some error reporting
console.error(error)
}
if (bundle) {
// closes the bundle
await bundle.close()
}
}
async function generateOutputs(bundle) {
const { output } = await bundle.generate(outputOptionsList[0])
return output
// for (const outputOptions of outputOptionsList) {
// const { output } = await bundle.generate(outputOptions)
// for (const chunkOrAsset of output) {
// if (chunkOrAsset.type === 'asset') {
// console.log('Asset', chunkOrAsset)
// } else {
// callback(output[0].code)
// // console.log('Chunk', chunkOrAsset.modules)
// }
// }
// }
}