Skip to content

Commit 23a2a15

Browse files
authored
Merge pull request webpack#4311 from willmendesneto/refactor-umd-main-template-plugin
refactor(UmdMainTemplatePlugin): upgrade to ES6
2 parents 6ef2c75 + 1a76669 commit 23a2a15

1 file changed

Lines changed: 141 additions & 143 deletions

File tree

‎lib/UmdMainTemplatePlugin.js‎

Lines changed: 141 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -2,177 +2,175 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
var ConcatSource = require("webpack-sources").ConcatSource;
6-
var OriginalSource = require("webpack-sources").OriginalSource;
7-
var Template = require("./Template");
5+
"use strict";
6+
7+
const ConcatSource = require("webpack-sources").ConcatSource;
8+
const OriginalSource = require("webpack-sources").OriginalSource;
9+
const Template = require("./Template");
810

911
function accessorToObjectAccess(accessor) {
10-
return accessor.map(function(a) {
11-
return "[" + JSON.stringify(a) + "]";
12-
}).join("");
12+
return accessor.map(a => `[${JSON.stringify(a)}]`).join("");
1313
}
1414

1515
function accessorAccess(base, accessor) {
1616
accessor = [].concat(accessor);
17-
return accessor.map(function(a, idx) {
17+
return accessor.map((a, idx) => {
1818
a = base + accessorToObjectAccess(accessor.slice(0, idx + 1));
1919
if(idx === accessor.length - 1) return a;
20-
return a + " = " + a + " || {}";
20+
return `${a} = ${a} || {}`;
2121
}).join(", ");
2222
}
2323

24-
function UmdMainTemplatePlugin(name, options) {
25-
this.name = name;
26-
this.optionalAmdExternalAsGlobal = options.optionalAmdExternalAsGlobal;
27-
this.namedDefine = options.namedDefine;
28-
this.auxiliaryComment = options.auxiliaryComment;
29-
}
30-
module.exports = UmdMainTemplatePlugin;
31-
UmdMainTemplatePlugin.prototype.apply = function(compilation) {
32-
var mainTemplate = compilation.mainTemplate;
33-
compilation.templatesPlugin("render-with-entry", function(source, chunk, hash) {
34-
var externals = chunk.modules.filter(function(m) {
35-
return m.external;
36-
});
37-
var optionalExternals = [],
38-
requiredExternals = [];
39-
if(this.optionalAmdExternalAsGlobal) {
40-
externals.forEach(function(m) {
41-
if(m.optional) {
42-
optionalExternals.push(m);
43-
} else {
44-
requiredExternals.push(m);
45-
}
46-
});
47-
externals = requiredExternals.concat(optionalExternals);
48-
} else {
49-
requiredExternals = externals;
50-
}
24+
class UmdMainTemplatePlugin {
25+
constructor(name, options) {
26+
this.name = name;
27+
this.optionalAmdExternalAsGlobal = options.optionalAmdExternalAsGlobal;
28+
this.namedDefine = options.namedDefine;
29+
this.auxiliaryComment = options.auxiliaryComment;
30+
}
31+
32+
apply(compilation) {
33+
const mainTemplate = compilation.mainTemplate;
34+
compilation.templatesPlugin("render-with-entry", function(source, chunk, hash) {
35+
let externals = chunk.modules.filter(m => m.external);
36+
const optionalExternals = [];
37+
let requiredExternals = [];
38+
if(this.optionalAmdExternalAsGlobal) {
39+
externals.forEach(m => {
40+
if(m.optional) {
41+
optionalExternals.push(m);
42+
} else {
43+
requiredExternals.push(m);
44+
}
45+
});
46+
externals = requiredExternals.concat(optionalExternals);
47+
} else {
48+
requiredExternals = externals;
49+
}
5150

52-
function replaceKeys(str) {
53-
return mainTemplate.applyPluginsWaterfall("asset-path", str, {
54-
hash: hash,
55-
chunk: chunk
56-
});
57-
}
51+
function replaceKeys(str) {
52+
return mainTemplate.applyPluginsWaterfall("asset-path", str, {
53+
hash,
54+
chunk
55+
});
56+
}
5857

59-
function externalsDepsArray(modules) {
60-
return "[" + replaceKeys(modules.map(function(m) {
61-
return JSON.stringify(typeof m.request === "object" ? m.request.amd : m.request);
62-
}).join(", ")) + "]";
63-
}
58+
function externalsDepsArray(modules) {
59+
return `[${replaceKeys(modules.map(m => JSON.stringify(typeof m.request === "object" ? m.request.amd : m.request)).join(", "))}]`;
60+
}
6461

65-
function externalsRootArray(modules) {
66-
return replaceKeys(modules.map(function(m) {
67-
var request = m.request;
68-
if(typeof request === "object") request = request.root;
69-
return "root" + accessorToObjectAccess([].concat(request));
70-
}).join(", "));
71-
}
62+
function externalsRootArray(modules) {
63+
return replaceKeys(modules.map(m => {
64+
let request = m.request;
65+
if(typeof request === "object") request = request.root;
66+
return `root${accessorToObjectAccess([].concat(request))}`;
67+
}).join(", "));
68+
}
7269

73-
function externalsRequireArray(type) {
74-
return replaceKeys(externals.map(function(m) {
75-
var expr;
76-
var request = m.request;
77-
if(typeof request === "object") request = request[type];
78-
if(Array.isArray(request)) {
79-
expr = "require(" + JSON.stringify(request[0]) + ")" + accessorToObjectAccess(request.slice(1));
80-
} else
81-
expr = "require(" + JSON.stringify(request) + ")";
82-
if(m.optional) {
83-
expr = "(function webpackLoadOptionalExternalModule() { try { return " + expr + "; } catch(e) {} }())";
84-
}
85-
return expr;
86-
}).join(", "));
87-
}
70+
function externalsRequireArray(type) {
71+
return replaceKeys(externals.map(m => {
72+
let expr;
73+
let request = m.request;
74+
if(typeof request === "object") request = request[type];
75+
if(Array.isArray(request)) {
76+
expr = `require(${JSON.stringify(request[0])})${accessorToObjectAccess(request.slice(1))}`;
77+
} else
78+
expr = `require(${JSON.stringify(request)})`;
79+
if(m.optional) {
80+
expr = `(function webpackLoadOptionalExternalModule() { try { return ${expr}; } catch(e) {} }())`;
81+
}
82+
return expr;
83+
}).join(", "));
84+
}
8885

89-
function externalsArguments(modules) {
90-
return modules.map(function(m) {
91-
return Template.toIdentifier("__WEBPACK_EXTERNAL_MODULE_" + m.id + "__");
92-
}).join(", ");
93-
}
86+
function externalsArguments(modules) {
87+
return modules.map(m => Template.toIdentifier(`__WEBPACK_EXTERNAL_MODULE_${m.id}__`)).join(", ");
88+
}
9489

95-
function libraryName(library) {
96-
return JSON.stringify(replaceKeys([].concat(library).pop()));
97-
}
90+
function libraryName(library) {
91+
return JSON.stringify(replaceKeys([].concat(library).pop()));
92+
}
9893

99-
var amdFactory;
100-
if(optionalExternals.length > 0) {
101-
amdFactory = "function webpackLoadOptionalExternalModuleAmd(" + externalsArguments(requiredExternals) + ") {\n" +
102-
" return factory(" + (
103-
requiredExternals.length > 0 ?
94+
let amdFactory;
95+
if(optionalExternals.length > 0) {
96+
const wrapperArguments = externalsArguments(requiredExternals);
97+
const factoryArguments = requiredExternals.length > 0 ?
10498
externalsArguments(requiredExternals) + ", " + externalsRootArray(optionalExternals) :
105-
externalsRootArray(optionalExternals)
106-
) + ");\n" +
107-
" }";
108-
} else {
109-
amdFactory = "factory";
110-
}
99+
externalsRootArray(optionalExternals);
100+
amdFactory = `function webpackLoadOptionalExternalModuleAmd(${wrapperArguments}) {\n` +
101+
` return factory(${factoryArguments});\n` +
102+
" }";
103+
} else {
104+
amdFactory = "factory";
105+
}
111106

112-
return new ConcatSource(new OriginalSource(
113-
"(function webpackUniversalModuleDefinition(root, factory) {\n" +
114-
(this.auxiliaryComment &&
115-
typeof this.auxiliaryComment === "string" ?
116-
" //" + this.auxiliaryComment + "\n" :
117-
this.auxiliaryComment.commonjs2 ?
118-
" //" + this.auxiliaryComment.commonjs2 + "\n" :
119-
""
120-
) +
121-
" if(typeof exports === 'object' && typeof module === 'object')\n" +
122-
" module.exports = factory(" + externalsRequireArray("commonjs2") + ");\n" +
123-
(this.auxiliaryComment &&
124-
typeof this.auxiliaryComment === "string" ?
125-
" //" + this.auxiliaryComment + "\n" :
126-
this.auxiliaryComment.amd ?
127-
" //" + this.auxiliaryComment.amd + "\n" :
128-
""
129-
) +
130-
" else if(typeof define === 'function' && define.amd)\n" +
131-
(requiredExternals.length > 0 ?
132-
(this.name && this.namedDefine === true ?
133-
" define(" + libraryName(this.name) + ", " + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" :
134-
" define(" + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n"
135-
) :
136-
(this.name && this.namedDefine === true ?
137-
" define(" + libraryName(this.name) + ", [], " + amdFactory + ");\n" :
138-
" define([], " + amdFactory + ");\n"
139-
)
140-
) +
141-
(this.name ?
107+
return new ConcatSource(new OriginalSource(
108+
"(function webpackUniversalModuleDefinition(root, factory) {\n" +
142109
(this.auxiliaryComment &&
143110
typeof this.auxiliaryComment === "string" ?
144111
" //" + this.auxiliaryComment + "\n" :
145-
this.auxiliaryComment.commonjs ?
146-
" //" + this.auxiliaryComment.commonjs + "\n" :
112+
this.auxiliaryComment.commonjs2 ?
113+
" //" + this.auxiliaryComment.commonjs2 + "\n" :
147114
""
148115
) +
149-
" else if(typeof exports === 'object')\n" +
150-
" exports[" + libraryName(this.name) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" +
116+
" if(typeof exports === 'object' && typeof module === 'object')\n" +
117+
" module.exports = factory(" + externalsRequireArray("commonjs2") + ");\n" +
151118
(this.auxiliaryComment &&
152119
typeof this.auxiliaryComment === "string" ?
153120
" //" + this.auxiliaryComment + "\n" :
154-
this.auxiliaryComment.root ?
155-
" //" + this.auxiliaryComment.root + "\n" :
121+
this.auxiliaryComment.amd ?
122+
" //" + this.auxiliaryComment.amd + "\n" :
156123
""
157124
) +
158-
" else\n" +
159-
" " + replaceKeys(accessorAccess("root", this.name)) + " = factory(" + externalsRootArray(externals) + ");\n" :
160-
" else {\n" +
161-
(externals.length > 0 ?
162-
" var a = typeof exports === 'object' ? factory(" + externalsRequireArray("commonjs") + ") : factory(" + externalsRootArray(externals) + ");\n" :
163-
" var a = factory();\n"
125+
" else if(typeof define === 'function' && define.amd)\n" +
126+
(requiredExternals.length > 0 ?
127+
(this.name && this.namedDefine === true ?
128+
" define(" + libraryName(this.name) + ", " + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" :
129+
" define(" + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n"
130+
) :
131+
(this.name && this.namedDefine === true ?
132+
" define(" + libraryName(this.name) + ", [], " + amdFactory + ");\n" :
133+
" define([], " + amdFactory + ");\n"
134+
)
164135
) +
165-
" for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n" +
166-
" }\n"
167-
) +
168-
"})(this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, ";\n})");
169-
}.bind(this));
170-
mainTemplate.plugin("global-hash-paths", function(paths) {
171-
if(this.name) paths = paths.concat(this.name);
172-
return paths;
173-
}.bind(this));
174-
mainTemplate.plugin("hash", function(hash) {
175-
hash.update("umd");
176-
hash.update(this.name + "");
177-
}.bind(this));
178-
};
136+
(this.name ?
137+
(this.auxiliaryComment &&
138+
typeof this.auxiliaryComment === "string" ?
139+
" //" + this.auxiliaryComment + "\n" :
140+
this.auxiliaryComment.commonjs ?
141+
" //" + this.auxiliaryComment.commonjs + "\n" :
142+
""
143+
) +
144+
" else if(typeof exports === 'object')\n" +
145+
" exports[" + libraryName(this.name) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" +
146+
(this.auxiliaryComment &&
147+
typeof this.auxiliaryComment === "string" ?
148+
" //" + this.auxiliaryComment + "\n" :
149+
this.auxiliaryComment.root ?
150+
" //" + this.auxiliaryComment.root + "\n" :
151+
""
152+
) +
153+
" else\n" +
154+
" " + replaceKeys(accessorAccess("root", this.name)) + " = factory(" + externalsRootArray(externals) + ");\n" :
155+
" else {\n" +
156+
(externals.length > 0 ?
157+
" var a = typeof exports === 'object' ? factory(" + externalsRequireArray("commonjs") + ") : factory(" + externalsRootArray(externals) + ");\n" :
158+
" var a = factory();\n"
159+
) +
160+
" for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n" +
161+
" }\n"
162+
) +
163+
"})(this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, ";\n})");
164+
}.bind(this));
165+
mainTemplate.plugin("global-hash-paths", function(paths) {
166+
if(this.name) paths = paths.concat(this.name);
167+
return paths;
168+
}.bind(this));
169+
mainTemplate.plugin("hash", function(hash) {
170+
hash.update("umd");
171+
hash.update(`${this.name}`);
172+
}.bind(this));
173+
}
174+
}
175+
176+
module.exports = UmdMainTemplatePlugin;

0 commit comments

Comments
 (0)