如何配置Vue-cli的生产环境和开发环境构建

4
我希望您能为生产构建和开发构建设置npm脚本,例如npm run build用于生产环境,npm run buildDev用于开发环境。每个环境文件中都有一些配置,例如ROOT_API:'"生产环境的url"'以及其他一些开发环境配置。构建将添加到dist文件夹中。我希望将生产构建添加到dist文件夹中,将开发构建添加到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个回答

1
在您的package.json文件中添加--mode development条目,如下所示:enter image description here

1
或者您可以使用命令“Yarn build --mode development”或“npm build --mode development”,无论您使用什么构建工具,如果您不想在package.json中提及。 - Manoj Verma

1
你尝试使用vue.config.js文件配置Vue构建行为了吗?你可以在vue.config.js中根据process.env.NODE_ENV指定一个outputDir
所有特定于环境的参数都将相应地设置在.env.development.env.production文件中。
当然,如果需要,你可以通过vue.config.js修改Webpack配置,这里有示例
输出目录参数更改示例在此处
最终,你的配置文件将仅依赖于环境变量和一些逻辑,例如:
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_前缀开头。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接