-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
112 lines (104 loc) · 2.67 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import path from "path"
import ts from "rollup-plugin-typescript2"
const target = process.env.TARGET
const packagesDir = path.resolve(__dirname, "packages")
const packageDir = path.resolve(packagesDir, target)
const resolve = p => path.resolve(packageDir, p)
const isProd = process.env.NODE_ENV === "production"
const allFormats = ["es", "cjs"]
const isGlobalPackage = pak => ["iife", "umd"].includes(pak)
const outputConfigs = allFormats.reduce((configs, format) => {
configs[format] = {
format,
file: resolve(`dist/${target}.${transformFormat(format)}.js`)
}
if (isGlobalPackage(format)) {
const name = require(resolve("package.json")).globalPackageName
configs[format].name = name || target
}
return configs
}, {})
const defaultFormats = allFormats
const envFormats = process.env.FORMATS && process.env.FORMATS.split(".")
const packageFormats = envFormats || defaultFormats
const packageConfigs = packageFormats.map(format =>
createConfig(format, outputConfigs[format])
)
if (isProd) {
packageFormats.forEach(format => {
if (format === "cjs") {
packageConfigs.push(createProdConfig(format))
} else if (isGlobalPackage(format)) {
packageConfigs.push(createMinifiedConfig(format))
}
})
}
// 创建 rollup 配置
function createConfig(format, output, plugins = []) {
if (!output) {
console.log(require("chalk").yellow(`invalid format: "${format}"`))
process.exit(1)
}
output.sourcemap = process.env.SOURCE_MAP === "true"
const shouldEmitTypes = process.env.TYPES === "true"
const tsPlugin = ts({
check: isProd,
tsconfig: path.resolve(__dirname, "tsconfig.json"),
tsconfigOverride: {
compilerOptions: {
sourceMap: output.sourcemap,
declaration: shouldEmitTypes,
declarationMap: shouldEmitTypes
},
exclude: ["**/__tests__"]
}
})
return {
output,
input: resolve("src/index.ts"),
plugins: [tsPlugin, ...plugins],
onwarn: (msg, warn) => {
if (!/Circular/.test(msg)) {
warn(msg)
}
},
treeshake: {
moduleSideEffects: false
}
}
}
function transformFormat(format) {
const formats = {
es: "esm",
cjs: "cjs",
umd: "umd",
iife: "global"
}
return formats[format] || format
}
function createProdConfig(format) {
return createConfig(format, {
format: outputConfigs[format].format,
file: outputConfigs[format].file.replace(/\.js$/, ".prod.js")
})
}
function createMinifiedConfig(format) {
const { terser } = require("rollup-plugin-terser")
return createConfig(
format,
{
...outputConfigs[format],
file: outputConfigs[format].file.replace(/\.js$/, ".prod.js")
},
[
terser({
module: format === "es",
compress: {
ecma: 2015,
pure_getters: true
}
})
]
)
}
export default packageConfigs