如何配置生产构建和开发构建vue-cli

时间:2019-06-20 14:05:11

标签: vue.js npm webpack vue-cli

我想为生产构建设置一个npm脚本,为开发构建设置一个。例如npm运行build进行生产,npm run buildDev进行开发。 我在每个环境文件中都有一些配置,例如:

ROOT_API:““产品的网址””和开发环境中的其他内容。

该版本将添加到dist文件夹中。 我希望将Production Build添加到dist文件夹,并将Development Build添加到distDev文件夹。

我尝试制作build.js文件的副本,但是没有运气。

config / index.js:

'use strict'

// see http://vuejs-templates.github.io/webpack for documentation.
const path = require('path')

module.exports = {
  build: {
  env: require('./prod.env'),
  index: path.resolve(__dirname, '../dist/index.html'),
  assetsRoot: path.resolve(__dirname, '../dist'),
  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
 },
 dev: {
  env: require('./dev.env'),
  port: 8080,
  autoOpenBrowser: true,
  assetsSubDirectory: 'static',
  assetsPublicPath: '/',
  proxyTable: {},
  // CSS Sourcemaps off by default because relative paths are 
  "buggy"
  // with this option, according to the CSS-Loader README
  // (https://github.com/webpack/css-loader#sourcemaps)
  // In our experience, they generally work as expected,
  // just be aware of this issue when enabling this option.
  cssSourceMap: false
 }
}

config / prod.env.js

module.exports = {
  NODE_ENV: '"production"',
  ROOT_API: '"url for production"'
}

config / dev.env.js

'use strict'

 const merge = require('webpack-merge')
 const prodEnv = require('./prod.env')

 module.exports = merge(prodEnv, {
   NODE_ENV: '"development"',
   ROOT_API: '"url for development"'
 })

build / build.js

'use strict'

require('./check-versions')()

process.env.NODE_ENV = 'production'

const ora = require('ora')
const rm = require('rimraf')
const path = require('path')
const chalk = require('chalk')
const webpack = require('webpack')
const config = require('../config')
const webpackConfig = require('./webpack.prod.conf')

const spinner = ora('building for production...')
spinner.start()

rm(path.join(config.build.assetsRoot, 
config.build.assetsSubDirectory), err => {
if (err) throw err
  webpack(webpackConfig, function (err, stats) {
  spinner.stop()
  if (err) throw err
  process.stdout.write(stats.toString({
    colors: true,
    modules: false,
    children: false,
    chunks: false,
    chunkModules: false
  }) + '\n\n')

  console.log(chalk.cyan('  Build complete.\n'))
  console.log(chalk.yellow(
    '  Tip: built files are meant to be served over an HTTP 
    server.\n' +
    '  Opening index.html over file:// won\'t work.\n'
   ))
 })
})

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

您不是尝试使用vue.config.js文件来配置Vue构建行为吗? 您可以根据outputDir上的process.env.NODE_ENV指定一个vue.config.js

将在.env.development.env.production文件中相应地设置所有特定于环境的参数。

当然,您可以根据需要通过vue.config.js examples here来修改Webpack配置。

输出目录参数更改示例is here

最后,您的配置文件将仅取决于环境变量以及某些逻辑,例如:

module.exports = {
  NODE_ENV: process.env.NODE_ENV,
  ROOT_API: process.env.VUE_APP_ROOT_API_URL,
  ANY_PARAM: process.env.VUE_APP_ANY_DOT_ENV_PARAM,
}

但是请记住,如果在模板上使用它们,则自定义.env参数应以VUE_APP_前缀开头。

答案 1 :(得分:0)

在package.json文件中添加--mode开发条目,例如:enter image description here