zoukankan      html  css  js  c++  java
  • 在vue2中使用.env环境(dotenv)

     .env文件

    const fs = require('fs')
    const path = require('path')
    
    const NODE_ENV = process.env.NODE_ENV || 'development'
    
    function resolve (dir) {
      // console.log(path.join(__dirname, dir))  //D:测试项目erperpuild.env
      return path.join(__dirname, '..', dir)   //.env文件的位置 D:测试项目erperp.env
    }
    
    const envFile = resolve('.env')
    
    // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
    var dotenvFiles = [
      `${envFile}.${NODE_ENV}.local`,
      `${envFile}.${NODE_ENV}`,
      // Don't include `.env.local` for `test` environment
      // since normally you expect tests to produce the same
      // results for everyone
      NODE_ENV !== 'test' && `${envFile}.local`,
      envFile,
    ].filter(Boolean);
    
    
    if (process.env.npm_config_test) {
      dotenvFiles = [
        `${envFile}.test.local`,
        `${envFile}.test`,
        // Don't include `.env.local` for `test` environment
        // since normally you expect tests to produce the same
        // results for everyone
        NODE_ENV !== 'test' && `${envFile}.local`,
        envFile,
      ].filter(Boolean);
    }
    
    // Load environment variables from .env* files. Suppress warnings using silent
    // if this file is missing. dotenv will never modify any environment variables
    // that have already been set.  Variable expansion is supported in .env files.
    // https://github.com/motdotla/dotenv
    // https://github.com/motdotla/dotenv-expand
    dotenvFiles.forEach(dotenvFile => {
      if (fs.existsSync(dotenvFile)) {
        require('dotenv-expand')(
          require('dotenv').config({
            path: dotenvFile,
          })
        );
      }
    });
    
    const reg = /^(VUE_APP_INFO)/
    
    const raw = Object.keys(process.env)
        .filter(key => reg.test(key))
        .reduce(
          (env, key) => {
            env[key] = process.env[key];
            // console.log( env[key] ) //http://39.108.136.247:8888/erp/
            return env;
          },{}
        );
    
    const stringified = Object.keys(raw).reduce((env, key) => {
        env[key] = JSON.stringify(raw[key]);
        // console.log( env[key] ) //"http://39.108.136.247:8888/erp/"
        return env;
      }, {})
    
    module.exports = stringified

    文件目录:

    插件中引入:

     

  • 相关阅读:
    npm registry
    JS函数addEventListener的浏览器差异性封装
    C# WinForm 异步执行耗时操作并将过程显示在界面中
    在server 2008/2003中 取消对网站的安全检查/去除添加信任网站
    SQL语句中将Datetime类型转换为字符串类型
    未在本地计算机上注册 Microsoft.Jet.OLEDB.4.0 提供程序
    当应用程序不是以 UserInteractive 模式运行时显示模式对话框或窗体是无效操作
    TFS2012常见问题及解答
    笔记《Hbase 权威指南》
    读Java 804
  • 原文地址:https://www.cnblogs.com/init00/p/12617969.html
Copyright © 2011-2022 走看看