欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-09 10:24:04
1、在build文件夹中创建testing.js文件 // 配置环境变量 type 为 testing process.env.type = '"testi...

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'),
  // 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
 },

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

路径都修改为根据正式、测试环境判断(不然执行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 => {

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。