Skip to content

Commit

Permalink
feat(router): add filesystem-based routing support for .ng files (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts authored Jan 5, 2024
1 parent faa4114 commit f0dadf0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
16 changes: 13 additions & 3 deletions apps/ng-app/src/app/app.component.ng
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<script lang="ts">
import { inject, signal, effect, computed } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import Hello from './hello.ng';
import AnotherOne from './another-one.ng';
import { JsonPipe } from '@angular/common';
import { RouterOutlet, RouterLink } from '@angular/router';
import { delay } from 'rxjs';

import Hello from './hello.ng';
import AnotherOne from './another-one.ng';
import Highlight from './highlight.ng';
import { HelloOriginal } from './hello';

defineMetadata({
selector: 'app-root',
imports: [JsonPipe, HelloOriginal],
imports: [JsonPipe, HelloOriginal, RouterOutlet, RouterLink],
exposes: [Math],
});

Expand Down Expand Up @@ -65,6 +67,14 @@
} @else {
<p>Loading todo...</p>
}

<br/>

<a routerLink="/">Home</a> | <a routerLink="/about">About</a>

<br/>

<router-outlet />
</template>

<style>
Expand Down
3 changes: 2 additions & 1 deletion apps/ng-app/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { provideFileRouter } from '@analogjs/router';
import { provideHttpClient } from '@angular/common/http';
import { ApplicationConfig } from '@angular/core';

export const appConfig: ApplicationConfig = {
providers: [provideHttpClient()],
providers: [provideFileRouter(), provideHttpClient()],
};
1 change: 1 addition & 0 deletions apps/ng-app/src/app/pages/(home).page.ng
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<template> Welcome Home </template>
9 changes: 9 additions & 0 deletions apps/ng-app/src/app/pages/about.page.ng
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script lang="ts">
import Hello from '../hello.ng';
</script>

<template>
About me works!

<Hello />
</template>
7 changes: 4 additions & 3 deletions packages/router/src/lib/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const FILES = import.meta.glob<RouteExport>([
'/app/routes/**/*.ts',
'/src/app/routes/**/*.ts',
'/src/app/pages/**/*.page.ts',
'/src/app/pages/**/*.page.ng',
]);

const CONTENT_FILES = import.meta.glob<string>(
Expand Down Expand Up @@ -123,7 +124,7 @@ function toRawPath(filename: string): string {
return filename
.replace(
// convert to relative path and remove file extension
/^\/(.*?)\/routes\/|^\/(.*?)\/pages\/|\/app\/routes\/|(\.page\.(js|ts)$)|(\.(ts|md)$)/g,
/^\/(.*?)\/routes\/|^\/(.*?)\/pages\/|\/app\/routes\/|(\.page\.(js|ts|ng)$)|(\.(ts|md|ng)$)/g,
''
)
.replace(/\[\.{3}.+\]/, '**') // [...not-found] => **
Expand Down Expand Up @@ -156,13 +157,13 @@ function toRoutes(rawRoutes: RawRoute[], files: Files): Route[] {
: (files[rawRoute.filename] as () => Promise<RouteExport>);

const endpointKey = rawRoute.filename.replace(
/\.page\.ts$/,
/\.page\.(ts|ng)$/,
ENDPOINT_EXTENSION
);

// get endpoint path
const rawEndpoint = rawRoute.filename
.replace(/\.page\.ts$/, '')
.replace(/\.page\.(ts|ng)$/, '')
.replace(/\[\.{3}.+\]/, '**') // [...not-found] => **
.split(APP_DIR)[1];

Expand Down

0 comments on commit f0dadf0

Please sign in to comment.