EnableLibraryPlugin.js 8.1 KB

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