-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: isolated cts import in Node v18 (#61)
- Loading branch information
1 parent
a74aa58
commit 042be03
Showing
8 changed files
with
120 additions
and
69 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,33 @@ | ||
import { pathToFileURL } from 'node:url'; | ||
|
||
const resolveSpecifier = ( | ||
specifier: string, | ||
fromFile: string, | ||
namespace: string, | ||
) => { | ||
const base = ( | ||
fromFile.startsWith('file://') | ||
? fromFile | ||
: pathToFileURL(fromFile) | ||
); | ||
const resolvedUrl = new URL(specifier, base); | ||
|
||
/** | ||
* A namespace query is added so we get our own module cache | ||
* | ||
* I considered using an import attribute for this, but it doesn't seem to | ||
* make the request unique so it gets cached. | ||
*/ | ||
resolvedUrl.searchParams.set('tsx-namespace', namespace); | ||
|
||
return resolvedUrl.toString(); | ||
}; | ||
import type { TsxRequest } from '../types.js'; | ||
import { fileUrlPrefix } from '../../utils/path-utils.js'; | ||
|
||
export type ScopedImport = ( | ||
specifier: string, | ||
parentURL: string, | ||
parent: string, | ||
) => Promise<any>; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
|
||
export const createScopedImport = ( | ||
namespace: string, | ||
): ScopedImport => ( | ||
specifier, | ||
parentURL, | ||
parent, | ||
) => { | ||
if (!parentURL) { | ||
if (!parent) { | ||
throw new Error('The current file path (import.meta.url) must be provided in the second argument of tsImport()'); | ||
} | ||
|
||
const parentURL = ( | ||
parent.startsWith(fileUrlPrefix) | ||
? parent | ||
: pathToFileURL(parent).toString() | ||
); | ||
|
||
return import( | ||
resolveSpecifier(specifier, parentURL, namespace) | ||
`tsx://${JSON.stringify({ | ||
specifier, | ||
parentURL, | ||
namespace, | ||
} satisfies TsxRequest)}` | ||
); | ||
}; |
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
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