EnableLibraryPlugin.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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: ${Array.from(
  51. getEnabledTypes(compiler)
  52. ).join(", ")}`
  53. );
  54. }
  55. }
  56. /**
  57. * Apply the plugin
  58. * @param {Compiler} compiler the compiler instance
  59. * @returns {void}
  60. */
  61. apply(compiler) {
  62. const { type } = this;
  63. // Only enable once
  64. const enabled = getEnabledTypes(compiler);
  65. if (enabled.has(type)) return;
  66. enabled.add(type);
  67. if (typeof type === "string") {
  68. const enableExportProperty = () => {
  69. const ExportPropertyTemplatePlugin = require("./ExportPropertyLibraryPlugin");
  70. new ExportPropertyTemplatePlugin({
  71. type,
  72. nsObjectUsed: !["module", "modern-module"].includes(type),
  73. runtimeExportsUsed: type !== "modern-module"
  74. }).apply(compiler);
  75. };
  76. switch (type) {
  77. case "var": {
  78. // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  79. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  80. new AssignLibraryPlugin({
  81. type,
  82. prefix: [],
  83. declare: "var",
  84. unnamed: "error"
  85. }).apply(compiler);
  86. break;
  87. }
  88. case "assign-properties": {
  89. // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  90. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  91. new AssignLibraryPlugin({
  92. type,
  93. prefix: [],
  94. declare: false,
  95. unnamed: "error",
  96. named: "copy"
  97. }).apply(compiler);
  98. break;
  99. }
  100. case "assign": {
  101. // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  102. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  103. new AssignLibraryPlugin({
  104. type,
  105. prefix: [],
  106. declare: false,
  107. unnamed: "error"
  108. }).apply(compiler);
  109. break;
  110. }
  111. case "this": {
  112. // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  113. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  114. new AssignLibraryPlugin({
  115. type,
  116. prefix: ["this"],
  117. declare: false,
  118. unnamed: "copy"
  119. }).apply(compiler);
  120. break;
  121. }
  122. case "window": {
  123. // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  124. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  125. new AssignLibraryPlugin({
  126. type,
  127. prefix: ["window"],
  128. declare: false,
  129. unnamed: "copy"
  130. }).apply(compiler);
  131. break;
  132. }
  133. case "self": {
  134. // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  135. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  136. new AssignLibraryPlugin({
  137. type,
  138. prefix: ["self"],
  139. declare: false,
  140. unnamed: "copy"
  141. }).apply(compiler);
  142. break;
  143. }
  144. case "global": {
  145. // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  146. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  147. new AssignLibraryPlugin({
  148. type,
  149. prefix: "global",
  150. declare: false,
  151. unnamed: "copy"
  152. }).apply(compiler);
  153. break;
  154. }
  155. case "commonjs": {
  156. // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  157. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  158. new AssignLibraryPlugin({
  159. type,
  160. prefix: ["exports"],
  161. declare: false,
  162. unnamed: "copy"
  163. }).apply(compiler);
  164. break;
  165. }
  166. case "commonjs-static": {
  167. // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  168. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  169. new AssignLibraryPlugin({
  170. type,
  171. prefix: ["exports"],
  172. declare: false,
  173. unnamed: "static"
  174. }).apply(compiler);
  175. break;
  176. }
  177. case "commonjs2":
  178. case "commonjs-module": {
  179. // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  180. const AssignLibraryPlugin = require("./AssignLibraryPlugin");
  181. new AssignLibraryPlugin({
  182. type,
  183. prefix: ["module", "exports"],
  184. declare: false,
  185. unnamed: "assign"
  186. }).apply(compiler);
  187. break;
  188. }
  189. case "amd":
  190. case "amd-require": {
  191. enableExportProperty();
  192. const AmdLibraryPlugin = require("./AmdLibraryPlugin");
  193. new AmdLibraryPlugin({
  194. type,
  195. requireAsWrapper: type === "amd-require"
  196. }).apply(compiler);
  197. break;
  198. }
  199. case "umd":
  200. case "umd2": {
  201. if (compiler.options.output.iife === false) {
  202. compiler.options.output.iife = true;
  203. class WarnFalseIifeUmdPlugin {
  204. apply(compiler) {
  205. compiler.hooks.thisCompilation.tap(
  206. "WarnFalseIifeUmdPlugin",
  207. compilation => {
  208. const FalseIIFEUmdWarning = require("../FalseIIFEUmdWarning");
  209. compilation.warnings.push(new FalseIIFEUmdWarning());
  210. }
  211. );
  212. }
  213. }
  214. new WarnFalseIifeUmdPlugin().apply(compiler);
  215. }
  216. enableExportProperty();
  217. const UmdLibraryPlugin = require("./UmdLibraryPlugin");
  218. new UmdLibraryPlugin({
  219. type,
  220. optionalAmdExternalAsGlobal: type === "umd2"
  221. }).apply(compiler);
  222. break;
  223. }
  224. case "system": {
  225. enableExportProperty();
  226. const SystemLibraryPlugin = require("./SystemLibraryPlugin");
  227. new SystemLibraryPlugin({
  228. type
  229. }).apply(compiler);
  230. break;
  231. }
  232. case "jsonp": {
  233. enableExportProperty();
  234. const JsonpLibraryPlugin = require("./JsonpLibraryPlugin");
  235. new JsonpLibraryPlugin({
  236. type
  237. }).apply(compiler);
  238. break;
  239. }
  240. case "module": {
  241. enableExportProperty();
  242. const ModuleLibraryPlugin = require("./ModuleLibraryPlugin");
  243. new ModuleLibraryPlugin({
  244. type
  245. }).apply(compiler);
  246. break;
  247. }
  248. case "modern-module": {
  249. enableExportProperty();
  250. const ModernModuleLibraryPlugin = require("./ModernModuleLibraryPlugin");
  251. new ModernModuleLibraryPlugin({
  252. type
  253. }).apply(compiler);
  254. break;
  255. }
  256. default:
  257. throw new Error(`Unsupported library type ${type}.
  258. Plugins which provide custom library types must call EnableLibraryPlugin.setEnabled(compiler, type) to disable this error.`);
  259. }
  260. } else {
  261. // TODO support plugin instances here
  262. // apply them to the compiler
  263. }
  264. }
  265. }
  266. module.exports = EnableLibraryPlugin;