Skip to content

Commit 314c897

Browse files
committed
Merge pull request webpack#2113 from TheLarkInn/master
Fixes webpack#1431. Add the ability to add auxiliary comments in UMD wrapper.
2 parents dc22ae2 + 5b25024 commit 314c897

File tree

8 files changed

+87
-3
lines changed

8 files changed

+87
-3
lines changed

lib/LibraryTemplatePlugin.js

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

25-
function LibraryTemplatePlugin(name, target, umdNamedDefine) {
25+
function LibraryTemplatePlugin(name, target, umdNamedDefine, auxiliaryComment) {
2626
this.name = name;
2727
this.target = target;
2828
this.umdNamedDefine = umdNamedDefine;
29+
this.auxiliaryComment = auxiliaryComment;
2930
}
3031
module.exports = LibraryTemplatePlugin;
3132
LibraryTemplatePlugin.prototype.apply = function(compiler) {
@@ -63,7 +64,8 @@ LibraryTemplatePlugin.prototype.apply = function(compiler) {
6364
var UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin");
6465
compilation.apply(new UmdMainTemplatePlugin(this.name, {
6566
optionalAmdExternalAsGlobal: this.target === "umd2",
66-
namedDefine: this.umdNamedDefine
67+
namedDefine: this.umdNamedDefine,
68+
auxiliaryComment: this.auxiliaryComment
6769
}));
6870
break;
6971
case "jsonp":

lib/UmdMainTemplatePlugin.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function UmdMainTemplatePlugin(name, options) {
2424
this.name = name;
2525
this.optionalAmdExternalAsGlobal = options.optionalAmdExternalAsGlobal;
2626
this.namedDefine = options.namedDefine;
27+
this.auxiliaryComment = options.auxiliaryComment;
2728
}
2829
module.exports = UmdMainTemplatePlugin;
2930
UmdMainTemplatePlugin.prototype.apply = function(compilation) {
@@ -109,8 +110,22 @@ UmdMainTemplatePlugin.prototype.apply = function(compilation) {
109110

110111
return new ConcatSource(new OriginalSource(
111112
"(function webpackUniversalModuleDefinition(root, factory) {\n" +
113+
(this.auxiliaryComment &&
114+
typeof this.auxiliaryComment === 'string' ?
115+
" //" + this.auxiliaryComment + "\n" :
116+
this.auxiliaryComment.commonjs2 ?
117+
" //" + this.auxiliaryComment.commonjs2 + "\n" :
118+
""
119+
) +
112120
" if(typeof exports === 'object' && typeof module === 'object')\n" +
113121
" module.exports = factory(" + externalsRequireArray("commonjs2") + ");\n" +
122+
(this.auxiliaryComment &&
123+
typeof this.auxiliaryComment === 'string' ?
124+
" //" + this.auxiliaryComment + "\n" :
125+
this.auxiliaryComment.amd ?
126+
" //" + this.auxiliaryComment.amd + "\n" :
127+
""
128+
) +
114129
" else if(typeof define === 'function' && define.amd)\n" +
115130
(requiredExternals.length > 0 ?
116131
(this.name && this.namedDefine === true ?
@@ -123,8 +138,22 @@ UmdMainTemplatePlugin.prototype.apply = function(compilation) {
123138
)
124139
) +
125140
(this.name ?
141+
(this.auxiliaryComment &&
142+
typeof this.auxiliaryComment === 'string' ?
143+
" //" + this.auxiliaryComment + "\n" :
144+
this.auxiliaryComment.commonjs ?
145+
" //" + this.auxiliaryComment.commonjs + "\n" :
146+
""
147+
) +
126148
" else if(typeof exports === 'object')\n" +
127149
" exports[" + libraryName(this.name) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" +
150+
(this.auxiliaryComment &&
151+
typeof this.auxiliaryComment === 'string' ?
152+
" //" + this.auxiliaryComment + "\n" :
153+
this.auxiliaryComment.root ?
154+
" //" + this.auxiliaryComment.root + "\n" :
155+
""
156+
) +
128157
" else\n" +
129158
" " + replaceKeys(accessorAccess("root", this.name)) + " = factory(" + externalsRootArray(externals) + ");\n" :
130159
" else {\n" +

lib/WebpackOptionsApply.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
185185
}
186186
if(options.output.library || options.output.libraryTarget !== "var") {
187187
var LibraryTemplatePlugin = require("./LibraryTemplatePlugin");
188-
compiler.apply(new LibraryTemplatePlugin(options.output.library, options.output.libraryTarget, options.output.umdNamedDefine));
188+
compiler.apply(new LibraryTemplatePlugin(options.output.library, options.output.libraryTarget, options.output.umdNamedDefine, options.output.auxiliaryComment || ""));
189189
}
190190
if(options.externals) {
191191
ExternalsPlugin = require("./ExternalsPlugin");

test/NodeTemplatePlugin.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ describe("NodeTemplatePlugin", function() {
4949
chunkFilename: "[hash].result2.[id].js",
5050
library: "def",
5151
libraryTarget: "umd",
52+
auxiliaryComment: "test"
5253
},
5354
entry: "./entry",
5455
plugins: [
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
it("should run", function() {
2+
3+
});
4+
5+
it("should have auxiliary comments", function() {
6+
var fs = require("fs");
7+
var source = fs.readFileSync(__filename, "utf-8");
8+
9+
source.should.containEql("//test " + "comment " + "commonjs");
10+
source.should.containEql("//test " + "comment " + "commonjs2");
11+
source.should.containEql("//test " + "comment " + "amd");
12+
source.should.containEql("//test " + "comment " + "root");
13+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
output: {
3+
library: "NamedLibrary",
4+
libraryTarget: "umd",
5+
umdNamedDefine: true,
6+
auxiliaryComment: {
7+
commonjs: "test comment commonjs",
8+
commonjs2: "test comment commonjs2",
9+
amd: "test comment amd",
10+
root: "test comment root"
11+
}
12+
},
13+
node: {
14+
__dirname: false,
15+
__filename: false
16+
}
17+
};
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 have auxiliary comment string", function() {
6+
var fs = require("fs");
7+
var source = fs.readFileSync(__filename, "utf-8");
8+
9+
source.should.containEql("//test " + "comment");
10+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
output: {
3+
library: "NamedLibrary",
4+
libraryTarget: "umd",
5+
umdNamedDefine: true,
6+
auxiliaryComment: "test comment"
7+
},
8+
node: {
9+
__dirname: false,
10+
__filename: false
11+
}
12+
};

0 commit comments

Comments
 (0)