-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathrollup.mjs
68 lines (62 loc) · 1.66 KB
/
rollup.mjs
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
import { rollup, defineConfig } from 'rollup'
import typescript from '@rollup/plugin-typescript'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import terser from '@rollup/plugin-terser'
import generateDts from 'rollup-plugin-dts'
import { tag, colors } from 'diy-log'
/**
* 构建
*
* @param {object} config 构建配置
* @param {object} options 构建选项
* @param {boolean} options.banner banner
* @param {boolean} options.pkg package.json
* @returns {Promise}
*/
async function build(config, { banner, pkg }) {
const { input, filename } = config
const buildOptions = defineConfig({
input,
plugins: [nodeResolve(), commonjs(), json(), typescript()],
external: config.external || [],
output: [
{
file: `${filename}.${pkg.type === 'module' ? 'js' : 'mjs'}`,
format: 'es',
banner,
},
{
file: `${filename}.${pkg.type === 'module' ? 'cjs' : 'js'}`,
format: 'cjs',
banner,
},
{
file: `${filename}.min.js`,
format: 'umd',
name: config.name,
banner,
plugins: [terser()],
globals: config.globals || {},
sourcemap: true,
},
],
})
const dtsOptions = defineConfig({
input,
plugins: [generateDts()],
output: {
file: `${filename}.d.ts`,
format: 'es',
},
})
const bundle = await rollup(buildOptions)
await Promise.all(buildOptions.output.map((options) => bundle.write(options)))
if (config.dts !== false) {
const dtsBundle = await rollup(dtsOptions)
await dtsBundle.write(dtsOptions.output)
}
tag.success(colors.blue(`[${config.name}] `) + 'Build complete.')
}
export { build as rollupBuild }