Skip to content

Commit 5c51f0c

Browse files
authored
Merge pull request webpack#7251 from webpack/types/library_templates
Add typings for library template plugins
2 parents 3f183b5 + 91546a1 commit 5c51f0c

5 files changed

+168
-65
lines changed

lib/AmdMainTemplatePlugin.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@
88
const { ConcatSource } = require("webpack-sources");
99
const Template = require("./Template");
1010

11+
/** @typedef {import("./Compilation")} Compilation */
12+
1113
class AmdMainTemplatePlugin {
14+
/**
15+
* @param {string} name the library name
16+
*/
1217
constructor(name) {
18+
/** @type {string} */
1319
this.name = name;
1420
}
1521

22+
/**
23+
* @param {Compilation} compilation the compilation instance
24+
* @returns {void}
25+
*/
1626
apply(compilation) {
1727
const { mainTemplate, chunkTemplate } = compilation;
1828

lib/ExportPropertyMainTemplatePlugin.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,28 @@
66

77
const { ConcatSource } = require("webpack-sources");
88

9+
/** @typedef {import("./Compilation")} Compilation */
10+
11+
/**
12+
* @param {string[]} accessor the accessor to convert to path
13+
* @returns {string} the path
14+
*/
915
const accessorToObjectAccess = accessor => {
1016
return accessor.map(a => `[${JSON.stringify(a)}]`).join("");
1117
};
1218

1319
class ExportPropertyMainTemplatePlugin {
20+
/**
21+
* @param {string|string[]} property the name of the property to export
22+
*/
1423
constructor(property) {
1524
this.property = property;
1625
}
1726

27+
/**
28+
* @param {Compilation} compilation the compilation instance
29+
* @returns {void}
30+
*/
1831
apply(compilation) {
1932
const { mainTemplate, chunkTemplate } = compilation;
2033

lib/LibraryTemplatePlugin.js

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,45 @@
66

77
const SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin");
88

9+
/** @typedef {import("./Compiler")} Compiler */
10+
11+
/**
12+
* @param {string[]} accessor the accessor to convert to path
13+
* @returns {string} the path
14+
*/
915
const accessorToObjectAccess = accessor => {
10-
return accessor
11-
.map(a => {
12-
return `[${JSON.stringify(a)}]`;
13-
})
14-
.join("");
16+
return accessor.map(a => `[${JSON.stringify(a)}]`).join("");
1517
};
1618

17-
const accessorAccess = (base, accessor, joinWith) => {
18-
accessor = [].concat(accessor);
19-
return accessor
20-
.map((a, idx) => {
21-
a = base
22-
? base + accessorToObjectAccess(accessor.slice(0, idx + 1))
23-
: accessor[0] + accessorToObjectAccess(accessor.slice(1, idx + 1));
24-
if (idx === accessor.length - 1) return a;
19+
/**
20+
* @param {string=} base the path prefix
21+
* @param {string|string[]} accessor the accessor
22+
* @param {string=} joinWith the element separator
23+
* @returns {string} the path
24+
*/
25+
const accessorAccess = (base, accessor, joinWith = "; ") => {
26+
const accessors = Array.isArray(accessor) ? accessor : [accessor];
27+
return accessors
28+
.map((_, idx) => {
29+
const a = base
30+
? base + accessorToObjectAccess(accessors.slice(0, idx + 1))
31+
: accessors[0] + accessorToObjectAccess(accessors.slice(1, idx + 1));
32+
if (idx === accessors.length - 1) return a;
2533
if (idx === 0 && typeof base === "undefined")
2634
return `${a} = typeof ${a} === "object" ? ${a} : {}`;
2735
return `${a} = ${a} || {}`;
2836
})
29-
.join(joinWith || "; ");
37+
.join(joinWith);
3038
};
3139

3240
class LibraryTemplatePlugin {
41+
/**
42+
* @param {string} name name of library
43+
* @param {string} target type of library
44+
* @param {boolean} umdNamedDefine setting this to true will name the UMD module
45+
* @param {string|TODO} auxiliaryComment comment in the UMD wrapper
46+
* @param {string|string[]} exportProperty which export should be exposed as library
47+
*/
3348
constructor(name, target, umdNamedDefine, auxiliaryComment, exportProperty) {
3449
this.name = name;
3550
this.target = target;
@@ -38,77 +53,95 @@ class LibraryTemplatePlugin {
3853
this.exportProperty = exportProperty;
3954
}
4055

56+
/**
57+
* @param {Compiler} compiler the compiler instance
58+
* @returns {void}
59+
*/
4160
apply(compiler) {
4261
compiler.hooks.thisCompilation.tap("LibraryTemplatePlugin", compilation => {
4362
if (this.exportProperty) {
44-
var ExportPropertyMainTemplatePlugin = require("./ExportPropertyMainTemplatePlugin");
63+
const ExportPropertyMainTemplatePlugin = require("./ExportPropertyMainTemplatePlugin");
4564
new ExportPropertyMainTemplatePlugin(this.exportProperty).apply(
4665
compilation
4766
);
4867
}
4968
switch (this.target) {
5069
case "var":
5170
new SetVarMainTemplatePlugin(
52-
`var ${accessorAccess(false, this.name)}`
71+
`var ${accessorAccess(undefined, this.name)}`,
72+
false
5373
).apply(compilation);
5474
break;
5575
case "assign":
5676
new SetVarMainTemplatePlugin(
57-
accessorAccess(undefined, this.name)
77+
accessorAccess(undefined, this.name),
78+
false
5879
).apply(compilation);
5980
break;
6081
case "this":
6182
case "self":
6283
case "window":
63-
if (this.name)
84+
if (this.name) {
6485
new SetVarMainTemplatePlugin(
65-
accessorAccess(this.target, this.name)
86+
accessorAccess(this.target, this.name),
87+
false
6688
).apply(compilation);
67-
else
89+
} else {
6890
new SetVarMainTemplatePlugin(this.target, true).apply(compilation);
91+
}
6992
break;
7093
case "global":
71-
if (this.name)
94+
if (this.name) {
7295
new SetVarMainTemplatePlugin(
7396
accessorAccess(
7497
compilation.runtimeTemplate.outputOptions.globalObject,
7598
this.name
76-
)
99+
),
100+
false
77101
).apply(compilation);
78-
else
102+
} else {
79103
new SetVarMainTemplatePlugin(
80104
compilation.runtimeTemplate.outputOptions.globalObject,
81105
true
82106
).apply(compilation);
107+
}
83108
break;
84109
case "commonjs":
85-
if (this.name)
110+
if (this.name) {
86111
new SetVarMainTemplatePlugin(
87-
accessorAccess("exports", this.name)
112+
accessorAccess("exports", this.name),
113+
false
88114
).apply(compilation);
89-
else new SetVarMainTemplatePlugin("exports", true).apply(compilation);
115+
} else {
116+
new SetVarMainTemplatePlugin("exports", true).apply(compilation);
117+
}
90118
break;
91119
case "commonjs2":
92120
case "commonjs-module":
93-
new SetVarMainTemplatePlugin("module.exports").apply(compilation);
121+
new SetVarMainTemplatePlugin("module.exports", false).apply(
122+
compilation
123+
);
94124
break;
95-
case "amd":
96-
var AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin");
125+
case "amd": {
126+
const AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin");
97127
new AmdMainTemplatePlugin(this.name).apply(compilation);
98128
break;
129+
}
99130
case "umd":
100-
case "umd2":
101-
var UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin");
131+
case "umd2": {
132+
const UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin");
102133
new UmdMainTemplatePlugin(this.name, {
103134
optionalAmdExternalAsGlobal: this.target === "umd2",
104135
namedDefine: this.umdNamedDefine,
105136
auxiliaryComment: this.auxiliaryComment
106137
}).apply(compilation);
107138
break;
108-
case "jsonp":
109-
var JsonpExportMainTemplatePlugin = require("./web/JsonpExportMainTemplatePlugin");
139+
}
140+
case "jsonp": {
141+
const JsonpExportMainTemplatePlugin = require("./web/JsonpExportMainTemplatePlugin");
110142
new JsonpExportMainTemplatePlugin(this.name).apply(compilation);
111143
break;
144+
}
112145
default:
113146
throw new Error(`${this.target} is not a valid Library target`);
114147
}

lib/SetVarMainTemplatePlugin.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,24 @@
66

77
const { ConcatSource } = require("webpack-sources");
88

9+
/** @typedef {import("./Compilation")} Compilation */
10+
911
class SetVarMainTemplatePlugin {
12+
/**
13+
* @param {string} varExpression the accessor where the library is exported
14+
* @param {boolean} copyObject specify copying the exports
15+
*/
1016
constructor(varExpression, copyObject) {
17+
/** @type {string} */
1118
this.varExpression = varExpression;
19+
/** @type {boolean} */
1220
this.copyObject = copyObject;
1321
}
1422

23+
/**
24+
* @param {Compilation} compilation the compilation instance
25+
* @returns {void}
26+
*/
1527
apply(compilation) {
1628
const { mainTemplate, chunkTemplate } = compilation;
1729

lib/UmdMainTemplatePlugin.js

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,59 @@
77
const { ConcatSource, OriginalSource } = require("webpack-sources");
88
const Template = require("./Template");
99

10-
function accessorToObjectAccess(accessor) {
10+
/** @typedef {import("./Compilation")} Compilation */
11+
12+
/**
13+
* @param {string[]} accessor the accessor to convert to path
14+
* @returns {string} the path
15+
*/
16+
const accessorToObjectAccess = accessor => {
1117
return accessor.map(a => `[${JSON.stringify(a)}]`).join("");
12-
}
18+
};
1319

14-
function accessorAccess(base, accessor) {
15-
accessor = [].concat(accessor);
16-
return accessor
17-
.map((a, idx) => {
18-
a = base + accessorToObjectAccess(accessor.slice(0, idx + 1));
19-
if (idx === accessor.length - 1) return a;
20+
/**
21+
* @param {string=} base the path prefix
22+
* @param {string|string[]} accessor the accessor
23+
* @param {string=} joinWith the element separator
24+
* @returns {string} the path
25+
*/
26+
const accessorAccess = (base, accessor, joinWith = ", ") => {
27+
const accessors = Array.isArray(accessor) ? accessor : [accessor];
28+
return accessors
29+
.map((_, idx) => {
30+
const a = base
31+
? base + accessorToObjectAccess(accessors.slice(0, idx + 1))
32+
: accessors[0] + accessorToObjectAccess(accessors.slice(1, idx + 1));
33+
if (idx === accessors.length - 1) return a;
34+
if (idx === 0 && typeof base === "undefined")
35+
return `${a} = typeof ${a} === "object" ? ${a} : {}`;
2036
return `${a} = ${a} || {}`;
2137
})
22-
.join(", ");
23-
}
38+
.join(joinWith);
39+
};
40+
41+
/** @typedef {string | string[] | Record<string, string | string[]>} UmdMainTemplatePluginName */
42+
43+
/**
44+
* @typedef {Object} AuxiliaryCommentObject
45+
* @property {string} root
46+
* @property {string} commonjs
47+
* @property {string} commonjs2
48+
* @property {string} amd
49+
*/
50+
51+
/**
52+
* @typedef {Object} UmdMainTemplatePluginOption
53+
* @property {boolean=} optionalAmdExternalAsGlobal
54+
* @property {boolean} namedDefine
55+
* @property {string | AuxiliaryCommentObject} auxiliaryComment
56+
*/
2457

2558
class UmdMainTemplatePlugin {
59+
/**
60+
* @param {UmdMainTemplatePluginName} name the name of the UMD library
61+
* @param {UmdMainTemplatePluginOption} options the plugin option
62+
*/
2663
constructor(name, options) {
2764
if (typeof name === "object" && !Array.isArray(name)) {
2865
this.name = name.root || name.amd || name.commonjs;
@@ -40,6 +77,10 @@ class UmdMainTemplatePlugin {
4077
this.auxiliaryComment = options.auxiliaryComment;
4178
}
4279

80+
/**
81+
* @param {Compilation} compilation the compilation instance
82+
* @returns {void}
83+
*/
4384
apply(compilation) {
4485
const { mainTemplate, chunkTemplate, runtimeTemplate } = compilation;
4586

@@ -152,23 +193,27 @@ class UmdMainTemplatePlugin {
152193
amdFactory = "factory";
153194
}
154195

196+
const auxiliaryComment = this.auxiliaryComment;
197+
198+
const getAuxilaryComment = type => {
199+
if (auxiliaryComment) {
200+
if (typeof auxiliaryComment === "string")
201+
return "\t//" + auxiliaryComment + "\n";
202+
if (auxiliaryComment[type])
203+
return "\t//" + auxiliaryComment[type] + "\n";
204+
}
205+
return "";
206+
};
207+
155208
return new ConcatSource(
156209
new OriginalSource(
157210
"(function webpackUniversalModuleDefinition(root, factory) {\n" +
158-
(this.auxiliaryComment && typeof this.auxiliaryComment === "string"
159-
? " //" + this.auxiliaryComment + "\n"
160-
: this.auxiliaryComment.commonjs2
161-
? " //" + this.auxiliaryComment.commonjs2 + "\n"
162-
: "") +
211+
getAuxilaryComment("commonjs2") +
163212
" if(typeof exports === 'object' && typeof module === 'object')\n" +
164213
" module.exports = factory(" +
165214
externalsRequireArray("commonjs2") +
166215
");\n" +
167-
(this.auxiliaryComment && typeof this.auxiliaryComment === "string"
168-
? " //" + this.auxiliaryComment + "\n"
169-
: this.auxiliaryComment.amd
170-
? " //" + this.auxiliaryComment.amd + "\n"
171-
: "") +
216+
getAuxilaryComment("amd") +
172217
" else if(typeof define === 'function' && define.amd)\n" +
173218
(requiredExternals.length > 0
174219
? this.names.amd && this.namedDefine === true
@@ -192,24 +237,14 @@ class UmdMainTemplatePlugin {
192237
");\n"
193238
: " define([], " + amdFactory + ");\n") +
194239
(this.names.root || this.names.commonjs
195-
? (this.auxiliaryComment &&
196-
typeof this.auxiliaryComment === "string"
197-
? " //" + this.auxiliaryComment + "\n"
198-
: this.auxiliaryComment.commonjs
199-
? " //" + this.auxiliaryComment.commonjs + "\n"
200-
: "") +
240+
? getAuxilaryComment("commonjs") +
201241
" else if(typeof exports === 'object')\n" +
202242
" exports[" +
203243
libraryName(this.names.commonjs || this.names.root) +
204244
"] = factory(" +
205245
externalsRequireArray("commonjs") +
206246
");\n" +
207-
(this.auxiliaryComment &&
208-
typeof this.auxiliaryComment === "string"
209-
? " //" + this.auxiliaryComment + "\n"
210-
: this.auxiliaryComment.root
211-
? " //" + this.auxiliaryComment.root + "\n"
212-
: "") +
247+
getAuxilaryComment("root") +
213248
" else\n" +
214249
" " +
215250
replaceKeys(

0 commit comments

Comments
 (0)