/**/n * Generate the code for the JsonpChunkLoadingRuntimeModule/n * @returns {string} The generated code/n *//ngenerate() {/n const { chunkGraph, compilation, chunk } = this;/n const { /n runtimeTemplate,/n outputOptions: { /n chunkLoadingGlobal,/n hotUpdateGlobal,/n crossOriginLoading,/n scriptType/n }/n } = compilation;/n const globalObject = runtimeTemplate.globalObject;/n const { linkPreload, linkPrefetch } = /n JsonpChunkLoadingRuntimeModule.getCompilationHooks(compilation);/n const fn = RuntimeGlobals.ensureChunkHandlers;/n const withBaseURI = this._runtimeRequirements.has(RuntimeGlobals.baseURI);/n const withLoading = this._runtimeRequirements.has(/n RuntimeGlobals.ensureChunkHandlers/n );/n const withCallback = this._runtimeRequirements.has(/n RuntimeGlobals.chunkCallback/n );/n const withOnChunkLoad = this._runtimeRequirements.has(/n RuntimeGlobals.onChunksLoaded/n );/n const withHmr = this._runtimeRequirements.has(/n RuntimeGlobals.hmrDownloadUpdateHandlers/n );/n const withHmrManifest = this._runtimeRequirements.has(/n RuntimeGlobals.hmrDownloadManifest/n );/n const withPrefetch = this._runtimeRequirements.has(/n RuntimeGlobals.prefetchChunkHandlers/n );/n const withPreload = this._runtimeRequirements.has(/n RuntimeGlobals.preloadChunkHandlers/n );/n const chunkLoadingGlobalExpr = ${globalObject}[${JSON.stringify(/n chunkLoadingGlobal/n )}];/n const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);/n const hasJsMatcher = compileBooleanMatcher(conditionMap);/n const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);/n/n const stateExpression = withHmr/n ? ${RuntimeGlobals.hmrRuntimeStatePrefix}_jsonp/n : undefined;/n/n return Template.asString([/n withBaseURI/n ? Template.asString([/n ${RuntimeGlobals.baseURI} = document.baseURI || self.location.href;/n ])/n : '// no baseURI',/n '',/n '// object to store loaded and loading chunks',/n '// undefined = chunk not loaded, null = chunk preloaded/prefetched',/n '// [resolve, reject, Promise] = chunk loading, 0 = chunk loaded',/n var installedChunks = ${ /n stateExpression ? ${stateExpression} = ${stateExpression} || : ''/n }{,/n Template.indent(/n Array.from(initialChunkIds, id => ${JSON.stringify(id)}: 0).join(/n ',/n' )/n ),/n '};',/n '',/n withLoading/n ? Template.asString([/n ${fn}.j = ${runtimeTemplate.basicFunction(/n 'chunkId, promises',/n hasJsMatcher !== false/n ? Template.indent([/n '// JSONP chunk loading for javascript',/n var installedChunkData = ${RuntimeGlobals.hasOwnProperty}(installedChunks, chunkId) ? installedChunks[chunkId] : undefined;,/n 'if(installedChunkData !== 0) { // 0 means 'already installed'.',/n Template.indent([/n '',/n '// a Promise means 'currently loading'.',/n 'if(installedChunkData) {',/n Template.indent([/n 'promises.push(installedChunkData[2]);'/n ]),/n '} else {',/n Template.indent([/n hasJsMatcher === true/n ? 'if(true) { // all chunks have JS'/n : if(${hasJsMatcher('chunkId')}) {,/n Template.indent([/n '// setup Promise in chunk cache',/n var promise = new Promise(${runtimeTemplate.expressionFunction(/n installedChunkData = installedChunks[chunkId] = [resolve, reject],/n 'resolve, reject'/n )});,/n 'promises.push(installedChunkData[2] = promise);',/n '',/n '// start chunk loading',/n var url = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkScriptFilename}(chunkId);,/n '// create error before stack unwound to get useful stacktrace later',/n 'var error = new Error();',/n var loadingEnded = ${runtimeTemplate.basicFunction('event', [/n if(${RuntimeGlobals.hasOwnProperty}(installedChunks, chunkId)) {,/n Template.indent([/n 'installedChunkData = installedChunks[chunkId];',/n 'if(installedChunkData !== 0) installedChunks[chunkId] = undefined;',/n 'if(installedChunkData) {',/n Template.indent([/n 'var errorType = event && (event.type === /'load/' ? /'missing/' : event.type);',/n 'var realSrc = event && event.target && event.target.src;',/n 'error.message = /'Loading chunk /' + chunkId + /' failed.//n(/' + errorType + /': /' + realSrc + /')/';',/n 'error.name = /'ChunkLoadError/';',/n 'error.type = errorType;',/n 'error.request = realSrc;',/n 'installedChunkData1;'/n ]),/n '}'/n ]),/n '}'/n ])};,/n ${RuntimeGlobals.loadScript}(url, loadingEnded, 'chunk-' + chunkId, chunkId);/n ]),/n '} else installedChunks[chunkId] = 0;'/n ]),/n '}'/n ]),/n '}'/n ])/n : Template.indent(['installedChunks[chunkId] = 0;'])/n )};/n ])/n : '// no chunk on demand loading',/n '',/n withPrefetch && hasJsMatcher !== false/n ? ${/n RuntimeGlobals.prefetchChunkHandlers/n }.j = ${runtimeTemplate.basicFunction('chunkId', [/n if((!${ /n RuntimeGlobals.hasOwnProperty/n }(installedChunks, chunkId) || installedChunks[chunkId] === undefined) && ${ /n hasJsMatcher === true ? 'true' : hasJsMatcher('chunkId')/n }) {,/n Template.indent([/n 'installedChunks[chunkId] = null;',/n linkPrefetch.call(/n Template.asString([/n 'var link = document.createElement(/'link/');',/n crossOriginLoading/n ? link.crossOrigin = ${JSON.stringify(/n crossOriginLoading/n )};/n : '',/n if (${RuntimeGlobals.scriptNonce}) {,/n Template.indent(/n link.setAttribute('nonce', ${RuntimeGlobals.scriptNonce});/n ),/n '}',/n 'link.rel = 'prefetch';',/n 'link.as = 'script';',/n link.href = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkScriptFilename}(chunkId);/n ]),/n chunk/n ),/n 'document.head.appendChild(link);'/n ]),/n '}'/n ])};/n : '// no prefetching',/n '',/n withPreload && hasJsMatcher !== false/n ? ${/n RuntimeGlobals.preloadChunkHandlers/n }.j = ${runtimeTemplate.basicFunction('chunkId', [/n if((!${ /n RuntimeGlobals.hasOwnProperty/n }(installedChunks, chunkId) || installedChunks[chunkId] === undefined) && ${ /n hasJsMatcher === true ? 'true' : hasJsMatcher('chunkId')/n }) {,/n Template.indent([/n 'installedChunks[chunkId] = null;',/n linkPreload.call(/n Template.asString([/n 'var link = document.createElement(/'link/');',/n scriptType/n ? link.type = ${JSON.stringify(scriptType)};/n : '',/n 'link.charset = /'utf-8/';',/n if (${RuntimeGlobals.scriptNonce}) {,/n Template.indent(/n link.setAttribute('nonce', ${RuntimeGlobals.scriptNonce});/n ),/n '}',/n 'link.rel = 'preload';',/n 'link.as = 'script';',/n link.href = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkScriptFilename}(chunkId);,/n crossOriginLoading/n ? Template.asString([/n 'if (link.href.indexOf(window.location.origin + /'//') !== 0) {',/n Template.indent(/n link.crossOrigin = ${JSON.stringify(/n crossOriginLoading/n )};/n ),/n '}'/n ])/n : ''/n ]),/n chunk/n ),/n 'document.head.appendChild(link);'/n ]),/n '}'/n ])};/n : '// no preloaded',/n '',/n withHmr/n ? Template.asString([/n 'var currentUpdatedModulesList;',/n 'var waitingUpdateResolves = {};',/n 'function loadUpdateChunk(chunkId) {',/n Template.indent([/n return new Promise(${runtimeTemplate.basicFunction(/n 'resolve, reject',/n [/n 'waitingUpdateResolves[chunkId] = resolve;',/n '// start update chunk loading',/n var url = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkUpdateScriptFilename}(chunkId);,/n '// create error before stack unwound to get useful stacktrace later',/n 'var error = new Error();',/n var loadingEnded = ${runtimeTemplate.basicFunction('event', [/n 'if(waitingUpdateResolves[chunkId]) {',/n Template.indent([/n 'waitingUpdateResolves[chunkId] = undefined',/n 'var errorType = event && (event.type === /'load/' ? /'missing/' : event.type);',/n 'var realSrc = event && event.target && event.target.src;',/n 'error.message = /'Loading hot update chunk /' + chunkId + /' failed.//n(/' + errorType + /': /' + realSrc + /')/';',/n 'error.name = /'ChunkLoadError/';',/n 'error.type = errorType;',/n 'error.request = realSrc;',/n 'reject(error);'/n ]),/n '}'/n ])};,/n ${RuntimeGlobals.loadScript}(url, loadingEnded);/n ]/n )});/n ]),/n '}',/n '',/n ${globalObject}[${JSON.stringify(/n hotUpdateGlobal/n )}] = ${runtimeTemplate.basicFunction(/n 'chunkId, moreModules, runtime',/n [/n 'for(var moduleId in moreModules) {',/n Template.indent([/n if(${RuntimeGlobals.hasOwnProperty}(moreModules, moduleId)) {,/n Template.indent([/n 'currentUpdate[moduleId] = moreModules[moduleId];',/n 'if(currentUpdatedModulesList) currentUpdatedModulesList.push(moduleId);'/n ]),/n '}'/n ]),/n '}',/n 'if(runtime) currentUpdateRuntime.push(runtime);',/n 'if(waitingUpdateResolves[chunkId]) {',/n Template.indent([/n 'waitingUpdateResolves[chunkId]();',/n 'waitingUpdateResolves[chunkId] = undefined;'/n ]),/n '}'/n ]/n )};,/n '',/n Template.getFunctionContent(/n require('../hmr/JavascriptHotModuleReplacement.runtime.js')/n )/n .replace(//$key/$/g, 'jsonp')/n .replace(//$installedChunks/$/g, 'installedChunks')/n .replace(//$loadUpdateChunk/$/g, 'loadUpdateChunk')/n .replace(//$moduleCache/$/g, RuntimeGlobals.moduleCache)/n .replace(//$moduleFactories/$/g, RuntimeGlobals.moduleFactories)/n .replace(/n //$ensureChunkHandlers/$/g,/n RuntimeGlobals.ensureChunkHandlers/n )/n .replace(//$hasOwnProperty/$/g, RuntimeGlobals.hasOwnProperty)/n .replace(//$hmrModuleData/$/g, RuntimeGlobals.hmrModuleData)/n .replace(/n //$hmrDownloadUpdateHandlers/$/g,/n RuntimeGlobals.hmrDownloadUpdateHandlers/n )/n .replace(/n //$hmrInvalidateModuleHandlers/$/g,/n RuntimeGlobals.hmrInvalidateModuleHandlers/n )/n ])/n : '// no HMR',/n '',/n withHmrManifest/n ? Template.asString([/n ${/n RuntimeGlobals.hmrDownloadManifest/n } = ${runtimeTemplate.basicFunction('', [/n 'if (typeof fetch === 'undefined') throw new Error('No browser support: need fetch API');',/n return fetch(${RuntimeGlobals.publicPath} + ${ /n RuntimeGlobals.getUpdateManifestFilename/n }()).then(${runtimeTemplate.basicFunction('response', [/n 'if(response.status === 404) return; // no update available',/n 'if(!response.ok) throw new Error('Failed to fetch update manifest ' + response.statusText);',/n 'return response.json();'/n ])});/n ])};/n ])/n : '// no HMR manifest',/n '',/n withOnChunkLoad/n ? ${/n RuntimeGlobals.onChunksLoaded/n }.j = ${runtimeTemplate.returningFunction(/n 'installedChunks[chunkId] === 0',/n 'chunkId'/n )};/n : '// no on chunks loaded',/n '',/n withCallback || withLoading/n ? Template.asString([/n '// install a JSONP callback for chunk loading',/n var webpackJsonpCallback = ${runtimeTemplate.basicFunction(/n 'parentChunkLoadingFunction, data',/n [/n runtimeTemplate.destructureArray(/n ['chunkIds', 'moreModules', 'runtime'],/n 'data'/n ),/n '// add 'moreModules' to the modules object,',/n '// then flag all 'chunkIds' as loaded and fire callback',/n 'var moduleId, chunkId, i = 0;',/n if(chunkIds.some(${runtimeTemplate.returningFunction(/n 'installedChunks[id] !== 0',/n 'id'/n )})) {,/n Template.indent([/n 'for(moduleId in moreModules) {',/n Template.indent([/n if(${RuntimeGlobals.hasOwnProperty}(moreModules, moduleId)) {,/n Template.indent(/n ${RuntimeGlobals.moduleFactories}[moduleId] = moreModules[moduleId];/n ),/n '}'/n ]),/n '}',/n 'if(runtime) var result = runtime(__webpack_require__);'/n ]),/n '}',/n 'if(parentChunkLoadingFunction) parentChunkLoadingFunction(data);',/n 'for(;i < chunkIds.length; i++) {',/n Template.indent([/n 'chunkId = chunkIds[i];',/n if(${RuntimeGlobals.hasOwnProperty}(installedChunks, chunkId) && installedChunks[chunkId]) {,/n Template.indent('installedChunks[chunkId][0]();'),/n '}',/n 'installedChunks[chunkIds[i]] = 0;'/n ]),/n '}',/n withOnChunkLoad/n ? return ${RuntimeGlobals.onChunksLoaded}(result);/n : ''/n ]/n )},/n '',/n var chunkLoadingGlobal = ${chunkLoadingGlobalExpr} = ${chunkLoadingGlobalExpr} || [];,/n 'chunkLoadingGlobal.forEach(webpackJsonpCallback.bind(null, 0));',/n 'chunkLoadingGlobal.push = webpackJsonpCallback.bind(null, chunkLoadingGlobal.push.bind(chunkLoadingGlobal));'/n ])/n : '// no jsonp function'/n ]);/n}/n

Webpack JSONP Chunk Loading Runtime Module: Generate Code for Dynamic Imports

原文地址: http://www.cveoy.top/t/topic/oM48 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录