Skip to content

Commit f15265a

Browse files
authored
add a way to generate output (#4)
fixes idrinth-api-bench/issues#1080
1 parent 1dd31d3 commit f15265a

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

src/configuration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface Routes {
1313
overridePathMappings: {
1414
[filesystemPath: string]: string,
1515
},
16+
useOutput: boolean,
1617
}
1718
interface HTMLMinify {
1819
collapseBooleanAttributes: boolean,
@@ -62,6 +63,7 @@ export default (cwd: string, cliArguments: string[]): Configuration => {
6263
home: '/',
6364
'not-found': '*',
6465
},
66+
useOutput: false,
6567
},
6668
htmlMinify: {
6769
collapseBooleanAttributes: true,

src/generate-routes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import Writer from './writer.js';
99
export default (cwd: string, configuration: Configuration) => {
1010
const writers: Writer[] = [];
1111
if (configuration.routes.build && configuration.routes.type === 'tsx') {
12-
writers.push(new TsxWriter(configuration.runtime.reloadWaitMs,))
12+
writers.push(new TsxWriter(configuration.runtime.reloadWaitMs, configuration.routes.useOutput,))
1313
}
1414
if (configuration.routes.build && configuration.routes.type === 'jsx') {
15-
writers.push(new JsxWriter(configuration.runtime.reloadWaitMs,))
15+
writers.push(new JsxWriter(configuration.runtime.reloadWaitMs, configuration.routes.useOutput,))
1616
}
1717
if (configuration.sitemap.build) {
1818
writers.push(new SitemapWriter(configuration.sitemap.domain));

src/jsx-writer.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@ export default class JsxWriter implements Writer {
55
return 'src/routes.jsx';
66
}
77
protected items: string[] = [];
8-
constructor(private readonly waitReloadMS: number) {
8+
constructor(private readonly waitReloadMS: number, private readonly useOutput: boolean,) {
99
}
1010
// @ts-ignore TS6133
1111
add(path: string, url: string, changed: string): void
1212
{
1313
this.items.push(` (() => {
1414
const LazyElement = buildLazyElement(OfflineLoader, RefreshLoader, import('./pages/${path}/index.tsx',),);
15-
return buildRoute(LazyElement, Loader, '${ url }');
15+
return buildRoute(LazyElement, Loader, '${ url }',);
1616
})(),`
1717
);
1818
}
1919
toString(): string {
2020
return 'import React, {\n' +
2121
' lazy,\n' +
2222
' Suspense,\n' +
23-
'} from \'react\';\n\n' +
23+
'} from \'react\';\n' +
24+
(this.useOutput ? 'import {\n Outlet,\n} from \'react-router-dom\';\n' : '' ) +
25+
'\n' +
2426
'const buildLazyElement = (OfflineLoader, RefreshLoader, imp,) => lazy(async() => {\n' +
2527
' try {\n' +
2628
' return await imp;\n' +
@@ -44,10 +46,13 @@ export default class JsxWriter implements Writer {
4446
'};\n\n' +
4547
'export default (' +
4648
' Loader,' +
49+
(this.useOutput ? ' Layout,' : '' ) +
4750
' RefreshLoader = undefined,' +
4851
' OfflineLoader = undefined,' +
4952
') => [\n '
53+
+ (this.useOutput ? '{element: <Layout><Outlet/></Layout>, children: [\n' : '')
5054
+ this.items.join('\n ')
55+
+ (this.useOutput ? '\n]}' : '')
5156
+ '\n];\n'
5257
}
5358
}

src/tsx-writer.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ export default class TsxWriter implements Writer {
55
return 'src/routes.tsx';
66
}
77
protected items: string[] = [];
8-
constructor(private readonly waitReloadMS: number) {
8+
constructor(private readonly waitReloadMS: number, private readonly useOutput: boolean,) {
99
}
1010
// @ts-ignore TS6133
1111
add(path: string, url: string, changed: string): void
1212
{
1313
this.items.push(` (() => {
1414
const LazyElement = buildLazyElement(OfflineLoader, RefreshLoader, import('./pages/${path}/index.tsx',),);
15-
return buildRoute(LazyElement, Loader, '${ url }');;
15+
return buildRoute(LazyElement, Loader, '${ url }',);
1616
})(),`
1717
);
1818
}
@@ -22,7 +22,9 @@ export default class TsxWriter implements Writer {
2222
' Suspense,\n' +
2323
' ElementType,\n' +
2424
' ComponentType,\n' +
25-
'} from \'react\';\n\n' +
25+
'} from \'react\';\n' +
26+
(this.useOutput ? 'import {\n Outlet,\n} from \'react-router-dom\';\n' : '' ) +
27+
'\n' +
2628
'const buildLazyElement = (\n' +
2729
' OfflineLoader: ComponentType|undefined,\n' +
2830
' RefreshLoader: ComponentType|undefined,\n' +
@@ -54,10 +56,13 @@ export default class TsxWriter implements Writer {
5456
'};\n\n' +
5557
'export default (\n' +
5658
' Loader: ElementType,\n' +
59+
(this.useOutput ? ' Layout: ElementType,\n' : '' ) +
5760
' RefreshLoader: ComponentType|undefined = undefined,\n' +
5861
' OfflineLoader: ComponentType|undefined = undefined,\n' +
5962
') => [\n '
63+
+ (this.useOutput ? '{element: <Layout><Outlet/></Layout>, children: [\n' : '')
6064
+ this.items.join('\n ')
65+
+ (this.useOutput ? '\n]}\n' : '')
6166
+ '\n];\n'
6267
}
6368
}

0 commit comments

Comments
 (0)