Skip to content

Commit db21a79

Browse files
author
Espen Volden
committed
Add option umdNamedDefine to toggle naming
1 parent dd5d866 commit db21a79

5 files changed

Lines changed: 33 additions & 7 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: 5 additions & 4 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) {
@@ -110,11 +111,11 @@ UmdMainTemplatePlugin.prototype.apply = function(compilation) {
110111
" module.exports = factory(" + externalsRequireArray("commonjs2") + ");\n" +
111112
" else if(typeof define === 'function' && define.amd)\n" +
112113
(requiredExternals.length > 0 ?
113-
(this.name ?
114+
(this.name && this.namedDefine === true ?
114115
" define(" + libraryName(this.name) + ", " + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" :
115116
" define(" + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n"
116117
) :
117-
(this.name ?
118+
(this.name && this.namedDefine === true ?
118119
" define(" + libraryName(this.name) + ", [], " + amdFactory + ");\n" :
119120
" define([], " + amdFactory + ");\n"
120121
)

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)