forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsdown-worker.config.ts
More file actions
99 lines (91 loc) · 3.82 KB
/
tsdown-worker.config.ts
File metadata and controls
99 lines (91 loc) · 3.82 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
import fs from 'node:fs';
import path from 'node:path';
import type { Plugin } from 'rolldown';
import { defineConfig } from 'tsdown';
// Plugin to automatically resolve .worker.ts files instead of .ts files
function workerAliasPlugin(): Plugin {
return {
name: 'worker-alias',
resolveId(source, importer) {
// Skip if no importer (entry point) or already a .worker file
if (!importer || source.includes('.worker')) {
return null;
}
// Handle relative imports
if (source.startsWith('.')) {
const importerDir = path.dirname(importer);
const resolved = path.resolve(importerDir, source);
// Try .worker.ts and .worker.tsx variants
for (const ext of ['.worker.ts', '.worker.tsx']) {
const workerPath = resolved + ext;
if (fs.existsSync(workerPath)) {
return workerPath;
}
// Also check if source already has extension
const withoutExt = resolved.replace(/\.(ts|tsx)$/, '');
const workerPathAlt = withoutExt + ext;
if (fs.existsSync(workerPathAlt)) {
return workerPathAlt;
}
}
}
// Handle @/ alias imports
if (source.startsWith('@/')) {
const relativePath = source.slice(2); // Remove @/
const libPath = path.resolve('./lib', relativePath);
// Try .worker.ts and .worker.tsx variants
for (const ext of ['.worker.ts', '.worker.tsx']) {
const workerPath = libPath + ext;
if (fs.existsSync(workerPath)) {
return workerPath;
}
// Handle directory imports (e.g., @/utils/cache -> @/utils/cache/index.worker.ts)
const indexWorkerPath = path.join(libPath, 'index') + ext;
if (fs.existsSync(indexWorkerPath)) {
return indexWorkerPath;
}
}
}
return null;
},
};
}
export default defineConfig({
entry: ['./lib/worker.ts'],
outDir: 'dist-worker',
format: 'esm',
minify: true,
clean: true,
platform: 'node',
target: 'esnext',
treeshake: true,
define: {
'process.env.NODE_ENV': JSON.stringify('production'),
'process.env.VERCEL_ENV': JSON.stringify(''),
'import.meta.dirname': JSON.stringify('/worker'),
'import.meta.url': JSON.stringify('file:///worker/index.mjs'),
// CommonJS compatibility
__dirname: JSON.stringify('/worker'),
__filename: JSON.stringify('/worker/index.mjs'),
},
external: [
// Exclude non-code files that might be accidentally imported
/\/_README$/,
/\.node$/,
],
noExternal: [/.*/],
plugins: [workerAliasPlugin()],
alias: {
// External dependencies that need Worker-compatible replacements
'node:module': path.resolve('./lib/shims/node-module.ts'),
'node:child_process': path.resolve('./lib/shims/node-child-process.ts'),
'dotenv/config': path.resolve('./lib/shims/dotenv-config.ts'),
'@sentry/node': path.resolve('./lib/shims/sentry-node.ts'),
'xxhash-wasm': path.resolve('./lib/shims/xxhash-wasm.ts'),
// Routes file with Worker-specific build (match relative import from lib/)
'../assets/build/routes.js': path.resolve('./assets/build/routes-worker.js'),
// routes.json is only used in test environment, but rolldown still tries to resolve it
'../assets/build/routes.json': path.resolve('./assets/build/routes-worker.js'),
},
inlineOnly: false,
});