vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
66.34k stars 5.93k forks source link

Env variables are NOT statically replaced. #14782

Closed Tnze closed 9 months ago

Tnze commented 9 months ago

Describe the bug

I want to build same code to different targets: Tauri or Web. In the project, some Rust code need to be compiled to native code or wasm.

In the front-end code, to tell what target we are building, I use the Env variables. I use something like this:

// Craft.ts
if (import.meta.env.VITE_MY_TARGET == "tauri") {
    var pkgTauri = import("@tauri-apps/api/tauri")
} else if (import.meta.env.VITE_MY_TARGET == "web") {
    var pkgWasm = import("../pkg-wasm")
}

But when I set VITE_MY_TARGET=tauri, it fails to compile with:

RollupError: Could not resolve "../pkg-wasm" from "src/Craft.ts"

The ../pkg-wasm not exist when I'm building for tauri.

Also tried this code:

// Craft.ts
if (import.meta.env.VITE_MY_TARGET == "tauri") {
    var pkgTauri = import("@tauri-apps/api/tauri")
} else if (false) {
    var pkgWasm = import("../pkg-wasm")
}

Which can successfully built.

Since the document says "these env variables are statically replaced". I expect both of the codes should act as same?

Reproduction

https://github.com/Tnze/vite-issue-4782-minimal-reproducible-example

Steps to reproduce

pnpm run build

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (32) x64 AMD Ryzen 9 5950X 16-Core Processor
    Memory: 31.74 GB / 63.92 GB
  Binaries:
    Node: 18.17.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 3.2.1 - C:\Program Files\nodejs\yarn.CMD
    npm: 8.3.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (118.0.2088.61)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    @vitejs/plugin-vue: 4.4.0 => 4.4.0
    vite: 4.4.11 => 4.4.11

Used Package Manager

yarn

Logs

vite v4.4.11 building for production... transforming... ✓ 689 modules transformed. Could not resolve "../pkg-wasm" from "src/Craft.ts" file: /Users/runner/work/ffxiv-best-craft/ffxiv-best-craft/src/Craft.ts ✓ built in 13.83s error during build: RollupError: Could not resolve "../pkg-wasm" from "src/Craft.ts" at error (file:///Users/runner/work/ffxiv-best-craft/ffxiv-best-craft/node_modules/rollup/dist/es/shared/node-entry.js:2287:30) at ModuleLoader.handleInvalidResolvedId (file:///Users/runner/work/ffxiv-best-craft/ffxiv-best-craft/node_modules/rollup/dist/es/shared/node-entry.js:24860:24) at ModuleLoader.resolveDynamicImport (file:///Users/runner/work/ffxiv-best-craft/ffxiv-best-craft/node_modules/rollup/dist/es/shared/node-entry.js:24920:58) at async file:///Users/runner/work/ffxiv-best-craft/ffxiv-best-craft/node_modules/rollup/dist/es/shared/node-entry.js:24807:32 error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. Error: Process completed with exit code 1.

Validations

bluwy commented 9 months ago

Duplicate of https://github.com/vitejs/vite/issues/5676