Skip to content

Commit fe16286

Browse files
committed
refactor: move extension rewrite logic to paths resolver
1 parent 305d095 commit fe16286

File tree

4 files changed

+19
-31
lines changed

4 files changed

+19
-31
lines changed

src/code_scanners/routes_scanner/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export class RoutesScanner {
202202
return {
203203
name,
204204
method: method ?? 'handle',
205-
path: string.toUnixSlash(this.pathsResolver.resolve(specifier)),
205+
path: string.toUnixSlash(this.pathsResolver.resolve(specifier, true)),
206206
import: {
207207
specifier,
208208
type: 'default',

src/paths_resolver.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class PathsResolver {
7373
* const path = resolver.resolve('#app/models/user')
7474
* const path2 = resolver.resolve('@/utils/helper')
7575
*/
76-
resolve(specifier: string) {
76+
resolve(specifier: string, rewriteAliasImportExtension: boolean = false) {
7777
if (isRelative(specifier)) {
7878
throw new Error('Cannot resolve relative paths using PathsResolver')
7979
}
@@ -84,7 +84,21 @@ export class PathsResolver {
8484
return cached
8585
}
8686

87-
this.#resolvedPaths[cacheKey] = fileURLToPath(this.#resolver(specifier, this.#appRoot))
87+
/**
88+
* Currently the PathsResolver relies on a non-standard way of resolving
89+
* absolute paths or paths with subpath imports. Because of which, the
90+
* on-disk file could be TypeScript, while the resolved filepath is
91+
* JavaScript.
92+
*
93+
* To overcome this limitation, we rewrite the file extension to ".ts" when
94+
* the import specifier starts with a "#".
95+
*/
96+
let resolvedPath = fileURLToPath(this.#resolver(specifier, this.#appRoot))
97+
if (rewriteAliasImportExtension && specifier.startsWith('#') && resolvedPath.endsWith('.js')) {
98+
resolvedPath = resolvedPath.replace(/\.js$/, '.ts')
99+
}
100+
101+
this.#resolvedPaths[cacheKey] = resolvedPath
88102
return this.#resolvedPaths[cacheKey]
89103
}
90104
}

src/virtual_file_system.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -86,32 +86,6 @@ export class VirtualFileSystem {
8686
this.#matcher = picomatch(this.#options.glob ?? DEFAULT_GLOB, this.#picoMatchOptions)
8787
}
8888

89-
/**
90-
* Currently the PathsResolver relies on a non-standard way of resolving
91-
* absolute paths or paths with subpath imports. Because of which, the
92-
* on-disk file could be TypeScript, while the resolved filepath is
93-
* JavaScript.
94-
*
95-
* To overcome this limitation, we start by first looking for a `.ts` file
96-
* (because of high probability) and then look for `.js` file
97-
*/
98-
async #readTSOrJSFile(filePath: string): Promise<string> {
99-
if (filePath.endsWith('.js')) {
100-
try {
101-
const contents = await readFile(filePath.replace(/\.js$/, '.ts'), 'utf-8')
102-
debug('read as TypeScript file "%s"', filePath)
103-
return contents
104-
} catch (error) {
105-
if (error.code === 'ENOENT') {
106-
return readFile(filePath, 'utf-8')
107-
}
108-
throw error
109-
}
110-
}
111-
112-
return readFile(filePath, 'utf-8')
113-
}
114-
11589
/**
11690
* Scans the filesystem to collect the files. Newly files must
11791
* be added via the ".add" method.
@@ -262,7 +236,7 @@ export class VirtualFileSystem {
262236
return cached
263237
}
264238

265-
const fileContents = await this.#readTSOrJSFile(filePath)
239+
const fileContents = await readFile(filePath, 'utf-8')
266240
debug('parsing "%s" file to AST', filePath)
267241

268242
this.#astCache.set(filePath, parse(Lang.TypeScript, fileContents).root())

tests/dev_server.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ test.group('DevServer', () => {
293293
await fs.createJson('tsconfig.json', { include: ['**/*'], exclude: [] })
294294
await fs.create(
295295
'bin/server.ts',
296-
`process.send({ type: 'hot-hook:invalidated', path: '${normalizePathForWindows(join(fs.basePath, 'start/routes.ts'))}' });`
296+
`process.send({ type: 'hot-hook:invalidated', paths: ['${normalizePathForWindows(join(fs.basePath, 'start/routes.ts'))}'] });`
297297
)
298298
await fs.create('start/routes.ts', ``)
299299
await fs.create('.env', 'PORT=3338')

0 commit comments

Comments
 (0)