摘要:一般会帮我们把所有的文件,,图片等编译成一个文件安装,使用,一般这个文件名为。当被加载到浏览器这个函数就会立即执行。模块缓存模块缓存所有被加载过的模块都会成为对象的属性。这一步主要是靠函数做到的。
webpack一般会帮我们把所有的文件(js,css,图片等)编译成一个js文件(webpack安装,使用),一般这个文件名为bundle.js。我们直接在html文件中用script标签引入就行了,就想下面这样:
</>复制代码
那bundle.js文件如何组织我们的资源文件的呢,我们下面通过一个例子看一下
创建entry.js文件
</>复制代码
module.exports = "我是入口js!";
运行下面命令
</>复制代码
webpack ./entry.js bundle.js
会生成bundle.js文件
</>复制代码
/******/ (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] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function 执行模块方法
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded 标记已被加载的模块
/******/ module.loaded = 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;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports) {
module.exports = "我是入口js!";
/***/ }
/******/ ]);
其实bundle.js文件里就是一个立即调用的函数表达式(Immediately-Invoked Function Expression)像下面这样:
</>复制代码
(function(/*parameter*/){
/* code */
})(/*argument*/)
这个函数里定义了一个变量installedModules和一个函数function __webpack_require__(moduleId)。当bundle.js被加载到浏览器这个函数就会立即执行。
</>复制代码
// The module cache 模块缓存
var installedModules = {};
所有被加载过的模块都会成为installedModules对象的属性。这一步主要是靠函数__webpack_require__做到的。
</>复制代码
// 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] = {
exports: {},
id: moduleId,
loaded: false
};
// Execute the module function 执行模块方法
modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
// Flag the module as loaded 标记已被加载的模块
module.loaded = true;
// Return the exports of the module 返回模块的输出
return module.exports;
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/49681.html
摘要:一般会帮我们把所有的文件,,图片等编译成一个文件安装,使用,一般这个文件名为。当被加载到浏览器这个函数就会立即执行。模块缓存模块缓存所有被加载过的模块都会成为对象的属性。这一步主要是靠函数做到的。 webpack一般会帮我们把所有的文件(js,css,图片等)编译成一个js文件(webpack安装,使用),一般这个文件名为bundle.js。我们直接在html文件中用script标签引...
摘要:一般会帮我们把所有的文件,,图片等编译成一个文件安装,使用,一般这个文件名为。当被加载到浏览器这个函数就会立即执行。模块缓存模块缓存所有被加载过的模块都会成为对象的属性。这一步主要是靠函数做到的。 webpack一般会帮我们把所有的文件(js,css,图片等)编译成一个js文件(webpack安装,使用),一般这个文件名为bundle.js。我们直接在html文件中用script标签引...
阅读 1997·2021-11-22 09:34
阅读 1215·2021-10-09 09:44
阅读 3079·2021-09-29 09:35
阅读 3665·2021-09-14 18:01
阅读 1538·2021-08-16 10:49
阅读 1138·2019-08-29 14:11
阅读 889·2019-08-29 12:47
阅读 3114·2019-08-26 13:47
极致性价比!云服务器续费无忧!
Tesla A100/A800、Tesla V100S等多种GPU云主机特惠2折起,不限台数,续费同价。
NVIDIA RTX 40系,高性价比推理显卡,满足AI应用场景需要。
乌兰察布+上海青浦,满足东推西训AI场景需要