Skip to content

Commit

Permalink
Handle path mappings in Nx monorepos (#300)
Browse files Browse the repository at this point in the history
* Handle path mappings in Nx monorepos

* Rename the import cache to prevent shadowing
  • Loading branch information
mgechev committed Jan 13, 2020
1 parent 164ed04 commit f901574
Show file tree
Hide file tree
Showing 165 changed files with 18,390 additions and 39 deletions.
40 changes: 22 additions & 18 deletions packages/guess-parser/src/angular/index.ts
Expand Up @@ -21,20 +21,20 @@ const imports = (
child: string,
program: ts.Program,
host: ts.CompilerHost,
cache: {[parent: string]: {[child: string]: boolean}},
importCache: {[parent: string]: {[child: string]: boolean}},
visited: { [key: string]: boolean } = {}
) => {
if (cache[parent] && cache[parent][child] !== undefined) {
return cache[parent][child];
if (importCache[parent] && importCache[parent][child] !== undefined) {
return importCache[parent][child];
}
cache[parent] = cache[parent] || {};
importCache[parent] = importCache[parent] || {};
const sf = program.getSourceFile(parent);
if (!sf) {
cache[parent][child] = false;
importCache[parent][child] = false;
return false;
}
if (visited[parent]) {
cache[parent][child] = false;
importCache[parent][child] = false;
return false;
}
visited[parent] = true;
Expand All @@ -58,10 +58,10 @@ const imports = (
}
// We don't want to dig into node_modules to find an entry point.
if (!found && existsSync(fullPath) && !fullPath.includes('node_modules')) {
found = imports(fullPath, child, program, host, cache, visited);
found = imports(fullPath, child, program, host, importCache, visited);
}
});
cache[parent][child] = found;
importCache[parent][child] = found;
return found;
};

Expand Down Expand Up @@ -194,13 +194,15 @@ const readChildren = (
return null;
};

const getModulePathFromRoute = (parentPath: string, loadChildren: string) => {
const childModule = loadChildren.split('#')[0] + '.ts';
if (loadChildren.startsWith('.')) {
return join(dirname(parentPath), childModule);
const getModulePathFromRoute = (parentPath: string, loadChildren: string, program: ts.Program, host: ts.CompilerHost) => {
const childModule = loadChildren.split('#')[0];
const { resolvedModule } = ts.resolveModuleName(childModule, parentPath, program.getCompilerOptions(), host);
if (resolvedModule) {
return resolvedModule.resolvedFileName;
}
const childModuleFile = childModule + '.ts';
const parentSegments = dirname(parentPath).split(sep);
const childSegments = childModule.split('/');
const childSegments = childModuleFile.split('/');
const max = Math.min(parentSegments.length, childSegments.length);
let maxCommon = 0;
for (let i = 1; i < max; i += 1) {
Expand All @@ -218,7 +220,7 @@ const getModulePathFromRoute = (parentPath: string, loadChildren: string) => {
}
return join(
dirname(parentPath),
childModule
childModuleFile
.split('/')
.slice(maxCommon, childSegments.length)
.join('/')
Expand Down Expand Up @@ -269,7 +271,7 @@ const getRoute = (
program,
host
);
const module = getModulePathFromRoute(parent, loadChildren);
const module = getModulePathFromRoute(parent, loadChildren, program, host);
return {
...route,
module
Expand Down Expand Up @@ -422,15 +424,16 @@ const findMainModule = (program: ts.Program) => {

const getLazyEntryPoints = (
node: ts.ObjectLiteralExpression,
program: ts.Program
program: ts.Program,
host: ts.CompilerHost
) => {
const value = readLoadChildren(node, program.getTypeChecker());
if (!value) {
return null;
}

const parent = resolve(node.getSourceFile().fileName);
const module = getModulePathFromRoute(parent, value);
const module = getModulePathFromRoute(parent, value, program, host);
return module;
};

Expand Down Expand Up @@ -492,7 +495,8 @@ export const parseRoutes = (
visitNode.bind(null, s, (n: ts.Node) => {
const path = getLazyEntryPoints(
n as ts.ObjectLiteralExpression,
program
program,
host
);
if (!path) {
return;
Expand Down
28 changes: 24 additions & 4 deletions packages/guess-parser/test/angular.spec.ts
@@ -1,6 +1,6 @@
import { parseRoutes } from '../src/angular';

const fixtureRoutes = new Set<string>([
const fixtureRoutes = new Set([
'/foo',
'/foo/baz',
'/foo/index',
Expand All @@ -11,26 +11,46 @@ const fixtureRoutes = new Set<string>([
'/bar-simple'
]);

const nxRoutes = new Set([
'/login',
'/home',
'/customers/list',
'/customers'
]);

describe('Angular parser', () => {
it('should parse an app', () => {
expect(() =>
parseRoutes('packages/guess-parser/test/fixtures/angular/src/tsconfig.app.json')
parseRoutes(
'packages/guess-parser/test/fixtures/angular/src/tsconfig.app.json'
)
).not.toThrow();
});

it('should produce routes', () => {
const routes = parseRoutes('packages/guess-parser/test/fixtures/angular/src/tsconfig.app.json');
const routes = parseRoutes(
'packages/guess-parser/test/fixtures/angular/src/tsconfig.app.json'
);
expect(routes instanceof Array).toBeTruthy();
const allRoutes = new Set(routes.map(r => r.path));
[...allRoutes].forEach(r => expect(fixtureRoutes).toContain(r));
expect(allRoutes.size).toEqual(fixtureRoutes.size);
});

it('should produce routes with proper paths', () => {
const routes = parseRoutes('packages/guess-parser/test/fixtures/angular/src/tsconfig.app.json');
const routes = parseRoutes(
'packages/guess-parser/test/fixtures/angular/src/tsconfig.app.json'
);
const route = routes.find(r => r.path === '/foo');
expect(route!.modulePath.endsWith('foo.module.ts')).toBeTruthy();
expect(route!.lazy).toBeTruthy();
expect(route!.parentModulePath!.endsWith('app.module.ts')).toBeTruthy();
});

it('should work with nx monorepo with path mappings', () => {
const routes = parseRoutes(
'packages/guess-parser/test/fixtures/nx/apps/ng-cli-app/tsconfig.app.json'
).map(r => r.path);
[...routes].forEach(r => expect(nxRoutes).toContain(r));
});
});
21 changes: 21 additions & 0 deletions packages/guess-parser/test/fixtures/nx/LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Christian Janz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions packages/guess-parser/test/fixtures/nx/README.md
@@ -0,0 +1,3 @@
# Angular monorepo example using Nx

This app shows the process of refactoring an Angular app to a monorepo with the help of [Nx](https://nx.dev/angular)

0 comments on commit f901574

Please sign in to comment.