feat: PDF自绘阅读器+抓手(空格按住/H切换/顶部按钮)+缩放适应宽度(pdf.js worker注入bundle零服务端依赖)

This commit is contained in:
Aisland
2026-08-17 22:50:14 +08:00
parent eaaef0945e
commit b86b2361e8
12 changed files with 666 additions and 46 deletions
+37 -42
View File
@@ -3,59 +3,54 @@
* - lib/index.js 服务端 ESM(cordis 插件,健康路由)
* - lib/client.js 客户端单文件 CJS(window.__ModuleLoader__.load 握手;
* react / @deepseek-ai/* 由宿主模块系统提供,保持 external)
* esbuild 优先用本包 node_modules 的平台二进制,其次复用相邻项目 vendor。
* esbuild 走 JS API;pdf.js worker 源码以字符串注入客户端 banner,
* 运行时用 Blob URL 起 module worker(不依赖服务端路由,F5 即生效)。
*/
import { execFileSync } from 'node:child_process'
import { existsSync, mkdirSync } from 'node:fs'
import { build } from 'esbuild'
import { readFileSync, mkdirSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
const here = dirname(fileURLToPath(import.meta.url))
mkdirSync(join(here, 'lib'), { recursive: true })
const esbuildCandidates = [
join(here, 'node_modules', '@esbuild', 'win32-x64', 'esbuild.exe'),
join(here, '..', '..', 'dsh-travelatlas', 'node_modules', '@esbuild', 'win32-x64', 'esbuild.exe'),
]
const esbuildBin = esbuildCandidates.find((p) => existsSync(p))
if (!esbuildBin) {
console.error('[dsh-worktable build] 找不到 esbuild.exe:请先在 01_content 运行 npm install')
process.exit(1)
}
console.log('[dsh-worktable build] esbuild: ' + esbuildBin)
// pdf.js 工作线程源码(legacy ESM 构建)注入客户端全局,避免任何服务端依赖
const pdfWorkerSrc = readFileSync(
join(here, 'node_modules', 'pdfjs-dist', 'legacy', 'build', 'pdf.worker.min.mjs'),
'utf8',
)
const clientBanner = {
js: "window.__ModuleLoader__.load({ id: 'dsh-worktable', factory: (require) => { var module = { exports: {} }; var exports = module.exports;",
js: "var __WT_PDF_WORKER__ = " + JSON.stringify(pdfWorkerSrc) + ";\n" +
"window.__ModuleLoader__.load({ id: 'dsh-worktable', factory: (require) => { var module = { exports: {} }; var exports = module.exports;",
}
const clientFooter = { js: 'return module.exports; } });' }
const jobs = [
{
entryPoints: [join(here, 'src/index.ts')], outfile: 'lib/index.js',
platform: 'node', format: 'esm', target: ['node22'],
external: ['@deepseek-ai/*', 'node:*', 'ws', 'node-pty'],
},
{
entryPoints: [join(here, 'src/client/index.tsx')], outfile: 'lib/client.js',
platform: 'browser', format: 'cjs', target: ['es2022'], jsx: 'automatic',
external: ['@deepseek-ai/*', 'react', 'react-dom', 'react/jsx-runtime', 'react/jsx-dev-runtime', 'scheduler'],
banner: clientBanner, footer: clientFooter,
},
]
await build({
entryPoints: [join(here, 'src/index.ts')],
outfile: 'lib/index.js',
bundle: true,
sourcemap: true,
logLevel: 'info',
platform: 'node',
format: 'esm',
target: ['node22'],
external: ['@deepseek-ai/*', 'node:*', 'ws', 'node-pty'],
})
await build({
entryPoints: [join(here, 'src/client/index.tsx')],
outfile: 'lib/client.js',
bundle: true,
sourcemap: true,
logLevel: 'info',
platform: 'browser',
format: 'cjs',
target: ['es2022'],
jsx: 'automatic',
external: ['@deepseek-ai/*', 'react', 'react-dom', 'react/jsx-runtime', 'react/jsx-dev-runtime', 'scheduler'],
banner: clientBanner,
footer: clientFooter,
})
for (const job of jobs) {
const args = [
...job.entryPoints,
'--bundle', '--sourcemap', '--log-level=info',
'--platform=' + job.platform,
'--format=' + job.format,
'--target=' + job.target.join(','),
'--outfile=' + join(here, job.outfile),
...job.external.map((e) => '--external:' + e),
]
if (job.jsx) args.push('--jsx=' + job.jsx)
if (job.banner) args.push('--banner:js=' + job.banner.js)
if (job.footer) args.push('--footer:js=' + job.footer.js)
execFileSync(esbuildBin, args, { stdio: 'inherit' })
}
console.log('[dsh-worktable build] done: lib/index.js, lib/client.js')