EnableLibraryPlugin.js 7.2 KB

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