RequireContextPlugin.js 4.6 KB

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