美文网首页
vue:根据不同环境打包到不同目录

vue:根据不同环境打包到不同目录

作者: 夙挽清歌 | 来源:发表于2020-05-28 11:33 被阅读0次

1、在 build 文件夹中创建 testing.js 文件,添加配置:

// 配置环境变量 type 为 testing

process.env.type = '"testing"'

// 引入build.js文件

require('./build')

2、修改 config 文件夹中的 prod.env.js 文件

module.exports = {

 NODE_ENV: '"production"',

 // 将上文设置的环境变量,赋值到 type 属性上

 type: process.env.type

}

3、在 package.json 文件中添加 npm run testing 命令

"testing": "node build/testing.js", // 添加testing命令

"build": "node build/build.js"

4、config -> index.js 中把 build 这个命令复制一份改成 testing (此步为了打包到不同文件夹)

build: {

  env: require('./prod.env'),

  index: path.resolve(__dirname, '../dist/index.html'),

  // Paths

  assetsRoot: path.resolve(__dirname, '../dist'),

  assetsSubDirectory: 'static',

  assetsPublicPath: '/mshop/',

  /**

   * Source Maps

   */

  productionSourceMap: true,

  devtool: '#source-map',

  productionGzip: false,

  productionGzipExtensions: ['js', 'css'],

  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,

  productionGzip: false,

  productionGzipExtensions: ['js', 'css'],

  bundleAnalyzerReport: process.env.npm_config_report

 },

5、修改 build -> webpack.prod.conf 文件

修改filename

new HtmlWebpackPlugin({

   filename: process.env.type == '"testing"'? config.testing.index : config.build.index

  }),

修改output

output: {

 path: process.env.type == '"testing"'? config.testing.assetsRoot : config.build.assetsRoot,

},

6、修改 build -> build.js 文件

rm(path.join(process.env.type == '"testing"' ? config.testing.assetsRoot : config.build.assetsRoot, process.env.type == '"testing"' ? config.testing.assetsSubDirectory : config.build.assetsSubDirectory), err => {

7、根据不同环境配置不同域名地址

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文件夹

参考链接:详解vue.js根据不同环境(正式、测试)打包到不同目录

相关文章

网友评论

      本文标题:vue:根据不同环境打包到不同目录

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