webpack-chain 是什么?通过链式的方式修改webpack的配置
。
一些列出本人常用的使用 webpack-chain 的方式修改 webpack 配置. 整个文件为:
//vue.config.jsmodule.exports={chainWebpack:(webpackConfig)=>{}}复制代码
chainWebpack: config => {config.entryPoints.clear() // 会把默认的入口清空config.entry('main').add('./src/main.js')//新增入口config.entry('routes').add('./src/app-routes.js')//新增入口config.output.path("dist").filename("[name].[chunkhash].js").chunkFilename("chunks/[name].[chunkhash].js").libraryTarget("umd").library();}// 其余的output配置config.output.auxiliaryComment(auxiliaryComment).chunkFilename(chunkFilename).chunkLoadTimeout(chunkLoadTimeout).crossOriginLoading(crossOriginLoading).devtoolFallbackModuleFilenameTemplate(devtoolFallbackModuleFilenameTemplate).devtoolLineToLine(devtoolLineToLine).devtoolModuleFilenameTemplate(devtoolModuleFilenameTemplate).filename(filename).hashFunction(hashFunction).hashDigest(hashDigest).hashDigestLength(hashDigestLength).hashSalt(hashSalt).hotUpdateChunkFilename(hotUpdateChunkFilename).hotUpdateFunction(hotUpdateFunction).hotUpdateMainFilename(hotUpdateMainFilename).jsonpFunction(jsonpFunction).library(library).libraryExport(libraryExport).libraryTarget(libraryTarget).path(path).pathinfo(pathinfo).publicPath(publicPath).sourceMapFilename(sourceMapFilename).sourcePrefix(sourcePrefix).strictModuleExceptionHandling(strictModuleExceptionHandling).umdNamedDefine(umdNamedDefine)复制代码
const path = require('path');function resolve (dir) {return path.join(__dirname, dir)}module.exports = {lintOnSave: true,chainWebpack: (config)=>{config.resolve.alias.set('@$', resolve('src')).set('assets',resolve('src/assets')).set('components',resolve('src/components')).set('layout',resolve('src/layout')).set('base',resolve('src/base')).set('static',resolve('src/static')).delete('base') // 删掉指定的别名// .clear() 会把所有别名都删掉}}复制代码
devServe 的配置,请见这里
chainWebpack: config => {config.devServer.port(8888).open(true).proxy({'/dev': {target: 'http://123.57.153.106:8080/',changeOrigin: true,pathRewrite: {'^/dev': ''}}})}// chain其他队proxy的配置config.devServer.bonjour(bonjour).clientLogLevel(clientLogLevel).color(color).compress(compress).contentBase(contentBase).disableHostCheck(disableHostCheck).filename(filename).headers(headers).historyApiFallback(historyApiFallback).host(host).hot(hot).hotOnly(hotOnly).https(https).inline(inline).info(info).lazy(lazy).noInfo(noInfo).open(open).openPage(openPage).overlay(overlay).pfx(pfx).pfxPassphrase(pfxPassphrase).port(port).progress(progress).proxy(proxy).public(public).publicPath(publicPath).quiet(quiet).setup(setup).socket(socket).staticOptions(staticOptions).stats(stats).stdin(stdin).useLocalIp(useLocalIp).watchContentBase(watchContentBase).watchOptions(watchOptions)复制代码
插件相关配置请见这里
添加插件
// 添加APIconfig.plugin(name).use(WebpackPlugin, args)// 一个例子const fileManager = require("filemanager-webpack-plugin");...//注意:use部分,不能使用new的方式创建插件实例webpackConfig.plugin("zip").use(fileManager, [{onEnd: {archive: [{source: "dist",destination: zipName}]}}]);复制代码
修改插件参数
// 可以使用tap方式,修改插件参数config.plugin(name).tap(args => newArgs)// 一个例子config.plugin('env')//使用tag修改参数.tap(args => [...args, 'SECRET_KEY']);复制代码
修改插件初始化
config.plugin(name).init((Plugin, args) => new Plugin(...args));复制代码
移除插件
chainWebpack: config => {config.plugins.delete('prefetch')// 移除 preload 插件config.plugins.delete('preload');}复制代码
有时候需要 xx 插件在 aa 插件之前
调用。
config.plugin(name).before(otherName)// 一个例子:ScriptExtWebpackPlugin插件在HtmlWebpackTemplate插件前调用config.plugin('html-template').use(HtmlWebpackTemplate).end().plugin('script-ext').use(ScriptExtWebpackPlugin).before('html-template');复制代码
有时候需要 xx 插件在 aa 插件之后
调用。
config.plugin(name).after(otherName)// 一个例子html-template在script-ext之后调用config.plugin('html-template').after('script-ext').use(HtmlWebpackTemplate).end().plugin('script-ext').use(ScriptExtWebpackPlugin);复制代码
配置请见 webpack 参数:performance
config.performance.hints(hints)//false | "error" | "warning"。打开/关闭提示.maxEntrypointSize(maxEntrypointSize)//入口起点表示针对指定的入口,对于所有资源,要充分利用初始加载时(initial load time)期间。此选项根据入口起点的最大体积,控制 webpack 何时生成性能提示。默认值是:250000.maxAssetSize(maxAssetSize)//资源(asset)是从 webpack 生成的任何文件。此选项根据单个资源体积,控制 webpack 何时生成性能提示。默认值是:250000.assetFilter(assetFilter)//此属性允许 webpack 控制用于计算性能提示的文件复制代码
config.optimization.concatenateModules(concatenateModules).flagIncludedChunks(flagIncludedChunks).mergeDuplicateChunks(mergeDuplicateChunks).minimize(minimize) //boolean,默认为true,是否开启压缩.namedChunks(namedChunks).namedModules(namedModules).nodeEnv(nodeEnv).noEmitOnErrors(noEmitOnErrors).occurrenceOrder(occurrenceOrder).portableRecords(portableRecords).providedExports(providedExports).removeAvailableModules(removeAvailableModules).removeEmptyChunks(removeEmptyChunks).runtimeChunk(runtimeChunk).sideEffects(sideEffects).splitChunks(splitChunks)//object:代码分割。默认情况下,webpack v4 +为动态导入的模块提供了开箱即用的新通用块策略。.usedExports(usedExports)//举个例子config.optimization.splitChunks({chunks: "async", // 必须三选一: "initial" | "all"(推荐) | "async" (默认就是async)minSize: 30000, // 最小尺寸,30000minChunks: 1, // 最小 chunk ,默认1maxAsyncRequests: 5, // 最大异步请求数, 默认5maxInitialRequests : 3, // 最大初始化请求书,默认3automaticNameDelimiter: '~',// 打包分隔符name: function(){}, // 打包后的名称,此选项可接收 functioncacheGroups:{ // 这里开始设置缓存的 chunkspriority: 0, // 缓存组优先级vendor: { // key 为entry中定义的 入口名称chunks: "initial", // 必须三选一: "initial" | "all" | "async"(默认就是async)test: /react|lodash/, // 正则规则验证,如果符合就提取 chunkname: "vendor", // 要缓存的 分隔出来的 chunk 名称minSize: 30000,minChunks: 1,enforce: true,maxAsyncRequests: 5, // 最大异步请求数, 默认1maxInitialRequests : 3, // 最大初始化请求书,默认1reuseExistingChunk: true // 可设置是否重用该chunk}}});复制代码
webpack4.x 默认使用的TerserPlugin 做代码压缩。
//使用config.optimization.minimizer.use(WebpackPlugin,args);//删除config.optimization.minimizers.delete(name)// 一个例子config.optimization.minimizer('css').use(OptimizeCSSAssetsPlugin, [{ cssProcessorOptions: { safe: true } }])// Minimizer plugins can also be specified by their path, allowing the expensive require()s to be// skipped in cases where the plugin or webpack configuration won't end up being used.config.optimization.minimizer('css').use(require.resolve('optimize-css-assets-webpack-plugin'), [{ cssProcessorOptions: { safe: true } }])//是要tap修改插件参数config.optimization.minimizer('css').tap(args => [...args, { cssProcessorOptions: { safe: false } }])复制代码
首先请先了解一下 webpack 如何配置 loader. 官网链接
config.module.rule(name).use(name).loader(loader).options(options)// 一个例子config.module.rule('graphql').test(/\.graphql$/).use('graphql-tag/loader').loader('graphql-tag/loader').end()// 如果是非webpack-chain的话module:{rules:[{test:/\.graphql$/,use::[{loader:"graphql-tag/loader"}]}]}复制代码
// vue.config.jsmodule.exports = {chainWebpack: config => {config.module.rule('vue').use('vue-loader').loader('vue-loader').tap(options => {// 修改它的选项...return options})}}复制代码
注意 对于 CSS 相关 loader 来说,我们推荐使用
css.loaderOptions
而不是直接链式指定 loader。这是因为每种 CSS 文件类型都有多个规则,而 css.loaderOptions 可以确保你通过一个地方影响所有的规则。
// vue.config.jsmodule.exports = {chainWebpack: config => {const svgRule = config.module.rule('svg')// 清除已有的所有 loader。// 如果你不这样做,接下来的 loader 会附加在该规则现有的 loader 之后。svgRule.uses.clear()// 添加要替换的 loadersvgRule.use('vue-svg-loader').loader('vue-svg-loader')}}复制代码
consif.when(condition,truthyFunc,falsyFunc)// 一个例子,当构建生产包时添加minify插件,否则设置构建类型为source-map// devtool请见:https://www.webpackjs.com/configuration/devtool/config.when(process.env.NODE_ENV === 'production',config => config.plugin('minify').use(BabiliWebpackPlugin),config => config.devtool('source-map'));复制代码
注意 使用 toString()
生成的数据,不能直接在webpack
上使用。
config.module.rule('compile').test(/\.js$/).use('babel').loader('babel-loader');config.toString();/*{module: {rules: [/* config.module.rule('compile') */{test: /\.js$/,use: [/* config.module.rule('compile').use('babel') */{loader: 'babel-loader'}]}]}}*/复制代码
如果觉得此文对您有用欢迎来个Star: mrgaogang.github.io