Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make 'lit' always be bundled in SSR #3164

Merged
merged 2 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/old-plants-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/lit': patch
---

Fixes lit when running in SSR
4 changes: 3 additions & 1 deletion packages/astro/src/core/build/vite-plugin-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export function vitePluginPages(opts: StaticBuildOptions, internals: BuildIntern
let rendererItems = '';
for (const renderer of opts.astroConfig._ctx.renderers) {
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} }),`;
i++;
}
Expand Down
1 change: 1 addition & 0 deletions packages/astro/test/ssr-lit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('Lit integration in SSR', () => {
}

it('Is able to load', async () => {
delete globalThis.window;
const html = await fetchHTML('/');
const $ = cheerioLoad(html);
expect($('#win').text()).to.equal('function');
Expand Down
15 changes: 14 additions & 1 deletion packages/integrations/lit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'node:fs';
import type { AstroIntegration } from 'astro';
import type { AstroConfig, AstroIntegration } from 'astro';

function getViteConfiguration() {
return {
Expand Down Expand Up @@ -45,6 +45,19 @@ export default function (): AstroIntegration {
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')
}
}
},
},
};
}