This repository was archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathresolver.ts
More file actions
55 lines (44 loc) · 1.86 KB
/
resolver.ts
File metadata and controls
55 lines (44 loc) · 1.86 KB
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
import { parse, join } from "path";
import { statSync } from "fs";
export function getResolver(platforms: string[], explicitResolve?: string[], nsPackageFilters?: string[], platformSpecificExt?: string[]) {
explicitResolve = explicitResolve || [];
nsPackageFilters = nsPackageFilters || ['nativescript', 'tns', 'ns', '@nativescript'];
platformSpecificExt = platformSpecificExt || [".ts", ".js", ".scss", ".less", ".css", ".html", ".xml", ".vue", ".json"];
return function (path: string) {
const nmIndex = path.lastIndexOf('node_modules');
if (nmIndex !== -1) {
const subPath = path.substr(nmIndex + 'node_modules'.length).replace(/\\/g, '/');
const shouldResolve = explicitResolve.length && explicitResolve.some(packageName => subPath.indexOf(packageName) !== -1);
const pathParts = subPath.split(/[/\-_]/);
if (!shouldResolve && pathParts.every(p => nsPackageFilters.every(f => f !== p))) {
return path;
}
}
const { dir, name, ext } = parse(path);
if (platformSpecificExt.indexOf(ext) === -1) {
return path;
}
for (const platform of platforms) {
const platformFileName = `${name}.${platform}${ext}`;
const platformPath = toSystemPath(join(dir, platformFileName));
try {
if (statSync(platformPath)) {
return platformPath;
}
} catch (_e) {
// continue checking the other platforms
}
}
return path;
}
}
// Convert paths from \c\some\path to c:\some\path
function toSystemPath(path: string) {
if (!process.platform.startsWith("win32")) {
return path;
}
const drive = path.match(/^\\(\w)\\(.*)$/);
return drive ?
`${drive[1]}:\\${drive[2]}` :
path;
}