|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -var SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin"); |
| 5 | +"use strict"; |
| 6 | + |
| 7 | +const SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin"); |
6 | 8 |
|
7 | 9 | function accessorToObjectAccess(accessor) { |
8 | | - return accessor.map(function(a) { |
9 | | - return "[" + JSON.stringify(a) + "]"; |
| 10 | + return accessor.map((a) => { |
| 11 | + return `[${JSON.stringify(a)}]`; |
10 | 12 | }).join(""); |
11 | 13 | } |
12 | 14 |
|
13 | 15 | function accessorAccess(base, accessor, joinWith) { |
14 | 16 | accessor = [].concat(accessor); |
15 | | - return accessor.map(function(a, idx) { |
| 17 | + return accessor.map((a, idx) => { |
16 | 18 | a = base ? |
17 | 19 | base + accessorToObjectAccess(accessor.slice(0, idx + 1)) : |
18 | 20 | accessor[0] + accessorToObjectAccess(accessor.slice(1, idx + 1)); |
19 | 21 | if(idx === accessor.length - 1) return a; |
20 | | - if(idx === 0 && typeof base === "undefined") return a + " = typeof " + a + " === \"object\" ? " + a + " : {}"; |
21 | | - return a + " = " + a + " || {}"; |
| 22 | + if(idx === 0 && typeof base === "undefined") return `${a} = typeof ${a} === "object" ? ${a} : {}`; |
| 23 | + return `${a} = ${a} || {}`; |
22 | 24 | }).join(joinWith || "; "); |
23 | 25 | } |
24 | 26 |
|
25 | | -function LibraryTemplatePlugin(name, target, umdNamedDefine, auxiliaryComment) { |
26 | | - this.name = name; |
27 | | - this.target = target; |
28 | | - this.umdNamedDefine = umdNamedDefine; |
29 | | - this.auxiliaryComment = auxiliaryComment; |
| 27 | +class LibraryTemplatePlugin { |
| 28 | + |
| 29 | + constructor(name, target, umdNamedDefine, auxiliaryComment) { |
| 30 | + this.name = name; |
| 31 | + this.target = target; |
| 32 | + this.umdNamedDefine = umdNamedDefine; |
| 33 | + this.auxiliaryComment = auxiliaryComment; |
| 34 | + } |
| 35 | + |
| 36 | + apply(compiler) { |
| 37 | + compiler.plugin("this-compilation", (compilation) => { |
| 38 | + switch(this.target) { |
| 39 | + case "var": |
| 40 | + compilation.apply(new SetVarMainTemplatePlugin(`var ${accessorAccess(false, this.name)}`)); |
| 41 | + break; |
| 42 | + case "assign": |
| 43 | + compilation.apply(new SetVarMainTemplatePlugin(accessorAccess(undefined, this.name))); |
| 44 | + break; |
| 45 | + case "this": |
| 46 | + case "window": |
| 47 | + case "global": |
| 48 | + if(this.name) |
| 49 | + compilation.apply(new SetVarMainTemplatePlugin(accessorAccess(this.target, this.name))); |
| 50 | + else |
| 51 | + compilation.apply(new SetVarMainTemplatePlugin(this.target, true)); |
| 52 | + break; |
| 53 | + case "commonjs": |
| 54 | + if(this.name) |
| 55 | + compilation.apply(new SetVarMainTemplatePlugin(accessorAccess("exports", this.name))); |
| 56 | + else |
| 57 | + compilation.apply(new SetVarMainTemplatePlugin("exports", true)); |
| 58 | + break; |
| 59 | + case "commonjs2": |
| 60 | + case "commonjs-module": |
| 61 | + compilation.apply(new SetVarMainTemplatePlugin("module.exports")); |
| 62 | + break; |
| 63 | + case "amd": |
| 64 | + var AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin"); |
| 65 | + compilation.apply(new AmdMainTemplatePlugin(this.name)); |
| 66 | + break; |
| 67 | + case "umd": |
| 68 | + case "umd2": |
| 69 | + var UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin"); |
| 70 | + compilation.apply(new UmdMainTemplatePlugin(this.name, { |
| 71 | + optionalAmdExternalAsGlobal: this.target === "umd2", |
| 72 | + namedDefine: this.umdNamedDefine, |
| 73 | + auxiliaryComment: this.auxiliaryComment |
| 74 | + })); |
| 75 | + break; |
| 76 | + case "jsonp": |
| 77 | + var JsonpExportMainTemplatePlugin = require("./JsonpExportMainTemplatePlugin"); |
| 78 | + compilation.apply(new JsonpExportMainTemplatePlugin(this.name)); |
| 79 | + break; |
| 80 | + default: |
| 81 | + throw new Error(`${this.target} is not a valid Library target`); |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
30 | 85 | } |
| 86 | + |
31 | 87 | module.exports = LibraryTemplatePlugin; |
32 | | -LibraryTemplatePlugin.prototype.apply = function(compiler) { |
33 | | - compiler.plugin("this-compilation", function(compilation) { |
34 | | - switch(this.target) { |
35 | | - case "var": |
36 | | - compilation.apply(new SetVarMainTemplatePlugin("var " + accessorAccess(false, this.name))); |
37 | | - break; |
38 | | - case "assign": |
39 | | - compilation.apply(new SetVarMainTemplatePlugin(accessorAccess(undefined, this.name))); |
40 | | - break; |
41 | | - case "this": |
42 | | - case "window": |
43 | | - case "global": |
44 | | - if(this.name) |
45 | | - compilation.apply(new SetVarMainTemplatePlugin(accessorAccess(this.target, this.name))); |
46 | | - else |
47 | | - compilation.apply(new SetVarMainTemplatePlugin(this.target, true)); |
48 | | - break; |
49 | | - case "commonjs": |
50 | | - if(this.name) |
51 | | - compilation.apply(new SetVarMainTemplatePlugin(accessorAccess("exports", this.name))); |
52 | | - else |
53 | | - compilation.apply(new SetVarMainTemplatePlugin("exports", true)); |
54 | | - break; |
55 | | - case "commonjs2": |
56 | | - case "commonjs-module": |
57 | | - compilation.apply(new SetVarMainTemplatePlugin("module.exports")); |
58 | | - break; |
59 | | - case "amd": |
60 | | - var AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin"); |
61 | | - compilation.apply(new AmdMainTemplatePlugin(this.name)); |
62 | | - break; |
63 | | - case "umd": |
64 | | - case "umd2": |
65 | | - var UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin"); |
66 | | - compilation.apply(new UmdMainTemplatePlugin(this.name, { |
67 | | - optionalAmdExternalAsGlobal: this.target === "umd2", |
68 | | - namedDefine: this.umdNamedDefine, |
69 | | - auxiliaryComment: this.auxiliaryComment |
70 | | - })); |
71 | | - break; |
72 | | - case "jsonp": |
73 | | - var JsonpExportMainTemplatePlugin = require("./JsonpExportMainTemplatePlugin"); |
74 | | - compilation.apply(new JsonpExportMainTemplatePlugin(this.name)); |
75 | | - break; |
76 | | - default: |
77 | | - throw new Error(this.target + " is not a valid Library target"); |
78 | | - } |
79 | | - }.bind(this)); |
80 | | -}; |
|
0 commit comments