forked from huangxd-/danmu_api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-forward-widget.js
More file actions
205 lines (186 loc) · 7.52 KB
/
Copy pathbuild-forward-widget.js
File metadata and controls
205 lines (186 loc) · 7.52 KB
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
import * as esbuild from 'esbuild';
import fs from 'fs';
import path from 'node:path';
// 动态获取版本号
import { Globals } from './danmu_api/configs/globals.js';
// 定义要排除的UI相关模块
const uiModules = [
'./ui/template.js',
'../ui/template.js',
'../../ui/template.js',
'./ui/css/base.css.js',
'./ui/css/components.css.js',
'./ui/css/forms.css.js',
'./ui/css/responsive.css.js',
'./ui/js/main.js',
'./ui/js/preview.js',
'./ui/js/logview.js',
'./ui/js/apitest.js',
'./ui/js/pushdanmu.js',
'./ui/js/systemsettings.js',
'./utils/local-redis-util.js',
'./utils/bangumi-data-util.js',
'danmu_api/ui/template.js',
'danmu_api/ui/css/base.css.js',
'danmu_api/ui/css/components.css.js',
'danmu_api/ui/css/forms.css.js',
'danmu_api/ui/css/responsive.css.js',
'danmu_api/ui/js/main.js',
'danmu_api/ui/js/preview.js',
'danmu_api/ui/js/logview.js',
'danmu_api/ui/js/apitest.js',
'danmu_api/ui/js/pushdanmu.js',
'danmu_api/ui/js/systemsettings.js',
'danmu_api/utils/local-redis-util.js',
'danmu_api/utils/bangumi-data-util.js'
];
let customPolyfillContent = fs.readFileSync('forward/custom-polyfill.js', 'utf8');
const debugBuild = process.argv.includes('--debug');
const outputFile = debugBuild ? 'dist/logvar-danmu.debug.js' : 'dist/logvar-danmu.js';
// ForwardWidget runs in a browser-like JS runtime, so Node built-ins must not
// leak into the bundle. The server still imports the native implementations.
const forwardRuntimeCompatPlugin = {
name: 'forward-runtime-compat',
setup(build) {
const danAnyModulePath = path.resolve('danmu_api/utils/dan-any.js');
// Forward only consumes the native JSON/XML response paths. Keep dan-any
// available to the server while removing it and its transitive dependencies
// from the standalone widget bundle.
build.onResolve({ filter: /(?:^|[\\/])dan-any\.js$/ }, (args) => {
if (path.resolve(args.resolveDir, args.path) !== danAnyModulePath) return;
return { path: 'dan-any', namespace: 'forward-optional-modules' };
});
build.onResolve({ filter: /^node:async_hooks$/ }, () => ({
path: 'async-hooks',
namespace: 'forward-node-builtins'
}));
build.onResolve({ filter: /^node:(?:http|https)$/ }, (args) => ({
path: args.path.slice('node:'.length),
namespace: 'forward-node-builtins'
}));
// brotli ships a compressed dictionary specifically for browser bundles.
build.onResolve({ filter: /^\.\/dictionary-data$/ }, (args) => {
if (/[\\/]node_modules[\\/]brotli[\\/]dec[\\/]dictionary\.js$/.test(args.importer)) {
return { path: path.resolve('node_modules/brotli/dec/dictionary-browser.js') };
}
});
build.onLoad({ filter: /^async-hooks$/, namespace: 'forward-node-builtins' }, () => ({
loader: 'js',
contents: `
export class AsyncLocalStorage {
constructor() {
this.store = undefined;
}
getStore() {
return this.store;
}
run(store, callback, ...args) {
const previousStore = this.store;
this.store = store;
try {
return callback(...args);
} finally {
this.store = previousStore;
}
}
}
`
}));
build.onLoad({ filter: /^(?:http|https)$/, namespace: 'forward-node-builtins' }, () => ({
loader: 'js',
contents: `export default { Agent: class Agent {} };`
}));
build.onLoad({ filter: /^dan-any$/, namespace: 'forward-optional-modules' }, () => ({
loader: 'js',
contents: `
export const danAnyFormats = [];
export function convertDanAny() {
return null;
}
`
}));
}
};
(async () => {
try {
await esbuild.build({
entryPoints: ['forward/forward-widget.js'], // 新的入口文件
bundle: true,
minify: false, // 暂时关闭压缩以便调试
minifySyntax: true, // 折叠 debug 编译期开关,但保留可读变量名
sourcemap: false,
platform: 'neutral', // 改为neutral以避免Node.js特定的全局变量
target: 'es2020',
outfile: outputFile,
format: 'esm', // 保持ES模块格式
external: ['redis', 'fs', 'path', 'stream/promises', 'node-fetch'],
plugins: [
forwardRuntimeCompatPlugin,
// 插件:排除UI相关模块
{
name: 'exclude-ui-modules',
setup(build) {
// 拦截对UI相关模块的导入
build.onResolve({ filter: /.*ui.*\.(css|js)$|.*template\.js$|.*local-redis-util\.js$|.*bangumi-data-util\.js$/ }, (args) => {
// 直接匹配 bangumi-data-util.js 和 local-redis-util.js
if (args.path.includes('bangumi-data-util.js') || args.path.includes('local-redis-util.js')) {
return { path: args.path, external: true };
}
if (uiModules.some(uiModule => args.path.includes(uiModule.replace('./', '').replace('../', '')))) {
return { path: args.path, external: true };
}
});
}
},
// 插件:移除导出语句(仅对输出文件进行处理)
{
name: 'remove-exports',
setup(build) {
build.onEnd(async (result) => {
if (result.errors.length === 0) {
let outputContent = fs.readFileSync(outputFile, 'utf8');
// 更通用的模式,匹配包含这四个函数名的导出语句
const genericExportPattern = /export\s*{\s*(?:\s*(?:getCommentsById|getDanmuWithSegmentTime|getDetailById|searchDanmu)\s*,?\s*){4}\s*};?/g;
outputContent = outputContent.replace(genericExportPattern, '');
// 替换 httpGet 和 httpPost
const httpGetReplacement = debugBuild ? 'forwardDebugHttpGet' : 'Widget.http.get';
const httpPostReplacement = debugBuild ? 'forwardDebugHttpPost' : 'Widget.http.post';
// Replace awaited and promise-style calls while preserving the bundled declarations.
outputContent = outputContent.replace(/(?<!function\s)\bhttpGet\s*\(/g, `${httpGetReplacement}(`);
outputContent = outputContent.replace(/(?<!function\s)\bhttpPost\s*\(/g, `${httpPostReplacement}(`);
// Keep line removal linear even when dependencies contain very
// large single-line dictionaries (for example, opencc-js).
const excludedLineFragments = [
'setLocalRedisKey',
'updateLocalRedisCaches',
'bangumi-data-util.js'
];
outputContent = outputContent
.split(/\r?\n/)
.filter(line => !excludedLineFragments.some(fragment => line.includes(fragment)))
.join('\n');
// 保存修改后的内容
fs.writeFileSync(outputFile, outputContent);
}
});
}
}
],
define: {
'widgetVersion': `"${Globals.VERSION}"`,
'globalThis.__FORWARD_WIDGET__': 'true',
'globalThis.__FORWARD_WIDGET_DEBUG__': debugBuild ? 'true' : 'false'
},
banner: {
js: customPolyfillContent
},
logLevel: 'info'
});
console.log(`Forward widget ${debugBuild ? 'debug ' : ''}bundle created successfully: ${outputFile}`);
} catch (error) {
console.error('Build failed:', error);
process.exitCode = 1;
} finally {
await esbuild.stop();
}
})();