-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrollup.config.js
127 lines (111 loc) · 3.04 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
122
123
124
125
126
127
const glob = require('glob');
const path = require('path');
const typescript = require('@rollup/plugin-typescript');
const { babel } = require('@rollup/plugin-babel');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const { DEFAULT_EXTENSIONS } = require('@babel/core');
// const { getExtraBuildDir } = require('./script/build/rollup-helper');
const pkg = require('./package.json');
const PATHS = {
input: path.join(__dirname, '/src/index.ts'),
output: path.join(__dirname, '/lib'),
outputES: path.join(__dirname, '/es'),
};
function getAllSourceInput() {
const input = Object.fromEntries(glob.sync('src/**/*.ts')
.filter(item => !item.endsWith('.d.ts') && !item.endsWith('/types.ts'))
.map(file => [
// 这里将删除 `src/` 以及每个文件的扩展名。
// 因此,例如 src/nested/foo.js 会变成 nested/foo
path.relative(
'src',
file.slice(0, file.length - path.extname(file).length),
),
// 这里可以将相对路径扩展为绝对路径,例如
// src/nested/foo 会变成 /project/src/nested/foo.js
// fileURLToPath(new URL(file, import.meta.url)),
path.resolve(file),
]));
// console.log('[getAllSourceInput] input: ', input);
return input;
}
// plugins 需要注意引用顺序
const ROLLUP_PLUGINS = [
// 配合 commnjs 解析第三方模块
nodeResolve(),
// 使得 rollup 支持 commonjs 规范,识别 commonjs 规范的依赖
commonjs({
sourceMap: false,
}),
// filesize(),
babel({
babelHelpers: 'runtime',
// 只转换源代码,不运行外部依赖
exclude: 'node_modules/**',
// babel 默认不支持 ts 需要手动添加
extensions: [...DEFAULT_EXTENSIONS, '.ts'],
}),
// terser(),
];
const ROLLUP_EXTERNALS = [
'fs',
'path',
// ...Object.keys(pkg.dependencies),
/@babel\/runtime/,
'axios',
];
const allRollupConfig = [
{
input: getAllSourceInput(),
output: [{
format: 'cjs',
exports: 'named',
dir: PATHS.output,
// preserveModules: true,
}],
external: ROLLUP_EXTERNALS,
plugins: [
// ts 的功能只在于编译出声明文件,所以 target 为 ESNext,编译交给 babel 来做
typescript({
tsconfig: './tsconfig.json',
}),
...ROLLUP_PLUGINS,
],
},
{
input: getAllSourceInput(),
output: [{
format: 'es',
exports: 'named',
dir: PATHS.outputES,
}],
external: ROLLUP_EXTERNALS,
plugins: [
typescript({
tsconfig: './tsconfig-es.json',
}),
...ROLLUP_PLUGINS,
],
},
{
input: PATHS.input,
output: [{
file: path.join(PATHS.output, 'index.esm.js'),
format: 'es',
name: pkg.name,
}],
external: ROLLUP_EXTERNALS,
plugins: [
typescript({
tsconfig: './tsconfig.json',
}),
...ROLLUP_PLUGINS,
],
},
// ...getExtraBuildDir({
// external: ROLLUP_EXTERNALS,
// plugins: ROLLUP_PLUGINS,
// }),
];
module.exports = allRollupConfig;