Skip to content

Commit

Permalink
Fix: is self accepting pt 2 module graph boogaloo (#2872)
Browse files Browse the repository at this point in the history
* fix: isSelfAccepting Preact error

* refactor: add .js ext to preact entrypoints for consistency

* chore: changeset

* fix: remove ./client and ./server from preact pkg exp
  • Loading branch information
bholmesdev authored Mar 24, 2022
1 parent 003df32 commit 098f6f6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .changeset/wise-garlics-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/preact': patch
---

Fix `isSelfAccepting` errors when using the Preact integration with the Astro dev server
18 changes: 5 additions & 13 deletions packages/astro/src/core/render/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,13 @@ export type ComponentPreload = [SSRLoadedRenderer[], ComponentInstance];
export type RenderResponse = { type: 'html'; html: string } | { type: 'response'; response: Response };

const svelteStylesRE = /svelte\?svelte&type=style/;
// Cache renderers to avoid re-resolving the module using Vite's `ssrLoadModule`
// This prevents an odd exception trying to resolve the same server-side module
// Multiple times. See `isSelfAccepting` issue: https://github.com/withastro/astro/pull/2852
const rendererCache = new Map<string, SSRLoadedRenderer['ssr']>();

async function loadRenderer(viteServer: vite.ViteDevServer, renderer: AstroRenderer): Promise<SSRLoadedRenderer> {
const { url } = await viteServer.moduleGraph.ensureEntryFromUrl(renderer.serverEntrypoint);

const cachedRenderer = rendererCache.get(url);
if (cachedRenderer) {
return { ...renderer, ssr: cachedRenderer };
}

const mod = (await viteServer.ssrLoadModule(url)) as { default: SSRLoadedRenderer['ssr'] };
rendererCache.set(url, mod.default);
// Vite modules can be out-of-date when using an un-resolved url
// We also encountered inconsistencies when using the resolveUrl and resolveId helpers
// We've found that pulling the ID directly from the urlToModuleMap is the most stable!
const id = viteServer.moduleGraph.urlToModuleMap.get(renderer.serverEntrypoint)?.id ?? renderer.serverEntrypoint;
const mod = (await viteServer.ssrLoadModule(id)) as { default: SSRLoadedRenderer['ssr'] };
return { ...renderer, ssr: mod.default };
}

Expand Down
2 changes: 0 additions & 2 deletions packages/integrations/preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
"homepage": "https://astro.build",
"exports": {
".": "./dist/index.js",
"./client": "./client",
"./client.js": "./client.js",
"./server": "./server",
"./server.js": "./server.js",
"./package.json": "./package.json"
},
Expand Down
8 changes: 4 additions & 4 deletions packages/integrations/preact/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { AstroIntegration } from 'astro';
function getRenderer() {
return {
name: '@astrojs/preact',
clientEntrypoint: '@astrojs/preact/client',
serverEntrypoint: '@astrojs/preact/server',
clientEntrypoint: '@astrojs/preact/client.js',
serverEntrypoint: '@astrojs/preact/server.js',
jsxImportSource: 'preact',
jsxTransformOptions: async () => {
const {
Expand All @@ -21,8 +21,8 @@ function getRenderer() {
function getViteConfiguration() {
return {
optimizeDeps: {
include: ['@astrojs/preact/client', 'preact', 'preact/jsx-runtime', 'preact-render-to-string'],
exclude: ['@astrojs/preact/server'],
include: ['@astrojs/preact/client.js', 'preact', 'preact/jsx-runtime', 'preact-render-to-string'],
exclude: ['@astrojs/preact/server.js'],
},
ssr: {
external: ['preact-render-to-string'],
Expand Down

0 comments on commit 098f6f6

Please sign in to comment.