Skip to content

Commit 88891de

Browse files
crisbetoVivian Hu
authored and
Vivian Hu
committed
build: example list outputting quoted components (#13955)
In c64503e the logic that outputs the list of components was reworked to use `JSON.stringify`. As a result, the data that is generated has the `component` inside quotes, rather than a reference to the component class itself. This breaks the demo app. The following changes add some extra logic to remove the quotes around the `component`.
1 parent 01605d0 commit 88891de

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,30 @@ function buildImportsTemplate(data: ExampleMetadata): string {
2323
/** Inlines the example module template with the specified parsed data. */
2424
function inlineExampleModuleTemplate(parsedData: ExampleMetadata[]): string {
2525
const exampleImports = parsedData.map(m => buildImportsTemplate(m)).join('\n');
26+
const quotePlaceholder = '◬';
2627
const exampleList = parsedData.reduce((result, data) => {
2728
return result.concat(data.component).concat(data.additionalComponents);
2829
}, [] as string[]).join(',');
2930

30-
const exampleComponents = parsedData.map((metaData) => {
31-
const fields = [
32-
`title: '${metaData.title.trim()}'`,
33-
`component: ${metaData.component}`,
34-
];
35-
36-
if (metaData.additionalFiles.length) {
37-
fields.push(`additionalFiles: ${JSON.stringify(metaData.additionalFiles)}`);
38-
}
39-
if (metaData.selectorName.length) {
40-
fields.push(`selectorName: '${metaData.selectorName.join(', ')}'`);
41-
}
42-
const data = '\n' + fields.map(field => ' ' + field).join(',\n');
43-
return `'${metaData.id}': {${data}\n }`;
31+
const exampleComponents = parsedData.reduce((result, data) => {
32+
result[data.id] = {
33+
title: data.title,
34+
// Since we use JSON.stringify to output the data below, the `component` will be wrapped
35+
// in quotes, whereas we want a reference to the class. Add placeholder characters next to
36+
// where the quotes will be so that we can strip them away afterwards.
37+
component: `${quotePlaceholder}${data.component}${quotePlaceholder}`,
38+
additionalFiles: data.additionalFiles,
39+
selectorName: data.selectorName.join(', '),
40+
};
41+
42+
return result;
4443
}, {} as any);
4544

4645
return fs.readFileSync(require.resolve('./example-module.template'), 'utf8')
4746
.replace('${exampleImports}', exampleImports)
48-
.replace('${exampleComponents}', `{\n ${exampleComponents.join(',\n ')}}`)
49-
.replace('${exampleList}', `[${exampleList}]`);
47+
.replace('${exampleComponents}', JSON.stringify(exampleComponents))
48+
.replace('${exampleList}', `[${exampleList}]`)
49+
.replace(new RegExp(`"${quotePlaceholder}|${quotePlaceholder}"`, 'g'), '');
5050
}
5151

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

0 commit comments

Comments
 (0)