/
resolve-utils.ts
88 lines (67 loc) · 2.85 KB
/
resolve-utils.ts
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import type * as d from '../../../declarations';
import { IS_BROWSER_ENV, IS_FETCH_ENV } from '../environment';
import { normalizePath } from '@utils';
const COMMON_DIR_MODULE_EXTS = ['.tsx', '.ts', '.mjs', '.js', '.jsx', '.json', '.md'];
export const COMMON_DIR_FILENAMES = ['package.json', 'index.js', 'index.mjs'];
export const isDtsFile = (p: string) => p.endsWith('.d.ts');
export const isTsFile = (p: string) => !isDtsFile(p) && p.endsWith('.ts');
export const isTsxFile = (p: string) => p.endsWith('.tsx');
export const isJsxFile = (p: string) => p.endsWith('.jsx');
export const isJsFile = (p: string) => p.endsWith('.js');
export const isMjsFile = (p: string) => p.endsWith('.mjs');
export const isJsonFile = (p: string) => p.endsWith('.json');
export const getCommonDirName = (dirPath: string, fileName: string) => dirPath + '/' + fileName;
export const isCommonDirModuleFile = (p: string) => COMMON_DIR_MODULE_EXTS.some(ext => p.endsWith(ext));
export const setPackageVersion = (pkgVersions: Map<string, string>, pkgName: string, pkgVersion: string) => {
pkgVersions.set(pkgName, pkgVersion);
};
export const setPackageVersionByContent = (pkgVersions: Map<string, string>, pkgContent: string) => {
try {
const pkg = JSON.parse(pkgContent) as d.PackageJsonData;
if (pkg.name && pkg.version) {
setPackageVersion(pkgVersions, pkg.name, pkg.version);
}
} catch (e) {}
};
export const isLocalModule = (p: string) => p.startsWith('.') || p.startsWith('/');
export const isStencilCoreImport = (p: string) => p.startsWith('@stencil/core');
export const shouldFetchModule = (p: string) => IS_FETCH_ENV && IS_BROWSER_ENV && isNodeModulePath(p);
export const isNodeModulePath = (p: string) => normalizePath(p).split('/').includes('node_modules');
export const getModuleId = (orgImport: string) => {
if (orgImport.startsWith('~')) {
orgImport = orgImport.substring(1);
}
const splt = orgImport.split('/');
const m = {
moduleId: null as string,
filePath: null as string,
scope: null as string,
scopeSubModuleId: null as string,
};
if (orgImport.startsWith('@') && splt.length > 1) {
m.moduleId = splt.slice(0, 2).join('/');
m.filePath = splt.slice(2).join('/');
m.scope = splt[0];
m.scopeSubModuleId = splt[1];
} else {
m.moduleId = splt[0];
m.filePath = splt.slice(1).join('/');
}
return m;
};
export const getPackageDirPath = (p: string, moduleId: string) => {
const parts = normalizePath(p).split('/');
const m = getModuleId(moduleId);
for (let i = parts.length - 1; i >= 1; i--) {
if (parts[i - 1] === 'node_modules') {
if (m.scope) {
if (parts[i] === m.scope && parts[i + 1] === m.scopeSubModuleId) {
return parts.slice(0, i + 2).join('/');
}
} else if (parts[i] === m.moduleId) {
return parts.slice(0, i + 1).join('/');
}
}
}
return null;
};