forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryTemplatePlugin.js
More file actions
45 lines (44 loc) · 1.69 KB
/
LibraryTemplatePlugin.js
File metadata and controls
45 lines (44 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var SetVarMainTemplateDecorator = require("./SetVarMainTemplateDecorator");
var UmdMainTemplateDecorator = require("./UmdMainTemplateDecorator");
function LibraryTemplatePlugin(name, target) {
this.name = name;
this.target = target;
}
module.exports = LibraryTemplatePlugin;
LibraryTemplatePlugin.prototype.apply = function(compiler) {
switch(this.target) {
case "var":
compiler.mainTemplate = new SetVarMainTemplateDecorator(compiler.mainTemplate, "var " + this.name);
break;
case "this":
if(this.name)
compiler.mainTemplate = new SetVarMainTemplateDecorator(compiler.mainTemplate, "this[" + JSON.stringify(this.name) + "]");
else
compiler.mainTemplate = new SetVarMainTemplateDecorator(compiler.mainTemplate, "this", true);
break;
case "window":
if(this.name)
compiler.mainTemplate = new SetVarMainTemplateDecorator(compiler.mainTemplate, "window[" + JSON.stringify(this.name) + "]");
else
compiler.mainTemplate = new SetVarMainTemplateDecorator(compiler.mainTemplate, "window", true);
break;
case "commonjs":
if(this.name)
compiler.mainTemplate = new SetVarMainTemplateDecorator(compiler.mainTemplate, "exports[" + JSON.stringify(this.name) + "]");
else
compiler.mainTemplate = new SetVarMainTemplateDecorator(compiler.mainTemplate, "exports", true);
break;
case "commonjs2":
compiler.mainTemplate = new SetVarMainTemplateDecorator(compiler.mainTemplate, "module.exports");
break;
case "umd":
compiler.mainTemplate = new UmdMainTemplateDecorator(compiler.mainTemplate, this.name);
break;
default:
throw new Error(this.target + " is not a valid Library target");
}
};