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

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

Fix frontend appearance

  • Property mode set to 100644
File size: 2.9 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 WebpackError = require("../errors/WebpackError");
9const {
10 evaluateToString,
11 toConstantDependency
12} = require("../javascript/JavascriptParserHelpers");
13const makeSerializable = require("../util/makeSerializable");
14const RequireIncludeDependency = require("./RequireIncludeDependency");
15
16/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
17/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
18/** @typedef {import("../javascript/JavascriptParser").Range} Range */
19
20const PLUGIN_NAME = "RequireIncludeDependencyParserPlugin";
21
22module.exports = class RequireIncludeDependencyParserPlugin {
23 /**
24 * Creates an instance of RequireIncludeDependencyParserPlugin.
25 * @param {boolean} warn true: warn about deprecation, false: don't warn
26 */
27 constructor(warn) {
28 this.warn = warn;
29 }
30
31 /**
32 * Applies the plugin by registering its hooks on the compiler.
33 * @param {JavascriptParser} parser the parser
34 * @returns {void}
35 */
36 apply(parser) {
37 const { warn } = this;
38 parser.hooks.call.for("require.include").tap(PLUGIN_NAME, (expr) => {
39 if (expr.arguments.length !== 1) return;
40 const param = parser.evaluateExpression(expr.arguments[0]);
41 if (!param.isString()) return;
42
43 if (warn) {
44 parser.state.module.addWarning(
45 new RequireIncludeDeprecationWarning(
46 /** @type {DependencyLocation} */
47 (expr.loc)
48 )
49 );
50 }
51
52 const dep = new RequireIncludeDependency(
53 /** @type {string} */ (param.string),
54 /** @type {Range} */ (expr.range)
55 );
56 dep.loc = /** @type {DependencyLocation} */ (expr.loc);
57 parser.state.current.addDependency(dep);
58 return true;
59 });
60 parser.hooks.evaluateTypeof
61 .for("require.include")
62 .tap(PLUGIN_NAME, (expr) => {
63 if (warn) {
64 parser.state.module.addWarning(
65 new RequireIncludeDeprecationWarning(
66 /** @type {DependencyLocation} */ (expr.loc)
67 )
68 );
69 }
70 return evaluateToString("function")(expr);
71 });
72 parser.hooks.typeof.for("require.include").tap(PLUGIN_NAME, (expr) => {
73 if (warn) {
74 parser.state.module.addWarning(
75 new RequireIncludeDeprecationWarning(
76 /** @type {DependencyLocation} */ (expr.loc)
77 )
78 );
79 }
80 return toConstantDependency(parser, JSON.stringify("function"))(expr);
81 });
82 }
83};
84
85class RequireIncludeDeprecationWarning extends WebpackError {
86 /**
87 * Creates an instance of RequireIncludeDeprecationWarning.
88 * @param {DependencyLocation} loc location
89 */
90 constructor(loc) {
91 super("require.include() is deprecated and will be removed soon.");
92
93 this.name = "RequireIncludeDeprecationWarning";
94
95 this.loc = loc;
96 }
97}
98
99makeSerializable(
100 RequireIncludeDeprecationWarning,
101 "webpack/lib/dependencies/RequireIncludeDependencyParserPlugin",
102 "RequireIncludeDeprecationWarning"
103);
Note: See TracBrowser for help on using the repository browser.