webpack/lib/library/EnableLibraryPlugin.js

176 lines
4.2 KiB
JavaScript

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
/** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
/** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
/** @typedef {import("../Compiler")} Compiler */
/** @type {WeakMap<Compiler, Set<LibraryType>>} */
const enabledTypes = new WeakMap();
const getEnabledTypes = compiler => {
let set = enabledTypes.get(compiler);
if (set === undefined) {
set = new Set();
enabledTypes.set(compiler, set);
}
return set;
};
class EnableLibraryPlugin {
/**
* @param {LibraryType} type library type that should be available
*/
constructor(type) {
this.type = type;
}
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
const { type } = this;
// Only enable once
if (getEnabledTypes(compiler).has(type)) return;
if (typeof type === "string") {
const ExportPropertyTemplatePlugin = require("./ExportPropertyLibraryPlugin");
new ExportPropertyTemplatePlugin({
type,
nsObjectUsed: type !== "module"
}).apply(compiler);
switch (type) {
case "var": {
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
prefix: [],
declare: "var",
unnamed: "error"
}).apply(compiler);
break;
}
case "assign": {
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
prefix: [],
declare: false,
unnamed: "error"
}).apply(compiler);
break;
}
case "this": {
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
prefix: ["this"],
declare: false,
unnamed: "copy"
}).apply(compiler);
break;
}
case "window": {
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
prefix: ["window"],
declare: false,
unnamed: "copy"
}).apply(compiler);
break;
}
case "self": {
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
prefix: ["self"],
declare: false,
unnamed: "copy"
}).apply(compiler);
break;
}
case "global": {
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
prefix: "global",
declare: false,
unnamed: "copy"
}).apply(compiler);
break;
}
case "commonjs": {
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
prefix: ["exports"],
declare: false,
unnamed: "copy"
}).apply(compiler);
break;
}
case "commonjs2":
case "commonjs-module": {
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
prefix: ["module", "exports"],
declare: false,
unnamed: "assign"
}).apply(compiler);
break;
}
case "amd":
case "amd-require": {
const AmdLibraryPlugin = require("./AmdLibraryPlugin");
new AmdLibraryPlugin({
type,
requireAsWrapper: type === "amd-require"
}).apply(compiler);
break;
}
case "umd":
case "umd2": {
const UmdLibraryPlugin = require("./UmdLibraryPlugin");
new UmdLibraryPlugin({
type,
optionalAmdExternalAsGlobal: type === "umd2"
}).apply(compiler);
break;
}
case "system": {
const SystemLibraryPlugin = require("./SystemLibraryPlugin");
new SystemLibraryPlugin({
type
}).apply(compiler);
break;
}
case "jsonp": {
const JsonpLibraryPlugin = require("./JsonpLibraryPlugin");
new JsonpLibraryPlugin({
type
}).apply(compiler);
break;
}
case "module":
// TODO implement module library
break;
default:
throw new Error(`Unsupported library type ${type}`);
}
} else {
// TODO support plugin instances here
// apply them to the compiler
}
}
}
module.exports = EnableLibraryPlugin;