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

import.meta.env.MODE=production while import.meta.env.PROD returned false #6302

Closed
7 tasks done
RainKolwa opened this issue Dec 29, 2021 · 8 comments · Fixed by #6303 or #6304
Closed
7 tasks done

import.meta.env.MODE=production while import.meta.env.PROD returned false #6302

RainKolwa opened this issue Dec 29, 2021 · 8 comments · Fixed by #6303 or #6304

Comments

@RainKolwa
Copy link
Contributor

RainKolwa commented Dec 29, 2021

Describe the bug

When I set NODE_ENV=development in .env file, and set NODE_ENV=production in .env.production file, then run vite build with production mode(by default), the import.meta.env.PROD variable turned out to be false, which is expected to be true.

Screen Shot 2021-12-29 at 11 04 38 PM

I found that at vite/packages/vite/src/node/config.ts, there is a loadEnv function, in which process.env.VITE_USER_NODE_ENV would be override by .env file

// only keys that start with prefix are exposed to client
for (const [key, value] of Object.entries(parsed)) {
if (
prefixes.some((prefix) => key.startsWith(prefix)) &&
env[key] === undefined
) {
env[key] = value
} else if (key === 'NODE_ENV') {
// NODE_ENV override in .env file
process.env.VITE_USER_NODE_ENV = value
}
}

const isProduction = (process.env.VITE_USER_NODE_ENV || mode) === 'production'

when vite build, isProduction would be false.

In this case, I think maybe change the condition below may work:

// change this
if (key === 'NODE_ENV') { xxx }
// to
if (key === 'NODE_ENV' && process.env.VITE_USER_NODE_ENV === undefined) { xxx }

Reproduction

https://github.com/RainKolwa/reproduce-vite-env

System Info

System:
    OS: macOS 12.1
    CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
    Memory: 115.40 MB / 8.00 GB
    Shell: 3.1.2 - /usr/local/bin/fish
  Binaries:
    Node: 14.17.5 - ~/.nvm/versions/node/v14.17.5/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 6.14.14 - ~/.nvm/versions/node/v14.17.5/bin/npm
  Browsers:
    Chrome: 96.0.4664.110
    Firefox: 92.0
    Safari: 15.2
  npmPackages:
    @vitejs/plugin-vue: ^2.0.0 => 2.0.1
    vite: ^2.7.2 => 2.7.9

Used Package Manager

pnpm

Logs

vite:config bundled config file loaded in 64.31ms +0ms
[dotenv][DEBUG] did not match key and value when parsing line 2:
  vite:config using resolved config: {
  vite:config   plugins: [
  vite:config     'alias',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-script-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:vue',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:watch-package-data',
  vite:config     'vite:build-html',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'rollup-plugin-dynamic-import-variables',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   server: { open: true, fs: { strict: true, allow: [Array], deny: [Array] } },
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: '/Users/rainkolwa/Personal/reproduce-vite-env/dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     cssTarget: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {
  vite:config       input: '/Users/rainkolwa/Personal/reproduce-vite-env/index.html'
  vite:config     },
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] }
  vite:config   },
  vite:config   define: { __VUE_OPTIONS_API__: true, __VUE_PROD_DEVTOOLS__: false },
  vite:config   ssr: { external: [ 'vue', '@vue/server-renderer' ] },
  vite:config   configFile: '/Users/rainkolwa/Personal/reproduce-vite-env/vite.config.js',
  vite:config   configFileDependencies: [ 'vite.config.js' ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     build: {}
  vite:config   },
  vite:config   root: '/Users/rainkolwa/Personal/reproduce-vite-env',
  vite:config   base: '/',
  vite:config   resolve: { dedupe: undefined, alias: [ [Object], [Object] ] },
  vite:config   publicDir: '/Users/rainkolwa/Personal/reproduce-vite-env/public',
  vite:config   cacheDir: '/Users/rainkolwa/Personal/reproduce-vite-env/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isProduction: false,
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: true,
  vite:config     proxy: undefined,
  vite:config     cors: undefined
  vite:config   },
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: true, PROD: false },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   packageCache: Map(0) { set: [Function (anonymous)] },
  vite:config   createResolver: [Function: createResolver],
  vite:config   optimizeDeps: {
  vite:config     esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
  vite:config   }
  vite:config } +186ms
vite v2.7.9 building for production...
✓ 9 modules transformed.
dist/index.html                  0.38 KiB
dist/assets/index.b5862e96.js    1.17 KiB / gzip: 0.66 KiB
dist/assets/vendor.af2b58ca.js   49.45 KiB / gzip: 19.96 KiB

Validations

@poyoho
Copy link
Member

poyoho commented Dec 29, 2021

so .env had high priority ?

@RainKolwa
Copy link
Contributor Author

RainKolwa commented Dec 29, 2021

yes, which is not expected

RainKolwa added a commit to RainKolwa/vite that referenced this issue Dec 29, 2021
RainKolwa added a commit to RainKolwa/vite that referenced this issue Dec 29, 2021
@poyoho
Copy link
Member

poyoho commented Dec 29, 2021

Is adding documents better than modifying logic?😀

@RainKolwa
Copy link
Contributor Author

Is adding documents better than modifying logic?😀

I'm afraid not.It is common practice to use same key name in different env files.The problem here is thatNODE_ENV is specially treated.

@RainKolwa RainKolwa changed the title import.meta.env.PROD returned false when NODE_ENV=development is set in .env import.meta.env.MODE=production while import.meta.env.PROD returned false Dec 29, 2021
@poyoho
Copy link
Member

poyoho commented Dec 29, 2021

expect .env had low priority ? I think just exec envFiles.reverse()

but .env had high priority, I think maybe meet to vite's expectations 😂

@bluwy
Copy link
Member

bluwy commented Dec 30, 2021

I think .env should have a lower priority, and should be fixed. It makes sense for scoped .env to overrides the unscoped env files.

EDIT: From the code, looks like it was intentional. I think we should document instead 🤔

@bluwy
Copy link
Member

bluwy commented Dec 31, 2021

Note: The .env priority is indeed correct, just that NODE_ENV priority wasn't handled specifically.

@RainKolwa
Copy link
Contributor Author

RainKolwa commented Dec 31, 2021

I think we also need this PR to make sure NODE_ENV is handled correctly.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants