[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const WebpackError = require("../WebpackError");
|
---|
| 9 | const {
|
---|
| 10 | evaluateToString,
|
---|
| 11 | toConstantDependency
|
---|
| 12 | } = require("../javascript/JavascriptParserHelpers");
|
---|
| 13 | const makeSerializable = require("../util/makeSerializable");
|
---|
| 14 | const RequireIncludeDependency = require("./RequireIncludeDependency");
|
---|
| 15 |
|
---|
| 16 | /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
|
---|
| 17 | /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
---|
| 18 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
| 19 |
|
---|
| 20 | module.exports = class RequireIncludeDependencyParserPlugin {
|
---|
| 21 | /**
|
---|
| 22 | * @param {boolean} warn true: warn about deprecation, false: don't warn
|
---|
| 23 | */
|
---|
| 24 | constructor(warn) {
|
---|
| 25 | this.warn = warn;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * @param {JavascriptParser} parser the parser
|
---|
| 30 | * @returns {void}
|
---|
| 31 | */
|
---|
| 32 | apply(parser) {
|
---|
| 33 | const { warn } = this;
|
---|
| 34 | parser.hooks.call
|
---|
| 35 | .for("require.include")
|
---|
| 36 | .tap("RequireIncludeDependencyParserPlugin", expr => {
|
---|
| 37 | if (expr.arguments.length !== 1) return;
|
---|
| 38 | const param = parser.evaluateExpression(expr.arguments[0]);
|
---|
| 39 | if (!param.isString()) return;
|
---|
| 40 |
|
---|
| 41 | if (warn) {
|
---|
| 42 | parser.state.module.addWarning(
|
---|
| 43 | new RequireIncludeDeprecationWarning(
|
---|
| 44 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
| 45 | )
|
---|
| 46 | );
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | const dep = new RequireIncludeDependency(
|
---|
| 50 | /** @type {string} */ (param.string),
|
---|
| 51 | /** @type {Range} */ (expr.range)
|
---|
| 52 | );
|
---|
| 53 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
| 54 | parser.state.current.addDependency(dep);
|
---|
| 55 | return true;
|
---|
| 56 | });
|
---|
| 57 | parser.hooks.evaluateTypeof
|
---|
| 58 | .for("require.include")
|
---|
| 59 | .tap("RequireIncludePlugin", expr => {
|
---|
| 60 | if (warn) {
|
---|
| 61 | parser.state.module.addWarning(
|
---|
| 62 | new RequireIncludeDeprecationWarning(
|
---|
| 63 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
| 64 | )
|
---|
| 65 | );
|
---|
| 66 | }
|
---|
| 67 | return evaluateToString("function")(expr);
|
---|
| 68 | });
|
---|
| 69 | parser.hooks.typeof
|
---|
| 70 | .for("require.include")
|
---|
| 71 | .tap("RequireIncludePlugin", expr => {
|
---|
| 72 | if (warn) {
|
---|
| 73 | parser.state.module.addWarning(
|
---|
| 74 | new RequireIncludeDeprecationWarning(
|
---|
| 75 | /** @type {DependencyLocation} */ (expr.loc)
|
---|
| 76 | )
|
---|
| 77 | );
|
---|
| 78 | }
|
---|
| 79 | return toConstantDependency(parser, JSON.stringify("function"))(expr);
|
---|
| 80 | });
|
---|
| 81 | }
|
---|
| 82 | };
|
---|
| 83 |
|
---|
| 84 | class RequireIncludeDeprecationWarning extends WebpackError {
|
---|
| 85 | /**
|
---|
| 86 | * @param {DependencyLocation} loc location
|
---|
| 87 | */
|
---|
| 88 | constructor(loc) {
|
---|
| 89 | super("require.include() is deprecated and will be removed soon.");
|
---|
| 90 |
|
---|
| 91 | this.name = "RequireIncludeDeprecationWarning";
|
---|
| 92 |
|
---|
| 93 | this.loc = loc;
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | makeSerializable(
|
---|
| 98 | RequireIncludeDeprecationWarning,
|
---|
| 99 | "webpack/lib/dependencies/RequireIncludeDependencyParserPlugin",
|
---|
| 100 | "RequireIncludeDeprecationWarning"
|
---|
| 101 | );
|
---|