我正在建立一个网站使用Vue3,打字,Vue路由和VITE.这款应用程序托管在一台apache 网络服务器上.这个应用程序构建成功,并在我的本地计算机上按预期工作.

然而,当我将构建移动到生产服务器时,当页面重新加载时,它会中断,并出现以下错误:Uncaught SyntaxError: Invalid or unexpected token,这来自Vue构建的js文件.当我查看错误所在时,它不是人类可读的.此外,当此错误发生时,网页只显示一个空白屏幕.

Package.json

{
  "name": "enigma-vue",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vue-tsc && vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "@ckeditor/ckeditor5-build-balloon": "^38.1.0",
    "@ckeditor/ckeditor5-build-balloon-block": "^38.1.0",
    "@ckeditor/ckeditor5-build-classic": "^38.1.0",
    "@ckeditor/ckeditor5-build-decoupled-document": "^38.1.0",
    "@ckeditor/ckeditor5-build-inline": "^38.1.0",
    "@fortawesome/fontawesome-svg-core": "^6.5.1",
    "@fortawesome/free-brands-svg-icons": "^6.5.1",
    "@fortawesome/free-regular-svg-icons": "^6.5.1",
    "@fortawesome/free-solid-svg-icons": "^6.5.1",
    "@fortawesome/vue-fontawesome": "^3.0.5",
    "@fullcalendar/daygrid": "^5.11.3",
    "@fullcalendar/interaction": "^5.11.3",
    "@fullcalendar/list": "^5.11.3",
    "@fullcalendar/timegrid": "^5.11.3",
    "@fullcalendar/vue3": "^5.11.2",
    "@headlessui/vue": "^1.7.13",
    "@vuelidate/core": "^2.0.0",
    "@vuelidate/validators": "^2.0.0",
    "axios": "^1.5.0",
    "chart.js": "^3.9.1",
    "dayjs": "^1.11.5",
    "dropzone": "^6.0.0-beta.2",
    "flat": "^5.0.2",
    "highlight.js": "^11.6.0",
    "js-beautify": "^1.14.6",
    "leaflet": "^1.9.3",
    "leaflet.markercluster": "^1.5.3",
    "litepicker": "^2.0.12",
    "lucide": "^0.90.0",
    "lucide-vue-next": "^0.95.0",
    "nprogress": "^0.2.0",
    "pinia": "^2.0.23",
    "pinia-plugin-persistedstate": "^3.2.0",
    "simplebar": "^5.3.9",
    "tabulator-tables": "^5.3.4",
    "tiny-slider": "^2.9.4",
    "tippy.js": "^6.3.7",
    "toastify-js": "^1.12.0",
    "tom-select": "^2.2.0",
    "vue": "^3.3.4",
    "vue-router": "^4.1.6",
    "vue3-dropzone": "^2.1.2",
    "xlsx": "^0.18.5",
    "zoom-vanilla.js": "^2.0.6"
  },
  "devDependencies": {
    "@tailwindcss/forms": "^0.5.3",
    "@types/dropzone": "^5.7.8",
    "@types/flat": "^5.0.2",
    "@types/js-beautify": "^1.13.3",
    "@types/leaflet": "^1.9.3",
    "@types/leaflet.markercluster": "^1.5.1",
    "@types/lodash": "^4.14.195",
    "@types/nprogress": "^0.2.3",
    "@types/tabulator-tables": "^5.4.8",
    "@types/toastify-js": "^1.11.1",
    "@vitejs/plugin-vue": "^4.2.3",
    "autoprefixer": "^10.4.14",
    "lodash": "^4.17.21",
    "postcss": "^8.4.25",
    "postcss-advanced-variables": "^3.0.1",
    "postcss-import": "^15.1.0",
    "postcss-nesting": "^12.0.0",
    "tailwind-merge": "^1.13.2",
    "tailwindcss": "^3.3.2",
    "typescript": "^5.1.6",
    "vite": "^4.4.2",
    "vue-tsc": "^1.8.4"
  }
}

Vite.config.ts

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from "path";

// https://vitejs.dev/config/
export default defineConfig({
  build: {
    commonjsOptions: {
      include: ["tailwind.config.js", "node_modules/**"],
    },
  },
  optimizeDeps: {
    include: ["tailwind-config"],
  },
  plugins: [vue()],
  resolve: {
    alias: {
        "tailwind-config": path.resolve(__dirname, "./tailwind.config.js"),
        "@": path.resolve(__dirname, "./src") 
      },
  },
  base: '/'
})

Index.html file generated by vite

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title></title>
    <base href="/" />
    <script type="module" crossorigin src="/assets/index-2c92196c.js"></script>
    <link rel="stylesheet" href="/assets/index-14613a76.css">
  </head>
  <body>
    <div id="app"></div>
    <div id="modal"></div>
  </body>
</html>

Image of the error code at the bottom and the blank page it produces

到目前为止,我已经try 过了:

  • 删除.htaccess文件以查看这是否会导致问题
  • 按照类似票据中的建议将<base href="/" />添加到index.html文件中
  • 更改Vue路由历史记录以查看这是否是问题所在
  • 在vite.config.ts中删除和添加基本路径选项

所有这些更改都不会对错误产生影响.

我对它进行了测试:

  • Google Chrome
  • 勇敢的
  • 边缘
  • Firefox-我得到一个略有不同的错误:未捕获语法错误:标识符紧跟在数字文字之后开始

我真的很感激任何帮助,因为我在这一个亏损.

先谢谢你

推荐答案

错误消息指向您的Java脚本文件中的(无效)代码1.toString.通过搜索这个文本和‘语法错误’,我找到了this条帖子.

在VITE首先缩小代码之后,似乎有什么东西在重新缩小代码,这导致了语法错误.在上面的帖子中,它是云耀斑.

try 禁用Cloudflare缩小,看看它是否能解决您的问题.

Typescript相关问答推荐

Typescript对每个属性具有约束的通用类型

使用前的组件渲染状态更新

Angular 17 -如何在for循环内创建一些加载?

当类型断言函数返回假时,TypScript正在推断从不类型

JS Redux Devtools扩展不工作

泛型函数即使没有提供类型

TypeScript:在作为参数传递给另一个函数的记录中对函数的参数实现类型约束

不推荐使用Marker类-Google map API警告

被推断为原始类型的交集的扩充类型的交集

记录键的泛型类型

ANGLING v17 CLI默认设置为独立:延迟加载是否仍需要@NgModule进行路由?

TYPE FOR ARRAY,其中每个泛型元素是单独的键类型对,然后在该数组上循环

从route.参数获取值.订阅提供";无法读取未定义";的属性

类型脚本-处理值或返回值的函数

如何通过属性名在两个泛型数组中找到匹配的对象?

声明遵循映射类型的接口

是否将Angular *ngFor和*ngIf迁移到新的v17语法?

使用Cypress测试从Reaction受控列表中删除项目

对于VUE3,错误XX不存在于从不存在的类型上意味着什么?

如何将元素推入对象中的类型化数组