|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -var ConcatSource = require("webpack-sources").ConcatSource; |
| 5 | +"use strict"; |
6 | 6 |
|
7 | | -function JsonpExportMainTemplatePlugin(name) { |
8 | | - this.name = name; |
| 7 | +const ConcatSource = require("webpack-sources").ConcatSource; |
| 8 | + |
| 9 | +class JsonpExportMainTemplatePlugin { |
| 10 | + constructor(name) { |
| 11 | + this.name = name; |
| 12 | + } |
| 13 | + |
| 14 | + apply(compilation) { |
| 15 | + const mainTemplate = compilation.mainTemplate; |
| 16 | + |
| 17 | + compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => { |
| 18 | + const name = mainTemplate.applyPluginsWaterfall("asset-path", this.name || "", { |
| 19 | + hash: hash, |
| 20 | + chunk: chunk |
| 21 | + }); |
| 22 | + return new ConcatSource(`${name}(`, source, ");"); |
| 23 | + }); |
| 24 | + |
| 25 | + mainTemplate.plugin("global-hash-paths", paths => { |
| 26 | + if(this.name) paths.push(this.name); |
| 27 | + return paths; |
| 28 | + }); |
| 29 | + |
| 30 | + mainTemplate.plugin("hash", hash => { |
| 31 | + hash.update("jsonp export"); |
| 32 | + hash.update(`${this.name}`); |
| 33 | + }); |
| 34 | + } |
9 | 35 | } |
| 36 | + |
10 | 37 | module.exports = JsonpExportMainTemplatePlugin; |
11 | | -JsonpExportMainTemplatePlugin.prototype.apply = function(compilation) { |
12 | | - var mainTemplate = compilation.mainTemplate; |
13 | | - compilation.templatesPlugin("render-with-entry", function(source, chunk, hash) { |
14 | | - var name = mainTemplate.applyPluginsWaterfall("asset-path", this.name || "", { |
15 | | - hash: hash, |
16 | | - chunk: chunk |
17 | | - }); |
18 | | - return new ConcatSource(name + "(", source, ");"); |
19 | | - }.bind(this)); |
20 | | - mainTemplate.plugin("global-hash-paths", function(paths) { |
21 | | - if(this.name) paths.push(this.name); |
22 | | - return paths; |
23 | | - }.bind(this)); |
24 | | - mainTemplate.plugin("hash", function(hash) { |
25 | | - hash.update("jsonp export"); |
26 | | - hash.update(this.name + ""); |
27 | | - }.bind(this)); |
28 | | -}; |
|
0 commit comments