Skip to content

Commit b7cd807

Browse files
committed
add 'amd' library target
fixes webpack#164
1 parent 3b3f6cb commit b7cd807

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/AmdMainTemplateDecorator.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var ConcatSource = require("webpack-core/lib/ConcatSource");
6+
7+
function AmdMainTemplateDecorator(mainTemplate, name) {
8+
this.mainTemplate = mainTemplate;
9+
this.name = name;
10+
}
11+
module.exports = AmdMainTemplateDecorator;
12+
AmdMainTemplateDecorator.prototype.render = function(hash, chunk, moduleTemplate, dependencyTemplates) {
13+
var source = this.mainTemplate.render(hash, chunk, moduleTemplate, dependencyTemplates);
14+
if(this.name) {
15+
return new ConcatSource("define(" + JSON.stringify(this.name) + ", [], function() { return ", source, "});");
16+
} else {
17+
return new ConcatSource("define(function() { return ", source, "});");
18+
}
19+
};
20+
AmdMainTemplateDecorator.prototype.updateHash = function(hash) {
21+
hash.update("exports amd");
22+
hash.update(this.name + "");
23+
this.mainTemplate.updateHash(hash);
24+
};

lib/LibraryTemplatePlugin.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Author Tobias Koppers @sokra
44
*/
55
var SetVarMainTemplateDecorator = require("./SetVarMainTemplateDecorator");
6-
var UmdMainTemplateDecorator = require("./UmdMainTemplateDecorator");
76

87
function LibraryTemplatePlugin(name, target) {
98
this.name = name;
@@ -36,7 +35,12 @@ LibraryTemplatePlugin.prototype.apply = function(compiler) {
3635
case "commonjs2":
3736
compiler.mainTemplate = new SetVarMainTemplateDecorator(compiler.mainTemplate, "module.exports");
3837
break;
38+
case "amd":
39+
var AmdMainTemplateDecorator = require("./AmdMainTemplateDecorator");
40+
compiler.mainTemplate = new AmdMainTemplateDecorator(compiler.mainTemplate, this.name);
41+
break;
3942
case "umd":
43+
var UmdMainTemplateDecorator = require("./UmdMainTemplateDecorator");
4044
compiler.mainTemplate = new UmdMainTemplateDecorator(compiler.mainTemplate, this.name);
4145
break;
4246
default:

0 commit comments

Comments
 (0)