module
module.noParse
module.noParse 防止 webpack
解析那些任何与给定正则表达式相匹配的文件。忽略的文件中不应该含有 import
, require
, define
的调用,或任何其他导入机制。忽略大型的 library
可以提高构建性能。
背景
如果一些第三方模块没有AMD/CommonJS
规范版本,可以使用 noParse
来标识这个模块,这样 Webpack
会引入这些模块,但是不进行转化和解析,从而提升 Webpack
的构建性能 ,例如:jquery
、lodash
。
配置
-
配置不解析
jquery
、lodash
模块module:{
noParse:/jquery|lodash/,
}
原理: 通过 noParse
匹配到的模块还是可以正常打包,但是不会再检查模块内部的require
、import
等语句了
module.rules
module.rules.oneOf
module.rules.oneOf 当规则匹配时,只使用第一个匹配规则。
场景:
-
通过
oneOf
根据规则,选择不同的loader
来执行const Path = require("path");
module.exports = {
mode: "development",
entry: Path.resolve(__dirname, "src", "index.js"),
output: {
filename: "index.js",
path: Path.resolve(__dirname, "build"),
},
module: {
rules: [
{
test: /\.css$/,
oneOf: [
{
resourceQuery: /inline/, // foo.css?inline
use: 'url-loader',
},
{
resourceQuery: /external/, // foo.css?external
use: 'file-loader',
},
],
},
],
},
};