1 |
3a515b92
|
cagy
|
/*
|
2 |
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
3 |
|
|
Author Tobias Koppers @sokra
|
4 |
|
|
*/
|
5 |
|
|
"use strict";
|
6 |
|
|
|
7 |
|
|
const SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin");
|
8 |
|
|
|
9 |
|
|
/** @typedef {import("../declarations/WebpackOptions").LibraryCustomUmdObject} LibraryCustomUmdObject */
|
10 |
|
|
/** @typedef {import("./Compiler")} Compiler */
|
11 |
|
|
|
12 |
|
|
/**
|
13 |
|
|
* @param {string[]} accessor the accessor to convert to path
|
14 |
|
|
* @returns {string} the path
|
15 |
|
|
*/
|
16 |
|
|
const accessorToObjectAccess = accessor => {
|
17 |
|
|
return accessor.map(a => `[${JSON.stringify(a)}]`).join("");
|
18 |
|
|
};
|
19 |
|
|
|
20 |
|
|
/**
|
21 |
|
|
* @param {string=} base the path prefix
|
22 |
|
|
* @param {string|string[]|LibraryCustomUmdObject} accessor the accessor
|
23 |
|
|
* @param {"amd" | "commonjs" | "root"} umdProperty property used when a custom umd object is provided
|
24 |
|
|
* @param {string=} joinWith the element separator
|
25 |
|
|
* @returns {string} the path
|
26 |
|
|
*/
|
27 |
|
|
const accessorAccess = (base, accessor, umdProperty, joinWith = "; ") => {
|
28 |
|
|
const normalizedAccessor =
|
29 |
|
|
typeof accessor === "object" && !Array.isArray(accessor)
|
30 |
|
|
? accessor[umdProperty]
|
31 |
|
|
: accessor;
|
32 |
|
|
const accessors = Array.isArray(normalizedAccessor)
|
33 |
|
|
? normalizedAccessor
|
34 |
|
|
: [normalizedAccessor];
|
35 |
|
|
return accessors
|
36 |
|
|
.map((_, idx) => {
|
37 |
|
|
const a = base
|
38 |
|
|
? base + accessorToObjectAccess(accessors.slice(0, idx + 1))
|
39 |
|
|
: accessors[0] + accessorToObjectAccess(accessors.slice(1, idx + 1));
|
40 |
|
|
if (idx === accessors.length - 1) return a;
|
41 |
|
|
if (idx === 0 && base === undefined) {
|
42 |
|
|
return `${a} = typeof ${a} === "object" ? ${a} : {}`;
|
43 |
|
|
}
|
44 |
|
|
return `${a} = ${a} || {}`;
|
45 |
|
|
})
|
46 |
|
|
.join(joinWith);
|
47 |
|
|
};
|
48 |
|
|
|
49 |
|
|
class LibraryTemplatePlugin {
|
50 |
|
|
/**
|
51 |
|
|
* @param {string|string[]|LibraryCustomUmdObject} name name of library
|
52 |
|
|
* @param {string} target type of library
|
53 |
|
|
* @param {boolean} umdNamedDefine setting this to true will name the UMD module
|
54 |
|
|
* @param {string|TODO} auxiliaryComment comment in the UMD wrapper
|
55 |
|
|
* @param {string|string[]} exportProperty which export should be exposed as library
|
56 |
|
|
*/
|
57 |
|
|
constructor(name, target, umdNamedDefine, auxiliaryComment, exportProperty) {
|
58 |
|
|
this.name = name;
|
59 |
|
|
this.target = target;
|
60 |
|
|
this.umdNamedDefine = umdNamedDefine;
|
61 |
|
|
this.auxiliaryComment = auxiliaryComment;
|
62 |
|
|
this.exportProperty = exportProperty;
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
/**
|
66 |
|
|
* @param {Compiler} compiler the compiler instance
|
67 |
|
|
* @returns {void}
|
68 |
|
|
*/
|
69 |
|
|
apply(compiler) {
|
70 |
|
|
compiler.hooks.thisCompilation.tap("LibraryTemplatePlugin", compilation => {
|
71 |
|
|
if (this.exportProperty) {
|
72 |
|
|
const ExportPropertyMainTemplatePlugin = require("./ExportPropertyMainTemplatePlugin");
|
73 |
|
|
new ExportPropertyMainTemplatePlugin(this.exportProperty).apply(
|
74 |
|
|
compilation
|
75 |
|
|
);
|
76 |
|
|
}
|
77 |
|
|
switch (this.target) {
|
78 |
|
|
case "var":
|
79 |
|
|
if (
|
80 |
|
|
!this.name ||
|
81 |
|
|
(typeof this.name === "object" && !Array.isArray(this.name))
|
82 |
|
|
) {
|
83 |
|
|
throw new Error(
|
84 |
|
|
"library name must be set and not an UMD custom object for non-UMD target"
|
85 |
|
|
);
|
86 |
|
|
}
|
87 |
|
|
new SetVarMainTemplatePlugin(
|
88 |
|
|
`var ${accessorAccess(undefined, this.name, "root")}`,
|
89 |
|
|
false
|
90 |
|
|
).apply(compilation);
|
91 |
|
|
break;
|
92 |
|
|
case "assign":
|
93 |
|
|
new SetVarMainTemplatePlugin(
|
94 |
|
|
accessorAccess(undefined, this.name, "root"),
|
95 |
|
|
false
|
96 |
|
|
).apply(compilation);
|
97 |
|
|
break;
|
98 |
|
|
case "this":
|
99 |
|
|
case "self":
|
100 |
|
|
case "window":
|
101 |
|
|
if (this.name) {
|
102 |
|
|
new SetVarMainTemplatePlugin(
|
103 |
|
|
accessorAccess(this.target, this.name, "root"),
|
104 |
|
|
false
|
105 |
|
|
).apply(compilation);
|
106 |
|
|
} else {
|
107 |
|
|
new SetVarMainTemplatePlugin(this.target, true).apply(compilation);
|
108 |
|
|
}
|
109 |
|
|
break;
|
110 |
|
|
case "global":
|
111 |
|
|
if (this.name) {
|
112 |
|
|
new SetVarMainTemplatePlugin(
|
113 |
|
|
accessorAccess(
|
114 |
|
|
compilation.runtimeTemplate.outputOptions.globalObject,
|
115 |
|
|
this.name,
|
116 |
|
|
"root"
|
117 |
|
|
),
|
118 |
|
|
false
|
119 |
|
|
).apply(compilation);
|
120 |
|
|
} else {
|
121 |
|
|
new SetVarMainTemplatePlugin(
|
122 |
|
|
compilation.runtimeTemplate.outputOptions.globalObject,
|
123 |
|
|
true
|
124 |
|
|
).apply(compilation);
|
125 |
|
|
}
|
126 |
|
|
break;
|
127 |
|
|
case "commonjs":
|
128 |
|
|
if (this.name) {
|
129 |
|
|
new SetVarMainTemplatePlugin(
|
130 |
|
|
accessorAccess("exports", this.name, "commonjs"),
|
131 |
|
|
false
|
132 |
|
|
).apply(compilation);
|
133 |
|
|
} else {
|
134 |
|
|
new SetVarMainTemplatePlugin("exports", true).apply(compilation);
|
135 |
|
|
}
|
136 |
|
|
break;
|
137 |
|
|
case "commonjs2":
|
138 |
|
|
case "commonjs-module":
|
139 |
|
|
new SetVarMainTemplatePlugin("module.exports", false).apply(
|
140 |
|
|
compilation
|
141 |
|
|
);
|
142 |
|
|
break;
|
143 |
|
|
case "amd":
|
144 |
|
|
case "amd-require": {
|
145 |
|
|
const AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin");
|
146 |
|
|
if (this.name && typeof this.name !== "string") {
|
147 |
|
|
throw new Error("library name must be a string for amd target");
|
148 |
|
|
}
|
149 |
|
|
new AmdMainTemplatePlugin({
|
150 |
|
|
name: this.name,
|
151 |
|
|
requireAsWrapper: this.target === "amd-require"
|
152 |
|
|
}).apply(compilation);
|
153 |
|
|
break;
|
154 |
|
|
}
|
155 |
|
|
case "umd":
|
156 |
|
|
case "umd2": {
|
157 |
|
|
const UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin");
|
158 |
|
|
new UmdMainTemplatePlugin(this.name, {
|
159 |
|
|
optionalAmdExternalAsGlobal: this.target === "umd2",
|
160 |
|
|
namedDefine: this.umdNamedDefine,
|
161 |
|
|
auxiliaryComment: this.auxiliaryComment
|
162 |
|
|
}).apply(compilation);
|
163 |
|
|
break;
|
164 |
|
|
}
|
165 |
|
|
case "jsonp": {
|
166 |
|
|
const JsonpExportMainTemplatePlugin = require("./web/JsonpExportMainTemplatePlugin");
|
167 |
|
|
if (typeof this.name !== "string")
|
168 |
|
|
throw new Error("library name must be a string for jsonp target");
|
169 |
|
|
new JsonpExportMainTemplatePlugin(this.name).apply(compilation);
|
170 |
|
|
break;
|
171 |
|
|
}
|
172 |
|
|
case "system": {
|
173 |
|
|
const SystemMainTemplatePlugin = require("./SystemMainTemplatePlugin");
|
174 |
|
|
new SystemMainTemplatePlugin({
|
175 |
|
|
name: this.name
|
176 |
|
|
}).apply(compilation);
|
177 |
|
|
break;
|
178 |
|
|
}
|
179 |
|
|
default:
|
180 |
|
|
throw new Error(`${this.target} is not a valid Library target`);
|
181 |
|
|
}
|
182 |
|
|
});
|
183 |
|
|
}
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
module.exports = LibraryTemplatePlugin;
|