EnableLibraryPlugin.js 7.7 KB

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