Skip to content

Commit

Permalink
fix: parentURL normalizing
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 11, 2024
1 parent 6b1833e commit 6fbe688
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Importing a TypeScript module with `importx`:

<!-- TABLE_START -->

> Generated with version `v0.0.3` at 2024-05-11T11:50:32.206Z
> Generated with version `v0.1.0` at 2024-05-11T12:28:48.832Z
| | native | tsx | jiti | bundle-require |
| ------- | --- | --- | --- | --- |
Expand Down
10 changes: 7 additions & 3 deletions src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function importx<T = any>(specifier: string, options: string | URL

const {
loaderOptions = {},
parentURL,
parentURL: inputUserUrl,
cache = null,
ignoreImportxWarning = false,
...otherOptions
Expand All @@ -45,12 +45,16 @@ export async function importx<T = any>(specifier: string, options: string | URL
if (loader === 'auto')
loader = await detectLoader(cache, isTypeScriptFile(specifier))

const parentPath = fileURLToPath(parentURL)
const parentPath = (typeof inputUserUrl === 'string' && !inputUserUrl.includes('://'))
? inputUserUrl
: fileURLToPath(inputUserUrl)
const parentURL = pathToFileURL(parentPath)

const info: ImportxModuleInfo = {
loader,
specifier,
parentURL: parentPath,
parentURL,
parentPath,
timestampInit: Date.now(),
timestampLoad: -1,
}
Expand Down
8 changes: 6 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ export interface ImportxModuleInfo {
*/
specifier: string
/**
* User passed parent URL.
* Parent URL, normalized to file URL.
*/
parentURL: string
parentURL: URL
/**
* Parent path, normalized to file path.
*/
parentPath: string
/**
* Timestamp when import is initialized.
*/
Expand Down
14 changes: 14 additions & 0 deletions test/get-module-info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ it('should get module info', async () => {
expect(info?.dependencies).not.toBeDefined()
})

it('should get module info with parent filepath', async () => {
const mod = await importx('./fixtures/basic/foo.mts', __filename)
const info = getModuleInfo(mod)

expect(mod.default).toBe(MOD_EXPORT)

expect(info).toBeDefined()
expect(info).toMatchObject({
loader: 'native', // because Vitest supports importing TypeScript files
specifier: './fixtures/basic/foo.mts',
})
expect(info?.dependencies).not.toBeDefined()
})

it('should get dependencies with tsx', async () => {
const mod = await importx('./fixtures/basic/foo.mts', {
loader: 'tsx',
Expand Down

0 comments on commit 6fbe688

Please sign in to comment.