Fix: is self accepting pt 2 module graph boogaloo (#2872)

* fix: isSelfAccepting Preact error

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

* chore: changeset

* fix: remove ./client and ./server from preact pkg exp
This commit is contained in:
Ben Holmes 2022-03-24 12:28:50 -04:00 committed by GitHub
parent 003df32122
commit 098f6f6b06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 19 deletions

View file

@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/preact': patch
---
Fix `isSelfAccepting` errors when using the Preact integration with the Astro dev server

View file

@ -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 };
}

View file

@ -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"
},

View file

@ -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 {
@ -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'],