Vite React app working in dev mode but not building

I have a React app with Vite and TypeScript that works just fine in dev mode. However, if I try to build the app, I get the error:

[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)

This is the package.json and the 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: {},
  },
})

In vite.config.js I declare global because otherwise the dev mode won't work. However, it will build the app if I remove that definition, just to show a white screen. It seem that the error is caused by gl-react, but I can't figure out a solution. I tried several things, such as using @vitejs/plugin-legacy or installing pool, but none worked.

Upvotes: 1

Views: 982

Answers (1)

mcritz
mcritz

Reputation: 813

You can define window.global in a script tag in index.html like:

<script> window.global = window </script>

Upvotes: 0

Related Questions