Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reference in node_modules folder #132

Merged
merged 1 commit into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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