Skip to content

Commit b0a56e0

Browse files
committed
use compiled extension in loaded responseURL
1 parent 53a3a7a commit b0a56e0

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/hooks/hooks.mts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
LoadHook,
77
ResolveHook,
88
} from 'node:module'
9-
import { resolve as pathResolve } from 'node:path'
9+
import { extname, resolve as pathResolve } from 'node:path'
1010
import { fileURLToPath, pathToFileURL } from 'node:url'
1111
import { format } from 'node:util'
1212
import { MessagePort } from 'node:worker_threads'
@@ -92,10 +92,20 @@ export const load: LoadHook = async (url, context, nextLoad) => {
9292
}
9393
const format = classifyModule(inputFile)
9494
hookedCJS ||= format === 'commonjs'
95+
const responseURL = new URL(url)
96+
const sourceExtension = extname(responseURL.pathname)
97+
const compiledExtension = extname(fileName)
98+
/* c8 ignore start */
99+
responseURL.pathname =
100+
(sourceExtension
101+
? responseURL.pathname.slice(0, -sourceExtension.length)
102+
: responseURL.pathname) + compiledExtension
103+
/* c8 ignore stop */
95104
return {
96105
source: await readFile(fileName, 'utf8'),
97106
shortCircuit: true,
98107
format,
108+
responseURL: String(responseURL),
99109
}
100110
}
101111

test/hooks/hooks.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,29 @@ t.test('load', async t => {
128128
})
129129

130130
t.test('do a compile, with diagnostics', async t => {
131-
const dir = t.testdir({ file: 'contents' })
131+
const dir = t.testdir({ 'file-compiled.js': 'contents' })
132132
const nextLoad = (url: string, context?: LoadHookContext) => ({
133133
url,
134134
...context,
135135
})
136136
delete MockDaemonClient.compileRequest
137137
MockDaemonClient.compileResponse = {
138-
fileName: resolve(dir, 'file'),
138+
fileName: resolve(dir, 'file-compiled.js'),
139139
diagnostics: ['diagnostics'],
140140
}
141-
t.strictSame(await hooks.load(import.meta.url, {}, nextLoad), {
142-
source: 'contents',
143-
shortCircuit: true,
144-
format: 'module',
145-
})
141+
t.strictSame(
142+
await hooks.load(
143+
String(pathToFileURL(resolve(dir, 'file.ts'))),
144+
{},
145+
nextLoad
146+
),
147+
{
148+
source: 'contents',
149+
shortCircuit: true,
150+
format: 'module',
151+
responseURL: String(pathToFileURL(resolve(dir, 'file.js'))),
152+
}
153+
)
146154
t.strictSame(stderrWrites, ['diagnostics\n'])
147155
stderrWrites.length = 0
148156
})
@@ -212,6 +220,9 @@ t.test('load', async t => {
212220
format: 'commonjs',
213221
source: readFileSync(resolve(dir, 'project/foo.js'), 'utf8'),
214222
shortCircuit: true,
223+
responseURL: String(
224+
pathToFileURL(resolve(dir, 'project/foo.js'))
225+
),
215226
})
216227
const outRes = await hooks.load(
217228
String(pathToFileURL(resolve(dir, 'outside.js'))),

0 commit comments

Comments
 (0)