原本该在过WebpackOptionsApply时讲解这个方法的,但是当时一不小心过掉了,所以在这里补上。

compiler.resolvers

  该对象的三个方法均在WebpackOptionsApply中生成,代码如下:

compiler.resolvers.normal = ResolverFactory.createResolver(Object.assign({
fileSystem: compiler.inputFileSystem
}, options.resolve));
compiler.resolvers.context = ResolverFactory.createResolver(Object.assign({
fileSystem: compiler.inputFileSystem,
resolveToContext: true
}, options.resolve));
compiler.resolvers.loader = ResolverFactory.createResolver(Object.assign({
fileSystem: compiler.inputFileSystem
}, options.resolveLoader));

  由于调用的是一个工厂函数,所以用normal作为示例讲解。

  

/*
"resolve": {
"unsafeCache": true,
"modules": ["node_modules"],
"extensions": [".js", ".json"],
"mainFiles": ["index"],
"aliasFields": ["browser"],
"mainFields": ["browser", "module", "main"],
"cacheWithContext": false
},
*/
compiler.resolvers.normal = ResolverFactory.createResolver(Object.assign({
fileSystem: compiler.inputFileSystem
}, options.resolve));

  其中参数中的resolve取了默认值,如注释所示。

ResolveFactory.createResolver

  这个方法比较有意思,一块一块的来看源码,所有注释保留英文原文更好理解:

exports.createResolver = function(options) {
//// OPTIONS //// // A list of directories to resolve modules from, can be absolute path or folder name
// 模块文件夹的目录或者文件夹名称
var modules = options.modules || ["node_modules"]; // A list of description files to read from
// 描述配置文件名
var descriptionFiles = options.descriptionFiles || ["package.json"]; // A list of additional resolve plugins which should be applied
// The slice is there to create a copy, because otherwise pushing into plugins
// changes the original options.plugins array, causing duplicate plugins
// 额外的插件
var plugins = (options.plugins && options.plugins.slice()) || []; // A list of main fields in description files
// 不知道干啥的
var mainFields = options.mainFields || ["main"]; // A list of alias fields in description files
// 不知道干啥的
var aliasFields = options.aliasFields || []; // A list of main files in directories
// 模块主入口文件名
var mainFiles = options.mainFiles || ["index"]; // A list of extensions which should be tried for files
// 默认的文件扩展名
var extensions = options.extensions || [".js", ".json", ".node"]; // Enforce that a extension from extensions must be used
var enforceExtension = options.enforceExtension || false; // A list of module extensions which should be tried for modules
var moduleExtensions = options.moduleExtensions || []; // Enforce that a extension from moduleExtensions must be used
var enforceModuleExtension = options.enforceModuleExtension || false; // A list of module alias configurations or an object which maps key to value
// 别名
var alias = options.alias || []; // ...还有一些其他奇奇怪怪的属性 //// options processing //// // ...第二部分
};

  这一步是包装参数,主要看注释,基本上对resolve参数下的各个key都做了解释,有一些实在不知道干啥用的就省略了。

  基本上可能会自定义的大概只有extensions、alias两个属性。

  下面来看第二部分:

exports.createResolver = function(options) {
//// OPTIONS //// // ...第一部分 //// options processing //// if (!resolver) {
// useSyncFileSystemCalls默认为undefined
resolver = new Resolver(useSyncFileSystemCalls ? new SyncAsyncFileSystemDecorator(fileSystem) : fileSystem);
}
// 数组包装
extensions = [].concat(extensions);
moduleExtensions = [].concat(moduleExtensions);
// 返回[['node_modules']]
modules = mergeFilteredToArray([].concat(modules), function(item) {
return !isAbsolutePath(item);
});
// 不懂这个参数干啥的
// 返回一个对象数组
mainFields = mainFields.map(function(item) {
if (typeof item === "string") {
item = {
name: item,
forceRelative: true
};
}
return item;
});
// 处理别名
if (typeof alias === "object" && !Array.isArray(alias)) { /**/ } // 不知道什么东西
if (unsafeCache && typeof unsafeCache !== "object") {
unsafeCache = {};
} //// pipeline //// // ...第三部分
};

  这一部分是处理参数,resolver是最后返回的对象,到调用的时候再细看。

  几个参数由于不太懂什么作用,处理方法也很简单,就不做解释,这里看一下alias别名的处理:

/*
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': '../src'
}
*/
/*
alias:[
{
name: 'vue',
onlyModule: true,
alias: 'vue/dist/vue.esm.js'
},
{
name: '@',
onlyModule: false,
alias: '../src'
}
]
*/
if (typeof alias === "object" && !Array.isArray(alias)) {
alias = Object.keys(alias).map(function(key) {
var onlyModule = false;
var obj = alias[key];
// 测试是否以$结尾
if (/\$$/.test(key)) {
onlyModule = true;
key = key.substr(0, key.length - 1);
}
// alias的值是否为字符串
if (typeof obj === "string") {
obj = {
alias: obj
};
}
obj = Object.assign({
name: key,
onlyModule: onlyModule
}, obj);
return obj;
});
}

  这里以vue-cli为例,展示了转换后的alias,看注释就OK了。

  

  第三部分有点恶心,源码大概是这样子的:

exports.createResolver = function(options) {
//// OPTIONS //// // ...第一部分 //// options processing //// // ...第二部分 //// pipeline //// // resolve
if (unsafeCache) {
plugins.push(new UnsafeCachePlugin("resolve", cachePredicate, unsafeCache, cacheWithContext, "new-resolve"));
plugins.push(new ParsePlugin("new-resolve", "parsed-resolve"));
} else {
plugins.push(new ParsePlugin("resolve", "parsed-resolve"));
} // ...无穷多的if + plugins.push(...) //// RESOLVER //// plugins.forEach(function(plugin) {
resolver.apply(plugin);
});
return resolver;
};

  虽然有非常多的plugin,但是内部处理形式大同小异。所以就一个常用参数作为例子,比如说:

// described-resolve
alias.forEach(function(item) {
plugins.push(new AliasPlugin("described-resolve", item, "resolve"));
});

  简要的看一下内部,这里的alias就是上面转换后的对象数组。

class AliasPlugin {
constructor(source, options, target) {
this.source = source;
this.name = options.name;
this.alias = options.alias;
this.onlyModule = options.onlyModule;
this.target = target;
}
apply(resolver) {
var target = this.target;
var name = this.name;
var alias = this.alias;
var onlyModule = this.onlyModule;
resolver.plugin(this.source, function(request, callback) { /**/ });
}
}

  第三部分所有的plugins都是这样的形式。

1、构造函数仅仅获取并初始化值

2、有一个apply方法,接受一个resolver参数

3、注入参数source的事件流

4、在source事件流最后,target参数会在resolver.doResolve方法中被调用,这里省略了代码

  在函数的最后,可以看到有一个这样的调用:

plugins.forEach(function(plugin) {
resolver.apply(plugin);
});

  这里就是依次执行所有plugin的apply方法,传入resolver作为参数。

  所有的插件plugin流程以示意图的形式给出,这里就不一一分析了。

  注入完所有的事件流后,返回这个resolver对象,也就是compiler.resolvers.normal(loader、context)。

.28-浅析webpack源码之compiler.resolvers的更多相关文章

  1. .30-浅析webpack源码之doResolve事件流(1)

    这里所有的插件都对应着一个小功能,画个图整理下目前流程: 上节是从ParsePlugin中出来,对'./input.js'入口文件的路径做了处理,返回如下: ParsePlugin.prototype ...

  2. .34-浅析webpack源码之事件流make(3)

    新年好呀~过个年光打游戏,function都写不顺溜了. 上一节的代码到这里了: // NormalModuleFactory的resolver事件流 this.plugin("resolv ...

  3. .30-浅析webpack源码之doResolve事件流(2)

    这里所有的插件都对应着一个小功能,画个图整理下目前流程: 上节是从ParsePlugin中出来,对'./input.js'入口文件的路径做了处理,返回如下: ParsePlugin.prototype ...

  4. .3-浅析webpack源码之预编译总览

    写在前面: 本来一开始想沿用之前vue源码的标题:webpack源码之***,但是这个工具比较巨大,所以为防止有人觉得我装逼跑来喷我(或者随时鸽),加上浅析二字,以示怂. 既然是浅析,那么案例就不必太 ...

  5. .17-浅析webpack源码之compile流程-入口函数run

    本节流程如图: 现在正式进入打包流程,起步方法为run: Compiler.prototype.run = (callback) => { const startTime = Date.now( ...

  6. 从Webpack源码探究打包流程,萌新也能看懂~

    简介 上一篇讲述了如何理解tapable这个钩子机制,因为这个是webpack程序的灵魂.虽然钩子机制很灵活,而然却变成了我们读懂webpack道路上的阻碍.每当webpack运行起来的时候,我的心态 ...

  7. 浅析libuv源码-node事件轮询解析(3)

    好像博客有观众,那每一篇都画个图吧! 本节简图如下. 上一篇其实啥也没讲,不过node本身就是这么复杂,走流程就要走全套.就像曾经看webpack源码,读了300行代码最后就为了取package.js ...

  8. webpack源码-依赖收集

    webpack源码-依赖收集 version:3.12.0 程序主要流程: 触发make钩子 Compilation.js 执行EntryOptionPlugin 中注册的make钩子 执行compi ...

  9. .20-浅析webpack源码之compile流程-Template模块

    这里的编译前指的是开始触发主要的事件流this-compilaiton.compilation之前,由于还有一些准备代码,这一节全部弄出来. 模块基本上只走构造函数,具体的方法调用的时候再具体讲解. ...

随机推荐

  1. 关于java 定时任务

    几种任务调度的 Java 实现方法与比较 综观目前的 Web 应用,多数应用都具备任务调度的功能.本文由浅入深介绍了几种任务调度的 Java 实现方法,包括 Timer,Scheduler, Quar ...

  2. S2 深入.NET和C#编程 一: 深入C#.NET框架

    深入C#.NET框架 1..NET框架 之一   推荐一个代码管理平台,博客发布平台 git   之前的复习:   学习的网站: git   github.com 2.类和对象的关系  Dept de ...

  3. 让git不再跟踪配置文件的变化

    我们经常会在配置文件里留下一些敏感信息 比如数据库链接字符串的用户名和密码 如果不提交配置文件到github或者其他源码管理网站 那么你的粉丝很可能就无法正确运行你的项目,就达不到开源的目的了 那么, ...

  4. RLP

    ** 原创勿转 ** 这是在看devp2p时看到的,英文原文地址:https://github.com/ethereum/wiki/wiki/RLP RLP:  Recursive Length Pr ...

  5. MYSQL数据库表按月备份,滚动,保留6次备份

    要求: 每月1日0点:在不影响业务的情况下,备份整月的数据,保留6次备份. 思路: 基于MYSQL事件功能,每月按时完成操作 RENAME语句具有原子性,新旧表无缝切换 RENAME语句仅修改表定义, ...

  6. git 本地代码到github

    一·什么是gitHub? 官网解释:gitHub是一个让无论处于何地的代码工作者能工作于同一个项目,同一个版本的平台.(GitHub is a code hosting platform for ve ...

  7. Redis随笔(一)Linux Redis 搭建

    1.到官网下载redis上传服务器或者使用wget 下载 wget redis下载的路径 2.查看linux是否安装编译环境gcc,没有先安装 yum -y install gcc 3.解压redis ...

  8. 阿里云正式上线移动直播问答解决方案,助力APP尽情“撒币”!

    2018年伊始,互联网圈就刮起了一阵"大佬狂撒币,网友喜答题"的热潮.以映客芝士超人等为代表的直播问答平台,通过答题分奖金的互动模式,迅速引爆网络热点.随后,多个直播和视频平台也上 ...

  9. Grafana最新版本4.3.1安装(后端使用mysql)

    环境 CentOS release 6.5 (Final) 64bitzabbix_server (Zabbix) 3.0.3 grafana-4.3.1mysql-5.6.21 一.安装grafan ...

  10. java_web学习(三) eclipse_jsp学习

    1.首先打开eclipse,新建一个Dynamac web project项目文件 2.在WebContent单击右键创建JSP File 3.过程 4.简单的jsp代码 运行结果: 5.导出war文 ...