| 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("../errors/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 | const PLUGIN_NAME = "RequireIncludeDependencyParserPlugin";
|
|---|
| 21 |
|
|---|
| 22 | module.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 |
|
|---|
| 85 | class 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 |
|
|---|
| 99 | makeSerializable(
|
|---|
| 100 | RequireIncludeDeprecationWarning,
|
|---|
| 101 | "webpack/lib/dependencies/RequireIncludeDependencyParserPlugin",
|
|---|
| 102 | "RequireIncludeDeprecationWarning"
|
|---|
| 103 | );
|
|---|