-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(container): provide a virtual module to load renderers (#11144)
* feat(container): provide a virtual module to load renderers * address feedback * chore: restore some default to allow to have PHP prototype working * Thread through renderers and manifest * Pass manifest too * update changeset * add diff * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <[email protected]> * fix diff * rebase and update lock --------- Co-authored-by: Matthew Phillips <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]>
- Loading branch information
1 parent
587e75f
commit 803dd80
Showing
16 changed files
with
233 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
"@astrojs/preact": minor | ||
"@astrojs/svelte": minor | ||
"@astrojs/react": minor | ||
"@astrojs/solid-js": minor | ||
"@astrojs/lit": minor | ||
"@astrojs/mdx": minor | ||
"@astrojs/vue": minor | ||
"astro": patch | ||
--- | ||
|
||
The integration now exposes a function called `getContainerRenderer`, that can be used inside the Container APIs to load the relative renderer. | ||
|
||
```js | ||
import { experimental_AstroContainer as AstroContainer } from 'astro/container'; | ||
import ReactWrapper from '../src/components/ReactWrapper.astro'; | ||
import { loadRenderers } from "astro:container"; | ||
import { getContainerRenderer } from "@astrojs/react"; | ||
|
||
test('ReactWrapper with react renderer', async () => { | ||
const renderers = await loadRenderers([getContainerRenderer()]) | ||
const container = await AstroContainer.create({ | ||
renderers, | ||
}); | ||
const result = await container.renderToString(ReactWrapper); | ||
|
||
expect(result).toContain('Counter'); | ||
expect(result).toContain('Count: <!-- -->5'); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
"astro": patch | ||
--- | ||
|
||
**BREAKING CHANGE to the experimental Container API only** | ||
|
||
Changes the **type** of the `renderers` option of the `AstroContainer::create` function and adds a dedicated function `loadRenderers()` to load the rendering scripts from renderer integration packages (`@astrojs/react`, `@astrojs/preact`, `@astrojs/solid-js`, `@astrojs/svelte`, `@astrojs/vue`, `@astrojs/lit`, and `@astrojs/mdx`). | ||
|
||
You no longer need to know the individual, direct file paths to the client and server rendering scripts for each renderer integration package. Now, there is a dedicated function to load the renderer from each package, which is available from `getContainerRenderer()`: | ||
|
||
```diff | ||
import { experimental_AstroContainer as AstroContainer } from 'astro/container'; | ||
import ReactWrapper from '../src/components/ReactWrapper.astro'; | ||
import { loadRenderers } from "astro:container"; | ||
import { getContainerRenderer } from "@astrojs/react"; | ||
|
||
test('ReactWrapper with react renderer', async () => { | ||
+ const renderers = await loadRenderers([getContainerRenderer()]) | ||
- const renderers = [ | ||
- { | ||
- name: '@astrojs/react', | ||
- clientEntrypoint: '@astrojs/react/client.js', | ||
- serverEntrypoint: '@astrojs/react/server.js', | ||
- }, | ||
- ]; | ||
const container = await AstroContainer.create({ | ||
renderers, | ||
}); | ||
const result = await container.renderToString(ReactWrapper); | ||
|
||
expect(result).toContain('Counter'); | ||
expect(result).toContain('Count: <!-- -->5'); | ||
}); | ||
``` | ||
|
||
The new `loadRenderers()` helper function is available from `astro:container`, a virtual module that can be used when running the Astro container inside `vite`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { AstroRenderer, SSRLoadedRenderer } from '../@types/astro.js'; | ||
|
||
/** | ||
* Use this function to provide renderers to the `AstroContainer`: | ||
* | ||
* ```js | ||
* import { getContainerRenderer } from "@astrojs/react"; | ||
* import { experimental_AstroContainer as AstroContainer } from "astro/container"; | ||
* import { loadRenderers } from "astro:container"; // use this only when using vite/vitest | ||
* | ||
* const renderers = await loadRenderers([ getContainerRenderer ]); | ||
* const container = await AstroContainer.create({ renderers }); | ||
* | ||
* ``` | ||
* @param renderers | ||
*/ | ||
export async function loadRenderers(renderers: AstroRenderer[]) { | ||
const loadedRenderers = await Promise.all( | ||
renderers.map(async (renderer) => { | ||
const mod = await import(renderer.serverEntrypoint); | ||
if (typeof mod.default !== 'undefined') { | ||
return { | ||
...renderer, | ||
ssr: mod.default, | ||
} as SSRLoadedRenderer; | ||
} | ||
return undefined; | ||
}) | ||
); | ||
|
||
return loadedRenderers.filter((r): r is SSRLoadedRenderer => Boolean(r)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.