Make 'lit' always be bundled in SSR (#3164)
* Make 'lit' always be bundled in SSR * Adds a changeset
This commit is contained in:
parent
add650a47f
commit
e85b16e2b3
4 changed files with 24 additions and 2 deletions
6
.changeset/old-plants-glow.md
Normal file
6
.changeset/old-plants-glow.md
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
'@astrojs/lit': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixes lit when running in SSR
|
|
@ -40,7 +40,9 @@ export function vitePluginPages(opts: StaticBuildOptions, internals: BuildIntern
|
||||||
let rendererItems = '';
|
let rendererItems = '';
|
||||||
for (const renderer of opts.astroConfig._ctx.renderers) {
|
for (const renderer of opts.astroConfig._ctx.renderers) {
|
||||||
const variable = `_renderer${i}`;
|
const variable = `_renderer${i}`;
|
||||||
imports.push(`import ${variable} from '${renderer.serverEntrypoint}';`);
|
// Use unshift so that renderers are imported before user code, in case they set globals
|
||||||
|
// that user code depends on.
|
||||||
|
imports.unshift(`import ${variable} from '${renderer.serverEntrypoint}';`);
|
||||||
rendererItems += `Object.assign(${JSON.stringify(renderer)}, { ssr: ${variable} }),`;
|
rendererItems += `Object.assign(${JSON.stringify(renderer)}, { ssr: ${variable} }),`;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ describe('Lit integration in SSR', () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
it('Is able to load', async () => {
|
it('Is able to load', async () => {
|
||||||
|
delete globalThis.window;
|
||||||
const html = await fetchHTML('/');
|
const html = await fetchHTML('/');
|
||||||
const $ = cheerioLoad(html);
|
const $ = cheerioLoad(html);
|
||||||
expect($('#win').text()).to.equal('function');
|
expect($('#win').text()).to.equal('function');
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { readFileSync } from 'node:fs';
|
import { readFileSync } from 'node:fs';
|
||||||
import type { AstroIntegration } from 'astro';
|
import type { AstroConfig, AstroIntegration } from 'astro';
|
||||||
|
|
||||||
function getViteConfiguration() {
|
function getViteConfiguration() {
|
||||||
return {
|
return {
|
||||||
|
@ -45,6 +45,19 @@ export default function (): AstroIntegration {
|
||||||
vite: getViteConfiguration(),
|
vite: getViteConfiguration(),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
'astro:build:setup': ({ vite, target }) => {
|
||||||
|
if (target === 'server') {
|
||||||
|
if(!vite.ssr) {
|
||||||
|
vite.ssr = {};
|
||||||
|
}
|
||||||
|
if(!vite.ssr.noExternal) {
|
||||||
|
vite.ssr.noExternal = [];
|
||||||
|
}
|
||||||
|
if(Array.isArray(vite.ssr.noExternal)) {
|
||||||
|
vite.ssr.noExternal.push('lit')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue