资讯专栏INFORMATION COLUMN

如何配置 vue-cli 3.0 的 vue.config.js

xuweijian / 1893人阅读

摘要:那么,我们该如何去配置自己的项目了其实这一切都是因为的项目初始化,帮开发者已经解决了,甚至绝大部分情形下的配置。

vue-cli 3 英文文档
vue-cli 3 中文文档
webpack 4 plugins
webpack-chain

TLDR

vue-cli 3 与 2 版本有很大区别

vue-cli 3 的 github 仓库由原有独立的 github 仓库迁移到了 vue 项目下

vue-cli 3 的项目架构完全抛弃了 vue-cli 2 的原有架构,3 的设计更加抽象和简洁(此处后续可以详细介绍)

vue-cli 3 是基于 webpack 4 打造,vue-cli 2 还是 webapck 3

vue-cli 3 的设计原则是“0配置”

vue-cli 3 提供了 vue ui 命令,提供了可视化配置,更加人性化

由于 vue-cli 3 也学习了 rollup 的零配置思路,所以项目初始化后,没有了以前熟悉的 build 目录,也就没有了 webpack.base.config.js、webpack.dev.config.js 、webpack.prod.config.js 等配置文件。

那么,我们该如何去配置自己的项目了?

其实这一切都是因为 vue-cli 3 的项目初始化,帮开发者已经解决了 80% ,甚至绝大部分情形下的 webpack 配置。

上述功能就是由 @vue/cli-service 依赖去处理,当你打开 node_modules 目录下 @vue 中的 cli-service 看一眼,是不是找到了熟悉的感觉?

说了这么多,开发者在实际开发过程中,肯定还有需要自己去修改配置的地方,那么,该怎么做了?

这点就需要在项目根目录下手动新建一个 vue.config.js,此处参考我提供的一个基础模板:

module.exports = {
  baseUrl: process.env.NODE_ENV === "production"
    ? "//your_url"
    : "/",

  outputDir: "dist",

  assetsDir: "static",

  filenameHashing: true,

  // When building in multi-pages mode, the webpack config will contain different plugins
  // (there will be multiple instances of html-webpack-plugin and preload-webpack-plugin).
  // Make sure to run vue inspect if you are trying to modify the options for those plugins.
  pages: {
    index: {
      // entry for the pages
      entry: "src/pages/index/index.js",
      // the source template
      template: "src/pages/index/index.html",
      // output as dist/index.html
      filename: "index.html",
      // when using title option,
      // template title tag needs to be <%= htmlWebpackPlugin.options.title %>
      title: "首页",
      // chunks to include on this pages, by default includes
      // extracted common chunks and vendor chunks.
      chunks: ["chunk-vendors", "chunk-common", "index"]
    }
    // when using the entry-only string format,
    // template is inferred to be `public/subpage.html`
    // and falls back to `public/index.html` if not found.
    // Output filename is inferred to be `subpage.html`.
    // subpage: ""
  },

  // eslint-loader 是否在保存的时候检查
  lintOnSave: true,

  // 是否使用包含运行时编译器的Vue核心的构建
  runtimeCompiler: false,

  // 默认情况下 babel-loader 忽略其中的所有文件 node_modules
  transpileDependencies: [],

  // 生产环境 sourceMap
  productionSourceMap: false,

  // cors 相关 https://jakearchibald.com/2017/es-modules-in-browsers/#always-cors
  // corsUseCredentials: false,
  // webpack 配置,键值对象时会合并配置,为方法时会改写配置
  // https://cli.vuejs.org/guide/webpack.html#simple-configuration
  configureWebpack: (config) => {
  },

  // webpack 链接 API,用于生成和修改 webapck 配置
  // https://github.com/mozilla-neutrino/webpack-chain
  chainWebpack: (config) => {
    // 因为是多页面,所以取消 chunks,每个页面只对应一个多带带的 JS / CSS
    config.optimization
      .splitChunks({
        cacheGroups: {}
      });

    // "src/lib" 目录下为外部库文件,不参与 eslint 检测
    config.module
      .rule("eslint")
      .exclude
      .add("/Users/maybexia/Downloads/FE/community_built-in/src/lib")
      .end()
  },

  // 配置高于chainWebpack中关于 css loader 的配置
  css: {
    // 是否开启支持 foo.module.css 样式
    modules: false,

    // 是否使用 css 分离插件 ExtractTextPlugin,采用独立样式文件载入,不采用