oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.13k stars 2.67k forks source link

`vite-plugin-top-level-await` errors #9860

Open mybearworld opened 5 months ago

mybearworld commented 5 months ago

What version of Bun is running?

1.1.0+5903a6141

What platform is your computer?

Microsoft Windows NT 10.0.22631.0 x64

What steps can reproduce the bug?

When a Vite project uses vite-plugin-top-level-await, the dev server doesn't start with Bun.

// vite.config.ts
import { defineConfig } from "vite";
import topLevelAwait from "vite-plugin-top-level-await";

export default defineConfig({
  plugins: [topLevelAwait()],
});

What is the expected behavior?

The server starts correctly. This behavior is seen with bun run dev:

$ bun run dev
$ vite

  VITE v5.2.7  ready in 551 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h + enter to show help

What do you see instead?

The server doesn't start, instead giving an error:

$ bun --bun run dev
$ vite
failed to load config from .\vite.config.ts
error when starting dev server:
TypeError: virtualModule.require is not a function. (In 'virtualModule.require(wantedModuleName)', 'virtualModule.require' is undefined)
    at requireFrom (.\node_modules\vite-plugin-top-level-await\dist\esbuild.js:6:33)
    at <anonymous> (.\node_modules\vite-plugin-top-level-await\dist\esbuild.js:14:32)
    at anonymous (native)
    at <anonymous> (.\node_modules\vite-plugin-top-level-await\dist\index.js:117:44)
    at <anonymous> (:11:43)
    at processTicksAndRejections (:12:39)
error: script "dev" exited with code 1

(I've edited the error message not to include my username - instead of .\, the errors contain the absolute path to the file.)

Additional information

No response

81reap commented 2 months ago

I am getting this same issue on MacOS 14.5 and Bun 1.1.18. I have tried the following two vite.config.js but it did not work.

import { defineConfig } from 'vite';
import topLevelAwait from "vite-plugin-top-level-await";

export default defineConfig({
  plugins: [
    topLevelAwait({
      // The export name of top-level await promise for each chunk module
      promiseExportName: "__tla",
      // The function to generate import names of top-level await promise in each chunk module
      promiseImportName: i => `__tla_${i}`
    })
  ]
});
import { defineConfig } from "vite";
import topLevelAwait from "vite-plugin-top-level-await";

export default defineConfig({
  plugins: [topLevelAwait()],
});

Some other files that may help.

{
  "name": "iso-game",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "update": "bun upgrade && bun update",
    "dev": "bunx --bun vite",
    "build": "tsc && vite build && biome check .",
    "preview": "vite preview",
    "format": "biome format ."
  },
  "devDependencies": {
    "@types/bun": "latest",
    "@types/three": "^0.166.0",
    "biome": "^0.3.3",
    "typescript": "^5.5.3",
    "vite": "^5.3.3",
    "vite-plugin-top-level-await": "^1.4.1"
  },
  "peerDependencies": {
    "typescript": "^5.0.0"
  },
  "dependencies": {
    "three": "^0.166.1"
  }
}
{
  "compilerOptions": {
    "target": "ES2020",
    "useDefineForClassFields": true,
    "module": "ESNext",
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "skipLibCheck": true,

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "moduleDetection": "force",
    "noEmit": true,

    /* Linting */
    "strict": true,
    "strictNullChecks": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noFallthroughCasesInSwitch": true,
    "types": ["@types/three", "@types/bun", "three",
      "three/examples/jsm/controls/OrbitControls",
      "three/addons/capabilities/WebGPU",
      "three/addons/renderers/webgpu/WebGPURenderer",
      "three/addons/libs/stats.module"],
  },
  "include": ["src/**/*"]
}
philfreshman commented 3 weeks ago

Same issue, running on bun -v 1.1.25