You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
webpack 中每个模块都会有不同的 id,也就是 modules 数组中的 index 值(0,1,2.....)
真正执行 index.js 的是 modules[moduleId].call()
(function(modules){// webpackBootstrap// The module cache// 模块缓存varinstalledModules={}// The require function// webpack require 函数function__webpack_require__(moduleId){// Check if module is in cache// 检查模块是否被缓存if(installedModules[moduleId]){returninstalledModules[moduleId].exports}// Create a new module (and put it into the cache)// 创建一个新模块并放入缓存中varmodule=installedModules[moduleId]={i: moduleId,l: false,exports: {}}// Execute the module function// 执行模块函数// 此处可以看出直接使用 exports = xxx 将会失败,需使用 module.exports,这由 JS 的传值策略决定modules[moduleId].call(module.exports,module,module.exports,__webpack_require__)// Flag the module as loaded// 标记模块为已加载module.l=true// Return the exports of the module// 返回模块的exportsreturnmodule.exports}// expose the modules object (__webpack_modules__)// 暴露 modules 方法__webpack_require__.m=modules// expose the module cache// 暴露 modules 缓存__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){vargetter=module&&module.__esModule
? functiongetDefault(){returnmodule['default']}
: functiongetModuleExports(){returnmodule}__webpack_require__.d(getter,'a',getter)returngetter}// Object.prototype.hasOwnProperty.call__webpack_require__.o=function(object,property){returnObject.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__){varother=__webpack_require__(1)console.log('index')other.f1()}),/* 1 */(function(module,exports){exports.f1=function(){console.log('f1')}exports.f2=function(){console.log('f2')}})])
webpack 对 ES6 模块的实现
(function(modules){// webpackBootstrap// The module cachevarinstalledModules={}// The require functionfunction__webpack_require__(moduleId){// Check if module is in cacheif(installedModules[moduleId]){returninstalledModules[moduleId].exports}// Create a new module (and put it into the cache)varmodule=installedModules[moduleId]={i: moduleId,l: false,exports: {}}// Execute the module functionmodules[moduleId].call(module.exports,module,module.exports,__webpack_require__)// Flag the module as loadedmodule.l=true// Return the exports of the modulereturnmodule.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// 如果遇到 commonjs 和 es6 模块混用时,这段代码将发生作用// __webpack_require__.n 会判断 module 是否为 es6 模块,// 当 __esModule 为 true 的时候,那么 module.a 默认返回 module.default,否则返回 module__webpack_require__.n=function(module){vargetter=module&&module.__esModule
? functiongetDefault(){returnmodule['default']}
: functiongetModuleExports(){returnmodule}__webpack_require__.d(getter,'a',getter)returngetter}// Object.prototype.hasOwnProperty.call__webpack_require__.o=function(object,property){returnObject.prototype.hasOwnProperty.call(object,property)}// __webpack_public_path____webpack_require__.p=''// Load entry module and return exportsreturn__webpack_require__(__webpack_require__.s=0)})([/* 0 */(function(module,__webpack_exports__,__webpack_require__){'use strict'Object.defineProperty(__webpack_exports__,'__esModule',{value: true})/* harmony import */var__WEBPACK_IMPORTED_MODULE_0__other_js__=__webpack_require__(1)console.log('index')Object(__WEBPACK_IMPORTED_MODULE_0__other_js__['a'/* default */])()Object(__WEBPACK_IMPORTED_MODULE_0__other_js__['b'/* f2 */])()}),/* 1 */(function(module,__webpack_exports__,__webpack_require__){'use strict'/* harmony export (immutable) */__webpack_exports__['a']=f1/* harmony export (immutable) */__webpack_exports__['b']=f2functionf1(){console.log('f1')}functionf2(){console.log('f2')}})])
webpack 对基于 import() 动态导入进行代码分离的实现
bundle.js
// 分离代码为 0.0.bundle.js(function(modules){// webpackBootstrap// install a JSONP callback for chunk loadingvarparentJsonpFunction=window['webpackJsonp']window['webpackJsonp']=functionwebpackJsonpCallback(chunkIds,moreModules,executeModules){// add "moreModules" to the modules object,// then flag all "chunkIds" as loaded and fire callbackvarmoduleId,chunkId,i=0,resolves=[],resultfor(;i<chunkIds.length;i++){chunkId=chunkIds[i]if(installedChunks[chunkId]){resolves.push(installedChunks[chunkId][0])}installedChunks[chunkId]=0}for(moduleIdinmoreModules){if(Object.prototype.hasOwnProperty.call(moreModules,moduleId)){modules[moduleId]=moreModules[moduleId]}}if(parentJsonpFunction)parentJsonpFunction(chunkIds,moreModules,executeModules)while(resolves.length){resolves.shift()()}}// The module cachevarinstalledModules={}// objects to store loaded and loading chunksvarinstalledChunks={1: 0}// The require functionfunction__webpack_require__(moduleId){// Check if module is in cacheif(installedModules[moduleId]){returninstalledModules[moduleId].exports}// Create a new module (and put it into the cache)varmodule=installedModules[moduleId]={i: moduleId,l: false,exports: {}}// Execute the module functionmodules[moduleId].call(module.exports,module,module.exports,__webpack_require__)// Flag the module as loadedmodule.l=true// Return the exports of the modulereturnmodule.exports}// This file contains only the entry chunk.// The chunk loading function for additional chunks// 对于 code splitting 的支持,webpack 使用 __webpack_require__.e 实现基于 promise 的动态加载__webpack_require__.e=functionrequireEnsure(chunkId){varinstalledChunkData=installedChunks[chunkId]// 当请求同一脚本文件时,由于对应的 module 已经被加载,因而直接返回一个成功的 promise 即可if(installedChunkData===0){returnnewPromise(function(resolve){resolve()})}// a Promise means "currently loading".// 取缓存的 promiseif(installedChunkData){returninstalledChunkData[2]}// setup Promise in chunk cachevarpromise=newPromise(function(resolve,reject){installedChunkData=installedChunks[chunkId]=[resolve,reject]})// 将数组的第三项赋值为这个 promise,用于缓存 promiseinstalledChunkData[2]=promise// start chunk loading// 创建一个 script 标签动态加载脚本varhead=document.getElementsByTagName('head')[0]varscript=document.createElement('script')script.type='text/javascript'script.charset='utf-8'script.async=truescript.timeout=120000if(__webpack_require__.nc){script.setAttribute('nonce',__webpack_require__.nc)}script.src=__webpack_require__.p+''+chunkId+'.'+chunkId+'.bundle.js'vartimeout=setTimeout(onScriptComplete,120000)script.onerror=script.onload=onScriptCompletefunctiononScriptComplete(){// avoid mem leaks in IE.script.onerror=script.onload=nullclearTimeout(timeout)varchunk=installedChunks[chunkId]// 由 webpackJsonpCallback 方法得知脚本文件下载成功后,installedChunks[chunkId] = 0// 脚本下载失败的情况if(chunk!==0){if(chunk){chunk[1](newError('Loading chunk '+chunkId+' failed.'))}installedChunks[chunkId]=undefined}}head.appendChild(script)returnpromise}// 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){vargetter=module&&module.__esModule
? functiongetDefault(){returnmodule['default']}
: functiongetModuleExports(){returnmodule}__webpack_require__.d(getter,'a',getter)returngetter}// Object.prototype.hasOwnProperty.call__webpack_require__.o=function(object,property){returnObject.prototype.hasOwnProperty.call(object,property)}// __webpack_public_path____webpack_require__.p=''// on error function for async loading__webpack_require__.oe=function(err){console.error(err)throwerr}// Load entry module and return exportsreturn__webpack_require__(__webpack_require__.s=0)})([/* 0 */(function(module,__webpack_exports__,__webpack_require__){'use strict'Object.defineProperty(__webpack_exports__,'__esModule',{value: true})/* harmony import */var__WEBPACK_IMPORTED_MODULE_0__other_js__=__webpack_require__(1)console.log('index')Object(__WEBPACK_IMPORTED_MODULE_0__other_js__['a'/* default */])()Object(__WEBPACK_IMPORTED_MODULE_0__other_js__['b'/* f2 */])()__webpack_require__.e/* import() */(0).then(__webpack_require__.bind(null,2)).then(foo=>{console.log(foo.fa())})}),/* 1 */(function(module,__webpack_exports__,__webpack_require__){'use strict'/* harmony export (immutable) */__webpack_exports__['a']=f1/* harmony export (immutable) */__webpack_exports__['b']=f2functionf1(){console.log('f1')}functionf2(){console.log('f2')}})])
webpack 支持大多数模块系统包括:ES2015 import 语句,CommonJS require() 语句和 AMD define 和 require 语句
webpack 对 CommonJS 模块的实现
传入参数是一个数组,数组首项包含一个函数,函数为 index.js 里的内容。
__webpack_require_
是模块加载函数,接收模块idwebpack 中每个模块都会有不同的 id,也就是 modules 数组中的 index 值(0,1,2.....)
modules[moduleId].call()
webpack 对 ES6 模块的实现
webpack 对基于 import() 动态导入进行代码分离的实现
bundle.js
0.0.bundle.js
The text was updated successfully, but these errors were encountered: