2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-02-25 23:21:24 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin");
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2017-11-08 18:32:05 +08:00
|
|
|
const accessorToObjectAccess = (accessor) => {
|
2017-02-25 23:21:24 +08:00
|
|
|
return accessor.map((a) => {
|
|
|
|
return `[${JSON.stringify(a)}]`;
|
2014-04-06 00:10:47 +08:00
|
|
|
}).join("");
|
2017-11-08 18:32:05 +08:00
|
|
|
};
|
2014-04-06 00:10:47 +08:00
|
|
|
|
2017-11-08 18:32:05 +08:00
|
|
|
const accessorAccess = (base, accessor, joinWith) => {
|
2014-04-06 00:10:47 +08:00
|
|
|
accessor = [].concat(accessor);
|
2017-02-25 23:21:24 +08:00
|
|
|
return accessor.map((a, idx) => {
|
2015-04-24 05:55:50 +08:00
|
|
|
a = base ?
|
|
|
|
base + accessorToObjectAccess(accessor.slice(0, idx + 1)) :
|
|
|
|
accessor[0] + accessorToObjectAccess(accessor.slice(1, idx + 1));
|
2015-07-16 06:19:23 +08:00
|
|
|
if(idx === accessor.length - 1) return a;
|
2017-02-25 23:21:24 +08:00
|
|
|
if(idx === 0 && typeof base === "undefined") return `${a} = typeof ${a} === "object" ? ${a} : {}`;
|
|
|
|
return `${a} = ${a} || {}`;
|
2014-04-06 00:10:47 +08:00
|
|
|
}).join(joinWith || "; ");
|
2017-11-08 18:32:05 +08:00
|
|
|
};
|
2014-04-06 00:10:47 +08:00
|
|
|
|
2017-02-25 23:21:24 +08:00
|
|
|
class LibraryTemplatePlugin {
|
|
|
|
|
2017-06-02 20:52:41 +08:00
|
|
|
constructor(name, target, umdNamedDefine, auxiliaryComment, exportProperty) {
|
2017-02-25 23:21:24 +08:00
|
|
|
this.name = name;
|
|
|
|
this.target = target;
|
|
|
|
this.umdNamedDefine = umdNamedDefine;
|
|
|
|
this.auxiliaryComment = auxiliaryComment;
|
2017-06-02 20:52:41 +08:00
|
|
|
this.exportProperty = exportProperty;
|
2017-02-25 23:21:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
apply(compiler) {
|
2017-12-06 22:01:25 +08:00
|
|
|
compiler.hooks.thisCompilation.tap("LibraryTemplatePlugin", (compilation) => {
|
2017-06-02 20:52:41 +08:00
|
|
|
if(this.exportProperty) {
|
|
|
|
var ExportPropertyMainTemplatePlugin = require("./ExportPropertyMainTemplatePlugin");
|
2017-12-20 16:53:33 +08:00
|
|
|
new ExportPropertyMainTemplatePlugin(this.exportProperty).apply(compilation);
|
2017-06-02 20:52:41 +08:00
|
|
|
}
|
2017-02-25 23:21:24 +08:00
|
|
|
switch(this.target) {
|
|
|
|
case "var":
|
2017-12-20 16:53:33 +08:00
|
|
|
new SetVarMainTemplatePlugin(`var ${accessorAccess(false, this.name)}`).apply(compilation);
|
2017-02-25 23:21:24 +08:00
|
|
|
break;
|
|
|
|
case "assign":
|
2017-12-20 16:53:33 +08:00
|
|
|
new SetVarMainTemplatePlugin(accessorAccess(undefined, this.name)).apply(compilation);
|
2017-02-25 23:21:24 +08:00
|
|
|
break;
|
|
|
|
case "this":
|
2017-12-28 01:46:37 +08:00
|
|
|
case "self":
|
2017-02-25 23:21:24 +08:00
|
|
|
case "window":
|
|
|
|
if(this.name)
|
2017-12-20 16:53:33 +08:00
|
|
|
new SetVarMainTemplatePlugin(accessorAccess(this.target, this.name)).apply(compilation);
|
2017-02-25 23:21:24 +08:00
|
|
|
else
|
2017-12-20 16:53:33 +08:00
|
|
|
new SetVarMainTemplatePlugin(this.target, true).apply(compilation);
|
2017-02-25 23:21:24 +08:00
|
|
|
break;
|
2017-12-28 01:46:37 +08:00
|
|
|
case "global":
|
|
|
|
if(this.name)
|
|
|
|
new SetVarMainTemplatePlugin(accessorAccess(compilation.runtimeTemplate.outputOptions.globalObject, this.name)).apply(compilation);
|
|
|
|
else
|
|
|
|
new SetVarMainTemplatePlugin(compilation.runtimeTemplate.outputOptions.globalObject, true).apply(compilation);
|
|
|
|
break;
|
2017-02-25 23:21:24 +08:00
|
|
|
case "commonjs":
|
|
|
|
if(this.name)
|
2017-12-20 16:53:33 +08:00
|
|
|
new SetVarMainTemplatePlugin(accessorAccess("exports", this.name)).apply(compilation);
|
2017-02-25 23:21:24 +08:00
|
|
|
else
|
2017-12-20 16:53:33 +08:00
|
|
|
new SetVarMainTemplatePlugin("exports", true).apply(compilation);
|
2017-02-25 23:21:24 +08:00
|
|
|
break;
|
|
|
|
case "commonjs2":
|
|
|
|
case "commonjs-module":
|
2017-12-20 16:53:33 +08:00
|
|
|
new SetVarMainTemplatePlugin("module.exports").apply(compilation);
|
2017-02-25 23:21:24 +08:00
|
|
|
break;
|
|
|
|
case "amd":
|
|
|
|
var AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin");
|
2017-12-20 16:53:33 +08:00
|
|
|
new AmdMainTemplatePlugin(this.name).apply(compilation);
|
2017-02-25 23:21:24 +08:00
|
|
|
break;
|
|
|
|
case "umd":
|
|
|
|
case "umd2":
|
|
|
|
var UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin");
|
2017-12-20 16:53:33 +08:00
|
|
|
new UmdMainTemplatePlugin(this.name, {
|
2017-02-25 23:21:24 +08:00
|
|
|
optionalAmdExternalAsGlobal: this.target === "umd2",
|
|
|
|
namedDefine: this.umdNamedDefine,
|
|
|
|
auxiliaryComment: this.auxiliaryComment
|
2017-12-20 16:53:33 +08:00
|
|
|
}).apply(compilation);
|
2017-02-25 23:21:24 +08:00
|
|
|
break;
|
|
|
|
case "jsonp":
|
2017-11-12 01:48:29 +08:00
|
|
|
var JsonpExportMainTemplatePlugin = require("./web/JsonpExportMainTemplatePlugin");
|
2017-12-20 16:53:33 +08:00
|
|
|
new JsonpExportMainTemplatePlugin(this.name).apply(compilation);
|
2017-02-25 23:21:24 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error(`${this.target} is not a valid Library target`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
2017-02-25 23:21:24 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
module.exports = LibraryTemplatePlugin;
|