|
1 | 1 | import slash from 'slash'; |
2 | 2 | import path from 'path'; |
3 | | - |
4 | 3 | const root = slash(global.rootPath || process.cwd()); |
5 | 4 |
|
6 | 5 | export const hasRootPathPrefixInString = (importPath, rootPathPrefix = '~') => { |
7 | | - let containsRootPathPrefix = false; |
8 | | - |
9 | | - if (typeof importPath === 'string') { |
10 | | - if (importPath.substring(0, 1) === rootPathPrefix) { |
11 | | - containsRootPathPrefix = true; |
12 | | - } |
13 | | - |
14 | | - const firstTwoCharactersOfString = importPath.substring(0, 2); |
15 | | - if (firstTwoCharactersOfString === `${rootPathPrefix}/`) { |
16 | | - containsRootPathPrefix = true; |
17 | | - } |
18 | | - } |
19 | | - |
20 | | - return containsRootPathPrefix; |
| 6 | + return !!(typeof importPath === 'string' && importPath.indexOf(rootPathPrefix) === 0); |
21 | 7 | }; |
22 | 8 |
|
23 | 9 | export const transformRelativeToRootPath = (importPath, rootPathSuffix, rootPathPrefix, sourceFile = '') => { |
24 | | - let withoutRootPathPrefix = ''; |
25 | 10 | if (hasRootPathPrefixInString(importPath, rootPathPrefix)) { |
26 | | - if (importPath.substring(0, 1) === '/') { |
27 | | - withoutRootPathPrefix = importPath.substring(1, importPath.length); |
28 | | - } else { |
29 | | - withoutRootPathPrefix = importPath.substring(2, importPath.length); |
30 | | - } |
| 11 | + const withoutRootPathPrefix = importPath.replace(rootPathPrefix, ''); |
31 | 12 |
|
32 | 13 | const absolutePath = path.resolve(`${rootPathSuffix ? rootPathSuffix : './'}/${withoutRootPathPrefix}`); |
33 | 14 | let sourcePath = sourceFile.substring(0, sourceFile.lastIndexOf('/')); |
|
0 commit comments