-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
to-relative-specifier.mts
69 lines (61 loc) · 1.88 KB
/
to-relative-specifier.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @file toRelativeSpecifier
* @module mlly/lib/toRelativeSpecifier
*/
import type { ModuleId } from '@flex-development/mlly'
import pathe from '@flex-development/pathe'
import { ok } from 'devlop'
/**
* Turn `url` into a *relative specifier*.
*
* ::: info
* The relative specifier will only include a file extension if `specifier`
* includes a file extension.
* :::
*
* @see {@linkcode ModuleId}
* @see https://nodejs.org/api/esm.html#terminology
*
* @param {ModuleId} url
* The `file:` URL to convert
* @param {ModuleId} parent
* Parent module id
* @return {string}
* Relative specifier
*/
function toRelativeSpecifier(url: ModuleId, parent: ModuleId): string {
parent = pathe.fileURLToPath(parent)
url = pathe.fileURLToPath(url)
/**
* Directory name of {@linkcode parent} path.
*
* @const {string} dirname
*/
const dirname: string = pathe.dirname(parent)
/**
* Relative specifier.
*
* @var {string} specifier
*/
let specifier: string
if (url.startsWith(parent) && parent.endsWith(pathe.sep)) {
// url is inside same directory as parent
specifier = pathe.dot + pathe.sep + url.slice(parent.length)
} else if (url === dirname || url.startsWith(dirname + pathe.sep)) {
// url is directory name of parent or inside same directory as parent
specifier = pathe.dot + pathe.sep + url.slice(dirname.length + 1)
} else {
// url is outside directory of parent
specifier = pathe.relative(parent, url)
ok(specifier.startsWith(pathe.dot), 'expected result to start with "."')
if (specifier !== pathe.dot && specifier !== pathe.dot.repeat(2)) {
if (/(?:\.\.\/){2,}/.test(specifier)) {
// remove first '../' segment:
// pathe.relative backs out of an extra directory
specifier = specifier.slice(specifier.indexOf(pathe.sep) + 1)
}
}
}
return specifier
}
export default toRelativeSpecifier