Skip to content

Commit e41e196

Browse files
committed
Updates entry_point to remove support for returning a string from an entry point.
Refs #25. Since the original renderer tool was removed, it is no longer ever valid for users to return a `string` or `Promise<string>` type, meaning there is no need to support it. This removes that case from `entry_point` and removes an assertion error from `renderer`.
1 parent 72c704d commit e41e196

5 files changed

Lines changed: 14 additions & 88 deletions

File tree

packages/renderer/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ ts_library(
1919
":entry_point",
2020
"//common:binary",
2121
"//common:formatters",
22-
"//common/models:prerender_resource",
2322
"@npm//@types/node",
2423
"@npm//@types/yargs",
2524
],

packages/renderer/entry_point.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ import { PrerenderResource } from 'rules_prerender/common/models/prerender_resou
66
* the default function export, and validates the result before returning it.
77
*
88
* @param entryPoint A path to a JavaScript CommonJS module. This module should
9-
* have a default export of a function. This function accepts no arguments
10-
* and returns a `string` or a `Promise<string>`. The module is imported
11-
* into the NodeJS runtime and invoked, propagating the return value to the
12-
* caller.
9+
* have a default export of a function with the type:
10+
*
11+
* ```
12+
* () => Iterable<PrerenderResource> | Promise<Iterable<PrerenderResource>>
13+
* | AsyncIterable<PrerenderResource>
14+
* ```
15+
*
16+
* The module is imported into the NodeJS runtime and invoked, propagating
17+
* the return value to the caller.
18+
* @returns The returned value of the invoked entry point.
1319
*/
1420
export async function invoke(entryPoint: string): Promise<
15-
string
1621
| Iterable<PrerenderResource>
1722
| AsyncIterable<PrerenderResource>
1823
> {
@@ -33,7 +38,6 @@ export async function invoke(entryPoint: string): Promise<
3338
}
3439

3540
const rendered = await defaultExport() as unknown;
36-
if (typeof rendered === 'string') return rendered;
3741
if (isIterable(rendered)) {
3842
return rendered as Iterable<PrerenderResource>;
3943
}
@@ -43,9 +47,6 @@ export async function invoke(entryPoint: string): Promise<
4347

4448
throw new Error(`Entry point (${entryPoint}) provided a default export`
4549
+ ` which returned a value that is not one of:\n${[
46-
'string',
47-
'Promise<string>',
48-
'Promise<PrerenderResource>',
4950
'Iterable<PrerenderResource>',
5051
'Promise<Iterable<PrerenderResource>>',
5152
'AsyncIterable<PrerenderResource>',

packages/renderer/entry_point_test.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,6 @@ import * as importLib from 'rules_prerender/packages/renderer/dynamic_import';
66

77
describe('entry_point', () => {
88
describe('invoke()', () => {
9-
it('invokes the given entry point and returns its string value', async () => {
10-
const defaultExport = jasmine.createSpy('defaultExport')
11-
.and.returnValue('Hello, World!');
12-
spyOn(importLib, 'dynamicImport').and.resolveTo({
13-
default: defaultExport,
14-
});
15-
16-
const rendered = await invoke('foo.js');
17-
18-
expect(importLib.dynamicImport).toHaveBeenCalledOnceWith('foo.js');
19-
expect(defaultExport).toHaveBeenCalledOnceWith();
20-
21-
expect(rendered).toBe('Hello, World!');
22-
});
23-
24-
it('invokes the given entry point and awaits its `Promise<string>` value', async () => {
25-
const defaultExport = jasmine.createSpy('defaultExport')
26-
.and.resolveTo('Hello, World!');
27-
spyOn(importLib, 'dynamicImport').and.resolveTo({
28-
default: defaultExport,
29-
});
30-
31-
const rendered = await invoke('foo.js');
32-
33-
expect(rendered).toBe('Hello, World!');
34-
});
35-
369
it('invokes the given entry point and returns its `Iterable<PrerenderResource>` value', async () => {
3710
const defaultExport =
3811
jasmine.createSpy('defaultExport').and.returnValue([

packages/renderer/renderer.ts

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as path from 'path';
33
import * as yargs from 'yargs';
44
import { main } from 'rules_prerender/common/binary';
55
import { mdSpacing } from 'rules_prerender/common/formatters';
6-
import { PrerenderResource } from 'rules_prerender/common/models/prerender_resource';
76
import { invoke } from 'rules_prerender/packages/renderer/entry_point';
87

98
main(async () => {
@@ -34,27 +33,14 @@ main(async () => {
3433
.argv;
3534

3635
// Invoke the provided entry point.
37-
let resources: string | Iterable<PrerenderResource>
38-
| AsyncIterable<PrerenderResource>;
36+
let resources: PromiseValue<ReturnType<typeof invoke>>;
3937
try {
4038
resources = await invoke(entryPoint);
4139
} catch (err) {
4240
console.error(err.message);
4341
return 1;
4442
}
4543

46-
// Validate the result of the entry point.
47-
if (typeof resources === 'string') {
48-
console.error(
49-
`Expected entry point (${entryPoint}) to return one of:\n${[
50-
'Iterable<PrerenderResource>',
51-
'Promise<Iterable<PrerenderResource>>',
52-
'AsyncIterable<PrerenderResource>',
53-
].join('\n')}\n\nInstead, got:\n${stringify(resources)}`,
54-
);
55-
return 1;
56-
}
57-
5844
// Write each resource to its file.
5945
const writes = [] as Promise<void>[];
6046
for await (const resource of resources) {
@@ -75,11 +61,6 @@ main(async () => {
7561
return 0;
7662
});
7763

78-
function stringify(value: unknown): string {
79-
if (typeof value === 'string') return value;
80-
if (typeof value === 'object') {
81-
const obj = value as Record<string, unknown>;
82-
if (obj.toString) return obj.toString();
83-
}
84-
return JSON.stringify(value);
85-
}
64+
// Extracts the generic value type from an input `Promise`.
65+
type PromiseValue<T extends Promise<unknown>> =
66+
T extends Promise<infer V> ? V : never;

packages/renderer/renderer_test.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -162,32 +162,4 @@ module.exports = 'Hello, World!'; // Not a function...
162162
});
163163
expect(outputFiles).toEqual([]);
164164
});
165-
166-
it('fails from user code returning a bad type', async () => {
167-
await fs.mkdir(`${tmpDir.get()}/output`);
168-
await fs.writeFile(`${tmpDir.get()}/foo.js`, `
169-
module.exports = () => {
170-
return 'Hello, World!'; // Not an iterable.
171-
};
172-
`.trim());
173-
174-
const { code, stdout, stderr } = await run({
175-
entryPoint: `${tmpDir.get()}/foo.js`,
176-
outputDir: `${tmpDir.get()}/output`,
177-
});
178-
179-
expect(code).toBe(1);
180-
expect(stdout).toBe('');
181-
expect(stderr).toContain(`${tmpDir.get()}/foo.js`);
182-
expect(stderr).toContain('return one of:');
183-
expect(stderr).not.toContain('\\n'); // String should not be escaped.
184-
// Stack trace should not be displayed for user-fault errors.
185-
expect(stderr).not.toContain(' at ');
186-
187-
// No output files should be written.
188-
const outputFiles = await fs.readdir(`${tmpDir.get()}/output`, {
189-
withFileTypes: true,
190-
});
191-
expect(outputFiles).toEqual([]);
192-
});
193165
});

0 commit comments

Comments
 (0)