source: frontend/node_modules/webpack/lib/rules/UseEffectRulePlugin.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: 7.0 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 util = require("util");
9
10/** @typedef {import("../../declarations/WebpackOptions").Falsy} Falsy */
11/** @typedef {import("../../declarations/WebpackOptions").RuleSetLoader} RuleSetLoader */
12/** @typedef {import("../../declarations/WebpackOptions").RuleSetLoaderOptions} RuleSetLoaderOptions */
13/** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */
14/** @typedef {import("../../declarations/WebpackOptions").RuleSetUse} RuleSetUse */
15/** @typedef {import("../../declarations/WebpackOptions").RuleSetUseItem} RuleSetUseItem */
16/** @typedef {import("../../declarations/WebpackOptions").RuleSetUseFunction} RuleSetUseFunction */
17/** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */
18/** @typedef {import("./RuleSetCompiler").Effect} Effect */
19/** @typedef {import("./RuleSetCompiler").EffectData} EffectData */
20/** @typedef {import("./RuleSetCompiler").EffectUseType} EffectUseType */
21
22const PLUGIN_NAME = "UseEffectRulePlugin";
23
24class UseEffectRulePlugin {
25 /**
26 * Applies the plugin by registering its hooks on the compiler.
27 * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler
28 * @returns {void}
29 */
30 apply(ruleSetCompiler) {
31 ruleSetCompiler.hooks.rule.tap(
32 PLUGIN_NAME,
33 (path, rule, unhandledProperties, result, references) => {
34 /**
35 * Processes the provided property.
36 * @param {keyof RuleSetRule} property property
37 * @param {string} correctProperty correct property
38 */
39 const conflictWith = (property, correctProperty) => {
40 if (unhandledProperties.has(property)) {
41 throw ruleSetCompiler.error(
42 `${path}.${property}`,
43 rule[property],
44 `A Rule must not have a '${property}' property when it has a '${correctProperty}' property`
45 );
46 }
47 };
48
49 if (unhandledProperties.has("use")) {
50 unhandledProperties.delete("use");
51 unhandledProperties.delete("enforce");
52
53 conflictWith("loader", "use");
54 conflictWith("options", "use");
55
56 const use = /** @type {RuleSetUse} */ (rule.use);
57 const enforce = rule.enforce;
58
59 const type =
60 /** @type {EffectUseType} */
61 (enforce ? `use-${enforce}` : "use");
62
63 /**
64 * Returns effect.
65 * @param {string} path options path
66 * @param {string} defaultIdent default ident when none is provided
67 * @param {RuleSetUseItem} item user provided use value
68 * @returns {(Effect | ((effectData: EffectData) => Effect[]))} effect
69 */
70 const useToEffect = (path, defaultIdent, item) => {
71 if (typeof item === "function") {
72 return (data) =>
73 useToEffectsWithoutIdent(
74 path,
75 /** @type {RuleSetUseItem | RuleSetUseItem[]} */
76 (item(data))
77 );
78 }
79 return useToEffectRaw(path, defaultIdent, item);
80 };
81
82 /**
83 * Returns effect.
84 * @param {string} path options path
85 * @param {string} defaultIdent default ident when none is provided
86 * @param {Exclude<NonNullable<RuleSetUseItem>, RuleSetUseFunction>} item user provided use value
87 * @returns {Effect} effect
88 */
89 const useToEffectRaw = (path, defaultIdent, item) => {
90 if (typeof item === "string") {
91 return {
92 type,
93 value: {
94 loader: item,
95 options: undefined,
96 ident: undefined
97 }
98 };
99 }
100 const loader = /** @type {string} */ (item.loader);
101 const options = item.options;
102 let ident = item.ident;
103 if (options && typeof options === "object") {
104 if (!ident) ident = defaultIdent;
105 references.set(ident, options);
106 }
107 if (typeof options === "string") {
108 util.deprecate(
109 () => {},
110 `Using a string as loader options is deprecated (${path}.options)`,
111 "DEP_WEBPACK_RULE_LOADER_OPTIONS_STRING"
112 )();
113 }
114 return {
115 type: enforce ? `use-${enforce}` : "use",
116 value: {
117 loader,
118 options,
119 ident
120 }
121 };
122 };
123
124 /**
125 * Use to effects without ident.
126 * @param {string} path options path
127 * @param {RuleSetUseItem | (Falsy | RuleSetUseItem)[]} items user provided use value
128 * @returns {Effect[]} effects
129 */
130 const useToEffectsWithoutIdent = (path, items) => {
131 if (Array.isArray(items)) {
132 return items.filter(Boolean).map((item, idx) =>
133 useToEffectRaw(
134 `${path}[${idx}]`,
135 "[[missing ident]]",
136 /** @type {Exclude<RuleSetUseItem, RuleSetUseFunction>} */
137 (item)
138 )
139 );
140 }
141 return [
142 useToEffectRaw(
143 path,
144 "[[missing ident]]",
145 /** @type {Exclude<RuleSetUseItem, RuleSetUseFunction>} */
146 (items)
147 )
148 ];
149 };
150
151 /**
152 * Returns effects.
153 * @param {string} path current path
154 * @param {RuleSetUse} items user provided use value
155 * @returns {(Effect | ((effectData: EffectData) => Effect[]))[]} effects
156 */
157 const useToEffects = (path, items) => {
158 if (Array.isArray(items)) {
159 return items.filter(Boolean).map((item, idx) => {
160 const subPath = `${path}[${idx}]`;
161 return useToEffect(
162 subPath,
163 subPath,
164 /** @type {RuleSetUseItem} */
165 (item)
166 );
167 });
168 }
169 return [
170 useToEffect(path, path, /** @type {RuleSetUseItem} */ (items))
171 ];
172 };
173
174 if (typeof use === "function") {
175 result.effects.push((data) =>
176 useToEffectsWithoutIdent(`${path}.use`, use(data))
177 );
178 } else {
179 for (const effect of useToEffects(`${path}.use`, use)) {
180 result.effects.push(effect);
181 }
182 }
183 }
184
185 if (unhandledProperties.has("loader")) {
186 unhandledProperties.delete("loader");
187 unhandledProperties.delete("options");
188 unhandledProperties.delete("enforce");
189
190 const loader = /** @type {RuleSetLoader} */ (rule.loader);
191 const options = rule.options;
192 const enforce = rule.enforce;
193
194 if (loader.includes("!")) {
195 throw ruleSetCompiler.error(
196 `${path}.loader`,
197 loader,
198 "Exclamation mark separated loader lists has been removed in favor of the 'use' property with arrays"
199 );
200 }
201
202 if (loader.includes("?")) {
203 throw ruleSetCompiler.error(
204 `${path}.loader`,
205 loader,
206 "Query arguments on 'loader' has been removed in favor of the 'options' property"
207 );
208 }
209
210 if (typeof options === "string") {
211 util.deprecate(
212 () => {},
213 `Using a string as loader options is deprecated (${path}.options)`,
214 "DEP_WEBPACK_RULE_LOADER_OPTIONS_STRING"
215 )();
216 }
217
218 const ident =
219 options && typeof options === "object" ? path : undefined;
220
221 if (ident) {
222 references.set(
223 ident,
224 /** @type {RuleSetLoaderOptions} */
225 (options)
226 );
227 }
228
229 result.effects.push({
230 type: enforce ? `use-${enforce}` : "use",
231 value: {
232 loader,
233 options,
234 ident
235 }
236 });
237 }
238 }
239 );
240 }
241}
242
243module.exports = UseEffectRulePlugin;
Note: See TracBrowser for help on using the repository browser.