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:
parent
003df32122
commit
098f6f6b06
4 changed files with 15 additions and 19 deletions
6
.changeset/wise-garlics-bathe.md
Normal file
6
.changeset/wise-garlics-bathe.md
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
'@astrojs/preact': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix `isSelfAccepting` errors when using the Preact integration with the Astro dev server
|
|
@ -40,21 +40,13 @@ export type ComponentPreload = [SSRLoadedRenderer[], ComponentInstance];
|
||||||
export type RenderResponse = { type: 'html'; html: string } | { type: 'response'; response: Response };
|
export type RenderResponse = { type: 'html'; html: string } | { type: 'response'; response: Response };
|
||||||
|
|
||||||
const svelteStylesRE = /svelte\?svelte&type=style/;
|
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> {
|
async function loadRenderer(viteServer: vite.ViteDevServer, renderer: AstroRenderer): Promise<SSRLoadedRenderer> {
|
||||||
const { url } = await viteServer.moduleGraph.ensureEntryFromUrl(renderer.serverEntrypoint);
|
// Vite modules can be out-of-date when using an un-resolved url
|
||||||
|
// We also encountered inconsistencies when using the resolveUrl and resolveId helpers
|
||||||
const cachedRenderer = rendererCache.get(url);
|
// We've found that pulling the ID directly from the urlToModuleMap is the most stable!
|
||||||
if (cachedRenderer) {
|
const id = viteServer.moduleGraph.urlToModuleMap.get(renderer.serverEntrypoint)?.id ?? renderer.serverEntrypoint;
|
||||||
return { ...renderer, ssr: cachedRenderer };
|
const mod = (await viteServer.ssrLoadModule(id)) as { default: SSRLoadedRenderer['ssr'] };
|
||||||
}
|
|
||||||
|
|
||||||
const mod = (await viteServer.ssrLoadModule(url)) as { default: SSRLoadedRenderer['ssr'] };
|
|
||||||
rendererCache.set(url, mod.default);
|
|
||||||
return { ...renderer, ssr: mod.default };
|
return { ...renderer, ssr: mod.default };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,9 +20,7 @@
|
||||||
"homepage": "https://astro.build",
|
"homepage": "https://astro.build",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./dist/index.js",
|
".": "./dist/index.js",
|
||||||
"./client": "./client",
|
|
||||||
"./client.js": "./client.js",
|
"./client.js": "./client.js",
|
||||||
"./server": "./server",
|
|
||||||
"./server.js": "./server.js",
|
"./server.js": "./server.js",
|
||||||
"./package.json": "./package.json"
|
"./package.json": "./package.json"
|
||||||
},
|
},
|
||||||
|
|
|
@ -3,8 +3,8 @@ import { AstroIntegration } from 'astro';
|
||||||
function getRenderer() {
|
function getRenderer() {
|
||||||
return {
|
return {
|
||||||
name: '@astrojs/preact',
|
name: '@astrojs/preact',
|
||||||
clientEntrypoint: '@astrojs/preact/client',
|
clientEntrypoint: '@astrojs/preact/client.js',
|
||||||
serverEntrypoint: '@astrojs/preact/server',
|
serverEntrypoint: '@astrojs/preact/server.js',
|
||||||
jsxImportSource: 'preact',
|
jsxImportSource: 'preact',
|
||||||
jsxTransformOptions: async () => {
|
jsxTransformOptions: async () => {
|
||||||
const {
|
const {
|
||||||
|
@ -21,8 +21,8 @@ function getRenderer() {
|
||||||
function getViteConfiguration() {
|
function getViteConfiguration() {
|
||||||
return {
|
return {
|
||||||
optimizeDeps: {
|
optimizeDeps: {
|
||||||
include: ['@astrojs/preact/client', 'preact', 'preact/jsx-runtime', 'preact-render-to-string'],
|
include: ['@astrojs/preact/client.js', 'preact', 'preact/jsx-runtime', 'preact-render-to-string'],
|
||||||
exclude: ['@astrojs/preact/server'],
|
exclude: ['@astrojs/preact/server.js'],
|
||||||
},
|
},
|
||||||
ssr: {
|
ssr: {
|
||||||
external: ['preact-render-to-string'],
|
external: ['preact-render-to-string'],
|
||||||
|
|
Loading…
Reference in a new issue