-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f116a16
commit 9b4ff86
Showing
22 changed files
with
110 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {glob} from 'glob'; | ||
import path from 'path'; | ||
import {fileURLToPath} from 'url'; | ||
import {writeFile, mkdir, rm} from 'fs/promises'; | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
const files = ( | ||
await glob('**/*.route.ts', { | ||
cwd: path.join(__dirname, '../src/app/samples'), | ||
nodir: true, | ||
}) | ||
).map((file) => file.slice(0, -9)); | ||
files.sort(); | ||
|
||
const content = ` | ||
import type {Route} from '@angular/router'; | ||
export const LINKS = ${JSON.stringify(files.map((file) => file.toLowerCase()))}; | ||
const importAndNotify = async (importFn: () => Promise<any>) => { | ||
const comp = await importFn(); | ||
if (window.parent) { | ||
window.parent.postMessage({type: 'sampleload'}); | ||
} | ||
return comp; | ||
} | ||
export const SAMPLE_ROUTES: Route[] = [${files.map( | ||
(file) => `{path: '${file.toLowerCase()}', loadComponent: () => importAndNotify(() => import('../samples/${file}.route'))}`, | ||
)}]; | ||
`; | ||
|
||
await rm(path.join(__dirname, '../src/app/generated'), {recursive: true, force: true}); | ||
await mkdir(path.join(__dirname, '../src/app/generated')); | ||
await writeFile(path.join(__dirname, '../src/app/generated/samples.route.ts'), content); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,13 @@ | ||
import type {Routes} from '@angular/router'; | ||
import {links} from './links.component'; | ||
import {links} from './links'; | ||
import {LINKS, SAMPLE_ROUTES} from './generated/samples.route'; | ||
|
||
const context = import.meta.webpackContext?.('./', { | ||
regExp: /[^/]*\.route\.ts$/, | ||
mode: 'lazy', | ||
}); | ||
|
||
const componentRegExp = /samples\/([^/]*)\/([^/]*).route\.ts$/; | ||
function replacePattern(webpackContext: __WebpackModuleApi.RequireContext) { | ||
const directComponents: Record<string, any> = {}; | ||
const keys = webpackContext.keys(); | ||
for (const key of keys) { | ||
const matches = key.match(componentRegExp); | ||
if (matches) { | ||
directComponents[`${matches[1]}/${matches[2]}`.toLowerCase()] = key; | ||
} | ||
} | ||
return directComponents; | ||
} | ||
const components = replacePattern(context!); | ||
export const ROUTES: Routes = [ | ||
{ | ||
path: '', | ||
pathMatch: 'full', | ||
loadComponent: () => import('./links.component').then((c) => c.LinksComponent), | ||
providers: [{provide: links, useValue: Object.keys(components)}], | ||
loadComponent: () => import('./links.component'), | ||
providers: [{provide: links, useValue: LINKS}], | ||
}, | ||
...Object.entries(components).map(([path, component]) => { | ||
return { | ||
path, | ||
loadComponent: async () => { | ||
const comp = (await context!(component)).default; | ||
if (window.parent) { | ||
window.parent.postMessage({type: 'sampleload'}); | ||
} | ||
return comp; | ||
}, | ||
}; | ||
}), | ||
...SAMPLE_ROUTES, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {InjectionToken} from '@angular/core'; | ||
|
||
export const links = new InjectionToken<string[]>('app-links'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
declare module '!raw-loader!*' { | ||
declare module '*.svg' { | ||
const contents: string; | ||
export = contents; | ||
} | ||
declare module '*.txt' { | ||
const contents: string; | ||
export = contents; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
declare module '!raw-loader!*' { | ||
declare module '*.svg' { | ||
const contents: string; | ||
export = contents; | ||
} | ||
declare module '*.txt' { | ||
const contents: string; | ||
export = contents; | ||
} |
Oops, something went wrong.