EnableLibraryPlugin.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
  7. /** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
  8. /** @typedef {import("../Compiler")} Compiler */
  9. /** @type {WeakMap<Compiler, Set<LibraryType>>} */
  10. const enabledTypes = new WeakMap();
  11. const getEnabledTypes = compiler => {
  12. let set = enabledTypes.get(compiler);
  13. if (set === undefined) {
  14. set = new Set();
  15. enabledTypes.set(compiler, set);
  16. }
  17. return set;
  18. };
  19. class EnableLibraryPlugin {
  20. /**
  21. * @param {LibraryType} type library type that should be available
  22. */
  23. constructor(type) {
  24. this.type = type;
  25. }
  26. /**
  27. * @param {Compiler} compiler the compiler instance
  28. * @param {LibraryType} type type of library
  29. * @returns {void}
  30. */
  31. static setEnabled(compiler, type) {
  32. getEnabledTypes(compiler).add(type);
  33. }
  34. /**
  35. * @param {Compiler} compiler the compiler instance
  36. * @param {LibraryType} type type of library
  37. * @returns {void}
  38. */
  39. static checkEnabled(compiler, type) {
  40. if (!getEnabledTypes(compiler).has(type)) {
  41. throw new Error(
  42. `Library type "${type}" is not enabled. ` +
  43. "EnableLibraryPlugin need to be used to enable this type of library. " +
  44. 'This usually happens through the "output.enabledLibraryTypes" option. ' +
  45. 'If you are using a function as entry which sets "library", you need to add all potential library types to "output.enabledLibraryTypes". ' +
  46. "These types are enabled: " +
  47. Array.from(getEnabledTypes(compiler)).join(", ")
  48. );
  49. }
  50. }
  51. /**
  52. * Apply the plugin
  53. * @param {Compiler} compiler the compiler instance
  54. * @returns {void}
  55. */
  56. apply(compiler) {
  57. const { type } = this;
  58. // Only enable once
  59. const enabled = getEnabledTypes(compiler);
  60. if (enabled.has(type)) return;
  61. enabled.add(type);
  62. if (typeof type === "string") {
  63. const enableExportProperty = () => {
  64. const ExportPropertyTemplatePlugin = require("./ExportPropertyLibraryPlugin");
  65. new ExportPropertyTemplatePlugin({
  66. type,
  67. nsObjectUsed: type !== "module"
  68. }).apply(compiler);
  69. };
  70. switch (type) {
  71. case "var": {
  72. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  73. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  74. new AssignLibraryPlugin({
  75. type,
  76. prefix: [],
  77. declare: "var",
  78. unnamed: "error"
  79. }).apply(compiler);
  80. break;
  81. }
  82. case "assign-properties": {
  83. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  84. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  85. new AssignLibraryPlugin({
  86. type,
  87. prefix: [],
  88. declare: false,
  89. unnamed: "error",
  90. named: "copy"
  91. }).apply(compiler);
  92. break;
  93. }
  94. case "assign": {
  95. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  96. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  97. new AssignLibraryPlugin({
  98. type,
  99. prefix: [],
  100. declare: false,
  101. unnamed: "error"
  102. }).apply(compiler);
  103. break;
  104. }
  105. case "this": {
  106. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  107. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  108. new AssignLibraryPlugin({
  109. type,
  110. prefix: ["this"],
  111. declare: false,
  112. unnamed: "copy"
  113. }).apply(compiler);
  114. break;
  115. }
  116. case "window": {
  117. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  118. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  119. new AssignLibraryPlugin({
  120. type,
  121. prefix: ["window"],
  122. declare: false,
  123. unnamed: "copy"
  124. }).apply(compiler);
  125. break;
  126. }
  127. case "self": {
  128. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  129. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  130. new AssignLibraryPlugin({
  131. type,
  132. prefix: ["self"],
  133. declare: false,
  134. unnamed: "copy"
  135. }).apply(compiler);
  136. break;
  137. }
  138. case "global": {
  139. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  140. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  141. new AssignLibraryPlugin({
  142. type,
  143. prefix: "global",
  144. declare: false,
  145. unnamed: "copy"
  146. }).apply(compiler);
  147. break;
  148. }
  149. case "commonjs": {
  150. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  151. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  152. new AssignLibraryPlugin({
  153. type,
  154. prefix: ["exports"],
  155. declare: false,
  156. unnamed: "copy"
  157. }).apply(compiler);
  158. break;
  159. }
  160. case "commonjs-static": {
  161. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  162. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  163. new AssignLibraryPlugin({
  164. type,
  165. prefix: ["exports"],
  166. declare: false,
  167. unnamed: "static"
  168. }).apply(compiler);
  169. break;
  170. }
  171. case "commonjs2":
  172. case "commonjs-module": {
  173. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  174. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  175. new AssignLibraryPlugin({
  176. type,
  177. prefix: ["module", "exports"],
  178. declare: false,
  179. unnamed: "assign"
  180. }).apply(compiler);
  181. break;
  182. }
  183. case "amd":
  184. case "amd-require": {
  185. enableExportProperty();
  186. const AmdLibraryPlugin = require("./AmdLibraryPlugin");
  187. new AmdLibraryPlugin({
  188. type,
  189. requireAsWrapper: type === "amd-require"
  190. }).apply(compiler);
  191. break;
  192. }
  193. case "umd":
  194. case "umd2": {
  195. enableExportProperty();
  196. const UmdLibraryPlugin = require("./UmdLibraryPlugin");
  197. new UmdLibraryPlugin({
  198. type,
  199. optionalAmdExternalAsGlobal: type === "umd2"
  200. }).apply(compiler);
  201. break;
  202. }
  203. case "system": {
  204. enableExportProperty();
  205. const SystemLibraryPlugin = require("./SystemLibraryPlugin");
  206. new SystemLibraryPlugin({
  207. type
  208. }).apply(compiler);
  209. break;
  210. }
  211. case "jsonp": {
  212. enableExportProperty();
  213. const JsonpLibraryPlugin = require("./JsonpLibraryPlugin");
  214. new JsonpLibraryPlugin({
  215. type
  216. }).apply(compiler);
  217. break;
  218. }
  219. case "module": {
  220. enableExportProperty();
  221. const ModuleLibraryPlugin = require("./ModuleLibraryPlugin");
  222. new ModuleLibraryPlugin({
  223. type
  224. }).apply(compiler);
  225. break;
  226. }
  227. default:
  228. throw new Error(`Unsupported library type ${type}.
  229. Plugins which provide custom library types must call EnableLibraryPlugin.setEnabled(compiler, type) to disable this error.`);
  230. }
  231. } else {
  232. // TODO support plugin instances here
  233. // apply them to the compiler
  234. }
  235. }
  236. }
  237. module.exports = EnableLibraryPlugin;