forked from FriedRiceNoodles/banana-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.mjs
127 lines (121 loc) · 2.86 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
import { babel } from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import nodeResolve from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
import typescript from '@rollup/plugin-typescript';
import fs from 'fs';
import process from 'process';
import filesize from 'rollup-plugin-filesize';
const babelConfig = {
babelHelpers: 'bundled',
presets: ['@babel/preset-env'],
plugins: [
// ['@babel/plugin-transform-runtime', { useESModules: true }],
['@babel/plugin-proposal-decorators', { decoratorsBeforeExport: true }],
['@babel/plugin-proposal-class-properties'],
],
assumptions: {
setPublicClassFields: true,
},
exclude: 'node_modules/**',
extensions: ['.ts', '.js'],
};
const componentNames = fs
// 获取所有文件夹及文件
.readdirSync('./src', { withFileTypes: true })
// 筛选出所有文件夹
.filter((p) => p.isDirectory())
// 数据预处理
.map((p) => ({
path: `${p.name}/index`,
name: p.name,
}))
// 带上package/src/index.ts
.concat({ path: 'index', name: 'index' });
// 获取包名(当前所在的文件夹名)并转换成大驼峰
const UMDName = process
.cwd()
.split('/')
.pop()
.replace(/(^|-)(\w)/g, (_, _$1, $2) => $2.toUpperCase());
const rollupConfig = [
{
input: './src/index.ts',
output: {
dir: './umd',
format: 'umd',
name: UMDName,
},
plugins: [
typescript({
declaration: true,
declarationDir: './umd',
}),
nodeResolve({
extensions: ['.ts', '.js'],
}),
commonjs(),
babel(babelConfig),
terser(),
filesize(),
],
},
{
input: componentNames.reduce((result, p) => {
result[p.path] = `./src/${p.name}`;
return result;
}, {}),
output: {
dir: 'dist',
format: 'es',
entryFileNames: '[name].js',
},
plugins: [
typescript(),
nodeResolve({
extensions: ['.ts', '.js'],
}),
commonjs(),
babel(babelConfig),
terser(),
],
},
{
input: componentNames.reduce((result, p) => {
result[p.path] = `./src/${p.name}`;
return result;
}, {}),
output: {
dir: 'node',
format: 'es',
entryFileNames: '[name].js',
},
plugins: [
typescript(),
nodeResolve({
exportConditions: ['node'],
extensions: ['.ts', '.js'],
}),
commonjs(),
babel(babelConfig),
],
},
];
// 如果存在./src/autoloader.ts,则添加到rollupConfig中
if (fs.existsSync('./src/banana-autoloader.ts')) {
rollupConfig.push({
input: './src/banana-autoloader.ts',
output: {
dir: 'dist',
format: 'es',
entryFileNames: 'banana-autoloader.js',
},
plugins: [
typescript(),
nodeResolve({
extensions: ['.ts', '.js'],
}),
],
});
}
export default rollupConfig;