我有一个带有 Vite 和 TypeScript 的 React 应用程序,它在开发模式下工作得很好。但是,如果我尝试构建应用程序,则会收到错误消息:
[commonjs--resolver] Unexpected token (9:4) in C:/···/node_modules/typedarray-pool/pool.js
file: C:/···/node_modules/gl-react/lib/createSurface.js:9:4
7: //Legacy pool support
8: if(!global.__TYPEDARRAY_POOL) {
9: global.__TYPEDARRAY_POOL = {
^
11: , UINT16 : dup([32, 0])
error during build:
SyntaxError: Unexpected token (9:4)
这是
package.json
和vite.config.js
:
// package.json
{
"name": "3d-visualizer",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build ",
"preview": "vite preview"
},
"dependencies": {
"@nextui-org/react": "^1.0.0-beta.12",
"@types/jest": "^29.5.1",
"@types/node": "^18.16.0",
"gl-react": "^5.2.0",
"gl-react-dom": "^5.2.1",
"nanoid": "^4.0.1",
"react": "^18.2.0",
"react-contexify": "^6.0.0",
"react-contextmenu": "^2.14.0",
"react-device-detect": "^2.2.3",
"react-dom": "^18.2.0",
"react-icons": "^4.8.0",
"react-range": "^1.8.14",
"react-scroll-wheel-handler": "^2.2.0",
"react-useanimations": "^2.10.0",
"reactflow": "^11.6.1",
"twind": "^0.16.19",
"typescript": "^4.9.5",
"zustand": "^4.3.6"
},
"devDependencies": {
"@types/react": "^18.0.38",
"@types/react-dom": "^18.0.11",
"@vitejs/plugin-react": "^3.1.0",
"vite": "^4.2.0"
}
}
// vite.config.js
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
base: './',
define: {
global: {},
},
})
在
vite.config.js
我声明global
因为否则开发模式将无法工作。但是,如果我删除该定义,它将构建应用程序,只是为了显示白屏。好像是gl-react
导致的错误,但是我想不出解决办法。我尝试了几种方法,例如使用 @vitejs/plugin-legacy
或安装 pool
,但都没有用。
您可以在 index.html 的脚本标签中定义 window.global,例如:
<script> window.global = window </script>