Skip to content

Commit 524c243

Browse files
authored
build: add a generated helper function to load examples (#26968)
* build: add a generated helper function to load examples This new helper removes the examples' reliance on internal APF structure and naming. Currently the examples hard code the ECMAScript version into the application which can and will change over time. The helper function also removes the need to use Webpack-specific magic comments in the documentation website to prevent unwanted files from being loaded. The recent updates to the APF for v16 required several manual changes to both the examples and the documentation site which will no longer be needed in the future. This change retains the existing functionality to allow for the documentation site to be updated with the new functionality. Once updated, additional cleanup is possible to remove the then unused loading functionality. * fixup! build: add a generated helper function to load examples
1 parent 4e01b84 commit 524c243

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

tools/example-module/generate-example-module.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,23 @@ function inlineExampleModuleTemplate(parsedData: AnalyzedExamples): string {
6060
return result;
6161
}, {} as any);
6262

63-
return fs
64-
.readFileSync(require.resolve('./example-module.template'), 'utf8')
65-
.replace(/\${exampleComponents}/g, JSON.stringify(exampleComponents, null, 2));
63+
const loadText = [
64+
`export async function loadExample(id: string): Promise<any> {`,
65+
` switch (id) {`,
66+
...exampleMetadata.map(
67+
data =>
68+
` case '${data.id}':\nreturn import('@angular/components-examples/${data.module.packagePath}');`,
69+
),
70+
` default:\nreturn undefined;`,
71+
` }`,
72+
'}',
73+
].join('\n');
74+
75+
return (
76+
fs
77+
.readFileSync(require.resolve('./example-module.template'), 'utf8')
78+
.replace(/\${exampleComponents}/g, JSON.stringify(exampleComponents, null, 2)) + loadText
79+
);
6680
}
6781

6882
/** Converts a given camel-cased string to a dash-cased string. */

0 commit comments

Comments
 (0)