source: frontend/node_modules/webpack/lib/dependencies/RequireContextPlugin.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

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