Skip to content

Commit dd5d866

Browse files
author
Espen Volden
committed
Name AMD module in UMD target if library is set
In the AMD target the AMD module gets named, but in the UMD target its is always anonymous, even if `library` is set in the config. This patch names the AMD module if `library` is set. See the discussion in the referenced issue to see the background for this change. If its a multi part library it uses the last key in the same was as it does with the exports hash part of the UMD declaration. Is this the best solution? Resolves webpack/issues/989
1 parent 05f0cfc commit dd5d866

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

‎lib/UmdMainTemplatePlugin.js‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ UmdMainTemplatePlugin.prototype.apply = function(compilation) {
8787
return "__WEBPACK_EXTERNAL_MODULE_" + m.id + "__";
8888
}).join(", ");
8989
}
90+
91+
function libraryName(library) {
92+
return JSON.stringify(replaceKeys([].concat(library).pop()));
93+
}
94+
9095
if(optionalExternals.length > 0) {
9196
var amdFactory = "function webpackLoadOptionalExternalModuleAmd(" + externalsArguments(requiredExternals) + ") {\n" +
9297
" return factory(" + (
@@ -98,18 +103,25 @@ UmdMainTemplatePlugin.prototype.apply = function(compilation) {
98103
} else {
99104
var amdFactory = "factory";
100105
}
106+
101107
return new ConcatSource(new OriginalSource(
102108
"(function webpackUniversalModuleDefinition(root, factory) {\n" +
103109
" if(typeof exports === 'object' && typeof module === 'object')\n" +
104110
" module.exports = factory(" + externalsRequireArray("commonjs2") + ");\n" +
105111
" else if(typeof define === 'function' && define.amd)\n" +
106112
(requiredExternals.length > 0 ?
107-
" define(" + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" :
108-
" define([], " + amdFactory + ");\n"
113+
(this.name ?
114+
" define(" + libraryName(this.name) + ", " + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" :
115+
" define(" + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n"
116+
) :
117+
(this.name ?
118+
" define(" + libraryName(this.name) + ", [], " + amdFactory + ");\n" :
119+
" define([], " + amdFactory + ");\n"
120+
)
109121
) +
110122
(this.name ?
111123
" else if(typeof exports === 'object')\n" +
112-
" exports[" + JSON.stringify(replaceKeys([].concat(this.name).pop())) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" +
124+
" exports[" + libraryName(this.name) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" +
113125
" else\n" +
114126
" " + replaceKeys(accessorAccess("root", this.name)) + " = factory(" + externalsRootArray(externals) + ");\n" :
115127
" else {\n" +

0 commit comments

Comments
 (0)