|
1 | 1 | /* |
2 | | - MIT License http://www.opensource.org/licenses/mit-license.php |
3 | | - Author Tobias Koppers @sokra |
4 | | -*/ |
5 | | -var ConcatSource = require("webpack-sources").ConcatSource; |
| 2 | + MIT License http://www.opensource.org/licenses/mit-license.php |
| 3 | + Author Tobias Koppers @sokra |
| 4 | + */ |
6 | 5 |
|
7 | | -function AmdMainTemplatePlugin(name) { |
8 | | - this.name = name; |
| 6 | +"use strict"; |
| 7 | + |
| 8 | +const ConcatSource = require("webpack-sources").ConcatSource; |
| 9 | + |
| 10 | +class AmdMainTemplatePlugin { |
| 11 | + constructor(name) { |
| 12 | + this.name = name; |
| 13 | + } |
| 14 | + |
| 15 | + apply(compilation) { |
| 16 | + let mainTemplate = compilation.mainTemplate; |
| 17 | + |
| 18 | + compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => { |
| 19 | + let externals = chunk.modules.filter((m) => m.external); |
| 20 | + let externalsDepsArray = JSON.stringify(externals.map((m) => |
| 21 | + typeof m.request === "object" ? m.request.amd : m.request |
| 22 | + )); |
| 23 | + let externalsArguments = externals.map((m) => |
| 24 | + `__WEBPACK_EXTERNAL_MODULE_${m.id}__` |
| 25 | + ).join(", "); |
| 26 | + |
| 27 | + if(this.name) { |
| 28 | + let name = mainTemplate.applyPluginsWaterfall("asset-path", this.name, { |
| 29 | + hash, |
| 30 | + chunk |
| 31 | + }); |
| 32 | + |
| 33 | + return new ConcatSource( |
| 34 | + `define(${JSON.stringify(name)}, ${externalsDepsArray}, function(${externalsArguments}) { return `, source, "});" |
| 35 | + ); |
| 36 | + } else if(externalsArguments) { |
| 37 | + return new ConcatSource(`define(${externalsDepsArray}, function(${externalsArguments}) { return `, source, "});"); |
| 38 | + } else { |
| 39 | + return new ConcatSource("define(function() { return ", source, "});"); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + mainTemplate.plugin("global-hash-paths", (paths) => { |
| 44 | + if(this.name) paths.push(this.name); |
| 45 | + return paths; |
| 46 | + }); |
| 47 | + |
| 48 | + mainTemplate.plugin("hash", (hash) => { |
| 49 | + hash.update("exports amd"); |
| 50 | + hash.update(`${this.name}`); |
| 51 | + }); |
| 52 | + } |
9 | 53 | } |
| 54 | + |
10 | 55 | module.exports = AmdMainTemplatePlugin; |
11 | | -AmdMainTemplatePlugin.prototype.apply = function(compilation) { |
12 | | - var mainTemplate = compilation.mainTemplate; |
13 | | - compilation.templatesPlugin("render-with-entry", function(source, chunk, hash) { |
14 | | - var externals = chunk.modules.filter(function(m) { |
15 | | - return m.external; |
16 | | - }); |
17 | | - var externalsDepsArray = JSON.stringify(externals.map(function(m) { |
18 | | - return typeof m.request === "object" ? m.request.amd : m.request; |
19 | | - })); |
20 | | - var externalsArguments = externals.map(function(m) { |
21 | | - return "__WEBPACK_EXTERNAL_MODULE_" + m.id + "__"; |
22 | | - }).join(", "); |
23 | | - if(this.name) { |
24 | | - var name = mainTemplate.applyPluginsWaterfall("asset-path", this.name, { |
25 | | - hash: hash, |
26 | | - chunk: chunk |
27 | | - }); |
28 | | - return new ConcatSource("define(" + JSON.stringify(name) + ", " + externalsDepsArray + ", function(" + externalsArguments + ") { return ", source, "});"); |
29 | | - } else if(externalsArguments) { |
30 | | - return new ConcatSource("define(" + externalsDepsArray + ", function(" + externalsArguments + ") { return ", source, "});"); |
31 | | - } else { |
32 | | - return new ConcatSource("define(function() { return ", source, "});"); |
33 | | - } |
34 | | - }.bind(this)); |
35 | | - mainTemplate.plugin("global-hash-paths", function(paths) { |
36 | | - if(this.name) paths.push(this.name); |
37 | | - return paths; |
38 | | - }.bind(this)); |
39 | | - mainTemplate.plugin("hash", function(hash) { |
40 | | - hash.update("exports amd"); |
41 | | - hash.update(this.name + ""); |
42 | | - }.bind(this)); |
43 | | -}; |
|
0 commit comments