-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
61 lines (51 loc) · 1.78 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
import path from 'path'
import { createRequire } from 'node:module'
import { fileURLToPath } from 'node:url'
import json from '@rollup/plugin-json'
import ts from 'rollup-plugin-typescript2'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
const require = createRequire(import.meta.url)
const __dirname = fileURLToPath(new URL('.', import.meta.url))
const packageFormats = process.env.FORMATS && process.env.FORMATS.split(',')
const sourcemap = process.env.SOURCE_MAP
const packagesDir = path.resolve(__dirname, 'packages') // 解析出 packages 文件夹
const packageDir = path.resolve(packagesDir, process.env.TARGET) // 解析出当前的文件
const resolve = p => path.resolve(packageDir, p)
const name = path.basename(packageDir)
const pkg = require(resolve(`package.json`)) // 解析出 package.json 文件内容
const outputConfig = {
'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 packageConfigs = packageFormats || pkg.buildOptions.formats
function createConfig(format, output) {
output.sourcemap = sourcemap
output.exports = 'named' // VueReactivity
let external = []
if (format === 'global') {
output.name = pkg.buildOptions.name
} else {
external = [...Object.keys(pkg.dependencies)]
}
// 返回的结果就是 rollup 的配置
return {
input: resolve(`src/index.ts`),
output,
external,
plugins: [json(), ts(), commonjs(), nodeResolve()],
}
}
export default packageConfigs.map(format =>
createConfig(format, outputConfig[format])
)