-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.mjs
165 lines (153 loc) · 3.39 KB
/
rollup.config.mjs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import alias from '@rollup/plugin-alias';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import del from 'rollup-plugin-delete';
import dts from 'rollup-plugin-dts';
import esbuild from 'rollup-plugin-esbuild';
import { terser } from 'rollup-plugin-terser';
import { visualizer } from 'rollup-plugin-visualizer';
import progress from 'rollup-plugin-progress';
const isProd = process.env.NODE_ENV === 'production';
const isAnalyze = process.env.ANALYZE === 'true';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const basePlugins = [
resolve({
preferBuiltins: true,
extensions: ['.ts', '.js', '.mjs', '.json'],
moduleDirectories: ['node_modules', 'src'],
verbose: true,
}),
alias({
entries: [
{ find: /^node:(.+)$/, replacement: '$1' },
// {
// find: '@hubxu/utils',
// replacement: path.resolve(__dirname, 'src'),
// },
],
}),
json(),
commonjs(),
esbuild({
target: 'es2015',
}),
// terser({
// compress: {
// drop_console: false,
// pure_funcs: ['console.log'],
// },
// format: {
// comments: true,
// },
// }),
progress({
clearLine: false,
}),
...(isAnalyze
? [
visualizer({
filename: 'stats.html',
open: true,
}),
]
: []),
];
const createConfig = (input, output, external = [], isMain = false) => ({
input,
output: output.map(out => ({
...out,
sourcemap: !isProd,
})),
external: [...external, 'lodash', 'lodash-es', '@hubxu/utils'],
plugins: [...(isMain ? [del({ targets: 'dist/**/*', force: true })] : []), ...basePlugins],
});
const createDtsConfig = (input, output, external = []) => ({
input,
output,
external: ['csstype', 'type-fest', 'lodash', 'lodash-es', ...external],
plugins: [dts({ respectExternal: true })],
});
const config = [
// 主包配置
createConfig(
'src/index.ts',
[
{
file: 'dist/index.mjs',
format: 'es',
},
{
file: 'dist/index.cjs',
format: 'cjs',
},
{
file: 'dist/index.js',
format: 'umd',
name: 'Utils',
globals: {
'lodash-es': '_',
},
},
],
[],
true,
),
// Vue 子包配置
createConfig(
'src/vue/index.ts',
[
{
file: 'dist/vue/index.mjs',
format: 'es',
},
{
file: 'dist/vue/index.cjs',
format: 'cjs',
},
],
['vue'],
), // 添加 vue 作为外部依赖
// React 子包配置
createConfig(
'src/react/index.ts',
[
{
file: 'dist/react/index.mjs',
format: 'es',
},
{
file: 'dist/react/index.cjs',
format: 'cjs',
},
],
['react'],
), // 添加 react 作为外部依赖
// 主包类型声明
createDtsConfig('src/index.ts', {
file: 'dist/index.d.ts',
format: 'esm',
}),
// Vue 类型声明
createDtsConfig(
'src/vue/index.ts',
{
file: 'dist/vue/index.d.ts',
format: 'esm',
},
['vue'],
),
// React 类型声明
createDtsConfig(
'src/react/index.ts',
{
file: 'dist/react/index.d.ts',
format: 'esm',
},
['react'],
),
];
export default config;