要想让打包后的文件可以阅读需要配置
devtool: false
打包时使用命令
webpack --mode developmet
使用 https://www.yuque.com/impressioncr/xosifb/eu2fgg 打包的dist文件分析。
语雀内容
1.Chunks文件
a.js
module.exports = "a";
b.js
module.exports = "b";
a_js-b_js.chunk.js
(window["webpackJsonp"] = window["webpackJsonp"] || []).push([["a_js-b_js"],{/***/ "./a.js":/*!**************!*\!*** ./a.js ***!\**************//*! unknown exports (runtime-defined) *//*! runtime requirements: module *//*! CommonJS bailout: module.exports is used directly at 1:0-14 *//***/ ((module) => {module.exports = "a";/***/ }),/***/ "./b.js":/*!**************!*\!*** ./b.js ***!\**************//*! unknown exports (runtime-defined) *//*! runtime requirements: module *//*! CommonJS bailout: module.exports is used directly at 1:0-14 *//***/ ((module) => {module.exports = "b";/***/ })}]);
可以看到chunk的代码就是 在webpackJsonp数组里push一个当前的模块。将代码在chrome控制台运行。
可以看到webpackJsonp数组第一项即为a_js-b_js模块。模块也是一个数组。
webpackJsonp[0][1]是一个对象,为模块具体代码,key为单个模块的路径,value为一个方法即对应的key里js的代码。
2.bundle文件
pageA.js
require(["./common"], function(common) {common(require("./a"));});
pageA.bundle.js
/******/ (() => { // webpackBootstrap/******/ var __webpack_modules__ = ({});/************************************************************************//******/ // The module cache/******/ var __webpack_module_cache__ = {};/******//******/ // The require function/******/ function __webpack_require__(moduleId) {/******/ // Check if module is in cache/******/ if(__webpack_module_cache__[moduleId]) {/******/ return __webpack_module_cache__[moduleId].exports;/******/ }/******/ // Create a new module (and put it into the cache)/******/ var module = __webpack_module_cache__[moduleId] = {/******/ // no module.id needed/******/ // no module.loaded needed/******/ exports: {}/******/ };/******//******/ // Execute the module function/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);/******//******/ // Return the exports of the module/******/ return module.exports;/******/ }/******//******/ // expose the modules object (__webpack_modules__)/******/ __webpack_require__.m = __webpack_modules__;/******//************************************************************************//******/ /* webpack/runtime/ensure chunk *//******/ (() => {/******/ __webpack_require__.f = {};/******/ // This file contains only the entry chunk./******/ // The chunk loading function for additional chunks/******/ __webpack_require__.e = (chunkId) => {/******/ return Promise.all(Object.keys(__webpack_require__.f).reduce((promises, key) => {/******/ __webpack_require__.f[key](chunkId, promises);/******/ return promises;/******/ }, []));/******/ };/******/ })();/******//******/ /* webpack/runtime/get javascript chunk filename *//******/ (() => {/******/ // This function allow to reference async chunks/******/ __webpack_require__.u = (chunkId) => {/******/ // return url for filenames based on template/******/ return "" + chunkId + ".chunk.js";/******/ };/******/ })();/******//******/ /* webpack/runtime/hasOwnProperty shorthand *//******/ (() => {/******/ __webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)/******/ })();/******//******/ /* webpack/runtime/load script *//******/ (() => {/******/ var inProgress = {};/******/ // data-webpack is not used as build has no uniqueName/******/ // loadScript function to load a script via script tag/******/ __webpack_require__.l = (url, done, key) => {/******/ if(inProgress[url]) { inProgress[url].push(done); return; }/******/ var script, needAttach;/******/ if(key !== undefined) {/******/ var scripts = document.getElementsByTagName("script");/******/ for(var i = 0; i < scripts.length; i++) {/******/ var s = scripts[i];/******/ if(s.getAttribute("src") == url) { script = s; break; }/******/ }/******/ }/******/ if(!script) {/******/ needAttach = true;/******/ script = document.createElement('script');/******//******/ script.charset = 'utf-8';/******/ script.timeout = 120;/******/ if (__webpack_require__.nc) {/******/ script.setAttribute("nonce", __webpack_require__.nc);/******/ }/******//******/ script.src = url;/******/ }/******/ inProgress[url] = [done];/******/ var onScriptComplete = (prev, event) => {/******/ // avoid mem leaks in IE./******/ script.onerror = script.onload = null;/******/ clearTimeout(timeout);/******/ var doneFns = inProgress[url];/******/ delete inProgress[url];/******/ script.parentNode && script.parentNode.removeChild(script);/******/ doneFns && doneFns.forEach((fn) => fn(event));/******/ if(prev) return prev(event);/******/ }/******/ ;/******/ var timeout = setTimeout(onScriptComplete.bind(null, undefined, { type: 'timeout', target: script }), 120000);/******/ script.onerror = onScriptComplete.bind(null, script.onerror);/******/ script.onload = onScriptComplete.bind(null, script.onload);/******/ needAttach && document.head.appendChild(script);/******/ };/******/ })();/******//******/ /* webpack/runtime/publicPath *//******/ (() => {/******/ __webpack_require__.p = "";/******/ })();/******//******/ /* webpack/runtime/jsonp chunk loading *//******/ (() => {/******/ // object to store loaded and loading chunks/******/ // undefined = chunk not loaded, null = chunk preloaded/prefetched/******/ // Promise = chunk loading, 0 = chunk loaded/******/ var installedChunks = {/******/ "pageA": 0/******/ };/******//******//******/ __webpack_require__.f.j = (chunkId, promises) => {/******/ // JSONP chunk loading for javascript/******/ var installedChunkData = __webpack_require__.o(installedChunks, chunkId) ? installedChunks[chunkId] : undefined;/******/ if(installedChunkData !== 0) { // 0 means "already installed"./******//******/ // a Promise means "currently loading"./******/ if(installedChunkData) {/******/ promises.push(installedChunkData[2]);/******/ } else {/******/ if(true) { // all chunks have JS/******/ // setup Promise in chunk cache/******/ var promise = new Promise((resolve, reject) => {/******/ installedChunkData = installedChunks[chunkId] = [resolve, reject];/******/ });/******/ promises.push(installedChunkData[2] = promise);/******//******/ // start chunk loading/******/ var url = __webpack_require__.p + __webpack_require__.u(chunkId);/******/ // create error before stack unwound to get useful stacktrace later/******/ var error = new Error();/******/ var loadingEnded = (event) => {/******/ if(__webpack_require__.o(installedChunks, chunkId)) {/******/ installedChunkData = installedChunks[chunkId];/******/ if(installedChunkData !== 0) installedChunks[chunkId] = undefined;/******/ if(installedChunkData) {/******/ var errorType = event && (event.type === 'load' ? 'missing' : event.type);/******/ var realSrc = event && event.target && event.target.src;/******/ error.message = 'Loading chunk ' + chunkId + ' failed.\n(' + errorType + ': ' + realSrc + ')';/******/ error.name = 'ChunkLoadError';/******/ error.type = errorType;/******/ error.request = realSrc;/******/ installedChunkData[1](error);/******/ }/******/ }/******/ };/******/ __webpack_require__.l(url, loadingEnded, "chunk-" + chunkId);/******/ } else installedChunks[chunkId] = 0;/******/ }/******/ }/******/ };/******//******/ // no prefetching/******//******/ // no preloaded/******//******/ // no HMR/******//******/ // no HMR manifest/******//******/ // no deferred startup/******//******/ // install a JSONP callback for chunk loading/******/ function webpackJsonpCallback(data) {/******/ var chunkIds = data[0];/******/ var moreModules = data[1];/******//******/ var runtime = data[3];/******/ // add "moreModules" to the modules object,/******/ // then flag all "chunkIds" as loaded and fire callback/******/ var moduleId, chunkId, i = 0, resolves = [];/******/ for(;i < chunkIds.length; i++) {/******/ chunkId = chunkIds[i];/******/ if(__webpack_require__.o(installedChunks, chunkId) && installedChunks[chunkId]) {/******/ resolves.push(installedChunks[chunkId][0]);/******/ }/******/ installedChunks[chunkId] = 0;/******/ }/******/ for(moduleId in moreModules) {/******/ if(__webpack_require__.o(moreModules, moduleId)) {/******/ __webpack_require__.m[moduleId] = moreModules[moduleId];/******/ }/******/ }/******/ if(runtime) runtime(__webpack_require__);/******/ if(parentJsonpFunction) parentJsonpFunction(data);/******/ while(resolves.length) {/******/ resolves.shift()();/******/ }/******//******/ };/******//******/ var jsonpArray = window["webpackJsonp"] = window["webpackJsonp"] || [];/******/ var oldJsonpFunction = jsonpArray.push.bind(jsonpArray);/******/ jsonpArray.push = webpackJsonpCallback;/******/ var parentJsonpFunction = oldJsonpFunction;/******/ })();/******//************************************************************************//*!******************!*\!*** ./pageA.js ***!\******************//*! unknown exports (runtime-defined) *//*! runtime requirements: __webpack_require__, __webpack_require__.e, __webpack_require__.oe, __webpack_require__.* */__webpack_require__.e(/*! AMD require */ "a_js-b_js-common_js").then(function() { var __WEBPACK_AMD_REQUIRE_ARRAY__ = [__webpack_require__(/*! ./common */ "./common.js")]; (function(common) {common(__webpack_require__(/*! ./a */ "./a.js"));}).apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__);}).catch(__webpack_require__.oe);/******/ })();
定义变量webpack_modules和webpack_module_cache
var __webpack_modules__ = ({});var __webpack_module_cache__ = {};
webpackrequire__ 方法
/ 定义 __webpack_require__ 方法 /function __webpack_require__(moduleId) {// 先在在缓存对象 __webpack_module_cache__ 里找if(__webpack_module_cache__[moduleId]) {return __webpack_module_cache__[moduleId].exports;}// Create a new module (and put it into the cache)var module = __webpack_module_cache__[moduleId] = {// no module.id needed// no module.loaded neededexports: {}};// 执行模块里的方法__webpack_modules__[moduleId](module, module.exports, __webpack_require__);/******/// 返回模块的exportsreturn module.exports;}
ensure 方法
(() => {__webpack_require__.f = {};// This file contains only the entry chunk.// The chunk loading function for additional chunks__webpack_require__.e = (chunkId) => {return Promise.all(Object.keys(__webpack_require__.f).reduce((promises, key) => {__webpack_require__.f[key](chunkId, promises);return promises;}, []));};})();
获取js文件名的方法
(() => {// This function allow to reference async chunks__webpack_require__.u = (chunkId) => {// return url for filenames based on templatereturn "" + chunkId + ".chunk.js";};})();
hasOwnProperty方法
(() => {__webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)})();
加载脚本的方法
/* webpack/runtime/load script */(() => {var inProgress = {};/ ... /})();
publicPath变量
/* webpack/runtime/publicPath */(() => {__webpack_require__.p = "";})();
加载chunks
/* webpack/runtime/jsonp chunk loading */(() => {/ ... /})();
执行模块
__webpack_require__.e(/*! AMD require */ "a_js-b_js-common_js").then(function() { var __WEBPACK_AMD_REQUIRE_ARRAY__ = [__webpack_require__(/*! ./common */ "./common.js")]; (function(common) {common(__webpack_require__(/*! ./a */ "./a.js"));}).apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__);}).catch(__webpack_require__.oe);
