Skip to content

Commit

Permalink
fix(register): Fix to resolve adjacent file path (#711)
Browse files Browse the repository at this point in the history
* fix(register): fix to resolve adjacent file path

* fix(integrate-module): Add new test case
  • Loading branch information
chloe463 committed Apr 24, 2023
1 parent 07eec33 commit 8dbea4f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/integrate-module/src/foo.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo() {
return 'foo'
}
16 changes: 16 additions & 0 deletions packages/integrate-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ import test from 'node:test'

import { supportedExtensions } from 'file-type'

import { foo } from './foo.mjs'
import { bar } from './subdirectory/bar.mjs'
import { baz } from './subdirectory/index.mjs'

await test('file-type should work', () => {
assert.ok(supportedExtensions.has('jpg'))
})

await test('resolve adjacent file path', () => {
assert.equal(foo(), 'foo')
})

await test('resolve nested file path', () => {
assert.equal(bar(), 'bar')
})

await test('resolve nested entry point', () => {
assert.equal(baz(), 'baz')
})
3 changes: 3 additions & 0 deletions packages/integrate-module/src/subdirectory/bar.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function bar() {
return 'bar'
}
3 changes: 3 additions & 0 deletions packages/integrate-module/src/subdirectory/baz.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function baz() {
return 'baz'
}
4 changes: 4 additions & 0 deletions packages/integrate-module/src/subdirectory/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { baz } from './baz.mjs'
export function module() {
return 'module'
}
6 changes: 3 additions & 3 deletions packages/register/esm.mts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const DEFAULT_EXTENSIONS = ['.ts', '.tsx', '.mts', '.cts']
const TRANSFORM_MAP = new Map<string, string>()

async function checkRequestURL(parentURL: string, requestURL: string) {
const { ext } = parse(requestURL)
const { dir, name, ext } = parse(requestURL)
const parentDir = join(parentURL.startsWith('file://') ? fileURLToPath(parentURL) : parentURL, '..')
if (ext && ext !== '.js' && ext !== '.mjs') {
try {
Expand All @@ -33,7 +33,7 @@ async function checkRequestURL(parentURL: string, requestURL: string) {
} else {
for (const ext of DEFAULT_EXTENSIONS) {
try {
const url = join(parentDir, requestURL + ext)
const url = join(parentDir, dir, `${name}${ext}`)
await fs.access(url, FSConstants.R_OK)
return url
} catch (e) {
Expand All @@ -59,7 +59,7 @@ export const resolve: ResolveFn = async (specifier, context, nextResolve) => {
if (parentURL && TRANSFORM_MAP.has(parentURL) && specifier.startsWith('.')) {
const existedURL = await checkRequestURL(parentURL, specifier)
if (existedURL) {
const url = `${existedURL}.mjs`
const { href: url } = pathToFileURL(existedURL)
TRANSFORM_MAP.set(url, existedURL)
return {
url: new URL(url).href,
Expand Down

0 comments on commit 8dbea4f

Please sign in to comment.