|
| 1 | +/* |
| 2 | + MIT License http://www.opensource.org/licenses/mit-license.php |
| 3 | + Author Ivan Kopeykin @vankop |
| 4 | +*/ |
| 5 | + |
| 6 | +"use strict"; |
| 7 | + |
| 8 | +const { |
| 9 | + evaluateToIdentifier |
| 10 | +} = require("../javascript/JavascriptParserHelpers"); |
| 11 | +const ImportMetaContextDependency = require("./ImportMetaContextDependency"); |
| 12 | + |
| 13 | +/** @typedef {import("estree").Expression} ExpressionNode */ |
| 14 | +/** @typedef {import("estree").ObjectExpression} ObjectExpressionNode */ |
| 15 | +/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */ |
| 16 | + |
| 17 | +/** |
| 18 | + * @param {JavascriptParser} parser parser |
| 19 | + * @param {ObjectExpressionNode} optionsNode node |
| 20 | + * @returns {{mode: string, recursive: boolean, regExp: RegExp}} options |
| 21 | + */ |
| 22 | +function getOptions(parser, optionsNode) { |
| 23 | + let regExp = /^\.\/.*$/; |
| 24 | + let recursive = true; |
| 25 | + let mode = "sync"; |
| 26 | + if (optionsNode) { |
| 27 | + for (const prop of optionsNode.properties) { |
| 28 | + if (prop.type !== "Property" || prop.key.type !== "Identifier") return; |
| 29 | + switch (prop.key.name) { |
| 30 | + case "regExp": { |
| 31 | + const regExpExpr = parser.evaluateExpression( |
| 32 | + /** @type {ExpressionNode} */ (prop.value) |
| 33 | + ); |
| 34 | + if (!regExpExpr.isRegExp()) return; |
| 35 | + regExp = regExpExpr.regExp; |
| 36 | + break; |
| 37 | + } |
| 38 | + case "mode": { |
| 39 | + const modeExpr = parser.evaluateExpression( |
| 40 | + /** @type {ExpressionNode} */ (prop.value) |
| 41 | + ); |
| 42 | + if (!modeExpr.isString()) return; |
| 43 | + mode = modeExpr.string; |
| 44 | + break; |
| 45 | + } |
| 46 | + case "recursive": { |
| 47 | + const recursiveExpr = parser.evaluateExpression( |
| 48 | + /** @type {ExpressionNode} */ (prop.value) |
| 49 | + ); |
| 50 | + if (!recursiveExpr.isBoolean()) return; |
| 51 | + recursive = recursiveExpr.bool; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return { recursive, regExp, mode }; |
| 58 | +} |
| 59 | + |
| 60 | +module.exports = class ImportMetaContextDependencyParserPlugin { |
| 61 | + apply(parser) { |
| 62 | + parser.hooks.evaluateIdentifier |
| 63 | + .for("import.meta.webpackContext") |
| 64 | + .tap("HotModuleReplacementPlugin", expr => { |
| 65 | + return evaluateToIdentifier( |
| 66 | + "import.meta.webpackContext", |
| 67 | + "import.meta", |
| 68 | + () => ["webpackContext"], |
| 69 | + true |
| 70 | + )(expr); |
| 71 | + }); |
| 72 | + parser.hooks.call |
| 73 | + .for("import.meta.webpackContext") |
| 74 | + .tap("ImportMetaContextDependencyParserPlugin", expr => { |
| 75 | + if (expr.arguments.length < 1 || expr.arguments.length > 2) return; |
| 76 | + const [directoryNode, optionsNode] = expr.arguments; |
| 77 | + if (optionsNode && optionsNode.type !== "ObjectExpression") return; |
| 78 | + const requestExpr = parser.evaluateExpression(directoryNode); |
| 79 | + if (!requestExpr.isString()) return; |
| 80 | + const request = requestExpr.string; |
| 81 | + const options = getOptions(parser, optionsNode); |
| 82 | + if (!options) return; |
| 83 | + |
| 84 | + const dep = new ImportMetaContextDependency( |
| 85 | + { |
| 86 | + request, |
| 87 | + ...options, |
| 88 | + category: "esm" |
| 89 | + }, |
| 90 | + expr.range |
| 91 | + ); |
| 92 | + dep.loc = expr.loc; |
| 93 | + dep.optional = !!parser.scope.inTry; |
| 94 | + parser.state.current.addDependency(dep); |
| 95 | + return true; |
| 96 | + }); |
| 97 | + } |
| 98 | +}; |
0 commit comments