webpack打包结果原理解读

文章详细解析了Webpack打包过程中生成的立即执行函数(IIFE)的工作原理,以及__webpack_require__函数如何实现模块的加载和执行。通过示例代码展示了模块的缓存、执行过程,以及如何处理模块间的依赖关系。
摘要由CSDN通过智能技术生成

运行 yarn webpack --mode none

打包结果

生成了一个立即执行函数IIFE,工作入口

/******/ (function(modules) { // webpackBootstrap --工作入库
/******/ 	// The module cache
/******/ 	var installedModules = {}; // 缓存模块
/******/
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {// 第一次moduleId为0
/******/
/******/ 		// 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是立即执行函数传进来的函数数组参数
/******/ 		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;
/******/  }
/******/
/******/ 	// Load entry module and return exports 
/******/  // 开始调用__webpack_require__函数,初始化调用参数数组的第一个元素,这里才开始加载模块
/******/ 	return __webpack_require__(__webpack_require__.s = 0);          
/******/ })
/************************************************************************/
/******/ ([
  /***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
/***/ }),
/******/ ]);

立即执行函数是入口,参数是数组,数组中每一个函数
第一执行 webpack_require(0) // moduleId为0

/******/ 	// Load entry module and return exports 
/******/  // 开始调用__webpack_require__函数,初始化调用参数数组的第一个元素,这里才开始加载模块
/******/ 	return __webpack_require__(__webpack_require__.s = 0);       

webpack_require 函数实现

/******/ 	function __webpack_require__(moduleId) { // 第一次moduleId为0
/******/
/******/ 		// 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, // 数组下标为模块id
/******/ 			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;
/******/ 	}

加载模块

/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

执行模块里面的函数,使用严格模式,调用__webpack_require__.r函数

/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);

// 这里调用__webpack_require__,参数是1,该模块用来加载依赖的第一个模块
/* harmony import */ var _head_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);


const head = Object(_head_js__WEBPACK_IMPORTED_MODULE_0__["default"])();

document.body.append(head);


/***/ }),

调用__webpack_require__.r函数,添加标识,用来标识是__esModule

/******/ 	// define __esModule on exports
/******/ 	__webpack_require__.r = function(exports) {
/******/ 		if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 			Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 		}
/******/ 		Object.defineProperty(exports, '__esModule', { value: true });
/******/ 	};

接下该模块内部加载依赖的模块,它依赖第一个模块,(从这里可以看出,不能自己引用自己

/* harmony import */ var _head_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);

开始调用该模块

const head = Object(_head_js__WEBPACK_IMPORTED_MODULE_0__["default"])();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值