From 717625fd22eac60b5522c89a50e67811e3dd4aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juste=20M=C3=A9thode=20B?= <36489637+justkey007@users.noreply.github.com> Date: Tue, 7 Mar 2023 09:31:59 +0100 Subject: [PATCH] [BUG] What is utils::path-cache::getAAP() doing? (#160) Fixes #155 --- projects/project1/src/index.ts | 6 ++++++ projects/project1/tsconfig.json | 2 +- src/helpers/path.ts | 2 +- src/replacers/base-url.replacer.ts | 4 +++- src/replacers/default.replacer.ts | 10 +++++++++- src/utils/path-cache.ts | 13 +++++++------ 6 files changed, 27 insertions(+), 10 deletions(-) diff --git a/projects/project1/src/index.ts b/projects/project1/src/index.ts index 1551d12..0410893 100644 --- a/projects/project1/src/index.ts +++ b/projects/project1/src/index.ts @@ -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); diff --git a/projects/project1/tsconfig.json b/projects/project1/tsconfig.json index 98ec764..66f124e 100644 --- a/projects/project1/tsconfig.json +++ b/projects/project1/tsconfig.json @@ -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"] } diff --git a/src/helpers/path.ts b/src/helpers/path.ts index d533f77..b91ad2f 100644 --- a/src/helpers/path.ts +++ b/src/helpers/path.ts @@ -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; } diff --git a/src/replacers/base-url.replacer.ts b/src/replacers/base-url.replacer.ts index 7b670f8..e8327ca 100644 --- a/src/replacers/base-url.replacer.ts +++ b/src/replacers/base-url.replacer.ts @@ -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('.')) { diff --git a/src/replacers/default.replacer.ts b/src/replacers/default.replacer.ts index 8ed77f3..76b820a 100644 --- a/src/replacers/default.replacer.ts +++ b/src/replacers/default.replacer.ts @@ -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 ); @@ -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( diff --git a/src/utils/path-cache.ts b/src/utils/path-cache.ts index 7628253..be902dd 100644 --- a/src/utils/path-cache.ts +++ b/src/utils/path-cache.ts @@ -97,7 +97,8 @@ export class PathCache { let aliasPathPart = aliasPathParts.shift() || ''; - let pathExists: boolean; + let pathExists = false; + while ( !(pathExists = this.exists(join(basePath, aliasPathPart))) && aliasPathParts.length @@ -105,11 +106,11 @@ export class PathCache { aliasPathPart = aliasPathParts.shift(); } - return join( - basePath, - pathExists ? aliasPathPart : '', - aliasPathParts.join('/') - ); + if (pathExists) { + return join(basePath, aliasPathPart, aliasPathParts.join('/')); + } + + return '---' + join(basePath, aliasPathParts.join('/')); } /**