EnableLibraryPlugin.js 8.1 KB

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