bun icon indicating copy to clipboard operation
bun copied to clipboard

bunx --bun vite build error during build: [vite:css] import.meta is only valid inside modules.

Open kunl opened this issue 11 months ago • 2 comments

What version of Bun is running?

1.1.39

What platform is your computer?

Darwin 23.6.0 arm64 arm

What steps can reproduce the bug?

  1. bun create vite my-app
  2. cd my-app
  3. pnpm install && pnpm install less
  4. bunx --bun vite build

package.json

{
  "name": "my-app",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "bun:build": "bunx --bun vite build"
  },
  "devDependencies": {
    "vite": "^6.0.3"
  },
  "dependencies": {
    "less": "^4.2.1"
  }
}

app.less -> body { background-color: aliceblue;} main.js -> import './app.less'

What is the expected behavior?

Image

What do you see instead?

bun bun:build $ bunx --bun vite build vite v6.0.3 building for production... ✓ 6 modules transformed. x Build failed in 49ms error during build: [vite:css] import.meta is only valid inside modules. file: /Users/user/code/code/my-app/src/app.less at (:6) at Function (native) at new FakeWorker (/Users/user/code/code/my-app/node_modules/.pnpm/[email protected][email protected]/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:12563:20) at new WorkerWithFallback (/Users/user/code/code/my-app/node_modules/.pnpm/[email protected][email protected]/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:12602:28) at makeLessWorker (/Users/user/code/code/my-app/node_modules/.pnpm/[email protected][email protected]/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:49164:22) at (/Users/user/code/code/my-app/node_modules/.pnpm/[email protected][email protected]/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:49246:11) at process (/Users/user/code/code/my-app/node_modules/.pnpm/[email protected][email protected]/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:49241:19) at (/Users/user/code/code/my-app/node_modules/.pnpm/[email protected][email protected]/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:48168:34) at compileCSSPreprocessors (/Users/user/code/code/my-app/node_modules/.pnpm/[email protected][email protected]/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:48154:40) at (/Users/user/code/code/my-app/node_modules/.pnpm/[email protected][email protected]/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:48226:38) at processTicksAndRejections (native:7:39) error: script "bun:build" exited with code 1

Additional information

This problem does not exist in vite 5.x, it only appears after upgrading vite to 6.x

kunl avatar Dec 13 '24 01:12 kunl

I can also reproduce this issue. Running Vite with Node works fine.

PalmDevs avatar Dec 17 '24 12:12 PalmDevs

I can also reproduce this issue + 1

clare0987 avatar Dec 19 '24 06:12 clare0987

I can also reproduce this issue . Running vite works fine but bunx --bun vite fails

kydemy-fran avatar Dec 23 '24 21:12 kydemy-fran

Reproduced on bun 1.1.42 and vite 6.0.5

razor-1 avatar Dec 24 '24 16:12 razor-1

I think issues related to Vite need to be taken seriously, as many people use Bun to run their build scripts.

HodgeWen avatar Dec 26 '24 02:12 HodgeWen

No known workaround other than downgrading vite to v5. So this also limits bun for nuxt users, as nuxt 3.15 is bundled with vite 6.

kaij avatar Dec 27 '24 13:12 kaij

Repro:

rep.zip

Jarred-Sumner avatar Dec 30 '24 23:12 Jarred-Sumner

For me Vite 6 works on bun v1.1.37. I want to use the latest bun, but settled for no Bun upgrades till this is sorted out

nolios avatar Jan 05 '25 23:01 nolios

+1 Vite v6.0.7 | bun v1.1.42 Any news?

P.S. for me it does not work on v1.1.37 as nick4814 mentioned

LeftGamer1 avatar Jan 06 '25 12:01 LeftGamer1

it seems a new version of vite uses Function.prototype.toString(), then passing the result of this to new Function(). the input file gets run through the transpiler, converting require to import.meta.require, which is not valid code when run through new Function. this only happens when in an ES module.

this isnt exactly what vite is doing, but this example shows it reasonably:

function hello() {
    return typeof require('fs') === 'string' ? 'PASS' : "FAIL";
}
const newFunctionBody = `return ${hello.toString()}`;
const loadFakeModule = new Function('require', newFunctionBody)((id) => `fake require ${id}`);
console.log(loadFakeModule());

export {}; // force es module transpilation

a reasonable solution for this would be to hoist the require implementation instead of inlining it

 // @bun
+const { require } = import.meta;
 function hello() {
-  return typeof import.meta.require("fs") === "string" ? "PASS" : "FAIL";
+  return typeof require("fs") === "string" ? "PASS" : "FAIL";
 }
 const newFunctionBody = `return ${hello.toString()}`;
 const loadFakeModule = new Function("require", newFunctionBody)((id) => `fake require ${id}`);
 console.log(loadFakeModule());

 export {};

paperclover avatar Jan 07 '25 02:01 paperclover

+1 Vite v6.0.7 | bun v1.1.42 Any news?

P.S. for me it does not work on v1.1.37 as nick4814 mentioned

Maybe it is due to your Vite version? I am on [email protected] with [email protected]

Other than that, strange that it works for me on v1.1.37 but does not for you

I haven't tried with Bun v1.1.43 however, it seems that the issue might be fixed now. I will try it out tomorrow

nolios avatar Jan 12 '25 11:01 nolios