mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 20:37:40 +00:00
feat(web): register pdf.worker route manually in prod mode
This commit is contained in:
+23
-1
@@ -1,5 +1,6 @@
|
||||
import { loadEnvConfig } from '@shumai/core/src/env-loader'
|
||||
import { join } from 'node:path'
|
||||
import { readdirSync } from 'node:fs'
|
||||
import { dirname, join } from 'node:path'
|
||||
loadEnvConfig(process.cwd(), process.env.NODE_ENV !== 'production')
|
||||
|
||||
import index from '@shumai/webui/index.html'
|
||||
@@ -84,6 +85,27 @@ async function run() {
|
||||
routes[routePath] = () => new Response(Bun.file(filePath), { headers: file.headers })
|
||||
}
|
||||
|
||||
const sampleFile = bundle.files[0]?.path || bundle.index || ''
|
||||
const bundleDir = join(import.meta.dir, dirname(sampleFile))
|
||||
try {
|
||||
const dirFiles = readdirSync(bundleDir)
|
||||
const pdfWorkerFile = dirFiles.find((f) => f.startsWith('pdf.worker'))
|
||||
if (pdfWorkerFile) {
|
||||
const routePath = '/' + pdfWorkerFile
|
||||
const filePath = join(bundleDir, pdfWorkerFile)
|
||||
const bunFile = Bun.file(filePath)
|
||||
routes[routePath] = () =>
|
||||
new Response(bunFile, {
|
||||
headers: {
|
||||
'content-type': 'text/javascript;charset=utf-8',
|
||||
etag: `"${bunFile.size}-${bunFile.lastModified}"`,
|
||||
},
|
||||
})
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('Failed to register pdf.worker route:', err)
|
||||
}
|
||||
|
||||
const mainHtmlPath = join(import.meta.dir, bundle.index)
|
||||
const mainHtmlFile = Bun.file(mainHtmlPath)
|
||||
const mainHtmlHeaders = bundle.files.find((f) => f.path === bundle.index)?.headers || {
|
||||
|
||||
@@ -6,9 +6,7 @@ import React, { useCallback, useEffect, useImperativeHandle, useRef, useState }
|
||||
import { FileViewerProps, MediaController } from '../types'
|
||||
import { PdfControlBar } from './pdf-control-bar'
|
||||
|
||||
const { default: pdfworker } = await import('@/ui/public/pdf.worker.min.mjs', {
|
||||
with: { type: 'file' },
|
||||
})
|
||||
import pdfworker from '@/ui/public/pdf.worker.min.mjs' with { type: 'file' }
|
||||
|
||||
if (typeof window !== 'undefined' && pdfjsLib.GlobalWorkerOptions) {
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfworker
|
||||
|
||||
Reference in New Issue
Block a user