From 847f9456e2787afa7e1177382ae9a81364ad0aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juste=20M=C3=A9thode=20B?= <36489637+justkey007@users.noreply.github.com> Date: Mon, 18 Jul 2022 13:41:30 +0200 Subject: [PATCH] fix: reference in node_modules folder (#132) Fixes #130 --- projects/project9/src/index.ts | 4 ++++ projects/project9/tsconfig.json | 6 ++++-- src/helpers/path.ts | 29 ++++++++++++++++++++++++----- tests/test.spec.ts | 8 +++++--- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/projects/project9/src/index.ts b/projects/project9/src/index.ts index d430d21..71e6882 100644 --- a/projects/project9/src/index.ts +++ b/projects/project9/src/index.ts @@ -1,3 +1,7 @@ import { HashMap } from '@esfx/collections-hashmap'; +import { HashMap as HashMap1 } from '@hashmap'; +import { HashMap as HashMap2 } from '@hashmap2'; const map = new HashMap(); +const map1 = new HashMap1(); +const map2 = new HashMap2(); diff --git a/projects/project9/tsconfig.json b/projects/project9/tsconfig.json index a56dc55..b9325ea 100644 --- a/projects/project9/tsconfig.json +++ b/projects/project9/tsconfig.json @@ -1,9 +1,11 @@ { "compilerOptions": { "outDir": "dist", - "baseUrl": "./src", + "baseUrl": "./", "paths": { - "@/*": ["./*"] + "@/*": ["./*"], + "@hashmap": ["node_modules/@esfx/collections-hashmap/dist/index"], + "@hashmap2": ["./node_modules/@esfx/collections-hashmap/dist/index"] } } } diff --git a/src/helpers/path.ts b/src/helpers/path.ts index 6945bd8..d533f77 100644 --- a/src/helpers/path.ts +++ b/src/helpers/path.ts @@ -7,7 +7,7 @@ /** */ import normalizePath = require('normalize-path'); import { sync } from 'globby'; -import { normalize, relative } from 'path'; +import { normalize, relative, resolve } from 'path'; import { AliasPath, IProjectConfig } from '../interfaces'; /** @@ -79,6 +79,8 @@ export function relativeOutPathToConfigDir(config: IProjectConfig) { export function findBasePathOfAlias(config: IProjectConfig) { return (path: string) => { const aliasPath = { path } as AliasPath; + + // If it's an alias that references a file outside the baseUrl if (normalize(aliasPath.path).includes('..')) { const tempBasePath = normalizePath( normalize( @@ -102,7 +104,24 @@ export function findBasePathOfAlias(config: IProjectConfig) { aliasPath.isExtra = true; aliasPath.basePath = absoluteBasePath; } - } else if (config.hasExtraModule) { + + return aliasPath; + } + + /** + * If the alias refers to a file in the node_modules folder + * located at the same level of baseUrl. + * Because typescript will not include the node_modules + * folder in the output folder (outDir). + */ + if (aliasPath.path.match(/^(\.\/|)node_modules/g)) { + aliasPath.basePath = resolve(config.baseUrl, aliasPath.path); + aliasPath.isExtra = false; + return aliasPath; + } + + // If the project references another external project + if (config.hasExtraModule) { aliasPath.isExtra = false; aliasPath.basePath = normalizePath( normalize( @@ -110,11 +129,11 @@ export function findBasePathOfAlias(config: IProjectConfig) { `${config.relConfDirPathInOutPath}/${config.baseUrl}` ) ); - } else { - aliasPath.basePath = config.outDir; - aliasPath.isExtra = false; + return aliasPath; } + aliasPath.basePath = config.outDir; + aliasPath.isExtra = false; return aliasPath; }; } diff --git a/tests/test.spec.ts b/tests/test.spec.ts index 18a5471..f282f12 100644 --- a/tests/test.spec.ts +++ b/tests/test.spec.ts @@ -62,15 +62,17 @@ it(`Import regex matches import statements`, () => { const importStatementMatches = sampleImportStatements.match( newImportStatementRegex('g') - ); + ) as RegExpMatchArray; expect(importStatementMatches).toHaveLength(expectedImportPaths.length); const foundImportPaths: string[] = []; for (const importStatement of importStatementMatches) { // Global match is a string, not a match group, so re-match without the global flag. - const pathMatch = importStatement.match(newStringRegex()); + const pathMatch = importStatement.match( + newStringRegex() + ) as RegExpMatchArray; expect(pathMatch).toBeTruthy(); - foundImportPaths.push(pathMatch.groups.path); + if (pathMatch.groups) foundImportPaths.push(pathMatch.groups.path); } expectedImportPaths.forEach((expectedPath, i) => { expect(expectedPath).toEqual(foundImportPaths[i]);