本文仅简单记录下基于vue-webpack模板升级到webpack4的过程

快速部署

  Vue CLI 的包名称由 vue-cli 改成了 @vue/cli

# 全局安装 vue-cli
$ npm install --global @vue/cli
# 创建一个基于 webpack 模板的新项目
$ vue init webpack my-project

升级

  升级的操作很简单,先删除,再安装即可(淘宝镜像)。但要注意webpack4版本中 webpack 与 webpack-cli 已分离为两个模块,需要都安装,建议本地安装即可。

  1. 升级webpack主要部件(PS:node版本 >= 8.9.4)

    npm uninstall -D webpack webpack-bundle-analyzer webpack-dev-server webpack-merge
    npm install -D webpack webpack-cli webpack-bundle-analyzer webpack-dev-server webpack-merge
  2. 升级webpack相关插件

    npm uninstall -D copy-webpack-plugin css-loader eslint-loader file-loader html-webpack-plugin url-loader  friendly-errors-webpack-plugin optimize-css-assets-webpack-plugin uglifyjs-webpack-plugin
    npm install -D copy-webpack-plugin css-loader eslint-loader file-loader html-webpack-plugin url-loader friendly-errors-webpack-plugin optimize-css-assets-webpack-plugin uglifyjs-webpack-plugin
  3. 升级vue-loader,替换webpack相关插件

    npm uninstall -D vue-loader extract-text-webpack-plugin
    npm install -D vue-loader mini-css-extract-plugin

配置

  下面的配置文件都是在build文件下:

  1. utils.js

    // const ExtractTextPlugin = require('extract-text-webpack-plugin')
    const MiniCssExtractPlugin = require('mini-css-extract-plugin')
    ...
    if (options.extract) {
    // return ExtractTextPlugin.extract({
    // use: loaders,
    // fallback: 'vue-style-loader'
    // })
    // MiniCssExtractPlugin.loader,
    return [MiniCssExtractPlugin.loader].concat(loaders)
    } else {
    return ['vue-style-loader'].concat(loaders)
    }
  2. webpack.base.conf.js
    const { VueLoaderPlugin } = require('vue-loader')
    ...
    plugins: [
    // vue-loader was used without the corresponding plugin. Make sure to include VueLoaderPlugin in your webpack config.
    new VueLoaderPlugin()
    ],
    // webpack4新增了一个mode配置选择,用来表示配置模式的选择情况
    module.exports = {
    mode: process.env.NODE_ENV,
    ...
  3. webpack.prop.conf.js
    // 将ExtractTextPlugin替换为MiniCssExtraPlugin
    const MiniCssExtractPlugin = require("mini-css-extract-plugin")
    // const ExtractTextPlugin = require('extract-text-webpack-plugin') ...
    // new ExtractTextPlugin({
    new MiniCssExtractPlugin({
    filename: utils.assetsPath('css/[name].[contenthash].css'),
    allChunks: true,
    }),
    ...
    // 删除UglifyJsPlugin配置项
    const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
    ...
    new UglifyJsPlugin({
    uglifyOptions: {
    compress: {
    warnings: false
    }
    },
    sourceMap: config.build.productionSourceMap,
    parallel: true
    })
    // 删除CommonsChunkPlugin配置项 && 添加optimization配置项
    // new webpack.optimize.CommonsChunkPlugin({
    // name: 'vendor',
    // minChunks (module) {
    // return (
    // module.resource &&
    // /\.js$/.test(module.resource) &&
    // module.resource.indexOf(
    // path.join(__dirname, '../node_modules')
    // ) === 0
    // )
    // }
    // }),
    // new webpack.optimize.CommonsChunkPlugin({
    // name: 'manifest',
    // minChunks: Infinity
    // }),
    // new webpack.optimize.CommonsChunkPlugin({
    // name: 'app',
    // async: 'vendor-async',
    // children: true,
    // minChunks: 3
    // }), // 与plugins同级
    optimization: {
    splitChunks: {
    chunks: 'async',
    minSize: 30000,
    minChunks: 1,
    maxAsyncRequests: 5,
    maxInitialRequests: 3,
    automaticNameDelimiter: '~',
    name: true,
    cacheGroups: {
    vendors: {
    test: /[\\/]node_modules[\\/]/,
    priority: -10
    },
    default: {
    minChunks: 2,
    priority: -20,
    reuseExistingChunk: true
    }
    }
    },
    runtimeChunk: { name: 'runtime' }
    },

总结

  demo地址:vue-webpack4

参考地址:

  https://cli.vuejs.org

  https://segmentfault.com/a/1190000014516899

  https://www.cnblogs.com/xiaohuochai/archive/2018/06/19/9196906.html

最新文章

  1. RMS:不能对生产服务器使用测试清单
  2. Atitit 软件开发中 瓦哈比派的核心含义以及修行方法以及对我们生活与工作中的指导意义
  3. The Genymotion Virtual device could not obtain an IP address解决办法
  4. loadView加载(变换成ScrollView)
  5. mongodb unclean shutdown 修复方法
  6. ExtJs之Ext.util.Format
  7. Android 70道面试题汇总
  8. Tomcat - java.lang.UnsupportedClassVersionError:Unsupported major.minor version 51.0 (unable to load class com.microsoft.sqlserver.jdbc.SQLS
  9. EAFP和LBYL 两种防御性编程风格
  10. 基于XMPP实现的Openfire的配置安装+Android客户端的实现[转]
  11. UVa 129 Krypton Factor困难的串 (dfs 递归搜索)
  12. linux的df命令
  13. 探讨 : Host在IIS上的WCF Service的执行方式
  14. java 静态导入 小结
  15. Ubuntu16.04下安装Hive
  16. 16个必须熟悉的linux服务器监控命令
  17. python 基础之注释变量常量
  18. OSI七层协议模型、TCP/IP四层模型和五层协议体系结构之间的关系
  19. strip函数
  20. Cocos2d-js 3.0 颜色变换(调整sprite/图片的色调)

热门文章

  1. Unable to connect to a repository at URL
  2. JavaScript对象之get/set方法
  3. 阿里云ECS服务器CentOS7配置vsftps及其问题解决
  4. Tomcat 端口配置,及原理详解
  5. windows_环境变量
  6. [USACO08JAN]Haybale Guessing(LuoguP2898)
  7. .net core 2.2 使用imagemagick 将pdf转化为png
  8. 5G手机来了,但这些问题让其短期内难以成为“香饽饽”
  9. HTMLElement.offsetParent(offsetLeft和offsetTop参照offsetParent的内边距边界)
  10. 在springboot项目中引入quartz任务调度器。