source: frontend/node_modules/webpack/lib/WebpackIsIncludedPlugin.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: 2.7 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Ivan Kopeykin @vankop
4*/
5
6"use strict";
7
8const {
9 JAVASCRIPT_MODULE_TYPE_AUTO,
10 JAVASCRIPT_MODULE_TYPE_DYNAMIC,
11 JAVASCRIPT_MODULE_TYPE_ESM
12} = require("./ModuleTypeConstants");
13const WebpackIsIncludedDependency = require("./dependencies/WebpackIsIncludedDependency");
14const IgnoreErrorModuleFactory = require("./errors/IgnoreErrorModuleFactory");
15const {
16 toConstantDependency
17} = require("./javascript/JavascriptParserHelpers");
18
19/** @typedef {import("./Compiler")} Compiler */
20/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
21/** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
22/** @typedef {import("./javascript/JavascriptParser").Range} Range */
23
24const PLUGIN_NAME = "WebpackIsIncludedPlugin";
25
26class WebpackIsIncludedPlugin {
27 /**
28 * Applies the plugin by registering its hooks on the compiler.
29 * @param {Compiler} compiler the compiler instance
30 * @returns {void}
31 */
32 apply(compiler) {
33 compiler.hooks.compilation.tap(
34 PLUGIN_NAME,
35 (compilation, { normalModuleFactory }) => {
36 compilation.dependencyFactories.set(
37 WebpackIsIncludedDependency,
38 new IgnoreErrorModuleFactory(normalModuleFactory)
39 );
40 compilation.dependencyTemplates.set(
41 WebpackIsIncludedDependency,
42 new WebpackIsIncludedDependency.Template()
43 );
44
45 /**
46 * Handles the hook callback for this code path.
47 * @param {JavascriptParser} parser the parser
48 * @returns {void}
49 */
50 const handler = (parser) => {
51 parser.hooks.call
52 .for("__webpack_is_included__")
53 .tap(PLUGIN_NAME, (expr) => {
54 if (
55 expr.type !== "CallExpression" ||
56 expr.arguments.length !== 1 ||
57 expr.arguments[0].type === "SpreadElement"
58 ) {
59 return;
60 }
61
62 const request = parser.evaluateExpression(expr.arguments[0]);
63
64 if (!request.isString()) return;
65
66 const dep = new WebpackIsIncludedDependency(
67 /** @type {string} */ (request.string),
68 /** @type {Range} */ (expr.range)
69 );
70 dep.loc = /** @type {DependencyLocation} */ (expr.loc);
71 parser.state.module.addDependency(dep);
72 return true;
73 });
74 parser.hooks.typeof
75 .for("__webpack_is_included__")
76 .tap(
77 PLUGIN_NAME,
78 toConstantDependency(parser, JSON.stringify("function"))
79 );
80 };
81 normalModuleFactory.hooks.parser
82 .for(JAVASCRIPT_MODULE_TYPE_AUTO)
83 .tap(PLUGIN_NAME, handler);
84 normalModuleFactory.hooks.parser
85 .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
86 .tap(PLUGIN_NAME, handler);
87 normalModuleFactory.hooks.parser
88 .for(JAVASCRIPT_MODULE_TYPE_ESM)
89 .tap(PLUGIN_NAME, handler);
90 }
91 );
92 }
93}
94
95module.exports = WebpackIsIncludedPlugin;
Note: See TracBrowser for help on using the repository browser.