feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@@ -6,15 +6,20 @@
"use strict";
const { pathToFileURL } = require("url");
const CommentCompilationWarning = require("../CommentCompilationWarning");
const {
JAVASCRIPT_MODULE_TYPE_AUTO,
JAVASCRIPT_MODULE_TYPE_ESM
} = require("../ModuleTypeConstants");
const RuntimeGlobals = require("../RuntimeGlobals");
const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
const BasicEvaluatedExpression = require("../javascript/BasicEvaluatedExpression");
const { approve } = require("../javascript/JavascriptParserHelpers");
const InnerGraph = require("../optimize/InnerGraph");
const ConstDependency = require("./ConstDependency");
const URLDependency = require("./URLDependency");
/** @typedef {import("estree").MemberExpression} MemberExpression */
/** @typedef {import("estree").NewExpression} NewExpressionNode */
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
/** @typedef {import("../Compiler")} Compiler */
@@ -44,8 +49,26 @@ class URLPlugin {
* @param {NormalModule} module module
* @returns {URL} file url
*/
const getUrl = module => {
return pathToFileURL(module.resource);
const getUrl = module => pathToFileURL(module.resource);
/**
* @param {Parser} parser parser parser
* @param {MemberExpression} arg arg
* @returns {boolean} true when it is `meta.url`, otherwise false
*/
const isMetaUrl = (parser, arg) => {
const chain = parser.extractMemberExpressionChain(arg);
if (
chain.members.length !== 1 ||
chain.object.type !== "MetaProperty" ||
chain.object.meta.name !== "import" ||
chain.object.property.name !== "meta" ||
chain.members[0] !== "url"
)
return false;
return true;
};
/**
@@ -72,16 +95,7 @@ class URLPlugin {
)
return;
const chain = parser.extractMemberExpressionChain(arg2);
if (
chain.members.length !== 1 ||
chain.object.type !== "MetaProperty" ||
chain.object.meta.name !== "import" ||
chain.object.property.name !== "meta" ||
chain.members[0] !== "url"
)
return;
if (!isMetaUrl(parser, arg2)) return;
return parser.evaluateExpression(arg1).asString();
};
@@ -100,6 +114,52 @@ class URLPlugin {
});
parser.hooks.new.for("URL").tap(PLUGIN_NAME, _expr => {
const expr = /** @type {NewExpressionNode} */ (_expr);
const { options: importOptions, errors: commentErrors } =
parser.parseCommentOptions(/** @type {Range} */ (expr.range));
if (commentErrors) {
for (const e of commentErrors) {
const { comment } = e;
parser.state.module.addWarning(
new CommentCompilationWarning(
`Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
/** @type {DependencyLocation} */ (comment.loc)
)
);
}
}
if (importOptions && importOptions.webpackIgnore !== undefined) {
if (typeof importOptions.webpackIgnore !== "boolean") {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
/** @type {DependencyLocation} */ (expr.loc)
)
);
return;
} else if (importOptions.webpackIgnore) {
if (expr.arguments.length !== 2) return;
const [, arg2] = expr.arguments;
if (
arg2.type !== "MemberExpression" ||
!isMetaUrl(parser, arg2)
)
return;
const dep = new ConstDependency(
RuntimeGlobals.baseURI,
/** @type {Range} */ (arg2.range),
[RuntimeGlobals.baseURI]
);
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
parser.state.module.addPresentationalDependency(dep);
return true;
}
}
const request = getUrlRequest(expr);