[踩坑试验]冲!🏃‍♂️ vuecli4升级vuecli5

2,942 阅读2分钟

背景故事

今年年初vue-cli正式更新了5.0版本,借这个机会我决定对一个两年前的老项目进行一次依赖更新,因为这个项目已经出现了一些包的版本过老,导致项目无法运行,必须锁版本。为了让项目还能再好好活几年,安享晚年我决定为它拼一把🤣。

vuejs/vue-cli 5.0.8

项目基础

EnvVersion
Windows10
Node.js16.15.1
Yarn1.22.19
Cmder1.3.13

package.json

"dependencies": {
    "axios": "^0.19.0",
    "core-js": "^3.1.2",
    "moment": "^2.24.0",
    "vue": "^2.6.10",
    "vue-router": "^3.1.2",
    "vue-svg-component-runtime": "^1.0.1",
    "vuex": "^3.1.1",
  },
  "devDependencies": {
    "@ant-design/colors": "^3.2.1",
    "@vue/cli-plugin-babel": "^4.0.4",
    "@vue/cli-plugin-eslint": "^4.0.4",
    "@vue/cli-plugin-router": "^4.0.4",
    "@vue/cli-plugin-unit-jest": "^4.0.4",
    "@vue/cli-plugin-vuex": "^4.0.4",
    "@vue/cli-service": "^4.0.4",
    "@vue/eslint-config-standard": "^4.0.0",
    "@vue/test-utils": "^1.0.0-beta.29",
    "babel-eslint": "^10.0.1",
    "babel-plugin-import": "^1.12.2",
    "babel-plugin-transform-remove-console": "^6.9.4",
    "eslint": "^5.16.0",
    "eslint-plugin-html": "^5.0.0",
    "eslint-plugin-vue": "^5.2.3",
    "less": "^3.0.4",
    "less-loader": "^5.0.0",
    "vue-svg-icon-loader": "^2.1.1",
    "vue-template-compiler": "^2.6.10",
    "webpack-theme-color-replacer": "^1.2.17"
  },

vue.config

const path = require('path')
const webpack = require('webpack')
const createThemeColorReplacerPlugin = require('./config/plugin.config')

function resolve (dir) {
  return path.join(__dirname, dir)
}

const isProd = process.env.NODE_ENV === 'production'

const assetsCDN = {
  // webpack build externals
  externals: {
    vue: 'Vue',
    'vue-router': 'VueRouter',
    vuex: 'Vuex',
    axios: 'axios'
  },
  css: [],
  js: [
    '//cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js',
    '//cdn.jsdelivr.net/npm/vue-router@3.1.3/dist/vue-router.min.js',
    '//cdn.jsdelivr.net/npm/vuex@3.1.1/dist/vuex.min.js',
    '//cdn.jsdelivr.net/npm/axios@0.19.0/dist/axios.min.js'
  ]
}

// vue.config.js
const vueConfig = {
  configureWebpack: {
    // webpack plugins
    plugins: [
      // Ignore all locale files of moment.js
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
    ],
    // if prod, add externals
    externals: isProd ? assetsCDN.externals : {}
  },

  chainWebpack: (config) => {
    config.resolve.alias
      .set('@$', resolve('src'))

    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule
      .oneOf('inline')
      .resourceQuery(/inline/)
      .use('vue-svg-icon-loader')
      .loader('vue-svg-icon-loader')
      .end()
      .end()
      .oneOf('external')
      .use('file-loader')
      .loader('file-loader')
      .options({
        name: 'assets/[name].[hash:8].[ext]'
      })

    // if prod is on
    // assets require on cdn
    if (isProd) {
      config.plugin('html').tap(args => {
        args[0].cdn = assetsCDN
        return args
      })
    }
  },

  css: {
    loaderOptions: {
      less: {
        modifyVars: {},
        // DO NOT REMOVE THIS LINE
        javascriptEnabled: true
      }
    }
  },

  devServer: {},

  // disable source map in production
  productionSourceMap: false,
  lintOnSave: undefined,
  // babel-loader no-ignore node_modules/*
  transpileDependencies: []
}

// preview.pro.loacg.com only do not use in your production;
if (process.env.VUE_APP_PREVIEW === 'true') {
  console.log('VUE_APP_PREVIEW', true)
  // add `ThemeColorReplacer` plugin to webpack plugins
  vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
}

module.exports = vueConfig

开始动手

1. 安装新版vue-cli并更新项目

npm update -g @vue/cli

# 或者
yarn global upgrade --latest @vue/cli

如果你没有装vue-cli,那么

npm install -g @vue/cli
# OR
yarn global add @vue/cli

检查版本,已经是最新版本

vue -V
@vue/cli 5.0.8

2. 安装依赖,修复问题完成升级

开始更新依赖

vue upgrade

这是用vue-cli4创建的项目,脚手架检查出了需要更新的依赖,同意更新之后,就会自动下载所需依赖.

image.png

核心的开发依赖变动主要有这些,特别是babel-eslint这个库是直接deprecated了,换成了@babel/elsint-parser,webpack也升级到了5.0版本以及一些重要的loaderplugin都得到了升级.

  "devDependencies": {
+    "@babel/core": "^7.12.16",
+    "@babel/eslint-parser": "^7.12.16",
-    "@vue/cli-plugin-babel": "^4.0.4",
+    "@vue/cli-plugin-babel": "~5.0.8",
-    "@vue/cli-plugin-eslint": "^4.0.4",
+    "@vue/cli-plugin-eslint": "~5.0.8",
-    "@vue/cli-plugin-router": "^4.0.4",
+    "@vue/cli-plugin-router": "~5.0.8",
-    "@vue/cli-plugin-unit-jest": "^4.0.4",
+    "@vue/cli-plugin-unit-jest": "~5.0.8",
-    "@vue/cli-plugin-vuex": "^4.0.4",
+    "@vue/cli-plugin-vuex": "~5.0.8",
-    "@vue/cli-service": "^4.0.4",
+    "@vue/cli-service": "~5.0.8",
-    "@vue/eslint-config-standard": "^4.0.0",
+    "@vue/eslint-config-standard": "^6.1.0",
+    "@vue/vue2-jest": "^27.0.0-alpha.3",
-    "eslint": "^5.16.0",
+    "eslint": "^7.32.0",
-    "babel-eslint": "^10.0.1",
+    "eslint-plugin-html": "^5.0.0",
+    "eslint-plugin-import": "^2.25.3",
+    "eslint-plugin-node": "^11.1.0",
+    "eslint-plugin-promise": "^5.1.0",
-    "eslint-plugin-vue": "^5.2.3",
+    "eslint-plugin-vue": "^8.0.3",
+    "jest": "^27.1.0",
  },

eslintr.js中原来的babel-eslint要替换为新的@babel/eslint-parser

parserOptions: {
-   parser: 'babel-eslint'
+   parser: '@babel/eslint-parser'
},

新版本webpack肯定会有一些api的变动,我们先来跑一下项目,看看到底需要改什么

image.png

上面写Ignore Plugin的写法已经改了,所以我们需要改一下vue.config

// webpack plugins
configureWebpack:  {
    plugins: [
     // Ignore all locale files of moment.js
-      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
+      new webpack.IgnorePlugin({resourceRegExp: /^\.\/locale$/, contextRegExp: /moment$/}),
    ],
 }

image.png 看了webpack5的升级文档,发现是因为webpack5不再自动添加node 模块的polyfill,如果我们用了就要自行处理了。

yarn add node-polyfill-webpack-plugin

修改vue.config

+ const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")

// webpack plugins
configureWebpack:  {
    plugins: [
+      new NodePolyfillPlugin(),
    ],
  },

至此,我们的项目可以顺利跑起来了。

image.png

顺便我还升级了项目基础生产依赖,感觉这个老项目又重获了一点生气,可以再战一年了😂

-    "ant-design-vue": "1.5.0-rc.6",
+    "ant-design-vue": "^1.7.6",
-    "axios": "^0.19.0",
+    "axios": ">=0.21.1",
-    "vue": "^2.6.10",
+    "vue": "^2.6.14",
-    "vue-router": "^3.1.2",
+    "vue-router": "^3.5.2",
-    "vue-template-compiler": "^2.6.10",
+    "vue-template-compiler": "^2.6.14",

vue-cli已经升级完成了,主要目的是想依靠最新的webpack模块联邦的功能,去更深层次调整项目架构,因为这个项目是基于微前端进行了改造。

下一步就是尝试模块联邦,帮助整个系统去统一化的管理公共资源,敬请期待。