Skip to content

Commit af7ce8d

Browse files
committed
feat: add paths resolver
1 parent 73ab533 commit af7ce8d

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"!build/bin",
1313
"!build/tests"
1414
],
15+
"imports": {
16+
"#src/*": "./src/*.ts"
17+
},
1518
"exports": {
1619
".": "./build/index.js",
1720
"./code_transformer": "./build/src/code_transformer/main.js",

src/paths_resolver.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) AdonisJS
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { fileURLToPath } from 'node:url'
11+
12+
/**
13+
* Encapsulates the API to resolve import specifiers with the ability
14+
* to define customer resolver.
15+
*/
16+
export class PathsResolver {
17+
#resolvedPaths: Record<string, string> = {}
18+
#resolver: (specifier: string) => string = (specifier) => import.meta.resolve(specifier)
19+
20+
/**
21+
* Define a custom resolver that resolves a path
22+
*/
23+
use(resolver: (specifier: string) => string) {
24+
this.#resolver = resolver
25+
}
26+
27+
/**
28+
* Resolve import specifier
29+
*/
30+
resolve(specifier: string) {
31+
if (specifier.startsWith('./') || specifier.startsWith('../')) {
32+
throw new Error('Cannot resolve relative paths')
33+
}
34+
35+
const cached = this.#resolvedPaths[specifier]
36+
if (cached) {
37+
return cached
38+
}
39+
40+
this.#resolvedPaths[specifier] = fileURLToPath(this.#resolver(specifier))
41+
return this.#resolvedPaths[specifier]
42+
}
43+
}

tests/paths_resolver.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) AdonisJS
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { test } from '@japa/runner'
11+
import { PathsResolver } from '../src/paths_resolver.ts'
12+
import { join } from 'node:path'
13+
14+
test.group('Paths resolver', () => {
15+
test('throw error when trying to resolve a relative path', () => {
16+
const resolver = new PathsResolver()
17+
resolver.resolve('./routes_scanner.spec.ts')
18+
}).throws('Cannot resolve relative paths')
19+
20+
test('resolve subpath import specifier', ({ assert }) => {
21+
const resolver = new PathsResolver()
22+
assert.equal(
23+
resolver.resolve('#src/file_system'),
24+
join(import.meta.dirname, '..', 'src', 'file_system.ts')
25+
)
26+
})
27+
28+
test('resolve package export specifier', ({ assert }) => {
29+
const resolver = new PathsResolver()
30+
assert.equal(
31+
resolver.resolve('@adonisjs/assembler/code_transformer'),
32+
join(import.meta.dirname, '..', 'build', 'src', 'code_transformer/main.js')
33+
)
34+
})
35+
36+
test('use custom resolver', ({ assert }) => {
37+
const resolver = new PathsResolver()
38+
resolver.use((specifier) => `file:///${specifier.replace('#', '')}`)
39+
assert.equal(resolver.resolve('#src/file_system'), '/src/file_system')
40+
})
41+
})

0 commit comments

Comments
 (0)