-
Notifications
You must be signed in to change notification settings - Fork 9
/
rollup.config.js
121 lines (119 loc) · 3.16 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
112
113
114
115
116
117
118
119
120
121
import path from 'path'
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 alias from '@rollup/plugin-alias'
import replace from '@rollup/plugin-replace'
import dts from 'rollup-plugin-dts'
import _ from 'lodash'
import { dependencies, name } from './package.json'
const external = Object.keys(dependencies) // 默认不打包 dependencies
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',
esModuleInterop: true,
allowSyntheticDefaultImports: true,
declaration: isDeclaration,
sourceMap: true,
}),
)
plugins.push(alias({
entries: [
{ find: '@', replacement: path.resolve(__dirname, './src') },
],
}))
plugins.push(
commonjs({
sourceMap: false,
}),
)
plugins.push(
json({}),
)
plugins.push(
replace({
exclude: 'node_modules/**',
'process.env.NODE_ENV': JSON.stringify(env.NODE_ENV || 'production'),
'process.browser': isBrowser,
preventAssignment: false,
}),
)
if (isMin) {
plugins.push(
terser({
module: true,
}),
)
}
if (__ANALYZER__) {
plugins.push(
analyzer({
}),
)
}
return plugins
}
export default [
{
input: 'src/index.ts', // 生成类型文件
external,
output: {
dir: 'dist',
format: 'esm',
name: outputName,
},
plugins: [alias({
entries: [
{ find: '@', replacement: path.resolve(__dirname, './src') },
],
}), 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.mjs', // 生成 esm
format: 'esm',
name: outputName,
sourcemap: true,
},
plugins: getPlugins({
isBrowser: false,
isDeclaration: false,
isMin: false,
}),
},
]