Skip to content

Commit af78089

Browse files
committed
Merge pull request webpack#1387 from voldern/named-define-umd
Name AMD module in UMD target if library is set
2 parents f155f32 + db21a79 commit af78089

5 files changed

Lines changed: 46 additions & 8 deletions

File tree

‎lib/LibraryTemplatePlugin.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ function accessorAccess(base, accessor, joinWith) {
2222
}).join(joinWith || "; ");
2323
}
2424

25-
function LibraryTemplatePlugin(name, target) {
25+
function LibraryTemplatePlugin(name, target, umdNamedDefine) {
2626
this.name = name;
2727
this.target = target;
28+
this.umdNamedDefine = umdNamedDefine;
2829
}
2930
module.exports = LibraryTemplatePlugin;
3031
LibraryTemplatePlugin.prototype.apply = function(compiler) {
@@ -60,7 +61,10 @@ LibraryTemplatePlugin.prototype.apply = function(compiler) {
6061
case "umd":
6162
case "umd2":
6263
var UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin");
63-
compilation.apply(new UmdMainTemplatePlugin(this.name, this.target === "umd2"));
64+
compilation.apply(new UmdMainTemplatePlugin(this.name, {
65+
optionalAmdExternalAsGlobal: this.target === "umd2",
66+
namedDefine: this.umdNamedDefine
67+
}));
6468
break;
6569
case "jsonp":
6670
var JsonpExportMainTemplatePlugin = require("./JsonpExportMainTemplatePlugin");

‎lib/UmdMainTemplatePlugin.js‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ function accessorAccess(base, accessor) {
2020
}).join(", ");
2121
}
2222

23-
function UmdMainTemplatePlugin(name, optionalAmdExternalAsGlobal) {
23+
function UmdMainTemplatePlugin(name, options) {
2424
this.name = name;
25-
this.optionalAmdExternalAsGlobal = optionalAmdExternalAsGlobal;
25+
this.optionalAmdExternalAsGlobal = options.optionalAmdExternalAsGlobal;
26+
this.namedDefine = options.namedDefine;
2627
}
2728
module.exports = UmdMainTemplatePlugin;
2829
UmdMainTemplatePlugin.prototype.apply = function(compilation) {
@@ -87,6 +88,11 @@ UmdMainTemplatePlugin.prototype.apply = function(compilation) {
8788
return "__WEBPACK_EXTERNAL_MODULE_" + m.id + "__";
8889
}).join(", ");
8990
}
91+
92+
function libraryName(library) {
93+
return JSON.stringify(replaceKeys([].concat(library).pop()));
94+
}
95+
9096
if(optionalExternals.length > 0) {
9197
var amdFactory = "function webpackLoadOptionalExternalModuleAmd(" + externalsArguments(requiredExternals) + ") {\n" +
9298
" return factory(" + (
@@ -98,18 +104,25 @@ UmdMainTemplatePlugin.prototype.apply = function(compilation) {
98104
} else {
99105
var amdFactory = "factory";
100106
}
107+
101108
return new ConcatSource(new OriginalSource(
102109
"(function webpackUniversalModuleDefinition(root, factory) {\n" +
103110
" if(typeof exports === 'object' && typeof module === 'object')\n" +
104111
" module.exports = factory(" + externalsRequireArray("commonjs2") + ");\n" +
105112
" else if(typeof define === 'function' && define.amd)\n" +
106113
(requiredExternals.length > 0 ?
107-
" define(" + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" :
108-
" define([], " + amdFactory + ");\n"
114+
(this.name && this.namedDefine === true ?
115+
" define(" + libraryName(this.name) + ", " + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" :
116+
" define(" + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n"
117+
) :
118+
(this.name && this.namedDefine === true ?
119+
" define(" + libraryName(this.name) + ", [], " + amdFactory + ");\n" :
120+
" define([], " + amdFactory + ");\n"
121+
)
109122
) +
110123
(this.name ?
111124
" else if(typeof exports === 'object')\n" +
112-
" exports[" + JSON.stringify(replaceKeys([].concat(this.name).pop())) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" +
125+
" exports[" + libraryName(this.name) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" +
113126
" else\n" +
114127
" " + replaceKeys(accessorAccess("root", this.name)) + " = factory(" + externalsRootArray(externals) + ");\n" :
115128
" else {\n" +

‎lib/WebpackOptionsApply.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
152152
}
153153
if(options.output.library || options.output.libraryTarget !== "var") {
154154
var LibraryTemplatePlugin = require("./LibraryTemplatePlugin");
155-
compiler.apply(new LibraryTemplatePlugin(options.output.library, options.output.libraryTarget));
155+
compiler.apply(new LibraryTemplatePlugin(options.output.library, options.output.libraryTarget, options.output.umdNamedDefine));
156156
}
157157
if(options.externals) {
158158
var ExternalsPlugin = require("./ExternalsPlugin");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
it("should run", function() {
2+
3+
});
4+
5+
it("should name define", function() {
6+
var fs = require("fs");
7+
var source = fs.readFileSync(__filename, "utf-8");
8+
9+
source.should.containEql("define(\"NamedLibrary\",");
10+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
output: {
3+
library: "NamedLibrary",
4+
libraryTarget: "umd",
5+
umdNamedDefine: true
6+
},
7+
node: {
8+
__dirname: false,
9+
__filename: false
10+
}
11+
};

0 commit comments

Comments
 (0)