Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

environment variable substitution error #10491

Closed
7 tasks done
sharh opened this issue Oct 17, 2022 · 1 comment
Closed
7 tasks done

environment variable substitution error #10491

sharh opened this issue Oct 17, 2022 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@sharh
Copy link

sharh commented Oct 17, 2022

Describe the bug

.env file:

VITE_BUILD_ENV=production

config.ts

const config = {
  development: {
  //...
  },
 test: {
  //...
  },
 production: {
 //...
 }
}
const ENV = import.meta.env.VITE_BUILD_ENV || "production"
console.log("import.meta.env.VITE_BUILD_ENV", ENV)
export default config[import.meta.env.VITE_BUILD_ENV] ;

when run vue-tsc --noEmit && vite build --mode production,the result is:

image

It is worked very good when use vite --host in develop mode;

when I look up then build asset, it is like this:

console.log(""production"", ENV)

However in webpack, use webpack.DefinePlugin :

new webpack.DefinePlugin({
  'process.env.VITE_BUILD_ENV': JSON.stringify("production")
});

The result is:

console.log('process.env.VITE_BUILD_ENV ', "production")

Reproduction

https://stackblitz.com/edit/vitejs-vite-yvjegt?file=dist/assets/index.60209a92.js

Steps to reproduce

Run npm install

Run npm run build

System Info

System:
    OS: Windows 10 10.0.19041
    CPU: (4) x64 Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz
    Memory: 3.79 GB / 15.95 GB
  Binaries:
    Node: 14.20.0 - D:\Program Files\nodejs\node.EXE
    Yarn: 1.22.10 - D:\Nodejs\npm_global\yarn.CMD
    npm: 7.24.0 - D:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (106.0.1370.42)
    Internet Explorer: 11.0.19041.1202

Used Package Manager

npm

Logs

Click to expand!
Unexpected token (Note that you need plugins to import files that are not JavaScript)
file: E:/project-dir/src/config/index.ts:46:42
44: };
45: const ENV = import.meta.env.VITE_BUILD_ENV || "production"
46: console.log("import.meta.env.VITE_BUILD_ENV", ENV)
                                              ^
47: export default config[ENV as "production" || "production"];
error during build:
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
    at error (file:///E:/project-dir/node_modules/rollup/dist/es/shared/rollup.js:1858:30)
    at Module.error (file:///E:/project-dir/node_modules/rollup/dist/es/shared/rollup.js:12429:16)
    at Module.tryParse (file:///E:/project-dir/node_modules/rollup/dist/es/shared/rollup.js:12806:25)       
    at Module.setSource (file:///E:/project-dir/node_modules/rollup/dist/es/shared/rollup.js:12711:24)      
    at ModuleLoader.addModuleSource (file:///E:/project-dir/node_modules/rollup/dist/es/shared/rollup.js:22153:20)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

Validations

@sapphi-red
Copy link
Member

Duplicate of #3304

@sapphi-red sapphi-red marked this as a duplicate of #3304 Oct 17, 2022
@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Oct 17, 2022
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed pending triage labels Oct 17, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Nov 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants