最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

浅谈Webpack 是如何加载模块的

来源:懂视网 责编:小采 时间:2020-11-27 22:14:05
文档

浅谈Webpack 是如何加载模块的

浅谈Webpack 是如何加载模块的:Webpack 在前端开发中作为模块打包工具非常受开发者的青睐,丰富的 loader 使它可以实现各种各样的功能。本文将通过 webpack 来打包一个 js 文件,看看 webpack 是如何加载各个模块的。 两个简单的源文件 为了方便分析 webpack 加载模块的原理,我
推荐度:
导读浅谈Webpack 是如何加载模块的:Webpack 在前端开发中作为模块打包工具非常受开发者的青睐,丰富的 loader 使它可以实现各种各样的功能。本文将通过 webpack 来打包一个 js 文件,看看 webpack 是如何加载各个模块的。 两个简单的源文件 为了方便分析 webpack 加载模块的原理,我

Webpack 在前端开发中作为模块打包工具非常受开发者的青睐,丰富的 loader 使它可以实现各种各样的功能。本文将通过 webpack 来打包一个 js 文件,看看 webpack 是如何加载各个模块的。

两个简单的源文件

为了方便分析 webpack 加载模块的原理,我们准备了两个文件:

hello.js

const hello = {
 say: arg => {
 console.info('hello ' + arg || 'world');
 }
};

export default hello;

index.js

import Hello from './hello';

Hello.say('man');

index.js 作为入口文件,引用了 hello.js 模块。

Webpack 打包

在命令行执行 webpack index.js bundle.js 对入口文件进行打包,生成 bundle.js ,大体结构为(为了方便阅读,我删除了部分多余的代码):

可以看到,最终生成的文件以 (function (modules) {})([模块1, 模块2]) 的方式启动,我们定义的模块被包装成一个个匿名函数,然后以数组的形式传递个一个匿名函数 function (modules) {},在这个匿名函数中定义了一个 __webpack_require__() 函数,用来加载模块,最后,通过 return __webpack_require__(__webpack_require__.s = 0); 来加载第一个模块 index.js

__webpack_require__() 函数

该函数接收一个 moduleId 作为参数,这个参数就是各个模块在数组中的索引,

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;
 /******/
 }

其中 installedModules 是用来缓存执行过的模块。通过 modules[moduleId].call() 来执行模块,最后返回模块的 exports。

模块接受的参数

以 hello.js 模块为例

 (function (module, __webpack_exports__, __webpack_require__) {

 "use strict";
 const hello = {
 say: arg => {
 console.info('hello ' + arg || 'world');
 }
 };

 /* harmony default export */
 __webpack_exports__["a"] = (hello);

 /***/
 })

webpack 会向模块传递 module, __webpack_exports__, __webpack_require__ 三个参数,前两个是用来导出模块内的变量,第三个参数为前面介绍的 __webpack_require__() 的引用,用来导入其它模块。

文档

浅谈Webpack 是如何加载模块的

浅谈Webpack 是如何加载模块的:Webpack 在前端开发中作为模块打包工具非常受开发者的青睐,丰富的 loader 使它可以实现各种各样的功能。本文将通过 webpack 来打包一个 js 文件,看看 webpack 是如何加载各个模块的。 两个简单的源文件 为了方便分析 webpack 加载模块的原理,我
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top