-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
79 lines (68 loc) · 2.28 KB
/
rollup.config.js
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
import path from 'path'
import ts from 'rollup-plugin-typescript2'
import json from '@rollup/plugin-json'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
if (!process.env.TARGET) {
throw new Error('TARGET package must be specified via --environment flag.')
}
// 需要根据target 找到要打包的目录
const packagesDir = path.resolve(__dirname, 'packages') // 根目录
const packageDir = path.resolve(packagesDir, process.env.TARGET) // 要打包的入口
const resolve = p => path.resolve(packageDir, p)
const pkg = require(resolve('package.json')) // 目标文件pkg
const packageOptions = pkg.buildOptions || {} // 自定义参数
const name = packageOptions.filename || path.basename(packageDir) // 目标文件名
const outputConfigs = {
'esm-bundler': {
file: resolve(`dist/${name}.esm-bundler.js`),
format: 'es'
},
cjs: {
file: resolve(`dist/${name}.cjs.js`),
format: 'cjs'
},
global: {
file: resolve(`dist/${name}.global.js`),
format: 'iife'
}
}
// 默认参数
const defaultFormats = ['esm-bundler', 'cjs']
// 从命令行传来的 参数
const inlineFormats = process.env.FORMATS && process.env.FORMATS.split(',')
// 最终的格式化结果
const packageFormats = inlineFormats || packageOptions.formats || defaultFormats
console.log('🤪 packageFormats >>:', packageFormats)
function createConfig(format, output, plugins = []) {
if (!output) {
console.log(require('chalk').yellow(`invalid format: "${format}"`))
process.exit(1)
}
const external = [] //排除文件
const isGlobalBuild = /global/.test(format)
output.sourcemap = !!process.env.SOURCE_MAP
if (isGlobalBuild) {
output.name = packageOptions.name
} else {
external = [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
...['path', 'url', 'stream'] // for @vue/compiler-sfc / server-renderer
]
}
return {
// createConfig的结果就是rollup的配置
input: resolve(`src/index.ts`),
output,
external,
plugins: [
json(),
ts(), // 将ts转化成js文件
commonjs(),
nodeResolve()
]
}
}
const packageConfigs = packageFormats.map(format => createConfig(format, outputConfigs[format]))
export default packageConfigs