vue.js分環境打包

一、在build文件夾中建立testing.js文件
// 配置環境變量 type 爲 testing
process.env.type = '"testing"'
// 引入build.js文件
require('./build')
二、修改config文件夾中的prod.env.js文件
module.exports = {
  NODE_ENV: '"production"',
  // 將上文設置的環境變量,賦值到 type 屬性上
  type: process.env.type
}
三、在package.json文件中添加npm run testing命令
"testing": "node build/testing.js", // 添加testing命令
"build": "node build/build.js"
四、config ->index.js中把build這個命令複製一份改爲testing(此步爲了打包到不一樣文件夾)
build: {
    env: require('./prod.env'),
    // Template for index.html
    index: path.resolve(__dirname, '../dist/index.html'),

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

    /**
     * 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
  },
  testing: {
    env: require('./prod.env'),
    index: path.resolve(__dirname, '../testing/index.html'),
    assetsRoot: path.resolve(__dirname, '../testing'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    productionSourceMap: true,
    // 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
  },
五、修改build->webpack.prod.conf文件
  1. 修改filenamecss

    new HtmlWebpackPlugin({html

    filename: process.env.type == '"testing"' ? config.testing.index : config.build.index
       }),
  2. 修改outputnode

    output: {
       path: process.env.type == '"testing"' ? config.testing.assetsRoot : config.build.assetsRoot,
     },
六、修改build->build.js文件
路徑都修改成根據正式、測試環境判斷(否則執行npm run testing, npm run build命令時輸出的文件有問題)
rm(path.join(process.env.type == '"testing"' ? config.testing.assetsRoot : config.build.assetsRoot, process.env.type == '"testing"' ? config.testing.assetsSubDirectory : config.build.assetsSubDirectory), err => {
七、根據不一樣環境配置不一樣域名地址
let baseUrl
if (process.env.NODE_ENV === 'production') {
  if (process.env.type === 'testing') { // 測試環境
    baseUrl = '測試環境地址'
  } else {                              // 正式環境
    baseUrl = '正式環境地址'
  }
} else {                               // 本地環境
  baseUrl = '本地環境地址'
}

最後執行:
    npm run testing 就會執行測試環境配置的地址,並生成testing文件夾
    npm run build就會執行正式環境配置的地址,並生成dist文件夾
相關文章
相關標籤/搜索