source: imaps-frontend/node_modules/webpack/lib/dependencies/RequireContextPlugin.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 4.5 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 * Apply the plugin
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 * @param {Parser} parser parser parser
53 * @param {JavascriptParserOptions} parserOptions parserOptions
54 * @returns {void}
55 */
56 const handler = (parser, parserOptions) => {
57 if (
58 parserOptions.requireContext !== undefined &&
59 !parserOptions.requireContext
60 )
61 return;
62
63 new RequireContextDependencyParserPlugin().apply(parser);
64 };
65
66 normalModuleFactory.hooks.parser
67 .for(JAVASCRIPT_MODULE_TYPE_AUTO)
68 .tap(PLUGIN_NAME, handler);
69 normalModuleFactory.hooks.parser
70 .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
71 .tap(PLUGIN_NAME, handler);
72
73 contextModuleFactory.hooks.alternativeRequests.tap(
74 PLUGIN_NAME,
75 (items, options) => {
76 if (items.length === 0) return items;
77
78 const finalResolveOptions = compiler.resolverFactory.get(
79 "normal",
80 cachedSetProperty(
81 options.resolveOptions || EMPTY_RESOLVE_OPTIONS,
82 "dependencyType",
83 /** @type {string} */ (options.category)
84 )
85 ).options;
86
87 let newItems;
88 if (!finalResolveOptions.fullySpecified) {
89 newItems = [];
90 for (const item of items) {
91 const { request, context } = item;
92 for (const ext of finalResolveOptions.extensions) {
93 if (request.endsWith(ext)) {
94 newItems.push({
95 context,
96 request: request.slice(0, -ext.length)
97 });
98 }
99 }
100 if (!finalResolveOptions.enforceExtension) {
101 newItems.push(item);
102 }
103 }
104 items = newItems;
105
106 newItems = [];
107 for (const obj of items) {
108 const { request, context } = obj;
109 for (const mainFile of finalResolveOptions.mainFiles) {
110 if (request.endsWith(`/${mainFile}`)) {
111 newItems.push({
112 context,
113 request: request.slice(0, -mainFile.length)
114 });
115 newItems.push({
116 context,
117 request: request.slice(0, -mainFile.length - 1)
118 });
119 }
120 }
121 newItems.push(obj);
122 }
123 items = newItems;
124 }
125
126 newItems = [];
127 for (const item of items) {
128 let hideOriginal = false;
129 for (const modulesItems of finalResolveOptions.modules) {
130 if (Array.isArray(modulesItems)) {
131 for (const dir of modulesItems) {
132 if (item.request.startsWith(`./${dir}/`)) {
133 newItems.push({
134 context: item.context,
135 request: item.request.slice(dir.length + 3)
136 });
137 hideOriginal = true;
138 }
139 }
140 } else {
141 const dir = modulesItems.replace(/\\/g, "/");
142 const fullPath =
143 item.context.replace(/\\/g, "/") + item.request.slice(1);
144 if (fullPath.startsWith(dir)) {
145 newItems.push({
146 context: item.context,
147 request: fullPath.slice(dir.length + 1)
148 });
149 }
150 }
151 }
152 if (!hideOriginal) {
153 newItems.push(item);
154 }
155 }
156 return newItems;
157 }
158 );
159 }
160 );
161 }
162}
163module.exports = RequireContextPlugin;
Note: See TracBrowser for help on using the repository browser.