Skip to content

Commit

Permalink
[BUG] What is utils::path-cache::getAAP() doing? (#160)
Browse files Browse the repository at this point in the history
Fixes #155
  • Loading branch information
justkey007 authored Mar 7, 2023
1 parent e391a04 commit 717625f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
6 changes: 6 additions & 0 deletions projects/project1/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import * as data from 'myproject/data.json';

export { DATA as XYZ } from '@commons';

console.log(DATA);

if (DATA !== 'mydata') {
throw new Error('Bad resolution');
}

console.log(DATA);
console.log(CUSTOM_MODULE);
console.log(EXTRA);
Expand Down
2 changes: 1 addition & 1 deletion projects/project1/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"resolveJsonModule": true,
"paths": {
"myproject/*": ["./*"],
"@commons": ["lib/commons/index.ts"],
"@commons": ["libo/commo/index-1.ts", "lib/commons/index.ts"],
"custom/*": ["custom_modules/*", "custom_modules/modules/*"],
"extra": ["../../project2/index"]
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export function findBasePathOfAlias(config: IProjectConfig) {
* folder in the output folder (outDir).
*/
if (aliasPath.path.match(/^(\.\/|)node_modules/g)) {
aliasPath.basePath = resolve(config.baseUrl, aliasPath.path);
aliasPath.basePath = resolve(config.baseUrl, 'node_modules');
aliasPath.isExtra = false;
return aliasPath;
}
Expand Down
4 changes: 3 additions & 1 deletion src/replacers/base-url.replacer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export default function replaceBaseUrlImport({
let relativePath: string = normalizePath(
relative(
dirname(file),
config.pathCache.getAbsoluteAliasPath(config.outPath, '')
config.pathCache
.getAbsoluteAliasPath(config.outPath, '')
.replace('---', '')
)
);
if (!relativePath.startsWith('.')) {
Expand Down
10 changes: 9 additions & 1 deletion src/replacers/default.replacer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function replaceImportStatement({

if (isAlias) {
for (let i = 0; i < alias.paths.length; i++) {
const absoluteAliasPath = config.pathCache.getAbsoluteAliasPath(
let absoluteAliasPath = config.pathCache.getAbsoluteAliasPath(
alias.paths[i].basePath,
alias.paths[i].path
);
Expand All @@ -65,6 +65,14 @@ export default function replaceImportStatement({
absoluteAliasPath
);

if (absoluteAliasPath.startsWith('---')) {
if (i === alias.paths.length - 1) {
absoluteAliasPath = absoluteAliasPath.replace('---', '');
} else {
continue;
}
}

// Check if path is valid.
if (
!config.pathCache.existsResolvedAlias(
Expand Down
13 changes: 7 additions & 6 deletions src/utils/path-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,20 @@ export class PathCache {

let aliasPathPart = aliasPathParts.shift() || '';

let pathExists: boolean;
let pathExists = false;

while (
!(pathExists = this.exists(join(basePath, aliasPathPart))) &&
aliasPathParts.length
) {
aliasPathPart = aliasPathParts.shift();
}

return join(
basePath,
pathExists ? aliasPathPart : '',
aliasPathParts.join('/')
);
if (pathExists) {
return join(basePath, aliasPathPart, aliasPathParts.join('/'));
}

return '---' + join(basePath, aliasPathParts.join('/'));
}

/**
Expand Down

0 comments on commit 717625f

Please sign in to comment.