wallet-core/node_modules/webpack/lib/LibraryTemplatePlugin.js
2018-09-20 02:56:13 +02:00

154 lines
4.4 KiB
JavaScript

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin");
/** @typedef {import("./Compiler")} Compiler */
/**
* @param {string[]} accessor the accessor to convert to path
* @returns {string} the path
*/
const accessorToObjectAccess = accessor => {
return accessor.map(a => `[${JSON.stringify(a)}]`).join("");
};
/**
* @param {string=} base the path prefix
* @param {string|string[]} accessor the accessor
* @param {string=} joinWith the element separator
* @returns {string} the path
*/
const accessorAccess = (base, accessor, joinWith = "; ") => {
const accessors = Array.isArray(accessor) ? accessor : [accessor];
return accessors
.map((_, idx) => {
const a = base
? base + accessorToObjectAccess(accessors.slice(0, idx + 1))
: accessors[0] + accessorToObjectAccess(accessors.slice(1, idx + 1));
if (idx === accessors.length - 1) return a;
if (idx === 0 && base === undefined) {
return `${a} = typeof ${a} === "object" ? ${a} : {}`;
}
return `${a} = ${a} || {}`;
})
.join(joinWith);
};
class LibraryTemplatePlugin {
/**
* @param {string} name name of library
* @param {string} target type of library
* @param {boolean} umdNamedDefine setting this to true will name the UMD module
* @param {string|TODO} auxiliaryComment comment in the UMD wrapper
* @param {string|string[]} exportProperty which export should be exposed as library
*/
constructor(name, target, umdNamedDefine, auxiliaryComment, exportProperty) {
this.name = name;
this.target = target;
this.umdNamedDefine = umdNamedDefine;
this.auxiliaryComment = auxiliaryComment;
this.exportProperty = exportProperty;
}
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.thisCompilation.tap("LibraryTemplatePlugin", compilation => {
if (this.exportProperty) {
const ExportPropertyMainTemplatePlugin = require("./ExportPropertyMainTemplatePlugin");
new ExportPropertyMainTemplatePlugin(this.exportProperty).apply(
compilation
);
}
switch (this.target) {
case "var":
new SetVarMainTemplatePlugin(
`var ${accessorAccess(undefined, this.name)}`,
false
).apply(compilation);
break;
case "assign":
new SetVarMainTemplatePlugin(
accessorAccess(undefined, this.name),
false
).apply(compilation);
break;
case "this":
case "self":
case "window":
if (this.name) {
new SetVarMainTemplatePlugin(
accessorAccess(this.target, this.name),
false
).apply(compilation);
} else {
new SetVarMainTemplatePlugin(this.target, true).apply(compilation);
}
break;
case "global":
if (this.name) {
new SetVarMainTemplatePlugin(
accessorAccess(
compilation.runtimeTemplate.outputOptions.globalObject,
this.name
),
false
).apply(compilation);
} else {
new SetVarMainTemplatePlugin(
compilation.runtimeTemplate.outputOptions.globalObject,
true
).apply(compilation);
}
break;
case "commonjs":
if (this.name) {
new SetVarMainTemplatePlugin(
accessorAccess("exports", this.name),
false
).apply(compilation);
} else {
new SetVarMainTemplatePlugin("exports", true).apply(compilation);
}
break;
case "commonjs2":
case "commonjs-module":
new SetVarMainTemplatePlugin("module.exports", false).apply(
compilation
);
break;
case "amd": {
const AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin");
new AmdMainTemplatePlugin(this.name).apply(compilation);
break;
}
case "umd":
case "umd2": {
const UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin");
new UmdMainTemplatePlugin(this.name, {
optionalAmdExternalAsGlobal: this.target === "umd2",
namedDefine: this.umdNamedDefine,
auxiliaryComment: this.auxiliaryComment
}).apply(compilation);
break;
}
case "jsonp": {
const JsonpExportMainTemplatePlugin = require("./web/JsonpExportMainTemplatePlugin");
new JsonpExportMainTemplatePlugin(this.name).apply(compilation);
break;
}
default:
throw new Error(`${this.target} is not a valid Library target`);
}
});
}
}
module.exports = LibraryTemplatePlugin;