我们都知道,webpack作为一个构建工具,解决了前端代码缺少模块化能力的问题。我们写的代码,经过webpack构建和包装之后,能够在浏览器以模块化的方式运行。这些能力,都是因为webpack对我们的代码进行了一层包装,本文就以webpack生成的代码入手,分析webpack是如何实现模块化的。
PS: webpack的模块不仅指js,包括css、图片等资源都可以以模块看待,但本文只关注js。
准备
首先我们创建一个简单入口模块index.js和一个依赖模块bar.js:
//index.js 'use strict'; var bar = require('./bar'); function foo() { return bar.bar(); }
//bar.js 'use strict'; exports.bar = function () { return 1; }
webpack配置如下:
var path = require("path"); module.exports = { entry: path.join(__dirname, 'index.js'), output: { path: path.join(__dirname, 'outs'), filename: 'index.js' }, };
这是一个最简单的配置,只指定了模块入口和输出路径,但已经满足了我们的要求。
在根目录下执行webpack
,得到经过webpack打包的代码如下(去掉了不必要的注释):
(function(modules) { // webpackBootstrap // The module cache var installedModules = {}; // The require function function __webpack_require__(moduleId) { // Check if module is in cache if(installedModules[moduleId]) { return installedModules[moduleId].exports; } // Create a new module (and put it into the cache) var module = installedModules[moduleId] = { i: moduleId, l: false, exports: {} }; // Execute the module function modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); // Flag the module as loaded module.l = true; // Return the exports of the module return module.exports; } // expose the modules object (__webpack_modules__) __webpack_require__.m = modules; // expose the module cache __webpack_require__.c = installedModules; // define getter function for harmony exports __webpack_require__.d = function(exports, name, getter) { if(!__webpack_require__.o(exports, name)) { Object.defineProperty(exports, name, { configurable: false, enumerable: true, get: getter }); } }; // getDefaultExport function for compatibility with non-harmony modules __webpack_require__.n = function(module) { var getter = module && module.__esModule ? function getDefault() { return module['default']; } : function getModuleExports() { return module; }; __webpack_require__.d(getter, 'a', getter); return getter; }; // Object.prototype.hasOwnProperty.call __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; // __webpack_public_path__ __webpack_require__.p = ""; // Load entry module and return exports return __webpack_require__(__webpack_require__.s = 0); }) /************************************************************************/ ([ /* 0 */ (function(module, exports, __webpack_require__) { "use strict"; var bar = __webpack_require__(1); bar.bar(); }), /* 1 */ (function(module, exports, __webpack_require__) { "use strict"; exports.bar = function () { return 1; } }) ]);
分析
上面webpack打包的代码,整体可以简化成下面的结构:
(function (modules) {/* 省略函数内容 */}) ([ function (module, exports, __webpack_require__) { /* 模块index.js的代码 */ }, function (module, exports, __webpack_require__) { /* 模块bar.js的代码 */ } ]);
可以看到,整个打包生成的代码是一个IIFE(立即执行函数),函数内容我们待会看,我们先来分析函数的参数。
函数参数是我们写的各个模块组成的数组,只不过我们的代码,被webpack包装在了一个函数的内部,也就是说我们的模块,在这里就是一个函数。为什么要这样做,是因为浏览器本身不支持模块化,那么webpack就用函数作用域来hack模块化的效果。
如果你debug过node代码,你会发现一样的hack方式,node中的模块也是函数,跟模块相关的参数exports
、require
,或者其他参数__filename
和__dirname
等都是通过函数传值作为模块中的变量,模块与外部模块的访问就是通过这些参数进行的,当然这对开发者来说是透明的。
同样的方式,webpack也控制了模块的module
、exports
和require
,那么我们就看看webpack是如何实现这些功能的。
下面是摘取的函数内容,并添加了一些注释:
// 1、模块缓存对象 var installedModules = {}; // 2、webpack实现的require function __webpack_require__(moduleId) { // 3、判断是否已缓存模块 if(installedModules[moduleId]) { return installedModules[moduleId].exports; } // 4、缓存模块 var module = installedModules[moduleId] = { i: moduleId, l: false, exports: {} }; // 5、调用模块函数 modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); // 6、标记模块为已加载 module.l = true; // 7、返回module.exports return module.exports; } // 8、require第一个模块 return __webpack_require__(__webpack_require__.s = 0);
模块数组作为参数传入IIFE函数后,IIFE做了一些初始化工作:
- IIFE首先定义了
installedModules
,这个变量被用来缓存已加载的模块。 - 定义了
__webpack_require__
这个函数,函数参数为模块的id。这个函数用来实现模块的require。 __webpack_require__
函数首先会检查是否缓存了已加载的模块,如果有则直接返回缓存模块的exports
。- 如果没有缓存,也就是第一次加载,则首先初始化模块,并将模块进行缓存。
- 然后调用模块函数,也就是前面webpack对我们的模块的包装函数,将
module
、module.exports
和__webpack_require__
作为参数传入。注意这里做了一个动态绑定,将模块函数的调用对象绑定为module.exports
,这是为了保证在模块中的this指向当前模块。 - 调用完成后,模块标记为已加载。
- 返回模块
exports
的内容。 - 利用前面定义的
__webpack_require__
函数,require第0个模块,也就是入口模块。
require入口模块时,入口模块会收到收到三个参数,下面是入口模块代码:
function(module, exports, __webpack_require__) { "use strict"; var bar = __webpack_require__(1); bar.bar(); }
webpack传入的第一个参数module
是当前缓存的模块,包含当前模块的信息和exports
;第二个参数exports
是module.exports
的引用,这也符合commonjs的规范;第三个__webpack_require__
则是require
的实现。
在我们的模块中,就可以对外使用module.exports
或exports
进行导出,使用__webpack_require__
导入需要的模块,代码跟commonjs完全一样。
这样,就完成了对第一个模块的require,然后第一个模块会根据自己对其他模块的require,依次加载其他模块,最终形成一个依赖网状结构。webpack管理着这些模块的缓存,如果一个模块被require多次,那么只会有一次加载过程,而返回的是缓存的内容,这也是commonjs的规范。
结论
到这里,webpack就hack了commonjs代码。
原理还是很简单的,其实就是实现exports
和require
,然后自动加载入口模块,控制缓存模块,that's all。
细心的你一定会发现,文章到这里只介绍了webpack对commonjs的实现,那么ES6 module是如何实现的呢?
上一篇文章介绍了webpack对commonjs模块的支持(如果你还没读过,建议你先阅读),这篇文章来探究一下,webpack是如何支持es模块的。
准备
我们依然写两个文件,m.js文件用es模块的方式export
一个default
函数和一个foo
函数,index.js import
该模块,具体代码如下:
// m.js 'use strict'; export default function bar () { return 1; }; export function foo () { return 2; }
// index.js 'use strict'; import bar, {foo} from './m'; bar(); foo();
webpack配置没有变化,依然以index.js作为入口:
var path = require("path"); module.exports = { entry: path.join(__dirname, 'index.js'), output: { path: path.join(__dirname, 'outs'), filename: 'index.js' }, };
在根目录下执行webpack
,得到经过webpack打包的代码如下(去掉了不必要的注释):
(function(modules) { // webpackBootstrap // The module cache var installedModules = {}; // The require function function __webpack_require__(moduleId) { // Check if module is in cache if(installedModules[moduleId]) { return installedModules[moduleId].exports; } // Create a new module (and put it into the cache) var module = installedModules[moduleId] = { i: moduleId, l: false, exports: {} }; // Execute the module function modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); // Flag the module as loaded module.l = true; // Return the exports of the module return module.exports; } // expose the modules object (__webpack_modules__) __webpack_require__.m = modules; // expose the module cache __webpack_require__.c = installedModules; // define getter function for harmony exports __webpack_require__.d = function(exports, name, getter) { if(!__webpack_require__.o(exports, name)) { Object.defineProperty(exports, name, { configurable: false, enumerable: true, get: getter }); } }; // getDefaultExport function for compatibility with non-harmony modules __webpack_require__.n = function(module) { var getter = module && module.__esModule ? function getDefault() { return module['default']; } : function getModuleExports() { return module; }; __webpack_require__.d(getter, 'a', getter); return getter; }; // Object.prototype.hasOwnProperty.call __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; // __webpack_public_path__ __webpack_require__.p = ""; // Load entry module and return exports return __webpack_require__(__webpack_require__.s = 0); }) ([ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__m__ = __webpack_require__(1); Object(__WEBPACK_IMPORTED_MODULE_0__m__["a" /* default */])(); Object(__WEBPACK_IMPORTED_MODULE_0__m__["b" /* foo */])(); }), (function(module, __webpack_exports__, __webpack_require__) { "use strict"; /* harmony export (immutable) */ __webpack_exports__["a"] = bar; /* harmony export (immutable) */ __webpack_exports__["b"] = foo; function bar () { return 1; }; function foo () { return 2; } }) ]);
分析
上一篇文章已经分析过了,webpack生成的代码是一个IIFE,这个IIFE完成一系列初始化工作后,就会通过__webpack_require__(0)
启动入口模块。
我们首先来看m.js模块是如何实现es的export
的,被webpack转换后的m.js代码如下:
__webpack_exports__["a"] = bar; __webpack_exports__["b"] = foo; function bar () { return 1; }; function foo () { return 2; }
其实一眼就能看出来,export default和export都被转换成了类似于commonjs的exports.xxx
,这里也已经不区分是不是default export了,所有的export对象都是__webpack_exports__
的属性。
我们继续来看看入口模块,被webpack转换后的index.js代码如下:
Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); var __WEBPACK_IMPORTED_MODULE_0__module__ = __webpack_require__(1); Object(__WEBPACK_IMPORTED_MODULE_0__m__["a" /* default */])(); Object(__WEBPACK_IMPORTED_MODULE_0__m__["b" /* foo */])();
index模块首先通过Object.defineProperty
在__webpack_exports__
上添加属性__esModule
,值为true
,表明这是一个es模块。在目前的代码下,这个标记是没有作用的,至于在什么情况下需要判断模块是否es模块,后面会分析。
然后就是通过__webpack_require__(1)
导入m.js模块,再然后通过module.xxx
获取m.js中export的对应属性。注意这里有一个重要的点,就是所有引入的模块属性都会用Object()包装成对象,这是为了保证像Boolean、String、Number这些基本数据类型转换成相应的类型对象。
commonjs与es6 module混用
我们前面分析的都是commonjs模块对commonjs模块的导入,或者es模块对es模块的导入,那么如果是es模块对commonjs模块的导入会是什么情况呢,反过来又会如何呢?
其实我们前面说到的__webpack_exports__. __esModule = true
就是针对这种情况的解决方法。
下面用具体代码来解释一下,首先修改m.js和index.js代码如下:
// m.js 'use strict'; exports.foo = function () { return 1; }
// index.js 'use strict'; import m from './m'; m.foo();
重新执行webpack后生成的代码如下(只截取IIFE的参数部分):
[ /* 0 */ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__m__ = __webpack_require__(1); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__m___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0__m__); __WEBPACK_IMPORTED_MODULE_0__m___default.a.foo(); }), /* 1 */ (function(module, exports, __webpack_require__) { "use strict"; exports.foo = function () { return 1; } }) ]
m.js转换后的代码跟转换前的代码基本没有变化,都是用webpack提供的exports
进行模块导出。但是index.js有一点不同,主要是多了一行代码:
var __WEBPACK_IMPORTED_MODULE_0__m___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0__m__);
这段代码作用是什么呢,看一下__webpack_require__.n
的定义就知道了:
// getDefaultExport function for compatibility with non-harmony modules __webpack_require__.n = function(module) { var getter = module && module.__esModule ? function getDefault() { return module['default']; } : function getModuleExports() { return module; }; __webpack_require__.d(getter, 'a', getter); return getter; };
__webpack_require__.n
会判断module是否为es模块,当__esModule
为true的时候,标识module为es模块,那么module.a
默认返回module.default
,否则返回module
。
具体实现则是通过 __webpack_require__.d
将具体操作绑定到属性a
的getter方法上的。
那么,当通过es模块的方式去import
一个commonjs规范的模块时,就会把require得到的module进行一层包装,从而兼容两种情况。
至于通过commonjs去require
一个es模块的情况,原理相同,就不过多解释了。
结论
webpack对于es模块的实现,也是基于自己实现的__webpack_require__
和__webpack_exports__
,装换成类似于commonjs的形式。对于es模块和commonjs混用的情况,则需要通过__webpack_require__.n
的形式做一层包装来实现。