Skip to content

Commit

Permalink
fix: node 20 目录解析
Browse files Browse the repository at this point in the history
  • Loading branch information
Eavid committed Sep 10, 2023
1 parent 0075ff3 commit 561af7a
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 86 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"env": {
"SAME_PARSER": "${input:SAME_PARSER}"
},
"autoAttachChildProcesses": true,
"sourceMaps": true,
"program": "${relativeFile}"
}
Expand Down
18 changes: 0 additions & 18 deletions src/internal/common_resolve.ts

This file was deleted.

33 changes: 30 additions & 3 deletions src/internal/errors/error.mts → src/internal/errors/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class ERR_INVALID_PACKAGE_TARGET extends CodeError {
}

export class ERR_INVALID_MODULE_SPECIFIER extends CodeError {
constructor(request: string, reason: string, base?: string) {
constructor(request: string | URL, reason: string, base?: string | null) {
super(
`Invalid module "${request}" ${reason}${base ? ` imported from ${base}` : ""}`,
ERR_INVALID_MODULE_SPECIFIER.name
Expand All @@ -47,7 +47,7 @@ export class ERR_INVALID_MODULE_SPECIFIER extends CodeError {
}

export class ERR_PACKAGE_IMPORT_NOT_DEFINED extends CodeError {
constructor(specifier: string, packagePath: string, base: string) {
constructor(specifier: string, packagePath: string | URL | undefined, base: string) {
super(
`Package import specifier "${specifier}" is not defined${
packagePath ? ` in package ${packagePath}package.json` : ""
Expand All @@ -56,7 +56,19 @@ export class ERR_PACKAGE_IMPORT_NOT_DEFINED extends CodeError {
);
}
}
export class ERR_PACKAGE_PATH_NOT_EXPORTED extends Error {
constructor(pkgPath: string, subpath: string, base: undefined | null | string | URL = undefined) {
let msg: string;
if (subpath === ".")
msg = `No "exports" main defined in ${pkgPath}package.json${base ? ` imported from ${base}` : ""}`;
else
msg = `Package subpath '${subpath}' is not defined by "exports" in ${pkgPath}package.json${
base ? ` imported from ${base}` : ""
}`;

super(msg);
}
}
export function invalidPackageTarget(subpath: string, target: string, packageJSONUrl: URL, internal: any, base: any) {
if (typeof target === "object" && target !== null) {
target = JSON.stringify(target, null, "");
Expand All @@ -71,7 +83,7 @@ export function invalidPackageTarget(subpath: string, target: string, packageJSO
base && fileURLToPath(base)
);
}
export function importNotDefined(specifier: string, packageJSONUrl: URL, base: string) {
export function importNotDefined(specifier: string, packageJSONUrl: URL | undefined, base: string | URL) {
return new ERR_PACKAGE_IMPORT_NOT_DEFINED(
specifier,
packageJSONUrl && fileURLToPath(new URL(".", packageJSONUrl)),
Expand All @@ -91,3 +103,18 @@ export function throwInvalidSubpath(
}" resolution of ${fileURLToPath(packageJSONUrl)}`;
throw new ERR_INVALID_MODULE_SPECIFIER(request, reason, base && fileURLToPath(base));
}

export function exportsNotFound(subpath: string, packageJSONUrl: URL, base?: string | URL | null) {
return new ERR_PACKAGE_PATH_NOT_EXPORTED(
fileURLToPath(new URL(".", packageJSONUrl)),
subpath,
base && fileURLToPath(base)
);
}
export function createEsmNotFoundErr(request: string, path: string) {
// eslint-disable-next-line no-restricted-syntax
const err = new CodeError(`Cannot find module '${request}'`, "MODULE_NOT_FOUND");
if (path) (err as any).path = path;
// TODO(BridgeAR): Add the requireStack as well.
return err;
}
4 changes: 2 additions & 2 deletions src/internal/esm_loader.mts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ async function tryResolveDir(
parentDir: string,
isCommonJs?: boolean
): Promise<NodeLoader.ResolveFxReturn | undefined> {
const pkgConfig = ExtraModule._readPackage(path);
if (pkgConfig) {
const pkgConfig = ExtraModule._readPackage(path); //node 20 即使不存在也会返回
if (pkgConfig && pkgConfig.exists !== false) {
return tryResolvePkg(nextResolve, path, parentDir);
} else if (isCommonJs) {
const absPath = await tryResolveFile(Path.resolve(path, "index"), [".ts", ".js", ".json"]);
Expand Down
Loading

0 comments on commit 561af7a

Please sign in to comment.