EnableLibraryPlugin.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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: type !== "module"
  72. }).apply(compiler);
  73. };
  74. switch (type) {
  75. case "var": {
  76. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  77. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  78. new AssignLibraryPlugin({
  79. type,
  80. prefix: [],
  81. declare: "var",
  82. unnamed: "error"
  83. }).apply(compiler);
  84. break;
  85. }
  86. case "assign-properties": {
  87. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  88. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  89. new AssignLibraryPlugin({
  90. type,
  91. prefix: [],
  92. declare: false,
  93. unnamed: "error",
  94. named: "copy"
  95. }).apply(compiler);
  96. break;
  97. }
  98. case "assign": {
  99. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  100. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  101. new AssignLibraryPlugin({
  102. type,
  103. prefix: [],
  104. declare: false,
  105. unnamed: "error"
  106. }).apply(compiler);
  107. break;
  108. }
  109. case "this": {
  110. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  111. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  112. new AssignLibraryPlugin({
  113. type,
  114. prefix: ["this"],
  115. declare: false,
  116. unnamed: "copy"
  117. }).apply(compiler);
  118. break;
  119. }
  120. case "window": {
  121. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  122. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  123. new AssignLibraryPlugin({
  124. type,
  125. prefix: ["window"],
  126. declare: false,
  127. unnamed: "copy"
  128. }).apply(compiler);
  129. break;
  130. }
  131. case "self": {
  132. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  133. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  134. new AssignLibraryPlugin({
  135. type,
  136. prefix: ["self"],
  137. declare: false,
  138. unnamed: "copy"
  139. }).apply(compiler);
  140. break;
  141. }
  142. case "global": {
  143. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  144. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  145. new AssignLibraryPlugin({
  146. type,
  147. prefix: "global",
  148. declare: false,
  149. unnamed: "copy"
  150. }).apply(compiler);
  151. break;
  152. }
  153. case "commonjs": {
  154. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  155. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  156. new AssignLibraryPlugin({
  157. type,
  158. prefix: ["exports"],
  159. declare: false,
  160. unnamed: "copy"
  161. }).apply(compiler);
  162. break;
  163. }
  164. case "commonjs-static": {
  165. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  166. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  167. new AssignLibraryPlugin({
  168. type,
  169. prefix: ["exports"],
  170. declare: false,
  171. unnamed: "static"
  172. }).apply(compiler);
  173. break;
  174. }
  175. case "commonjs2":
  176. case "commonjs-module": {
  177. //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  178. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  179. new AssignLibraryPlugin({
  180. type,
  181. prefix: ["module", "exports"],
  182. declare: false,
  183. unnamed: "assign"
  184. }).apply(compiler);
  185. break;
  186. }
  187. case "amd":
  188. case "amd-require": {
  189. enableExportProperty();
  190. const AmdLibraryPlugin = require("./AmdLibraryPlugin");
  191. new AmdLibraryPlugin({
  192. type,
  193. requireAsWrapper: type === "amd-require"
  194. }).apply(compiler);
  195. break;
  196. }
  197. case "umd":
  198. case "umd2": {
  199. enableExportProperty();
  200. const UmdLibraryPlugin = require("./UmdLibraryPlugin");
  201. new UmdLibraryPlugin({
  202. type,
  203. optionalAmdExternalAsGlobal: type === "umd2"
  204. }).apply(compiler);
  205. break;
  206. }
  207. case "system": {
  208. enableExportProperty();
  209. const SystemLibraryPlugin = require("./SystemLibraryPlugin");
  210. new SystemLibraryPlugin({
  211. type
  212. }).apply(compiler);
  213. break;
  214. }
  215. case "jsonp": {
  216. enableExportProperty();
  217. const JsonpLibraryPlugin = require("./JsonpLibraryPlugin");
  218. new JsonpLibraryPlugin({
  219. type
  220. }).apply(compiler);
  221. break;
  222. }
  223. case "module": {
  224. enableExportProperty();
  225. const ModuleLibraryPlugin = require("./ModuleLibraryPlugin");
  226. new ModuleLibraryPlugin({
  227. type
  228. }).apply(compiler);
  229. break;
  230. }
  231. default:
  232. throw new Error(`Unsupported library type ${type}.
  233. Plugins which provide custom library types must call EnableLibraryPlugin.setEnabled(compiler, type) to disable this error.`);
  234. }
  235. } else {
  236. // TODO support plugin instances here
  237. // apply them to the compiler
  238. }
  239. }
  240. }
  241. module.exports = EnableLibraryPlugin;