Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stream-pages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@doc-kit/generator-react': minor
---

`jsx-ast` and `html` stream their input: with more than one thread, page building and rendering overlap with the generators before them. `html` bundles as soon as every page's metadata is known, and renders pages as they are built. Streaming leaves the generated files unchanged, but `jsx-ast` now yields pages in the order their entries arrive, not the order the site lists them, and, once every page has arrived, a `{ manifest }` item listing their heads in the order the site lists them, the synthetic pages last. Bundler adapters' `compile` can now be called before and while `buildServer` and `buildClient` run.
12 changes: 11 additions & 1 deletion packages/core/src/caching.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
'use strict';

import logger from './logger/index.mjs';
import { once } from './utils/generators.mjs';
import { isAsyncIterable } from './utils/misc.mjs';
import { createTee } from './utils/tee.mjs';

const cachingLogger = logger.child('caching');

/**
* Gives a value as the single chunk of an async generator.
*
* @template T
* @param {T} value
* @returns {AsyncGenerator<T>}
*/
async function* once(value) {
yield value;
}

/**
* Collects all values from an async generator into a flat array.
* Each yielded chunk is spread into the results array.
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/generators.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { resolvePipeline } from './generators/pipeline.mjs';
import logger from './logger/index.mjs';
import createWorkerPool from './threading/index.mjs';
import createParallelWorker from './threading/parallel.mjs';
import { once } from './utils/generators.mjs';
import { loadMarkdownPlugins } from './utils/markdown/plugins.mjs';
import { isAsyncIterable } from './utils/misc.mjs';

Expand Down Expand Up @@ -55,7 +54,9 @@ const createGenerator = () => {
// and given as its only chunk
const whole = await cache.consume(dependsOn);

return once(whole);
return (async function* () {
yield whole;
})();
};

/**
Expand Down
9 changes: 0 additions & 9 deletions packages/core/src/utils/__tests__/generators.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
getVersionFromSemVer,
coerceSemVer,
getCompatibleVersions,
once,
} from '../generators.mjs';

describe('getEntryDescription', () => {
Expand Down Expand Up @@ -139,11 +138,3 @@ describe('getCompatibleVersions', () => {
assert.equal(result.length, 2);
});
});

describe('once', () => {
it('gives a value as the only chunk', async () => {
const chunks = await Array.fromAsync(once([1, 2]));

assert.deepEqual(chunks, [[1, 2]]);
});
});
11 changes: 0 additions & 11 deletions packages/core/src/utils/generators.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,6 @@ export const groupNodesByModule = nodes => {
return Map.groupBy(nodes, node => node.api);
};

/**
* Gives a value as the single chunk of an async generator.
*
* @template T
* @param {T} value
* @returns {AsyncGenerator<T>}
*/
export async function* once(value) {
yield value;
}

/**
* Parses the SemVer string into a Node.js-alike version
*
Expand Down
9 changes: 6 additions & 3 deletions packages/react/src/html/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -544,16 +544,19 @@ The `bundler` option takes a small adapter, so the site can be built with Vite

An adapter is an object with three methods. The generator calls `buildServer`
and `buildClient` once each and `compile` once per page, then renders the pages
from the compiled modules across the worker threads.
from the compiled modules across the worker threads. Pages are compiled as they
arrive, so `compile` can run before `buildServer`, and while either build still
runs: it must leave the module's imports as they are, as the library they point
to may not exist yet, and must not depend on the builds.

- `buildServer(options)` {Function} Bundles the component library for Node.js
and returns a promise of the `file:` URL of the built module.
- `entry` {string} The library's source: re-exports of every component a
page may render, Preact's `h` and `Fragment`, and `renderToStringAsync`.
- `virtualImports` {Object} In-memory modules to make available, by module
ID, `#theme/config` included.
- `outDir` {string} A temporary directory to write the library to, which the
generator removes afterwards.
- `outDir` {string} An empty temporary directory to write the library to,
which the generator removes afterwards.
- `config` {Object} The resolved `html` configuration.
- `compile(code, fileName)` {Function} Turns one page module, written in JSX,
into plain JavaScript Node.js can import, and returns a promise of it.
Expand Down
Loading
Loading