Lee 3 hafta önce
ebeveyn
işleme
8e71df6493

+ 5 - 0
components.d.ts

@@ -9,6 +9,11 @@ export {}
 declare module 'vue' {
   export interface GlobalComponents {
     CodeViewer: typeof import('./src/components/view_file/vendors/text/CodeViewer.vue')['default']
+    ElButton: typeof import('element-plus/es')['ElButton']
+    ElIcon: typeof import('element-plus/es')['ElIcon']
+    ElOption: typeof import('element-plus/es')['ElOption']
+    ElSelect: typeof import('element-plus/es')['ElSelect']
+    ElUpload: typeof import('element-plus/es')['ElUpload']
     ImageViewer: typeof import('./src/components/view_file/vendors/image/ImageViewer.vue')['default']
     Other: typeof import('./src/components/view_file/vendors/other/index.vue')['default']
     PdfView: typeof import('./src/components/view_file/vendors/pdf/PdfView.vue')['default']

+ 1 - 1
package.json

@@ -6,7 +6,7 @@
     "scripts": {
         "dev": "vite",
         "build": "vue-tsc -b && vite build",
-        "build:lib": "vite build --mode lib && node --input-type=module -e \"import { copyFileSync } from 'node:fs'; copyFileSync('src/components/view_file/lib.d.ts','dist-lib/index.d.ts');\"",
+        "build:lib": "vite build --mode lib && node --input-type=module -e \"import { copyFileSync } from 'node:fs'; copyFileSync('src/components/view_file/lib.d.ts','dist-lib/index.d.ts'); copyFileSync('node_modules/pdfjs-dist/build/pdf.worker.mjs', 'dist-lib/pdf.worker.js');\"",
         "prepublishOnly": "npm run build:lib",
         "preview": "vite preview"
     },

+ 37 - 4
src/components/view_file/vendors/pdf/PdfView.vue

@@ -21,10 +21,42 @@
 import { ref, onMounted } from "vue"
 import * as PDF from "pdfjs-dist"
 
-PDF.GlobalWorkerOptions.workerSrc = new URL(
-  'pdfjs-dist/build/pdf.worker.mjs',
-  import.meta.url
-).toString()
+let workerBlobUrl = null
+
+const initWorker = async () => {
+  if (workerBlobUrl) return
+
+  // 尝试多个可能的 Worker 路径
+  const urls = [
+    // 1. 生产环境:相对于库文件的路径(已在 build:lib 时拷贝并重命名为 .js)
+    new URL("./pdf.worker.js", import.meta.url).toString(),
+    // 2. 开发环境:node_modules 路径
+    new URL("pdfjs-dist/build/pdf.worker.mjs", import.meta.url).toString(),
+  ]
+
+  let lastError = null
+  for (const url of urls) {
+    try {
+      // 方案:通过 Fetch 获取 worker 内容并转换为 Blob URL
+      // 这样可以绕过服务器对 .mjs 文件的 MIME 类型校验(application/octet-stream)
+      const response = await fetch(url)
+      if (!response.ok) continue
+
+      const arrayBuffer = await response.arrayBuffer()
+      const blob = new Blob([arrayBuffer], { type: "application/javascript" })
+      workerBlobUrl = URL.createObjectURL(blob)
+      PDF.GlobalWorkerOptions.workerSrc = workerBlobUrl
+      return // 成功加载则退出
+    } catch (e) {
+      lastError = e
+      continue
+    }
+  }
+
+  console.warn("All PDF worker paths failed, using fallback", lastError)
+  // 最后的保底方案
+  PDF.GlobalWorkerOptions.workerSrc = urls[urls.length - 1]
+}
 
 const props = defineProps({
   data: Array,
@@ -60,6 +92,7 @@ const scaleX = () => {
 }
 
 const loadFile = async () => {
+  await initWorker()
   pdfDoc = await PDF.getDocument(props.data).promise
   pdf_pages.value = pdfDoc.numPages
   renderPage()

+ 43 - 1
vite.config.ts

@@ -2,7 +2,7 @@
  * @Author: LiZhiWei
  * @Date: 2025-04-24 15:29:01
  * @LastEditors: LiZhiWei
- * @LastEditTime: 2025-12-30 12:19:24
+ * @LastEditTime: 2025-12-30 15:41:33
  * @Description:
  */
 import { defineConfig } from "vite"
@@ -34,11 +34,32 @@ export default defineConfig(({ mode }) => {
     },
   }
 
+  const workerConfig = {
+    format: "es" as const,
+    rollupOptions: {
+      output: {
+        entryFileNames: (chunkInfo: any) => {
+          if (chunkInfo.name.includes("pdf.worker")) {
+            return "assets/pdf.worker.js"
+          }
+          return "assets/[name]-[hash].js"
+        },
+        chunkFileNames: (chunkInfo: any) => {
+          if (chunkInfo.name.includes("pdf.worker")) {
+            return "assets/pdf.worker.js"
+          }
+          return "assets/[name]-[hash].js"
+        },
+      },
+    },
+  }
+
   if (mode === "lib") {
     return {
       plugins,
       resolve,
       publicDir: false,
+      worker: workerConfig,
       build: {
         outDir: "dist-lib",
         emptyOutDir: true,
@@ -64,5 +85,26 @@ export default defineConfig(({ mode }) => {
   return {
     plugins,
     resolve,
+    worker: workerConfig,
+    build: {
+      rollupOptions: {
+        output: {
+          chunkFileNames: (chunkInfo: any) => {
+            if (chunkInfo.name.includes("pdf.worker")) {
+              return "assets/pdf.worker.js"
+            }
+            return "assets/[name]-[hash].js"
+          },
+          entryFileNames: "assets/[name]-[hash].js",
+          assetFileNames: (assetInfo: any) => {
+            const name = assetInfo.name || ""
+            if (name.endsWith(".mjs") || name.includes("pdf.worker")) {
+              return "assets/[name]-[hash].js"
+            }
+            return "assets/[name]-[hash].[ext]"
+          },
+        },
+      },
+    },
   }
 })