美文网首页
打包环境配置

打包环境配置

作者: kalrase | 来源:发表于2019-08-23 22:48 被阅读0次

本文引自https://my.oschina.net/shuaihong/blog/1938804

第1步:安装cross-env

在项目目录下运行如下命令安装cross-env,我的ide是webstorm,要以直接在ide里的Terminal窗口中运行,也可能通过windows的CMD、Linux的Terminal定位到项目根目录运行下面的命令。

npm i --save-dev cross-env

第2步:修改各环境下的参数

在config/目录下添加test.env.js、prod.env.js。修改prod.env.js里的内容,修改后的内容如下:

'use strict'
module.exports = {
  NODE_ENV: '"production"',
  ENV_CONFIG:'"prod"',
  BASE_API: '"https://api-prod"'
}

分别对test.env.js文件内容进修修改,修改后的内容如下:

'use strict'
module.exports = {
 NODE_ENV: '"testing"',
 ENV_CONFIG:'"test"',
 BASE_API: '"https://api-test"'
}

对dev.env.js文件内容进行修改,修改后的内容如下。dev环境配制了服务代理,API_ROOT前的api是配制的代理地址。
'use strict'
var merge = require('webpack-merge')
var prodEnv = require('./prod.env')

module.exports = merge(prodEnv, {
  NODE_ENV: '"development"',
  ENV_CONFIG:'"dev"',
  BASE_API: '"https://api-dev"'
})

第3步:修改项目package.json文件

对package.json文件中的scripts内容进行个性,添加上新定义的几种环境的打包过程,里的参数与前面的调协保持一致。
"scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "build": "node build/build.js",
    "build:test": "cross-env NODE_ENV=production env_config=test node build/build.js",
    "build:prod": "cross-env NODE_ENV=production env_config=prod node build/build.js"
  },
在这里,NODE_ENV最好都设成production,因为在utils.js只做了production一种判定,亲测不会影响各环境API参数。

第4步:修改config/index.js

修改config/index.js文件中build参数,这里的参数会在build/webpackage.prod.conf.js中使用到

build:{
 // 添加test prod 三处环境的配制
 prodEnv: require('./prod.env'),
 testEnv: require('./test.env'),

index: path.resolve(__dirname, '../outer-'+process.env.env_config+'/index.html'),
assetsRoot: path.resolve(__dirname, '../outer-'+process.env.env_config),

第5步:在webpackage.prod.conf.js中使用构建环境参数

对build/webpackage.prod.conf.js文件进行修改,调整env常量的生成方式。

// 个性env常量的定义
// const env = require('../config/prod.env')
const env = config.build[process.env.env_config+'Env']

第6步:调整build/build.js

删除process.env.NODE_ENV的赋值,修改spinner的定义,调整后的内容如下:

'use strict'
require('./check-versions')()
// 注释掉的代码
// process.env.NODE_ENV = 'production'
const ora = require('ora')
const rm = require('rimraf')
const path = require('path')
const chalk = require('chalk')
const webpack = require('webpack')
const config = require('../config')
const webpackConfig = require('./webpack.prod.conf')
// 修改spinner的定义
// const spinner = ora('building for production...')
var spinner = ora('building for ' + process.env.NODE_ENV + ' of ' + process.env.env_config+ ' mode...' )
//更多的其它内容,不需要做任何调整的内容 ...

补充:如何验证你的分环境打包正确与否

1⃣️ 验证1:main.js 中,
const service = axios.create({
  baseURL: process.env.BASE_API, // api的base_url
  timeout: 5000 // request timeout
})
随便你,只要引用一下步骤二中设置的baseURL,打包之后,全局搜索baseURL,看能不能正常解析,就知道配置的正确与否,每个环境对应不同的接口地址

2⃣️验证2: 步骤四中输出的文件夹名是否正确,
image
  • 运行打包时步骤六打包提示是否正确 image

<ins class="adsbygoogle" data-ad-client="ca-pub-7090564139599510" data-ad-slot="2321828009" data-adsbygoogle-status="done" style="box-sizing: inherit; display: inline-block; width: 728px; height: 90px;"><ins id="aswift_0_expand" style="box-sizing: inherit; display: inline-table; border: none; height: 90px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 728px; background-color: transparent;"><ins id="aswift_0_anchor" style="box-sizing: inherit; display: block; border: none; height: 90px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 728px; background-color: transparent;"><iframe width="728" height="90" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&&s.handlers,h=H&&H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&&d&&(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" id="aswift_0" name="aswift_0" style="box-sizing: inherit; left: 0px; position: absolute; top: 0px; border: 0px; width: 728px; height: 90px;"></iframe></ins></ins></ins>

相关文章

  • webpack5详细配置(附带注释)

    webpack5详细配置,包含基本配置、开发环境配置、生成环境配置、打包优化。 github地址 https://...

  • springboot之加载配置文件

    springboot多环境打包配置 application.yml pom文件配置:

  • 打包环境配置

    本文引自https://my.oschina.net/shuaihong/blog/1938804 第1步:安装c...

  • 09-配置多环境打包

    如何配置多环境的打包呢?因为各个环境的接口地址也不同,所以这就引出了需要配置不同环境来进行打包。 在根目录下新建....

  • 模版使用说明

    使用说明 git clone该模版后,执行npm install 安装所需依赖 环境配置 支持多环境打包配置,配置...

  • minitouch安装和使用

    环境配置、项目打包 安装并配置 adb[https://developer.android.com/studio/...

  • maven之多环境打包的实现

    实现不同环境的打包,就是在pom.xml中定义不同的环境配置,然后将需要的配置打包入最终的压缩包中。 首先...

  • JDK 环境变量配置 - win

    我已全部打包,下载地址:链接:Flutter 所有环境打包下载提取码:tdnz JDK 下载及环境配置 jdk 官...

  • javatoexe之exe4j和innosetup打包jar

    下面对几个重要配置进行说明 exe4j APP名和输出路径配置 选择需要打包的jar 打包jre运行环境 其他配置...

  • Python-for-Android安装笔记

    目标: 在Ubuntu上配置Android打包环境 环境配置: Python使用2.7的,尽量不要用3,否则会出现...

网友评论

      本文标题:打包环境配置

      本文链接:https://www.haomeiwen.com/subject/acbxectx.html