webpack/lib/JsonpMainTemplatePlugin.js

259 lines
9.7 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2017-02-23 00:54:16 +08:00
"use strict";
2017-02-23 00:54:16 +08:00
const Template = require("./Template");
2017-02-23 00:54:16 +08:00
class JsonpMainTemplatePlugin {
2017-02-23 00:54:16 +08:00
apply(mainTemplate) {
2017-11-08 18:32:05 +08:00
const needChunkLoadingCode = chunk => {
2017-11-09 04:28:51 +08:00
var otherChunksInEntry = chunk.getEntrypoints().some(entrypoint => entrypoint.chunks.length > 1);
2017-09-22 20:07:28 +08:00
var onDemandChunks = chunk.getNumberOfChunks() > 0;
return otherChunksInEntry || onDemandChunks;
2017-11-08 18:32:05 +08:00
};
mainTemplate.plugin("local-vars", (source, chunk) => {
if(needChunkLoadingCode(chunk)) {
2017-11-08 18:32:05 +08:00
return mainTemplate.asString([
2017-02-23 00:54:16 +08:00
source,
"",
2017-10-30 20:56:57 +08:00
"// object to store loaded and loading chunks",
2017-02-23 00:54:16 +08:00
"var installedChunks = {",
2017-11-08 18:32:05 +08:00
mainTemplate.indent(
2017-02-23 00:54:16 +08:00
chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n")
),
"};",
"",
"var scheduledModules = [];"
2017-02-23 00:54:16 +08:00
]);
}
return source;
});
2017-11-08 18:32:05 +08:00
mainTemplate.plugin("jsonp-script", (_, chunk, hash) => {
const chunkFilename = mainTemplate.outputOptions.chunkFilename;
2017-02-23 00:54:16 +08:00
const chunkMaps = chunk.getChunkMaps();
2017-11-08 18:32:05 +08:00
const crossOriginLoading = mainTemplate.outputOptions.crossOriginLoading;
const chunkLoadTimeout = mainTemplate.outputOptions.chunkLoadTimeout;
const scriptSrcPath = mainTemplate.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
hashWithLength: length => `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
chunk: {
id: "\" + chunkId + \"",
hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
hashWithLength(length) {
const shortChunkHashMap = Object.create(null);
Object.keys(chunkMaps.hash).forEach(chunkId => {
if(typeof chunkMaps.hash[chunkId] === "string")
shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(0, length);
});
return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
},
name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "`
}
});
2017-11-08 18:32:05 +08:00
return mainTemplate.asString([
2017-02-23 00:54:16 +08:00
"var script = document.createElement('script');",
"script.charset = 'utf-8';",
`script.timeout = ${chunkLoadTimeout};`,
2017-02-28 22:34:46 +08:00
crossOriginLoading ? `script.crossOrigin = ${JSON.stringify(crossOriginLoading)};` : "",
2017-11-08 18:32:05 +08:00
`if (${mainTemplate.requireFn}.nc) {`,
mainTemplate.indent(`script.setAttribute("nonce", ${mainTemplate.requireFn}.nc);`),
2017-02-23 00:54:16 +08:00
"}",
2017-11-08 18:32:05 +08:00
`script.src = ${mainTemplate.requireFn}.p + ${scriptSrcPath};`,
"var timeout = setTimeout(function(){",
2017-11-08 18:32:05 +08:00
mainTemplate.indent([
2017-09-11 18:53:30 +08:00
"onScriptComplete({ type: 'timeout', target: script });",
]),
`}, ${chunkLoadTimeout});`,
2017-02-23 00:54:16 +08:00
"script.onerror = script.onload = onScriptComplete;",
"function onScriptComplete(event) {",
2017-11-08 18:32:05 +08:00
mainTemplate.indent([
2017-02-23 00:54:16 +08:00
"// avoid mem leaks in IE.",
"script.onerror = script.onload = null;",
"clearTimeout(timeout);",
"var chunk = installedChunks[chunkId];",
"if(chunk !== 0) {",
2017-11-08 18:32:05 +08:00
mainTemplate.indent([
2017-04-09 15:45:56 +08:00
"if(chunk) {",
2017-11-08 18:32:05 +08:00
mainTemplate.indent([
2017-09-11 18:53:30 +08:00
"var errorType = event && (event.type === 'load' ? 'missing' : event.type);",
"var realSrc = event && event.target && event.target.src;",
"var error = new Error('Loading chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')');",
"error.type = errorType;",
"error.request = realSrc;",
"chunk[1](error);"
]),
2017-04-09 15:45:56 +08:00
"}",
2017-02-23 00:54:16 +08:00
"installedChunks[chunkId] = undefined;"
]),
"}"
]),
2017-02-23 00:54:16 +08:00
"};",
]);
2017-02-23 00:54:16 +08:00
});
2017-10-30 20:56:57 +08:00
mainTemplate.plugin("require-ensure", (source, chunk, hash) => {
2017-11-08 18:32:05 +08:00
return mainTemplate.asString([
2017-10-30 20:56:57 +08:00
source,
"",
2017-10-30 20:56:57 +08:00
"// JSONP chunk loading for javascript",
2017-02-23 00:54:16 +08:00
"",
2017-10-30 20:56:57 +08:00
"var installedChunkData = installedChunks[chunkId];",
"if(installedChunkData !== 0) { // 0 means \"already installed\".",
2017-11-08 18:32:05 +08:00
mainTemplate.indent([
2017-10-30 20:56:57 +08:00
"",
"// a Promise means \"currently loading\".",
"if(installedChunkData) {",
mainTemplate.indent([
"promises.push(installedChunkData[2]);"
]),
"} else {",
mainTemplate.indent([
"// setup Promise in chunk cache",
"var promise = new Promise(function(resolve, reject) {",
mainTemplate.indent([
"installedChunkData = installedChunks[chunkId] = [resolve, reject];"
]),
"installedChunkData[2] = promise;",
"});",
"",
"// start chunk loading",
"var head = document.getElementsByTagName('head')[0];",
mainTemplate.applyPluginsWaterfall("jsonp-script", "", chunk, hash),
"head.appendChild(script);",
"promises.push(promise);"
]),
"}",
2017-02-23 00:54:16 +08:00
]),
2017-10-30 20:56:57 +08:00
"}",
2017-02-23 00:54:16 +08:00
]);
});
2017-11-08 18:32:05 +08:00
mainTemplate.plugin("require-extensions", (source, chunk) => {
2017-09-22 20:07:28 +08:00
if(chunk.getNumberOfChunks() === 0) return source;
2017-02-23 00:54:16 +08:00
2017-11-08 18:32:05 +08:00
return mainTemplate.asString([
source,
"",
2017-02-23 00:54:16 +08:00
"// on error function for async loading",
2017-11-08 18:32:05 +08:00
`${mainTemplate.requireFn}.oe = function(err) { console.error(err); throw err; };`
2017-02-23 00:54:16 +08:00
]);
});
2017-11-08 18:32:05 +08:00
mainTemplate.plugin("bootstrap", (source, chunk, hash) => {
if(needChunkLoadingCode(chunk)) {
2017-11-08 18:32:05 +08:00
return mainTemplate.asString([
2017-02-23 00:54:16 +08:00
source,
"",
"// install a JSONP callback for chunk loading",
"function webpackJsonpCallback(data) {",
2017-11-08 18:32:05 +08:00
mainTemplate.indent([
"var chunkIds = data[0], moreModules = data[1], executeModules = data[2];",
2017-02-23 00:54:16 +08:00
"// add \"moreModules\" to the modules object,",
"// then flag all \"chunkIds\" as loaded and fire callback",
"var moduleId, chunkId, i = 0, resolves = [], result;",
"for(;i < chunkIds.length; i++) {",
2017-11-08 18:32:05 +08:00
mainTemplate.indent([
2017-02-23 00:54:16 +08:00
"chunkId = chunkIds[i];",
2017-04-09 15:45:56 +08:00
"if(installedChunks[chunkId]) {",
2017-11-08 18:32:05 +08:00
mainTemplate.indent("resolves.push(installedChunks[chunkId][0]);"),
2017-04-09 15:45:56 +08:00
"}",
2017-02-23 00:54:16 +08:00
"installedChunks[chunkId] = 0;"
]),
"}",
2017-02-23 00:54:16 +08:00
"for(moduleId in moreModules) {",
2017-11-08 18:32:05 +08:00
mainTemplate.indent([
2017-02-23 00:54:16 +08:00
"if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {",
2017-11-08 18:32:05 +08:00
mainTemplate.indent(mainTemplate.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")),
"}"
]),
2016-06-06 02:35:42 +08:00
"}",
"if(parentJsonpFunction) parentJsonpFunction(data);",
2017-04-09 15:45:56 +08:00
"while(resolves.length) {",
2017-11-08 18:32:05 +08:00
mainTemplate.indent("resolves.shift()();"),
2017-04-09 15:45:56 +08:00
"}",
2017-11-08 18:32:05 +08:00
mainTemplate.entryPointInChildren(chunk) ? [
"scheduledModules.push.apply(scheduledModules, executeModules || []);",
"",
"for(i = 0; i < scheduledModules.length; i++) {",
2017-11-08 18:32:05 +08:00
mainTemplate.indent([
"var scheduledModule = scheduledModules[i];",
"var fullfilled = true;",
"for(var j = 1; j < scheduledModule.length; j++) {",
2017-11-08 18:32:05 +08:00
mainTemplate.indent([
"var depId = scheduledModule[j];",
"if(installedChunks[depId] !== 0) fullfilled = false;"
]),
"}",
"if(fullfilled) {",
2017-11-08 18:32:05 +08:00
mainTemplate.indent([
"scheduledModules.splice(i--, 1);",
2017-11-08 18:32:05 +08:00
"result = " + mainTemplate.requireFn + "(" + mainTemplate.requireFn + ".s = scheduledModule[0]);",
]),
2017-02-23 00:54:16 +08:00
"}"
]),
"}",
"return result;",
] : ""
]),
"};"
]);
2014-08-22 19:51:24 +08:00
}
2017-02-23 00:54:16 +08:00
return source;
2014-08-22 19:51:24 +08:00
});
2017-11-08 18:32:05 +08:00
mainTemplate.plugin("startup", (source, chunk, hash) => {
if(needChunkLoadingCode(chunk)) {
2017-11-08 18:32:05 +08:00
var jsonpFunction = mainTemplate.outputOptions.jsonpFunction;
return mainTemplate.asString([
`var jsonpArray = window[${JSON.stringify(jsonpFunction)}] = window[${JSON.stringify(jsonpFunction)}] || [];`,
"var parentJsonpFunction = jsonpArray.push.bind(jsonpArray);",
"jsonpArray.push = webpackJsonpCallback;",
"jsonpArray = jsonpArray.slice();",
"for(var i = 0; i < jsonpArray.length; i++) webpackJsonpCallback(jsonpArray[i]);",
"",
source
]);
}
return source;
});
2017-11-08 18:32:05 +08:00
mainTemplate.plugin("hot-bootstrap", (source, chunk, hash) => {
const hotUpdateChunkFilename = mainTemplate.outputOptions.hotUpdateChunkFilename;
const hotUpdateMainFilename = mainTemplate.outputOptions.hotUpdateMainFilename;
const crossOriginLoading = mainTemplate.outputOptions.crossOriginLoading;
const hotUpdateFunction = mainTemplate.outputOptions.hotUpdateFunction;
const currentHotUpdateChunkFilename = mainTemplate.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), {
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
hashWithLength: length => `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
2017-02-23 00:54:16 +08:00
chunk: {
id: "\" + chunkId + \""
}
});
2017-11-08 18:32:05 +08:00
const currentHotUpdateMainFilename = mainTemplate.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), {
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
hashWithLength: length => `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`
2017-02-23 00:54:16 +08:00
});
2017-02-28 22:25:31 +08:00
const runtimeSource = Template.getFunctionContent(require("./JsonpMainTemplate.runtime.js"))
2017-02-23 00:54:16 +08:00
.replace(/\/\/\$semicolon/g, ";")
2017-11-08 18:32:05 +08:00
.replace(/\$require\$/g, mainTemplate.requireFn)
.replace(/\$crossOriginLoading\$/g, crossOriginLoading ? `script.crossOrigin = ${JSON.stringify(crossOriginLoading)}` : "")
2017-02-23 00:54:16 +08:00
.replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
.replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
.replace(/\$hash\$/g, JSON.stringify(hash));
return `${source}
function hotDisposeChunk(chunkId) {
delete installedChunks[chunkId];
}
var parentHotUpdateCallback = this[${JSON.stringify(hotUpdateFunction)}];
2017-02-28 22:25:31 +08:00
this[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
2014-08-22 19:51:24 +08:00
});
2017-11-08 18:32:05 +08:00
mainTemplate.plugin("hash", hash => {
2017-02-23 00:54:16 +08:00
hash.update("jsonp");
hash.update("5");
2017-11-08 18:32:05 +08:00
hash.update(`${mainTemplate.outputOptions.filename}`);
hash.update(`${mainTemplate.outputOptions.chunkFilename}`);
hash.update(`${mainTemplate.outputOptions.jsonpFunction}`);
hash.update(`${mainTemplate.outputOptions.hotUpdateFunction}`);
2017-02-23 00:54:16 +08:00
});
}
}
module.exports = JsonpMainTemplatePlugin;