vue-cli增加测试环境或者其他开发环境——解决混合开发传给原

2018-10-17  本文已影响0人  江南之城

1、在package.json中添加test命令

"scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "build": "node build/build.js",
    "test": "node build/test.js"
  },

2、修改config中的index的配置

// 测试环境
  testBuild: {
    // 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
  }

3、在config文件夹下添加test.env.js 内容为:

'use strict'
module.exports = {
  NODE_ENV: '"test"',
  API_HOST: '"http://127.0.0.1:3000/"'
}

API_HOST为测试环境的接口路径,可以在开发和正式环境都加上 API_HOST,然后根据 process.env.NODE_ENV 的不同的环境,进而得到不同的 process.env.API_HOST

4、在build文件夹下添加test.js ,可以直接将build文件里的build.js复制过来,只更改:

##环境变量为test
process.env.NODE_ENV = 'test'

##引入文件变为webpack.test.conf.js
const webpackConfig = require('./webpack.test.conf')

5、在build文件夹下添加webpack.test.conf.js ,可以直接将build文件里的webpack.prod.conf.js复制过来,只更改:

##引入文件变为config/test.env.js
const env = require('../config/test.env')

版权声明:本文为博主原创文章,转载请附上博文链接!

上一篇 下一篇

猜你喜欢

热点阅读