webpack模块化实现原理

/

一、准备

目录结构:

  1. ├── common_index.js
  2. ├── es_index.js
  3. ├── index.js
  4. └── js
  5. ├── format.js
  6. └── math.js

如果有需要源码的,评论或者私信一下,我也不确定有没有人需要,就懒得放了。

二、CommonJS实现原理

去掉所有注释,和一些 外层的立即执行函数后。

  1. // 定义了一个对象
  2. // 模块的路径(key): 函数(value)
  3. var __webpack_modules__ = {
  4. "./src/js/formate.js": (function (module) {
  5. const dateFormat = (date) => {
  6. console.log('2020-1-1');
  7. }
  8. const priceFormat = (price) => {
  9. console.log('100.00');
  10. }
  11. // 5.
  12. // 将我们要导出的其他变量,放入到module对象中的exports对象里
  13. module.exports = {
  14. dateFormat,
  15. priceFormat
  16. }
  17. })
  18. };
  19. // 定义了一个对象,作为加载模块的缓存
  20. var __webpack_module_cache__ = {};
  21. // 函数,当我们加载一个模块时,都会通过这个函数来加载
  22. function __webpack_require__(moduleId) {
  23. var cachedModule = __webpack_module_cache__[moduleId];
  24. // 2. 判断缓存中是否存在
  25. if (cachedModule !== undefined) {
  26. return cachedModule.exports;
  27. }
  28. // 3. 给 module对象 和 __webpack_module_cache__[moduleId] 赋值了同一个对象,
  29. var module = __webpack_module_cache__[moduleId] = {
  30. exports: {}
  31. };
  32. // 4. 加载执行模块
  33. __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
  34. // 6. 导出 module.exports
  35. return module.exports;
  36. }
  37. //
  38. var __webpack_exports__ = {};
  39. // 立即执行函数的变种写法,具体开始执行代码逻辑
  40. ! function () {
  41. // 1. 加载模块
  42. const {
  43. dateFormat,
  44. priceFormat
  45. } = __webpack_require__("./src/js/formate.js")
  46. // 7. 导出
  47. dateFormat('1')
  48. priceFormat('1')
  49. }();

三、ESModule实现原理

  1. // 1. 定义了一个对象,对象里边放的是我们的模块映射
  2. var __webpack_modules__ = ({
  3. "./src/js/math.js": (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) {
  4. __webpack_require__.r(__webpack_exports__);
  5. __webpack_require__.d(__webpack_exports__, {
  6. "sum": function () {
  7. return sum;
  8. },
  9. "mul": function () {
  10. return mul;
  11. }
  12. });
  13. const sum = (num1, num2) => {
  14. return num1 + num2;
  15. }
  16. const mul = (num1, num2) => {
  17. return num1 * num2;
  18. }
  19. })
  20. });
  21. // 2.缓存
  22. var __webpack_module_cache__ = {};
  23. // 3. require 函数的实现(加载模块)
  24. function __webpack_require__(moduleId) {
  25. var cachedModule = __webpack_module_cache__[moduleId];
  26. if (cachedModule !== undefined) {
  27. return cachedModule.exports;
  28. }
  29. var module = __webpack_module_cache__[moduleId] = {
  30. exports: {}
  31. };
  32. __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
  33. return module.exports;
  34. }
  35. ! function () {
  36. // 给这个函数对象添加了一个属性, d => funciton
  37. __webpack_require__.d = function (exports, definition) {
  38. for (var key in definition) {
  39. if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  40. // 做了一个代理,__webpack_modules__ 上的方法 代理到 _js_math__WEBPACK_IMPORTED_MODULE_0__
  41. Object.defineProperty(exports, key, {
  42. enumerable: true,
  43. get: definition[key]
  44. });
  45. }
  46. }
  47. };
  48. }();
  49. ! function () {
  50. // 给这个函数对象添加了一个属性, o => funciton
  51. __webpack_require__.o = function (obj, prop) {
  52. // 判断 obj 对象原生属性上是否具有 prop 属性
  53. return Object.prototype.hasOwnProperty.call(obj, prop);
  54. }
  55. }();
  56. ! function () {
  57. // 给这个函数对象添加了一个属性, r => funciton
  58. __webpack_require__.r = function (exports) {
  59. if (typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  60. // 增加了一个属性 值是 Module
  61. Object.defineProperty(exports, Symbol.toStringTag, {
  62. value: 'Module'
  63. });
  64. }
  65. // 增加了一个属性 值是 __esModule
  66. Object.defineProperty(exports, '__esModule', {
  67. value: true
  68. });
  69. };
  70. }();
  71. var __webpack_exports__ = {};
  72. ! function () {
  73. __webpack_require__.r(__webpack_exports__);
  74. var _js_math__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./src/js/math.js");
  75. console.log(_js_math__WEBPACK_IMPORTED_MODULE_0__.mul(20, 30));
  76. console.log(_js_math__WEBPACK_IMPORTED_MODULE_0__.sum(20, 30));
  77. }();

四、CommonJS和ESModule相互导入原理

  1. var __webpack_modules__ = ({
  2. "./src/index.js":
  3. (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) {
  4. "use strict";
  5. __webpack_require__.r(__webpack_exports__);
  6. var _js_format__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./src/js/format.js");
  7. var _js_format__WEBPACK_IMPORTED_MODULE_0___default = __webpack_require__.n(_js_format__WEBPACK_IMPORTED_MODULE_0__);
  8. // es module导出内容, CommonJS导入内容
  9. const math = __webpack_require__("./src/js/math.js");
  10. // CommonJS导出内容, es module导入内容
  11. console.log(math.sum(20, 30));
  12. console.log(math.mul(20, 30));
  13. console.log(_js_format__WEBPACK_IMPORTED_MODULE_0___default().dateFormat("aaa"));
  14. console.log(_js_format__WEBPACK_IMPORTED_MODULE_0___default().priceFormat("bbb"));
  15. }),
  16. "./src/js/format.js":
  17. (function (module) {
  18. const dateFormat = (date) => {
  19. return "2020-12-12";
  20. }
  21. const priceFormat = (price) => {
  22. return "100.00";
  23. }
  24. module.exports = {
  25. dateFormat,
  26. priceFormat
  27. }
  28. }),
  29. "./src/js/math.js":
  30. (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) {
  31. __webpack_require__.r(__webpack_exports__);
  32. __webpack_require__.d(__webpack_exports__, {
  33. "sum": function () { return sum; },
  34. "mul": function () { return mul; }
  35. });
  36. const sum = (num1, num2) => {
  37. return num1 + num2;
  38. }
  39. const mul = (num1, num2) => {
  40. return num1 * num2;
  41. }
  42. })
  43. });
  44. var __webpack_module_cache__ = {};
  45. // The require function
  46. function __webpack_require__(moduleId) {
  47. // Check if module is in cache
  48. if (__webpack_module_cache__[moduleId]) {
  49. return __webpack_module_cache__[moduleId].exports;
  50. }
  51. // Create a new module (and put it into the cache)
  52. var module = __webpack_module_cache__[moduleId] = {
  53. // no module.id needed
  54. // no module.loaded needed
  55. exports: {}
  56. };
  57. // Execute the module function
  58. __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
  59. // Return the exports of the module
  60. return module.exports;
  61. }
  62. !function () {
  63. // getDefaultExport function for compatibility with non-harmony modules
  64. __webpack_require__.n = function (module) {
  65. var getter = module && module.__esModule ?
  66. function () { return module['default']; } :
  67. function () { return module; };
  68. __webpack_require__.d(getter, { a: getter });
  69. return getter;
  70. };
  71. }();
  72. /* webpack/runtime/define property getters */
  73. !function () {
  74. // define getter functions for harmony exports
  75. __webpack_require__.d = function (exports, definition) {
  76. for (var key in definition) {
  77. if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  78. Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  79. }
  80. }
  81. };
  82. }();
  83. /* webpack/runtime/hasOwnProperty shorthand */
  84. !function () {
  85. __webpack_require__.o = function (obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
  86. }();
  87. /* webpack/runtime/make namespace object */
  88. !function () {
  89. // define __esModule on exports
  90. __webpack_require__.r = function (exports) {
  91. if (typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  92. Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  93. }
  94. Object.defineProperty(exports, '__esModule', { value: true });
  95. };
  96. }();
  97. __webpack_require__("./src/index.js");

转载请注明作者和出处,并添加本页链接。
原文链接: //www.v2ci.com/33.html