-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathrollup.config.ts
223 lines (197 loc) · 6.35 KB
/
rollup.config.ts
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* eslint-disable import/no-extraneous-dependencies */
import fs from 'fs';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import shell from 'shelljs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import { babel } from '@rollup/plugin-babel';
import styles from 'rollup-plugin-styles';
import solidPlugin from 'vite-plugin-solid';
import json from '@rollup/plugin-json';
import alias from '@rollup/plugin-alias';
import replace from '@rollup/plugin-replace';
import { watchExternal } from 'rollup-plugin-watch-external';
import type { InputPluginOption, Plugin, RollupOptions } from 'rollup';
import { createServer } from 'vite';
import { parse as parseMd } from 'marked';
import { selfPlugins, solidSvg } from './src/rollup-plugin';
import { getMetaData, updateReadme } from './metaHeader';
const __dirname = dirname(fileURLToPath(import.meta.url));
const DEV_PORT = 2405;
const isDevMode = process.env.NODE_ENV === 'development';
const { meta, createMetaHeader } = getMetaData(isDevMode);
const latestChangeHtml = await (() => {
const md = fs
.readFileSync(resolve(__dirname, `docs/.other/LatestChange.md`))
.toString();
const newMd = md
.match(/^### [^[].+?$|^\* .+?$/gm)!
.map((mdText) => {
switch (mdText[0]) {
case '#':
return mdText
.replaceAll('Features', '新增')
.replaceAll('Bug Fixes', '修复')
.replaceAll('Performance Improvements', '优化');
case '*':
return mdText
.replace(/(?<=^\* ):\w+?: /, '')
.replace(/(?<=^.*)\(\[\w+\]\(.+?\)\).*/, '');
default:
return '';
}
})
.join('\n\n');
return parseMd(newMd);
})();
export const buildOptions = (
fileName: string,
watchFiles?: string[],
...plugins: Array<Plugin | false>
): RollupOptions => {
const isUserScript = ['dev', 'index'].includes(fileName);
return {
treeshake: true,
external: [...Object.keys(meta.resource ?? {}), 'main', 'dmzjDecrypt'],
input: resolve(__dirname, 'src', fileName),
// 忽略使用 eval 的警告
onwarn(warning, warn) {
if (warning.code !== 'EVAL') warn(warning);
},
plugins: [
replace({
values: {
DEV_PORT: `${DEV_PORT}`,
isDevMode: `${isDevMode}`,
'process.env.NODE_ENV': isDevMode ? `'development'` : `'production'`,
'inject@LatestChange': latestChangeHtml,
},
preventAssignment: true,
}),
alias({ entries: { helper: resolve(__dirname, 'src/helper') } }),
json({ namedExports: false, indent: ' ' }),
nodeResolve({ browser: true, extensions: ['.js', '.ts', '.tsx'] }),
commonjs(),
styles({ mode: 'extract', modules: true }),
solidSvg() as InputPluginOption,
babel({
babelHelpers: 'runtime',
extensions: ['.ts', '.tsx'],
exclude: ['node_modules/**'],
presets: ['@babel/preset-env', '@babel/preset-typescript', 'solid'],
plugins: [
'@babel/plugin-transform-runtime',
[
'@babel/plugin-proposal-import-attributes-to-assertions',
{ deprecatedAssertSyntax: true },
],
],
}),
watchFiles && isDevMode && watchExternal({ entries: watchFiles }),
...plugins,
],
output: {
// dev 和 index 外的文件都放到 cache 文件夹下
dir: resolve(__dirname, isUserScript ? 'dist' : 'dist/cache'),
format: 'cjs',
strict: false,
generatedCode: 'es2015',
extend: true,
plugins: [
...selfPlugins,
{
name: 'selfPlugin',
renderChunk(rawCode) {
let code = rawCode;
switch (fileName) {
case 'index': {
updateReadme();
if (isDevMode)
code = [
`console.time('脚本启动消耗时间')`,
code,
`console.timeEnd('脚本启动消耗时间')`,
].join('\n');
code = createMetaHeader(meta) + code;
break;
}
case 'dev': {
code =
createMetaHeader({
...meta,
name: `${meta.name}Test`,
namespace: `${meta.namespace}Test`,
updateURL: undefined,
downloadURL: undefined,
}) + code;
break;
}
}
return code;
},
},
],
},
};
};
// 清空 dist 文件夹
shell.rm('-rf', resolve(__dirname, 'dist/*'));
(async () => {
if (!isDevMode) return;
// 创建一个 dist 文件夹的文件服务器,用于在浏览器获取最新的脚本代码
const server = await createServer({
root: resolve(__dirname, 'src'),
server: {
host: true,
port: DEV_PORT,
cors: false,
},
resolve: {
alias: { helper: resolve(__dirname, 'src/helper') },
},
plugins: [
{
name: 'selfPlugin',
enforce: 'pre',
transform(code, id): null | string {
if (id.includes('node_modules')) return null;
let newCode = code;
newCode = newCode.replace('isDevMode', 'true');
// 将 vite 不支持的 rollup-plugin-styles 相关 css 导出代码删除
newCode = newCode.replace(', { css as style }', '');
newCode = newCode.replace(/\n.+?Style = style;\n/, '');
return newCode;
},
},
solidSvg(),
solidPlugin(),
],
});
// 开启组件的测试服务器
await server.listen();
server.printUrls();
})();
export default [
// // 打包 dmzjDecrypt 时用的配置
// (() => {
// const options = buildOptions(
// 'helper/dmzjDecrypt',
// undefined,
// terser({
// keep_classnames: true,
// keep_fnames: true,
// format: { beautify: true, ecma: 2015 },
// }),
// );
// options.output = { ...options.output, name: 'dmzjDecrypt', format: 'umd' };
// return options;
// })(),
buildOptions('dev'),
buildOptions('main'),
...fs
.readdirSync('src/site')
.map((fileName) => buildOptions(`site/${fileName}`)),
buildOptions('helper/import', ['dist/cache/main.js']),
buildOptions('index', ['dist/**/*', '!dist/index.js']),
];