添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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 .env.[mode].local is useless , Because it will always be bundled , even if git has ignored it ||||| .env.[mode].local 的使用场景在哪?即使git忽略了它,它也总是会被打包进去 #15393 .env.[mode].local is useless , Because it will always be bundled , even if git has ignored it ||||| .env.[mode].local 的使用场景在哪?即使git忽略了它,它也总是会被打包进去 #15393 AnnCY1 opened this issue Dec 20, 2023 · 8 comments

Description

Let's say I have two files: .env.development and .env.development.local . Even though git has ignored the.local file, when npm run build-dev, vite would still bundle .env.development.local into the dist file, which would cause the environment variables in.local to overwrite the original file, and the development environment would work, but the production environment would break. The.local file should only exist locally.
Vite also didn't provide any configuration to ignore specific files, so I had to delete the.local file, change the.env.development file during development, and then restore the environment file in production.

比如我有两个文件: .env.development .env.development.local ,即使 git已经忽略了 .local 文件,但是在 npm run build-dev时,vite仍然会把 .env.development.local 打包到dist文件中,导致 .local文件中的环境变量覆盖了原文件,开发环境正常,而生产环境出现bug。我任务 .local文件应该仅仅存在本地才对。
同时Vite也没有提供忽略特定文件的配置项,导致我必须要删掉 .local文件,然后在开发时修改 .env.development,然后在生产时将环境文件恢复原状。

Suggested solution

Alternative

No response

Additional context

No response

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guidelines .
  • Read the docs .
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
  • I think OP referred to the env vars being inlined into the final bundle. But back to the original description, I don't think it makes .local useless? It's used for env vars that you don't want to commit to git. If there's a mismatch in dev and build where you forgot to create the .local , that's an issue in the dev workflow. Closing this issue for now, but feel free to clarify if I got it wrong.

    I think OP referred to the env vars being inlined into the final bundle. But back to the original description, I don't think it makes .local useless? It's used for env vars that you don't want to commit to git. If there's a mismatch in dev and build where you forgot to create the .local , that's an issue in the dev workflow. Closing this issue for now, but feel free to clarify if I got it wrong.

    My problem was that vite bundled.env.[mode].local files as well, and I couldn't make.local files truly local only.
    My Vue3 code looks like this:

    downloadFile(
          import.meta.env.VITE_MEDIAURL + resItem.file_path,
          resItem.file_name
    

    I'm sure the variables for .env.development and .env.development are different

    vite does not build the .env.development.local module during the build phase. Can you provide a minimal reproducible repository?

    I don't think so. After my many tests, vite only uses loadEnv(mode, process.cwd()).xxx in the vete.config.ts file, not the build.local file, but like this: import.meta.env.xxx, will be put into the dist file by vite build.

    package.json likes this:

            "build-dev": "run-p type-check build-only",
            "build-pro": "run-p type-check build-only2",
            "build-only": "vite build --mode development",
            "build-only2": "vite build --mode production",
            ......
            "vue": "^3.3.4",
            "vite": "^4.4.6"
              

    Without specifying a mode, Vite will default to loading the production .env module during the build phase. Vite will then load the following .env modules accordingly(when the module exists), with later rules overriding previous ones.

    .env                # loaded in all cases
    .env.local          # loaded in all cases, ignored by git
    .env.[mode]         # only loaded in specified mode
    .env.[mode].local   # only loaded in specified mode, ignored by git
    

    related

    Without specifying a mode, Vite will default to loading the production .env module during the build phase. Vite will then load the following .env modules accordingly(when the module exists), with later rules overriding previous ones.

    .env                # loaded in all cases
    .env.local          # loaded in all cases, ignored by git
    .env.[mode]         # only loaded in specified mode
    .env.[mode].local   # only loaded in specified mode, ignored by git
    

    related

    Yes, but the problem is that .env.[mode].local has higher priority than.env.[mode]. After vite build, variables in .env.[mode].local will overwrite variables in .env.[mode]. I also couldn't exclude .env.[mode].local, so I think this file, despite its name, is not purely local and will be shipped online by vite.

    It's used for env vars that you don't want to commit to git. If there's a mismatch in dev and build where you forgot to create the .local, that's an issue in the dev workflow.

    I think @bluwy has already explained the role of .env.[mode].local.