为了提高构建性能,在配置webpack.config.js时,会用到noParse,比如,将noParse字段值配置成一个正则表达式。
//包含"jquery"或者"chartjs"的文件路径,不解析
module.exports = {
module:{
noParse:/jquery|chartjs/,
rules:[]
}
}
webpack将根据这个noParse的配置来决定,是否要递归解析这个文件。
为了讲清楚这个【解析】,先把webpack中部分与noParse有关的代码拿出来。
const noParseRule = options.module && options.module.noParse;
if (this.shouldPreventParsing(noParseRule, this.request)) {
return callback();
}
const handleParseError = e => {
return callback();
};
const handleParseResult = result => {
return callback();
};
try {
const result = this.parser.parse();
if (result !== undefined) {
handleParseResult(result);
}
} catch (e) {
handleParseError(e);
}
这个【解析】就是指第13行的this.parser.parse()。什么情况下需要必须经历解析这个过程呢?
我们知道,纯javascript写的代码,可以直接在浏览器端运行。
但像comm