vue2+webpack怎樣分環境打包

  今年有機會作了一個vue2的單頁面應用的項目,從開發到上線經歷了兩個環境。測試環境以及正式環境我都是跑npm run build。這兩個環境的變量當前是不同的,每次打包都要去改變量,感受有點小麻煩。後來參考同事的,他們項目裏面分環境跑不一樣的命令,獲得不一樣的包。例如測試環境npm run test ,正式環境 npm run build。css

  需在文件config/prod.env.js配置  html

var obj={}
const target = process.env.npm_lifecycle_event;
if (target == 'test'){
  obj={
    NODE_ENV: '"production"',
    API_SANBOX:'"http://sandbox.gw.fdc.com.cn/router/rest"',
  }
}else if(target=="pre"){
  obj={
    NODE_ENV: '"production"',
    API_SANBOX:'"http://pregw.fdc.com.cn/router/rest"'
  }
}else{
  obj={
    NODE_ENV: '"production"',
    API_SANBOX:'"http://gw.fdc.com.cn/router/rest"'
  }
}
module.exports = obj;

 

npm 提供一個npm_lifecycle_event變量,返回當前正在運行的腳本名稱,好比pretesttestposttest等等。因此,能夠利用這個變量,在同一個腳本文件裏面,爲不一樣的npm scripts命令編寫代碼。vue

在package.json裏面要添加相應的命令node

"scripts": {

    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "lint": "eslint --ext .js,.vue src",
    "test": "node build/build.js",
    "pre": "node build/build.js",
    "build": "node build/build.js"
  }

定義在config/prod.env.js裏面的變量,可在js文件裏經過process.env.XX獲取到,例如你想要拿到API_ROOT_DICT的變量可經過process.env.API_ROOT_DICT獲得webpack

生成環境中,若測試環境和預發佈環境html和靜態資源是放在一塊兒的,而線上環境是分開的。靜態資源若是放在http://static.fdc.com.cn/zt下,則須要在config>>index.js中修改build裏git

assetsPublicPath的值
assetsPublicPath:process.env.npm_lifecycle_event=='build'?'http://static.fdc.com.cn/':'/' 

 

 第二種方式 :
  將不一樣的環境變量放在不一樣的js裏 。開發環境的變量在dev.env裏。生成環境內的測試環境、預發佈、線上環境分別對應test.env.js   pre.env.js  prod.env.js裏。以測試環境爲例,
'use strict'
const merge = require('webpack-merge')
const prodEnv = require('./prod.env')

module.exports = merge(prodEnv, {
  NODE_ENV: '"production"',
  API_SANBOX:'"http://sandbox.gw.fdc.com.cn/router/rest"'
})

需配置config內的index.js github

'use strict'
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation.

const path = require('path')
const merge = require('webpack-merge')

const environment = {
  test: {
    env: require('./test.env'),
    assetsPublicPath: '/',
    productionSourceMap: false
  },
  pre: {
    env: require('./pre.env'),
    assetsPublicPath: '/',
    productionSourceMap: false
  },
  build: {
    env: require('./prod.env'),
    assetsPublicPath: 'http://static.fdc.com.cn/map/', //對應線上靜態資源目錄
    productionSourceMap: true
  }
}

module.exports = {
  dev: {

    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {},

    // Various Dev Server settings
    host: '0.0.0.0', // can be overwritten by process.env.HOST
    port: 8686, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    autoOpenBrowser: false,
    errorOverlay: true,
    notifyOnErrors: true,
    poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-

    // Use Eslint Loader?
    // If true, your code will be linted during bundling and
    // linting errors and warnings will be shown in the console.
    useEslint: true,
    // If true, eslint errors and warnings will also be shown in the error overlay
    // in the browser.
    showEslintErrorsInOverlay: false,

    /**
     * Source Maps
     */

    // https://webpack.js.org/configuration/devtool/#development
    devtool: 'cheap-module-eval-source-map',

    // If you have problems debugging vue-files in devtools,
    // set this to false - it *may* help
    // https://vue-loader.vuejs.org/en/options.html#cachebusting
    cacheBusting: true,

    cssSourceMap: true
  },

  build: merge( {
    // Template for index.html
    index: path.resolve(__dirname, '../dist/index.html'),

    // Paths
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    assetsPublicPath:'/' ,

    /**
     * Source Maps
     */

    productionSourceMap: true,
    // https://webpack.js.org/configuration/devtool/#production
    devtool: '#source-map',

    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],

    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  },environment[process.env.npm_lifecycle_event])
}

修改的地方爲web

而後將build裏的webpack,prod.config內的js修改以下npm

在package.json裏script內設置以下json

"scripts": {

    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "lint": "eslint --ext .js,.vue src",
    "test": "node build/build.js",
    "pre": "node build/build.js",
    "build": "node build/build.js"
  }
相關文章
相關標籤/搜索