Files
RSSHub/tsdown-worker.config.ts
T
Tony 4cf494653c perf: replace acorn with oxc-parser (#23302)
* perf: speed up js parsing

* fix: drop parseScriptData for dongqiudi

* refactor: rename evaluate-script

* refactor: rename exports

* chore: move oxc-parser to dependencies
2026-09-15 20:49:09 +08:00

108 lines
4.2 KiB
TypeScript

import fs from 'node:fs';
import path from 'node:path';
import { defineConfig, type TsdownPlugin } from 'tsdown';
// Plugin to automatically resolve .worker.ts files instead of .ts files
function workerAliasPlugin(): TsdownPlugin {
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',
dts: false,
minify: true,
clean: true,
platform: 'node',
target: 'esnext',
treeshake: true,
copy: [{ from: 'node_modules/header-generator/data_files', to: 'dist-worker/modules' }],
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('/bundle'),
__filename: JSON.stringify('/worker/index.mjs'),
},
plugins: [workerAliasPlugin()],
alias: {
// External dependencies that need Worker-compatible replacements
'node:module': path.resolve('./lib/shims/node-module.ts'),
'dotenv/config': path.resolve('./lib/shims/dotenv-config.ts'),
'@sentry/node': path.resolve('./lib/shims/sentry-node.ts'),
'@honeybadger-io/js': path.resolve('./lib/shims/honeybadger.ts'),
typescript: path.resolve('./lib/shims/typescript.ts'),
jsdom: path.resolve('./lib/shims/jsdom.ts'),
vm2: path.resolve('./lib/shims/vm2.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'),
},
deps: {
onlyBundle: false,
neverBundle: [
// Let Wrangler select the package's static-WASM workerd export.
'xxhash-wasm',
'@oxc-parser/binding-wasm32-wasip1/wasm.wasm',
// Exclude non-code files that might be accidentally imported
/\/_README$/,
/\.node$/,
],
alwaysBundle: [/.*/],
},
});