-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollup.config.js
111 lines (107 loc) · 2.89 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 { upperFirst, camelCase } from 'lodash'
import { defineConfig } from 'rollup'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import { terser } from 'rollup-plugin-terser'
import commonjs from '@rollup/plugin-commonjs'
import typescript from '@rollup/plugin-typescript'
import json from '@rollup/plugin-json'
import analyzer from 'rollup-plugin-analyzer'
import replace from '@rollup/plugin-replace'
import dts from 'rollup-plugin-dts'
import { dependencies, peerDependencies, name } from './package.json'
const external = Object.keys({ ...dependencies, ...peerDependencies }) // 默认不打包 dependencies, peerDependencies
const outputName = upperFirst(camelCase(name))// 导出的模块名称 PascalCase
const env = process.env
const __PROD__ = env.NODE_ENV === 'production'
const __DEV__ = env.NODE_ENV === 'development'
const __ANALYZER__ = Boolean(env.ANALYZER)
function getPlugins({ isBrowser = false, isMin = false, isDeclaration = false }) {
const plugins = []
plugins.push(
nodeResolve({
browser: isBrowser,
preferBuiltins: true,
}),
)
plugins.push(
typescript({
tsconfig: 'tsconfig.json',
module: 'esnext',
target: 'es2019', // node >= 12
declaration: isDeclaration,
sourceMap: true,
}),
)
plugins.push(
commonjs({
sourceMap: true,
}),
)
plugins.push(
json({}),
)
plugins.push(
replace({
// 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
// 'env.NODE_ENV': JSON.stringify(env.NODE_ENV),
preventAssignment: true,
}),
)
if (isMin) {
plugins.push(
terser({
module: true,
}),
)
}
if (__ANALYZER__) {
plugins.push(
analyzer({
}),
)
}
return plugins
}
export default defineConfig([
{
input: 'src/index.ts', // 生成类型文件
external,
output: {
dir: 'dist',
format: 'esm',
name: outputName,
sourcemap: true,
},
plugins: [dts()],
},
{
input: 'src/index.ts',
external,
output: {
file: 'dist/index.js', // 生成 cjs
format: 'cjs',
name: outputName,
sourcemap: true,
},
plugins: getPlugins({
isBrowser: false,
isDeclaration: false,
isMin: false,
}),
},
{
input: 'src/index.ts',
external,
output: {
file: 'dist/index.esm.js', // 生成 esm
format: 'esm',
name: outputName,
sourcemap: true,
},
plugins: getPlugins({
isBrowser: false,
isDeclaration: false,
isMin: false,
}),
},
])