Skip to content

Commit

Permalink
Make 'lit' always be bundled in SSR (#3164)
Browse files Browse the repository at this point in the history
* Make 'lit' always be bundled in SSR

* Adds a changeset
  • Loading branch information
matthewp committed Apr 21, 2022
1 parent add650a commit e85b16e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
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')
}
}
},
},
};
}

0 comments on commit e85b16e

Please sign in to comment.