Skip to content

Commit

Permalink
fix: reference in node_modules folder (#132)
Browse files Browse the repository at this point in the history
Fixes #130
  • Loading branch information
justkey007 authored Jul 18, 2022
1 parent a6e465f commit 847f945
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
4 changes: 4 additions & 0 deletions projects/project9/src/index.ts
Original file line number Diff line number Diff line change
@@ -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();
6 changes: 4 additions & 2 deletions projects/project9/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}
}
}
29 changes: 24 additions & 5 deletions src/helpers/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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(
Expand All @@ -102,19 +104,36 @@ 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(
`${config.outDir}/` +
`${config.relConfDirPathInOutPath}/${config.baseUrl}`
)
);
} else {
aliasPath.basePath = config.outDir;
aliasPath.isExtra = false;
return aliasPath;
}

aliasPath.basePath = config.outDir;
aliasPath.isExtra = false;
return aliasPath;
};
}
8 changes: 5 additions & 3 deletions tests/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 847f945

Please sign in to comment.