前面咱们说了webpack的一些基础,如今咱们来使用webpack实际来编译写个项目。
用vue-cli建立一个项目,而后把它的vue-cli-service以及webpack等黑盒工具移除,而后咱们来本身编译它。
首先咱们要建立三个文件css
首先咱们来编写webpack.common文件html
const HtmlWebpackPlugin = require('html-webpack-plugin')// html模板 const webpack = require('webpack') const PreloadWebpackPlugin = require('preload-webpack-plugin')// 预加载 const path = require('path') const os = require('os') const VueLoaderPlugin = require('vue-loader/lib/plugin')// vue对应插件 const workers = { // 多线程编译 loader: 'thread-loader', options: { workers: os.cpus().length } } module.exports = { // entry: ['babel-polyfill', './src/main.js'], entry: './src/main.js', // 入口 output: { filename: 'bundle.js' // 输出 }, optimization: { concatenateModules: true, // 尽量合并模块到一个函数 splitChunks: { // 公共代码拆分 // include all types of chunks chunks: 'all' }, runtimeChunk: true // 运行时代码 拆出来 缓存 }, resolve: { // modules: [//解析时搜索得模块,配了致使ie报错 // path.resolve(__dirname, 'node_modules'), // ], alias: { // 配置别名 components: path.resolve(__dirname, 'src/components'), src: path.resolve(__dirname, 'src') }, extensions: ['.js', '.json', '.vue'] // 省略后缀 }, module: { rules: [ { // babel test: /\.js$/, exclude: /node_modules/, use: [ workers, { loader: 'babel-loader', options: { // babel-polyfill按需引入,差了1m左右 core polyfill原型 presets: [['@babel/preset-env', { useBuiltIns: 'usage', corejs: 3 }]], cacheDirectory: true, // 开启缓存 第二次构建时,会读取以前的缓存,吃内存,不开发了记得清下占用 // presets: ['@babel/preset-env'] plugins: [ // import()语法支持 '@babel/plugin-syntax-dynamic-import' ] } } ] }, { test: /\.(gif|png|jpe?g|svg)$/i, // 不超过10kb转为data:urlbase64 use: [ { loader: 'url-loader', options: { limit: 10 * 1024, // 10 KB name: 'image/[name]-[contenthash:8].[ext]', // 输出hash esModule: false } }] }, { test: /\.vue$/, // 解析vue文件 use: [ workers, 'vue-loader' ] } ] }, plugins: [ new PreloadWebpackPlugin({ // 加入预解析和预加载webpack4以后下载preload-webpack-plugin@next最新版 rel: 'preload', as (entry) { console.log(entry) if (/\.css$/.test(entry)) return 'style' if (/\.woff$/.test(entry)) return 'font' if (/\.(png|jpe?g|gif)$/.test(entry)) return 'image' return 'script' }, include: 'allChunks', fileBlacklist: [/\.(map)$/, /runtime~.+\.js$/] // 把runtime文件抽离出来,由于import()运行时文件变化了,runtime管理运行时babel导入文件的hash也会变化, // 默认webpack打包出来的依赖入口文件会把runtime导入,这样会致使它的hash也会变化,这样就会致使缓存失效, // 因此把runtime这个文件加载到html中,从以来入口文件中抽离,来避免依赖入口的hash变化。这样它就不须要进行预加载了。 }), new HtmlWebpackPlugin({ // html模板 title: 'Vue dev App', template: path.resolve(__dirname, 'public/index.html') }), new VueLoaderPlugin(), new webpack.DefinePlugin({ // 值要求的是一个代码片断 BASE_URL: '"/"' }) ] }
代码里都有注释,而后咱们作下思路分析:vue
)java
而后再配置开发阶段配置webpack.devnode
const { merge } = require('webpack-merge')// 合并配置插件 const common = require('./webpack.common')// 公共配置 const path = require('path') const os = require('os') const webpack = require('webpack') const workers = { // 多线程编译 loader: 'thread-loader', options: { workers: os.cpus().length } } module.exports = merge(common, { mode: 'development', devtool: 'eval-cheap-module-source-map', // devtool:'source-map', devServer: { hot: true, contentBase: path.join(__dirname, 'public'), open: true, stats: 'errors-only', clientLogLevel: 'none', disableHostCheck: true }, module: { rules: [ { test: /\.(js|vue)$/, // eslint enforce: 'pre', exclude: /node_modules/, include: path.resolve(__dirname, 'src'), loader: [workers, 'eslint-loader'] }, { test: /\.less$/, // 解析引入得less use: [ workers, 'style-loader', 'css-loader', 'postcss-loader', 'less-loader' ] }, { test: /\.css$/, // 解析组件内部style use: [ workers, 'style-loader', 'css-loader', 'postcss-loader' ] } ] }, plugins: [ new webpack.HotModuleReplacementPlugin() // 热加载 ] })
postcss.config.jswebpack
const postcssConfig = { plugins: [ // 配合package的browserslist require('postcss-import'), require('postcss-cssnext'), require('cssnano') ] } console.log(process.env.NODE_ENV) if (process.env.NODE_ENV === 'production') { // 使用postcss插件方式实现,webpack插件方式太麻烦了 const purgecss = require('@fullhuman/postcss-purgecss') const purgecssConfig = require('./purgecss.config') postcssConfig.plugins.push(purgecss(purgecssConfig)) } module.exports = postcssConfig
这个里面引入了一个生产阶段对vue内部style css摇树的插件配置,配置我贴出来
purgecss.configgit
module.exports = { content: ['./public/**/*.html', './src/**/*.vue'], /// / 清理范围,全部的vue component和和入口的html,js里引入的默认是全局使用的 defaultExtractor (content) { const contentWithoutStyleBlocks = content.replace(/<style[^]+?<\/style>/gi, '') return contentWithoutStyleBlocks.match(/[A-Za-z0-9-_/:]*[A-Za-z0-9-_/]+/g) || [] }, whitelist: [/el-.+$/], // 将elementuijs使用上的类也放在白名单不做处理 whitelistPatterns: [/-(leave|enter|appear)(|-(to|from|active))$/, /^(?!(|.*?:)cursor-move).+-move$/, /^router-link(|-exact)-active$/, /data-v-.*/, /el-.+$/] }
最后咱们配置生产文件
webpack.prodweb
const { merge } = require('webpack-merge') const { CleanWebpackPlugin } = require('clean-webpack-plugin')// 打包以前清空 const CopyWebpackPlugin = require('copy-webpack-plugin')// copy文件插件 const MiniCssExtractPlugin = require('mini-css-extract-plugin')// 把css抽成link的方式 const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin')// 压缩css const TerserWebpackPlugin = require('terser-webpack-plugin')// 压缩js const HtmlWebpackPlugin = require('html-webpack-plugin') const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin')// 匹配 runtime.chunk文件写到html中 const common = require('./webpack.common') const path = require('path') const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer') // webpack 打包结果分析 console.log(common) // common.module.rules.push({ // test: /\.(gif|png|jpe?g|svg)$/i, // //这个依赖下载要用 cnpm 淘宝源,而且用管理员权限下载,下不下来把依赖都删了,而后从新下载,其余都下不来,亲身经历 // //算了不用它了,经过它压缩事后的图片包括base64,在ie中不能解析 // loader: 'image-webpack-loader', // options: { // mozjpeg: { // progressive: true, // }, // // optipng.enabled: false will disable optipng // optipng: { // enabled: false, // }, // pngquant: { // quality: [0.65, 0.90], // speed: 4 // }, // gifsicle: { // interlaced: false, // }, // // the webp option will enable WEBP // webp: { // quality: 75 // } // } // }) module.exports = merge(common, { mode: 'production', output: { path: path.resolve(__dirname, 'dist'), filename: '[name].[contenthash:8].bundle.js', // 输出name hash chunkFilename: '[name].[contenthash:8].chunk.js' // chunk文件 hash }, devtool: 'none', module: { rules: [ { test: /\.(less)$/, use: [ MiniCssExtractPlugin.loader, // 生成环境,最后不适用style插入,使用link方式插入 // 'style-loader', 'css-loader', 'postcss-loader', 'less-loader' ] }, { test: /\.css$/, // 解析组件内部style use: [ MiniCssExtractPlugin.loader, // 'style-loader', 'css-loader', 'postcss-loader' ] } ] }, optimization: { minimizer: [ new TerserWebpackPlugin({ parallel: true// 开启多线程 }), // 压缩js new OptimizeCssAssetsWebpackPlugin({ cssProcessPluginOptions: { preset: ['default', { discardComments: { removeAll: true } }] } }) // 压缩css ], // 模块只导出被使用的成员 usedExports: true, sideEffects: true // 标记无反作用,配合package js摇树使用 }, plugins: [ new CleanWebpackPlugin(), new HtmlWebpackPlugin({ title: 'Vue prod App', template: path.resolve(__dirname, 'public/index.html'), minify: { removeComments: true, collapseWhitespace: true, removeRedundantAttributes: true, useShortDoctype: true, removeEmptyAttributes: true, removeStyleLinkTypeAttributes: true, keepClosingSlash: true, minifyJS: true, minifyCSS: true, minifyURLs: true } }), new MiniCssExtractPlugin({ // 生成link的css文件,名称hash filename: '[name].[contenthash:8].css' }), new CopyWebpackPlugin([ // 复制文件 'public' ]), new ScriptExtHtmlWebpackPlugin({ // 写入html中 inline: [/runtime~.+\.js$/] // 正则匹配runtime文件名 }), new BundleAnalyzerPlugin() // webpack打包结果分析 ] }) // css摇树使用postcss插件实现了
babel.config.js咱们有一个兼容[vue-cli的浏览器兼容babel的配置]vue-cli
module.exports = { presets: [ '@vue/cli-plugin-babel/preset' ] }
最后贴的就是package.jsonnpm
{ "name": "vue-app-base", "version": "0.1.0", "private": true, "scripts": { "serve": "cross-env NODE_ENV=development webpack-dev-server --config webpack.dev.js", "build": "cross-env NODE_ENV=production webpack --config webpack.prod.js", "lint": "eslint --ext .js,.vue src/", "all-lint-fix": "eslint --fix --ext .js,.vue src/", "precommit": "lint-staged" }, "dependencies": { "core-js": "3", "vue": "^2.6.11" }, "devDependencies": { "@babel/core": "^7.12.9", "@babel/plugin-syntax-dynamic-import": "^7.8.3", "@babel/preset-env": "^7.12.7", "@fullhuman/postcss-purgecss": "^3.0.0", "@vue/cli-plugin-babel": "^4.5.9", "babel-loader": "^8.2.1", "clean-webpack-plugin": "^3.0.0", "copy-webpack-plugin": "^5.0.4", "cross-env": "^7.0.3", "css-loader": "^5.0.1", "cssnano": "^4.1.10", "eslint": "^7.14.0", "eslint-config-standard": "^16.0.2", "eslint-loader": "^4.0.2", "eslint-plugin-import": "^2.22.1", "eslint-plugin-node": "^11.1.0", "eslint-plugin-promise": "^4.2.1", "eslint-plugin-vue": "^7.1.0", "file-loader": "^6.2.0", "html-webpack-plugin": "^4.5.0", "husky": "^4.3.0", "image-webpack-loader": "^7.0.1", "less": "^3.12.2", "less-loader": "^7.1.0", "lint-staged": "^10.5.2", "mini-css-extract-plugin": "^1.3.1", "optimize-css-assets-webpack-plugin": "^5.0.4", "postcss": "^8.1.10", "postcss-cssnext": "^3.1.0", "postcss-import": "^13.0.0", "postcss-loader": "^4.1.0", "preload-webpack-plugin": "^3.0.0-beta.4", "script-ext-html-webpack-plugin": "^2.1.5", "style-loader": "^2.0.0", "terser-webpack-plugin": "2.2.1", "thread-loader": "^3.0.1", "url-loader": "^4.1.1", "vue-loader": "^15.9.5", "vue-template-compiler": "^2.6.12", "webpack": "^4.41.2", "webpack-bundle-analyzer": "^4.1.0", "webpack-cli": "3.3", "webpack-dev-server": "^3.9.0", "webpack-merge": "^5.4.0" }, "lint-staged": { "src/**/*.(js|vue)": [ "eslint --fix", "git add" ] }, "husky": { "hooks": { "pre-commit": "npm run precommit" } }, "eslintConfig": { "root": true, "env": { "node": true }, "extends": [ "plugin:vue/essential", "eslint:recommended" ], "parserOptions": { "parser": "babel-eslint" }, "rules": {} }, "browserslist": [ "> 1%", "last 2 versions", "IE 10" ], "sideEffects": [ "*.css", "*.less", "*.vue" ] }
这样就配置完成了,这一套配置其实去除掉vue相关的就是一套通用配置。
作下webpack 的loader和plugin区别
webpack的执行流程咱们能够直接来参考这张图来看。