-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for named exports (#4606)
fix #4562 --------- Co-authored-by: Christopher Radek <[email protected]>
- Loading branch information
1 parent
b3dbbfd
commit 77edf34
Showing
22 changed files
with
1,292 additions
and
113 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
.chronus/changes/resolve-module-exports-2024-9-4-18-32-9.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking | ||
changeKind: feature | ||
packages: | ||
- "@typespec/compiler" | ||
--- | ||
|
||
Add support for node `exports` field. Specific typespec exports can be provided with the `typespec` field | ||
|
||
```json | ||
"exports": { | ||
".": { | ||
"typespec": "./lib/main.tsp", | ||
}, | ||
"./named": { | ||
"typespec": "./lib/named.tsp", | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking | ||
changeKind: fix | ||
packages: | ||
- "@typespec/playground" | ||
--- | ||
|
||
Do not treat path as relative internally |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/compiler/src/module-resolver/esm/resolve-package-exports.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Exports } from "../../types/package-json.js"; | ||
import { resolvePackageImportsExports } from "../esm/resolve-package-imports-exports.js"; | ||
import { resolvePackageTarget } from "../esm/resolve-package-target.js"; | ||
import { | ||
EsmResolutionContext, | ||
InvalidModuleSpecifierError, | ||
NoMatchingConditionsError, | ||
} from "./utils.js"; | ||
|
||
/** Implementation of PACKAGE_EXPORTS_RESOLVE https://github.com/nodejs/node/blob/main/doc/api/esm.md */ | ||
export async function resolvePackageExports( | ||
context: EsmResolutionContext, | ||
subpath: string, | ||
exports: Exports, | ||
): Promise<string | null | undefined> { | ||
if (exports === null) return undefined; | ||
|
||
if (subpath === ".") { | ||
let mainExport: Exports | undefined; | ||
if (typeof exports === "string" || Array.isArray(exports) || isConditions(exports)) { | ||
mainExport = exports; | ||
} else if (exports["."]) { | ||
mainExport = exports["."]; | ||
} | ||
|
||
if (mainExport) { | ||
if (context.ignoreDefaultCondition && typeof mainExport === "string") { | ||
return undefined; | ||
} | ||
const resolved = await resolvePackageTarget(context, { | ||
target: mainExport, | ||
isImports: false, | ||
}); | ||
|
||
// If resolved is not null or undefined, return resolved. | ||
if (resolved) { | ||
return resolved; | ||
} else { | ||
throw new NoMatchingConditionsError(context); | ||
} | ||
} | ||
} else if (isMappings(exports)) { | ||
// Let resolved be the result of PACKAGE_IMPORTS_EXPORTS_RESOLVE | ||
const resolvedMatch = await resolvePackageImportsExports(context, { | ||
matchKey: subpath, | ||
matchObj: exports, | ||
isImports: false, | ||
}); | ||
|
||
// If resolved is not null or undefined, return resolved. | ||
if (resolvedMatch) { | ||
return resolvedMatch; | ||
} | ||
} | ||
|
||
// 4. Throw a Package Path Not Exported error. | ||
throw new InvalidModuleSpecifierError(context); | ||
} | ||
|
||
/** Conditions is an export object where all keys are conditions(not a path starting with .). E.g. import, default, types, etc. */ | ||
function isConditions(item: Exports) { | ||
return typeof item === "object" && Object.keys(item).every((k) => !k.startsWith(".")); | ||
} | ||
/** | ||
* Mappings is an export object where all keys start with '. | ||
*/ | ||
export function isMappings(exports: Exports): exports is Record<string, Exports> { | ||
return typeof exports === "object" && !isConditions(exports); | ||
} |
Oops, something went wrong.