RequireContextPlugin.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const {
  7. JAVASCRIPT_MODULE_TYPE_AUTO,
  8. JAVASCRIPT_MODULE_TYPE_DYNAMIC
  9. } = require("../ModuleTypeConstants");
  10. const { cachedSetProperty } = require("../util/cleverMerge");
  11. const ContextElementDependency = require("./ContextElementDependency");
  12. const RequireContextDependency = require("./RequireContextDependency");
  13. const RequireContextDependencyParserPlugin = require("./RequireContextDependencyParserPlugin");
  14. /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
  15. /** @typedef {import("../../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
  16. /** @typedef {import("../Compiler")} Compiler */
  17. /** @typedef {import("../javascript/JavascriptParser")} Parser */
  18. /** @type {ResolveOptions} */
  19. const EMPTY_RESOLVE_OPTIONS = {};
  20. const PLUGIN_NAME = "RequireContextPlugin";
  21. class RequireContextPlugin {
  22. /**
  23. * Apply the plugin
  24. * @param {Compiler} compiler the compiler instance
  25. * @returns {void}
  26. */
  27. apply(compiler) {
  28. compiler.hooks.compilation.tap(
  29. PLUGIN_NAME,
  30. (compilation, { contextModuleFactory, normalModuleFactory }) => {
  31. compilation.dependencyFactories.set(
  32. RequireContextDependency,
  33. contextModuleFactory
  34. );
  35. compilation.dependencyTemplates.set(
  36. RequireContextDependency,
  37. new RequireContextDependency.Template()
  38. );
  39. compilation.dependencyFactories.set(
  40. ContextElementDependency,
  41. normalModuleFactory
  42. );
  43. /**
  44. * @param {Parser} parser parser parser
  45. * @param {JavascriptParserOptions} parserOptions parserOptions
  46. * @returns {void}
  47. */
  48. const handler = (parser, parserOptions) => {
  49. if (
  50. parserOptions.requireContext !== undefined &&
  51. !parserOptions.requireContext
  52. )
  53. return;
  54. new RequireContextDependencyParserPlugin().apply(parser);
  55. };
  56. normalModuleFactory.hooks.parser
  57. .for(JAVASCRIPT_MODULE_TYPE_AUTO)
  58. .tap(PLUGIN_NAME, handler);
  59. normalModuleFactory.hooks.parser
  60. .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
  61. .tap(PLUGIN_NAME, handler);
  62. contextModuleFactory.hooks.alternativeRequests.tap(
  63. PLUGIN_NAME,
  64. (items, options) => {
  65. if (items.length === 0) return items;
  66. const finalResolveOptions = compiler.resolverFactory.get(
  67. "normal",
  68. cachedSetProperty(
  69. options.resolveOptions || EMPTY_RESOLVE_OPTIONS,
  70. "dependencyType",
  71. /** @type {string} */ (options.category)
  72. )
  73. ).options;
  74. let newItems;
  75. if (!finalResolveOptions.fullySpecified) {
  76. newItems = [];
  77. for (const item of items) {
  78. const { request, context } = item;
  79. for (const ext of finalResolveOptions.extensions) {
  80. if (request.endsWith(ext)) {
  81. newItems.push({
  82. context,
  83. request: request.slice(0, -ext.length)
  84. });
  85. }
  86. }
  87. if (!finalResolveOptions.enforceExtension) {
  88. newItems.push(item);
  89. }
  90. }
  91. items = newItems;
  92. newItems = [];
  93. for (const obj of items) {
  94. const { request, context } = obj;
  95. for (const mainFile of finalResolveOptions.mainFiles) {
  96. if (request.endsWith(`/${mainFile}`)) {
  97. newItems.push({
  98. context,
  99. request: request.slice(0, -mainFile.length)
  100. });
  101. newItems.push({
  102. context,
  103. request: request.slice(0, -mainFile.length - 1)
  104. });
  105. }
  106. }
  107. newItems.push(obj);
  108. }
  109. items = newItems;
  110. }
  111. newItems = [];
  112. for (const item of items) {
  113. let hideOriginal = false;
  114. for (const modulesItems of finalResolveOptions.modules) {
  115. if (Array.isArray(modulesItems)) {
  116. for (const dir of modulesItems) {
  117. if (item.request.startsWith(`./${dir}/`)) {
  118. newItems.push({
  119. context: item.context,
  120. request: item.request.slice(dir.length + 3)
  121. });
  122. hideOriginal = true;
  123. }
  124. }
  125. } else {
  126. const dir = modulesItems.replace(/\\/g, "/");
  127. const fullPath =
  128. item.context.replace(/\\/g, "/") + item.request.slice(1);
  129. if (fullPath.startsWith(dir)) {
  130. newItems.push({
  131. context: item.context,
  132. request: fullPath.slice(dir.length + 1)
  133. });
  134. }
  135. }
  136. }
  137. if (!hideOriginal) {
  138. newItems.push(item);
  139. }
  140. }
  141. return newItems;
  142. }
  143. );
  144. }
  145. );
  146. }
  147. }
  148. module.exports = RequireContextPlugin;