UNPKG

1.11 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5"use strict";
6
7const { ConcatSource } = require("webpack-sources");
8
9class JsonpExportMainTemplatePlugin {
10 /**
11 * @param {string} name jsonp function name
12 */
13 constructor(name) {
14 this.name = name;
15 }
16
17 apply(compilation) {
18 const { mainTemplate, chunkTemplate } = compilation;
19
20 const onRenderWithEntry = (source, chunk, hash) => {
21 const name = mainTemplate.getAssetPath(this.name || "", {
22 hash,
23 chunk
24 });
25 return new ConcatSource(`${name}(`, source, ");");
26 };
27
28 for (const template of [mainTemplate, chunkTemplate]) {
29 template.hooks.renderWithEntry.tap(
30 "JsonpExportMainTemplatePlugin",
31 onRenderWithEntry
32 );
33 }
34
35 mainTemplate.hooks.globalHashPaths.tap(
36 "JsonpExportMainTemplatePlugin",
37 paths => {
38 if (this.name) paths.push(this.name);
39 return paths;
40 }
41 );
42
43 mainTemplate.hooks.hash.tap("JsonpExportMainTemplatePlugin", hash => {
44 hash.update("jsonp export");
45 hash.update(`${this.name}`);
46 });
47 }
48}
49
50module.exports = JsonpExportMainTemplatePlugin;