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

    文件目录:

    插件中引入:

     

  • 相关阅读:
    MAC OS 快捷键一览
    JavaScript检测实例属性, 原型属性
    jQuery $.each用法
    双击和单击事件冲突解决方法
    移动WEB前端开发资源整合
    纯CSS3实现自定义涂鸦风格的边框
    jquery如何阻止子元素相应mouseout事件
    jquery键盘事件全记录
    javascript类型系统之基本数据类型与包装类型
    经验分享:CSS浮动(float,clear)通俗讲解
  • 原文地址:https://www.cnblogs.com/init00/p/12617969.html
Copyright © 2011-2022 走看看