.env[mode]文件中如何添加注释

简介: .env[mode]文件中如何添加注释

前言


Vue-Cli 允许我们在项目根目录创建.env.[mode]文件来设置一些打包编译的启动参数,通过执行脚本的时候加mode参数,指定不同环境需要加载的配置文件

形如: .env.test

NODE_ENV='production'
VUE_APP_PATH='./'
VUE_APP_RUNTIME='prod'


问题


怎么在 .env.test 文件中添加注释呢?


分析


这要看vue是怎么解析 .env.test 文件的

通过启动脚本,一路找到了处理 .env.test 文件的逻辑,让我们一块看下

path:node_modules\@vue\cli-service\lib\Service.js

const dotenv = require('dotenv');
// ...
loadEnv (mode) {
    const logger = debug('vue:env')
    const basePath = path.resolve(this.context, `.env${mode ? `.${mode}` : ``}`)
    const localPath = `${basePath}.local`
    const load = envPath => {
      try {
        const env = dotenv.config({ path: envPath, debug: process.env.DEBUG })
        dotenvExpand(env)
        logger(envPath, env)
      } catch (err) {
        // only ignore error if file is not found
        if (err.toString().indexOf('ENOENT') < 0) {
          error(err)
        }
      }
    }
    load(localPath)
    load(basePath)
    // by default, NODE_ENV and BABEL_ENV are set to "development" unless mode
    // is production or test. However the value in .env files will take higher
    // priority.
    if (mode) {
      // always set NODE_ENV during tests
      // as that is necessary for tests to not be affected by each other
      const shouldForceDefaultEnv = (
        process.env.VUE_CLI_TEST &&
        !process.env.VUE_CLI_TEST_TESTING_ENV
      )
      const defaultNodeEnv = (mode === 'production' || mode === 'test')
        ? mode
        : 'development'
      if (shouldForceDefaultEnv || process.env.NODE_ENV == null) {
        process.env.NODE_ENV = defaultNodeEnv
      }
      if (shouldForceDefaultEnv || process.env.BABEL_ENV == null) {
        process.env.BABEL_ENV = defaultNodeEnv
      }
    }
  }

从上面的代码,可以得知,vue使用dotenv解析 .env.test 文件,并将环境变量从 .env[mode] 文件中加载到 process.env 环境变量中


解决(dotenv)


dotenv插件已经被 Vue-Cli 集成了

GitHub地址

README.md 中有这么一段话:


  • # marks the beginning of a comment (unless when the value is wrapped in quotes)

这就是我们要找的,在.env.[mode] 文件中,可以使用 # 进行注释


使用测试


  1. path: .env.prod 参数配置
NODE_ENV='production'
# VUE_APP_PATH='/pc/'
VUE_APP_PATH='./'
VUE_APP_RUNTIME='prod'
  1. package.json 文件脚本命令配置--mode参数
"scripts": {
    "build": "vue-cli-service build --mode prod",
  },
  1. 执行 npm run build 命令,输出如下:
NODE_ENV=== production
VUE_APP_PATH=== ./

本文到此为止,要想了解更多dotenv请自行探索,感谢观看😉

目录
相关文章
|
4天前
|
Linux iOS开发 MacOS
pnpm全局安装报错:Run “pnpm setup“ to create it automatically, or set the global-bin-dir setting, or the PN
pnpm全局安装报错:Run “pnpm setup“ to create it automatically, or set the global-bin-dir setting, or the PN
620 0
|
4天前
|
Linux Shell 开发工具
C++ 的 ini 配置文件读写/注释库 inicpp 用法 [ header-file-only ]
这是一个C++库,名为inicpp,用于读写带有注释的INI配置文件,仅包含一个hpp头文件,无需编译,支持C++11及以上版本。该库提供简单的接口,使得操作INI文件变得容易。用户可通过`git clone`从GitHub或Gitee获取库,并通过包含`inicpp.hpp`来使用`inicpp::iniReader`类。示例代码展示了读取、写入配置项以及添加注释的功能,还提供了转换为字符串、双精度和整型的函数。项目遵循MIT许可证,示例代码可在Linux环境下编译运行。
33 0
|
3天前
|
前端开发 JavaScript 开发者
ng new mystore --standalone=false 参数的含义介绍
ng new mystore --standalone=false 参数的含义介绍
12 1
|
4天前
使用 gopkg.in/ini.v1 包处理 INI 文件时,你可以使用 Section.HasKey 方法来检查某个 Section 中是否存在指定的 key
使用 gopkg.in/ini.v1 包处理 INI 文件时,你可以使用 Section.HasKey 方法来检查某个 Section 中是否存在指定的 key
|
6月前
|
JavaScript
命令行 npm config set legacy-peer-deps true 的作用
命令行 npm config set legacy-peer-deps true 的作用
88 0
关于 CMake编译出出现错误“Could not find compiler set in environment variable RC:” 的解决方法
关于 CMake编译出出现错误“Could not find compiler set in environment variable RC:” 的解决方法
关于 CMake编译出出现错误“Could not find compiler set in environment variable RC:” 的解决方法
|
Unix Linux iOS开发
FastAPI(64)- Settings and Environment Variables 配置项和环境变量(下)
FastAPI(64)- Settings and Environment Variables 配置项和环境变量(下)
478 0
FastAPI(64)- Settings and Environment Variables 配置项和环境变量(下)
对‘avformat_find_stream_info’未定义的引用、to the PKG_CONFIG_PATH environment variable
对‘avformat_find_stream_info’未定义的引用、to the PKG_CONFIG_PATH environment variable
66 0
env->FindClass()为NULL的一种解决办法
env->FindClass()为NULL的一种解决办法
85 0
|
Ruby
【Ruby on Rails问题】publish_name.rb文件中定义的变量显示没有定义NameError: uninitialized constant DB_CLASS
在rails项目中,config/initializers/publish_name.rb文件常用来定义的全局变量、全局常量。但是我们虽然在publish_name.rb文件中定义了常量,但是还是显示没有定义。来看一下解决方法。 问题描述: 在publish_name.rb文件中定义了变量DB_CLASS
100 0