1 |
|
2 |
|
3 |
|
4 | 'use strict'
|
5 |
|
6 |
|
7 | function CustomJsonpMainTemplatePlugin() {
|
8 | }
|
9 | CustomJsonpMainTemplatePlugin.prototype.constructor = CustomJsonpMainTemplatePlugin;
|
10 | CustomJsonpMainTemplatePlugin.prototype.apply = function (mainTemplate) {
|
11 |
|
12 |
|
13 | var newRequireEnsureTpl = function (_, chunk, hash) {
|
14 | var filename = this.outputOptions.filename || "bundle.js";
|
15 | var chunkFilename = this.outputOptions.chunkFilename || "[id]." + filename;
|
16 | return this.asString([
|
17 | "var triggerCustomEvent = function (eventName,eventParams) {" ,
|
18 | " var event;" ,
|
19 | " if (window.CustomEvent) " ,
|
20 | " { " ,
|
21 | " event = new CustomEvent(eventName, {detail: eventParams}); " ,
|
22 | " } else { " ,
|
23 | " event = document.createEvent('CustomEvent'); " ,
|
24 | " event.initCustomEvent(eventName, true, true, eventParams); " ,
|
25 | " }" ,
|
26 | " document.dispatchEvent(event); " ,
|
27 | "};",
|
28 |
|
29 | "// \"0\" is the signal for \"already loaded\"",
|
30 |
|
31 | "if(installedChunks[chunkId] === 0)",
|
32 | this.indent("return callback.call(null, " + this.requireFn + ");"),
|
33 | "",
|
34 | "// an array means \"currently loading\".",
|
35 | "if(installedChunks[chunkId] !== undefined) {",
|
36 | this.indent("installedChunks[chunkId].push(callback);"),
|
37 | "} else {",
|
38 | this.indent([
|
39 | "// start chunk loading",
|
40 | "installedChunks[chunkId] = [callback];",
|
41 | "var head = document.getElementsByTagName('head')[0];",
|
42 | this.applyPluginsWaterfall("jsonp-script", "", chunk, hash),
|
43 | "script.onload=function(){ triggerCustomEvent('webpackChunkLoaded'); };",
|
44 | "head.appendChild(script);",
|
45 | "triggerCustomEvent('webpackChunkLoading');"
|
46 | ]),
|
47 | "}"
|
48 | ]);
|
49 | };
|
50 |
|
51 | mainTemplate.plugin("require-ensure", newRequireEnsureTpl);
|
52 |
|
53 |
|
54 |
|
55 | };
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | function ChunkLoadingEventPlugin() {
|
65 | }
|
66 | ChunkLoadingEventPlugin.prototype.constructor = ChunkLoadingEventPlugin;
|
67 | ChunkLoadingEventPlugin.prototype.apply = function (compiler) {
|
68 | var compileTarget = compiler.options.target;
|
69 | if (["web", "node-webkit", "electron-renderer"].indexOf(compileTarget) > -1) {
|
70 |
|
71 | compiler.plugin("compilation", function (compilation) {
|
72 | compilation.mainTemplate.apply(new CustomJsonpMainTemplatePlugin());
|
73 | });
|
74 | }
|
75 | };
|
76 |
|
77 |
|
78 | module.exports = ChunkLoadingEventPlugin; |
\ | No newline at end of file |