Skip to content

Commit bedb438

Browse files
author
Lior Amsalem
committed
refactor libraryTemplatePlugin to es2015 class
1 parent 5a9e2b3 commit bedb438

1 file changed

Lines changed: 67 additions & 60 deletions

File tree

lib/LibraryTemplatePlugin.js

Lines changed: 67 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,86 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
var SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin");
5+
"use strict";
6+
7+
const SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin");
68

79
function accessorToObjectAccess(accessor) {
8-
return accessor.map(function(a) {
9-
return "[" + JSON.stringify(a) + "]";
10+
return accessor.map((a) => {
11+
return `[${JSON.stringify(a)}]`;
1012
}).join("");
1113
}
1214

1315
function accessorAccess(base, accessor, joinWith) {
1416
accessor = [].concat(accessor);
15-
return accessor.map(function(a, idx) {
17+
return accessor.map((a, idx) => {
1618
a = base ?
1719
base + accessorToObjectAccess(accessor.slice(0, idx + 1)) :
1820
accessor[0] + accessorToObjectAccess(accessor.slice(1, idx + 1));
1921
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} || {}`;
2224
}).join(joinWith || "; ");
2325
}
2426

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+
}
3085
}
86+
3187
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

Comments
 (0)