2014-06-03 05:40:50 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-02-23 23:33:36 +08:00
|
|
|
"use strict";
|
2014-06-03 05:40:50 +08:00
|
|
|
|
2018-03-22 19:05:58 +08:00
|
|
|
const { ConcatSource } = require("webpack-sources");
|
2018-08-14 22:40:37 +08:00
|
|
|
const getEntryInfo = require("./JsonpHelpers").getEntryInfo;
|
2014-06-03 05:40:50 +08:00
|
|
|
|
2018-08-14 22:40:37 +08:00
|
|
|
/** @typedef {import("../Chunk")} Chunk */
|
2018-06-26 22:07:19 +08:00
|
|
|
/** @typedef {import("../ChunkTemplate")} ChunkTemplate */
|
2018-08-14 17:18:22 +08:00
|
|
|
/** @typedef {import("../Compilation")} Compilation */
|
2018-06-26 22:07:19 +08:00
|
|
|
|
2017-02-23 23:33:36 +08:00
|
|
|
class JsonpChunkTemplatePlugin {
|
2018-08-14 17:18:22 +08:00
|
|
|
/**
|
|
|
|
* @param {Compilation} compilation the compilation
|
|
|
|
*/
|
|
|
|
constructor(compilation) {
|
|
|
|
this.compilation = compilation;
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:07:19 +08:00
|
|
|
/**
|
|
|
|
* @param {ChunkTemplate} chunkTemplate the chunk template
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-02-23 23:33:36 +08:00
|
|
|
apply(chunkTemplate) {
|
2018-02-25 09:00:20 +08:00
|
|
|
chunkTemplate.hooks.render.tap(
|
|
|
|
"JsonpChunkTemplatePlugin",
|
2018-08-14 17:18:22 +08:00
|
|
|
(modules, moduleTemplate, { chunk, chunkGraph }) => {
|
2018-02-25 09:00:20 +08:00
|
|
|
const jsonpFunction = chunkTemplate.outputOptions.jsonpFunction;
|
|
|
|
const globalObject = chunkTemplate.outputOptions.globalObject;
|
|
|
|
const source = new ConcatSource();
|
2018-08-14 17:18:22 +08:00
|
|
|
const prefetchChunks = chunk.getChildIdsByOrders(chunkGraph).prefetch;
|
2018-02-25 09:00:20 +08:00
|
|
|
source.add(
|
2018-03-26 22:56:10 +08:00
|
|
|
`(${globalObject}[${JSON.stringify(
|
|
|
|
jsonpFunction
|
|
|
|
)}] = ${globalObject}[${JSON.stringify(
|
|
|
|
jsonpFunction
|
|
|
|
)}] || []).push([${JSON.stringify(chunk.ids)},`
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
|
|
|
source.add(modules);
|
2018-08-14 22:40:37 +08:00
|
|
|
const entries = getEntryInfo(chunkGraph, chunk);
|
2018-02-25 09:00:20 +08:00
|
|
|
if (entries.length > 0) {
|
|
|
|
source.add(`,${JSON.stringify(entries)}`);
|
2018-06-02 01:51:39 +08:00
|
|
|
} else if (prefetchChunks && prefetchChunks.length) {
|
|
|
|
source.add(`,0`);
|
2018-05-31 06:36:50 +08:00
|
|
|
}
|
2018-06-02 01:51:39 +08:00
|
|
|
|
|
|
|
if (prefetchChunks && prefetchChunks.length) {
|
|
|
|
source.add(`,${JSON.stringify(prefetchChunks)}`);
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
|
|
|
source.add("])");
|
|
|
|
return source;
|
2017-02-23 23:33:36 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2017-12-15 22:16:39 +08:00
|
|
|
chunkTemplate.hooks.hash.tap("JsonpChunkTemplatePlugin", hash => {
|
2017-02-23 23:33:36 +08:00
|
|
|
hash.update("JsonpChunkTemplatePlugin");
|
2017-04-23 00:59:15 +08:00
|
|
|
hash.update("4");
|
2017-11-08 18:32:05 +08:00
|
|
|
hash.update(`${chunkTemplate.outputOptions.jsonpFunction}`);
|
2017-12-28 01:46:37 +08:00
|
|
|
hash.update(`${chunkTemplate.outputOptions.globalObject}`);
|
2015-07-25 21:00:05 +08:00
|
|
|
});
|
2018-06-26 22:07:19 +08:00
|
|
|
chunkTemplate.hooks.hashForChunk.tap(
|
|
|
|
"JsonpChunkTemplatePlugin",
|
|
|
|
(hash, chunk) => {
|
2018-08-14 17:18:22 +08:00
|
|
|
const chunkGraph = this.compilation.chunkGraph;
|
2018-08-14 22:40:37 +08:00
|
|
|
hash.update(JSON.stringify(getEntryInfo(chunkGraph, chunk)));
|
2018-08-14 17:18:22 +08:00
|
|
|
hash.update(
|
|
|
|
JSON.stringify(chunk.getChildIdsByOrders(chunkGraph).prefetch) || ""
|
|
|
|
);
|
2018-06-26 22:07:19 +08:00
|
|
|
}
|
|
|
|
);
|
2017-02-23 23:33:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = JsonpChunkTemplatePlugin;
|