Vue CLI配置参考

1,396 阅读2分钟

Vue CLI配置参考

官网

全局CLI配置

这个平时不常需要关注,有需要直接查看官网

vue.config.js

vue.config.js 是一个可选的配置文件,如果项目的 (和 package.json 同级的) 根目录中存在这个文件,那么它会被 @vue/cli-service 自动加载。你也可以使用 package.json 中的 vue 字段,但是注意这种写法需要你严格遵照 JSON 的格式来写。

这个文件应该导出一个包含了选项的对象:

// vue.config.js

/**
 * @type {import('@vue/cli-service').ProjectOptions}
 */
module.exports = {
  // 选项...
}

或者,你也可以使用 @vue/cli-service 提供的 defineConfig 帮手函数,以获得更好的类型提示: (5.x版本CLI构建的项目默认有这个文件,默认内容就默认使用了以下)

// vue.config.js
const { defineConfig } = require('@vue/cli-service')

module.exports = defineConfig({
  // 选项
})

image.png

详细options

// vue.config.js  以下中括号内容表示默认值
module.exports = {
  baseUrl: // 从3.3版本起已弃用,请使用publicPath
  publicPath: string ['/']
  outputDir: string ['dist']
  assetsDir: string ['']
  indexPath: string ['index.html']
  filenameHashing: boolean [true]
  pages: object [undefined]
  lintOnSave: boolean|'warning'|'default'|'error' ['default']
  runtimeCompiler: boolean [false]
  transpileDependencies: boolean|Array<string|RegExp> [false]
  productionSourceMap: boolean [true]
  crossorigin: string [undefined]
  integrity: boolean [false]
  configureWebpack: object|function
  chainWebpack: function
  css:{
      modules: // 从v4起已弃用, 请使requireModuleExtension
      requireModuleExtension: boolean [true]
      extract: boolean|object 生产环境下默认true,开发环境默认false
      sourceMap: boolean [false]
      loaderOptions: object [{}]
  }
  devServer: {
      host:
      port:
      https:
      proxy: string|object  // 更多的代理控制行为,可以使用一个 `path: options` 成对的对象。完整的选项可以查阅 [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware#proxycontext-config) 。
  }
  parallel: boolean [require('os').cpus().length > 1]
  pwa: object
  pluginOptions: object
}

Babel

Babel 可以通过 Babel.config.js进行配置

ESlint

ESLint 可以通过 .eslintrc 或 package.json 中的 eslintConfig 字段来配置。

更多细节可查阅 @vue/cli-plugin-eslint

TypeScript

TypeScript 可以通过 tsconfig.json 来配置。

更多细节可查阅 @vue/cli-plugin-typescript

单元测试

Jest

更多细节可查阅 @vue/cli-plugin-unit-jest

Mocha (配合 mocha-webpack)

更多细节可查阅 @vue/cli-plugin-unit-mocha

E2E测试

Cypress

更多细节可查阅 @vue/cli-plugin-e2e-cypress

Nightwatch

更多细节可查阅 @vue/cli-plugin-e2e-nightwatch