mission4-vue模板依赖升级至最新

首先推荐一个依赖更新工具包npm-check-updates,可以快速将package中的依赖升级至最新。

升级后主要的问题是webpack 3.x 升级至 webpack 4.x 遇到的各种问题。

参考了Webpack4 新特性 及 Vue-cli项目升级中的配置,主要修改如下:

添加依赖文件

npm i webpack-cli mini-css-extract-plugin -D

需要注意的是"babel-core": "^6.26.3","babel-loader": "^7.1.5",这两个版本要正确,babel-loader@8会报错

基础配置

webpack.base.conf.js

1
2
3
4
5
6
7
const { VueLoaderPlugin } = require('vue-loader')

module.exports = {
plugins: [
new VueLoaderPlugin()
]
}

开发环境

1
2
3
4
5
6
7
8
9
const devWebpackConfig = merge(baseWebpackConfig, {
mode: 'development',
// ...
plugins: [
//这两个功能已经内置,可以注销掉
// new webpack.NamedModulesPlugin(),
// new webpack.NoEmitOnErrorsPlugin(),
]
})

生产环境

utils.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// const ExtractTextPlugin = require('extract-text-webpack-plugin')
const MiniCssExtractPlugin= require('mini-css-extract-plugin')

function generateLoaders (loader, loaderOptions) {
const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader]

if (loader) {
loaders.push({
loader: loader + '-loader',
options: Object.assign({}, loaderOptions, {
sourceMap: options.sourceMap
})
})
}

return [
options.extract ? MiniCssExtractPlugin.loader : 'vue-style-loader',
].concat(loaders)
}

webpack.prod.conf.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// const ExtractTextPlugin = require('extract-text-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
// const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

const webpackConfig = merge(baseWebpackConfig, {
mode: 'production',
// 添加新配置项
optimization: {
splitChunks: {
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
chunks: 'initial',
name: 'vendors',
},
'async-vendors': {
test: /[\\/]node_modules[\\/]/,
minChunks: 2,
chunks: 'async',
name: 'async-vendors'
}
}
},
runtimeChunk: {
name: 'runtime'
}
},
plugins: [
// 去除已经废弃的功能
//new UglifyJsPlugin({})
//new webpack.optimize.CommonsChunkPlugin()
//new ExtractTextPlugin({//已经不能用了
new MiniCssExtractPlugin({//改用这个
filename: utils.assetsPath('css/[name].[contenthash].css'),
allChunks: true,
}),
]
})

然后就可以正常项目开发、生产了。

最后,最终项目依赖配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{
"devDependencies": {
"autoprefixer": "^9.1.5",
"babel-core": "^6.26.3",
"babel-helper-vue-jsx-merge-props": "^2.0.3",
"babel-loader": "^7.1.5",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-plugin-transform-vue-jsx": "^3.5.0",
"babel-preset-env": "^1.3.2",
"babel-preset-stage-2": "^6.22.0",
"chalk": "^2.0.1",
"copy-webpack-plugin": "^4.0.1",
"css-loader": "^1.0.0",
"file-loader": "^2.0.0",
"friendly-errors-webpack-plugin": "^1.6.1",
"html-webpack-plugin": "^3.2.0",
"mini-css-extract-plugin": "^0.4.3",
"node-notifier": "^5.1.2",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"ora": "^3.0.0",
"portfinder": "^1.0.13",
"postcss-import": "^12.0.0",
"postcss-loader": "^3.0.0",
"postcss-url": "^8.0.0",
"rimraf": "^2.6.0",
"semver": "^5.3.0",
"shelljs": "^0.8.2",
"uglifyjs-webpack-plugin": "^2.0.1",
"url-loader": "^1.1.1",
"vue-loader": "^15.4.2",
"vue-style-loader": "^4.1.2",
"vue-template-compiler": "^2.5.2",
"webpack": "^4.20.2",
"webpack-bundle-analyzer": "^3.0.2",
"webpack-cli": "^3.1.1",
"webpack-dev-server": "^3.1.9",
"webpack-merge": "^4.1.0"
}
}

坚持原创技术分享,您的支持将鼓励我继续创作!