-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathesbuild.js
More file actions
292 lines (260 loc) · 9.11 KB
/
Copy pathesbuild.js
File metadata and controls
292 lines (260 loc) · 9.11 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env node
const { build } = require('esbuild');
const path = require('path');
const fs = require('fs');
// 路径配置
const WORKERS_SRC = path.resolve(__dirname, 'src');
const ORIGINAL_SRC = path.resolve(__dirname, '..', 'Sub-Store', 'backend', 'src');
/** 插件:路径别名解析 */
function resolveFile(basePath) {
// 尝试加后缀
for (const ext of ['.js', '.json']) {
const full = basePath + ext;
if (fs.existsSync(full) && fs.statSync(full).isFile()) {
return full;
}
}
// 尝试原始路径
if (fs.existsSync(basePath) && fs.statSync(basePath).isFile()) {
return basePath;
}
// 尝试目录 index
if (fs.existsSync(basePath) && fs.statSync(basePath).isDirectory()) {
const indexPath = path.join(basePath, 'index.js');
if (fs.existsSync(indexPath)) {
return indexPath;
}
}
return null;
}
const aliasPlugin = {
name: 'substore-alias',
setup(build) {
// 解析 @/ 导入
build.onResolve({ filter: /^@\// }, (args) => {
const relPath = args.path.slice(2); // strip "@/"
// 优先 Workers 覆盖
const workersResolved = resolveFile(path.join(WORKERS_SRC, relPath));
if (workersResolved) return { path: workersResolved };
// 回退到原始源码
const originalResolved = resolveFile(path.join(ORIGINAL_SRC, relPath));
if (originalResolved) return { path: originalResolved };
console.warn(`[alias] Could not resolve: ${args.path}`);
return null;
});
},
};
/** 插件:eval 重写 */
const evalRewritePlugin = {
name: 'eval-rewrite',
setup(build) {
build.onLoad({ filter: /\.js$/ }, async (args) => {
// 仅处理原始源码
if (!args.path.startsWith(ORIGINAL_SRC)) return null;
const original = fs.readFileSync(args.path, 'utf8');
let contents = original;
// eval(require) → require
contents = contents.replace(
/eval\((['"`])(require\((['"`])(.+?)\3\))\1\)/g,
'$2',
);
// eval(process.env) → globalThis
contents = contents.replace(
/eval\((['"`])process\.env\.(\w+)\1\)/g,
'(globalThis.__workerEnv?.$2)',
);
// eval(process.version)
contents = contents.replace(
/eval\((['"`])process\.version\1\)/g,
'"workers"',
);
// eval(process.argv)
contents = contents.replace(
/eval\((['"`])process\.argv\1\)/g,
'[]',
);
// eval(__filename)
contents = contents.replace(
/eval\((['"`])__filename\1\)/g,
'"worker.js"',
);
// eval(__dirname)
contents = contents.replace(
/eval\((['"`])__dirname\1\)/g,
'"/"',
);
// eval(typeof require)
contents = contents.replace(
/eval\((['"`])typeof require !== (['"`])undefined\2\1\)/g,
'false',
);
// eval(typeof process)
contents = contents.replace(
/eval\((['"`])typeof process !== (['"`])undefined\2\1\)/g,
'false',
);
if (args.path.endsWith(path.join('core', 'proxy-utils', 'processors', 'index.js'))) {
contents = contents.replace(
/function createDynamicFunction\(name, script, \$arguments, \$options\) \{[\s\S]*?\n\}/,
`function createDynamicFunction(name, script, $arguments, $options) {
throw new Error('Script Operator is not supported in Cloudflare Workers because dynamic code execution through eval/new Function is disabled. Use built-in filters/operators, mihomo YAML patch, or an external trusted execution service.');
}`,
);
}
if (contents !== original) {
return {
contents,
loader: 'js',
};
}
return null;
});
},
};
/** 插件:peggy 预编译 */
const peggyPrecompilePlugin = {
name: 'peggy-precompile',
setup(build) {
const peggyDir = path.join(
ORIGINAL_SRC,
'core',
'proxy-utils',
'parsers',
'peggy',
);
// 拦截 peggy 文法文件
build.onLoad(
{ filter: /parsers[\\/]peggy[\\/].*\.js$/ },
async (args) => {
// 仅处理原始源码
if (!args.path.startsWith(peggyDir)) return null;
const source = fs.readFileSync(args.path, 'utf8');
// \u63d0\u53d6\u6587\u6cd5\u5b57\u7b26\u4e32
const grammarMatch = source.match(
/const grammars\s*=\s*String\.raw`([\s\S]*?)`;/,
);
if (!grammarMatch) {
console.warn(
`[peggy-precompile] Could not find grammar in ${args.path}`,
);
return null;
}
const grammar = grammarMatch[1];
try {
const peggy = require('peggy');
// 生成解析器源码
const parserSource = peggy.generate(grammar, {
output: 'source',
format: 'commonjs',
});
// 构建替代模块
const contents = `
let parser;
export default function getParser() {
if (!parser) {
parser = (function() {
var module = { exports: {} };
var exports = module.exports;
${parserSource}
return module.exports;
})();
}
return parser;
}
`;
console.log(
`[peggy-precompile] Pre-compiled: ${path.basename(args.path)}`,
);
return { contents, loader: 'js' };
} catch (e) {
console.error(
`[peggy-precompile] Failed to compile ${path.basename(args.path)}: ${e.message}`,
);
return null; // 回退到原始处理
}
},
);
},
};
/** 插件:Node 模块存根 */
const nodeStubPlugin = {
name: 'node-stub',
setup(build) {
// 存根 Node 专用模块
const stubs = [
'dotenv',
'cron',
'connect-history-api-fallback',
'http-proxy-middleware',
'body-parser',
'express',
'@maxmind/geoip2-node',
'undici',
'fetch-socks',
'child_process',
'stream/promises',
'dns-packet',
'mime-types',
'jsrsasign',
'fs',
'path',
'net',
'tls',
'http',
'https',
'os',
'crypto',
];
for (const mod of stubs) {
build.onResolve({ filter: new RegExp(`^${mod.replace(/[.*+?^${}()|[\]\\\/]/g, '\\$&')}$`) }, () => {
return { path: mod, namespace: 'node-stub' };
});
}
build.onLoad({ filter: /.*/, namespace: 'node-stub' }, (args) => {
return {
contents: `
module.exports = new Proxy({}, {
get(target, prop) {
if (prop === '__esModule') return false;
if (prop === 'default') return target;
return function() {
console.warn('[Workers stub] ${args.path}.' + prop + ' is not available in Workers');
return {};
};
}
});
`,
loader: 'js',
};
});
},
};
!(async () => {
console.log('Building Sub-Store Workers...');
console.log(`Workers source: ${WORKERS_SRC}`);
console.log(`Original source: ${ORIGINAL_SRC}`);
await build({
entryPoints: [path.join(WORKERS_SRC, 'index.js')],
bundle: true,
minify: true,
sourcemap: true,
platform: 'browser', // Workers 运行时
format: 'esm',
target: 'es2022',
outfile: path.join(__dirname, 'dist', 'worker.js'),
plugins: [aliasPlugin, peggyPrecompilePlugin, evalRewritePlugin, nodeStubPlugin],
define: {
'process.env.NODE_ENV': '"production"',
},
external: [],
nodePaths: [path.resolve(__dirname, 'node_modules')],
// Workers 包体积限制
logLevel: 'info',
});
const stats = fs.statSync(path.join(__dirname, 'dist', 'worker.js'));
console.log(`\nOutput: dist/worker.js (${(stats.size / 1024).toFixed(1)} KB)`);
console.log('Build complete!');
})().catch((e) => {
console.error('Build failed:', e);
process.exit(1);
});