[6a3a178] | 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 | module.exports = class RequireIncludeDependencyParserPlugin {
|
---|
| 17 | constructor(warn) {
|
---|
| 18 | this.warn = warn;
|
---|
| 19 | }
|
---|
| 20 | apply(parser) {
|
---|
| 21 | const { warn } = this;
|
---|
| 22 | parser.hooks.call
|
---|
| 23 | .for("require.include")
|
---|
| 24 | .tap("RequireIncludeDependencyParserPlugin", expr => {
|
---|
| 25 | if (expr.arguments.length !== 1) return;
|
---|
| 26 | const param = parser.evaluateExpression(expr.arguments[0]);
|
---|
| 27 | if (!param.isString()) return;
|
---|
| 28 |
|
---|
| 29 | if (warn) {
|
---|
| 30 | parser.state.module.addWarning(
|
---|
| 31 | new RequireIncludeDeprecationWarning(expr.loc)
|
---|
| 32 | );
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | const dep = new RequireIncludeDependency(param.string, expr.range);
|
---|
| 36 | dep.loc = expr.loc;
|
---|
| 37 | parser.state.current.addDependency(dep);
|
---|
| 38 | return true;
|
---|
| 39 | });
|
---|
| 40 | parser.hooks.evaluateTypeof
|
---|
| 41 | .for("require.include")
|
---|
| 42 | .tap("RequireIncludePlugin", expr => {
|
---|
| 43 | if (warn) {
|
---|
| 44 | parser.state.module.addWarning(
|
---|
| 45 | new RequireIncludeDeprecationWarning(expr.loc)
|
---|
| 46 | );
|
---|
| 47 | }
|
---|
| 48 | return evaluateToString("function")(expr);
|
---|
| 49 | });
|
---|
| 50 | parser.hooks.typeof
|
---|
| 51 | .for("require.include")
|
---|
| 52 | .tap("RequireIncludePlugin", expr => {
|
---|
| 53 | if (warn) {
|
---|
| 54 | parser.state.module.addWarning(
|
---|
| 55 | new RequireIncludeDeprecationWarning(expr.loc)
|
---|
| 56 | );
|
---|
| 57 | }
|
---|
| 58 | return toConstantDependency(parser, JSON.stringify("function"))(expr);
|
---|
| 59 | });
|
---|
| 60 | }
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 | class RequireIncludeDeprecationWarning extends WebpackError {
|
---|
| 64 | constructor(loc) {
|
---|
| 65 | super("require.include() is deprecated and will be removed soon.");
|
---|
| 66 |
|
---|
| 67 | this.name = "RequireIncludeDeprecationWarning";
|
---|
| 68 |
|
---|
| 69 | this.loc = loc;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | makeSerializable(
|
---|
| 74 | RequireIncludeDeprecationWarning,
|
---|
| 75 | "webpack/lib/dependencies/RequireIncludeDependencyParserPlugin",
|
---|
| 76 | "RequireIncludeDeprecationWarning"
|
---|
| 77 | );
|
---|