2022-06-06 16:49:53 +00:00
|
|
|
import type { AstroIntegration } from 'astro';
|
2022-03-18 22:35:45 +00:00
|
|
|
import { readFileSync } from 'node:fs';
|
|
|
|
|
|
|
|
function getViteConfiguration() {
|
|
|
|
return {
|
|
|
|
optimizeDeps: {
|
|
|
|
include: [
|
|
|
|
'@astrojs/lit/client-shim.js',
|
|
|
|
'@astrojs/lit/hydration-support.js',
|
|
|
|
'@webcomponents/template-shadowroot/template-shadowroot.js',
|
|
|
|
'lit/experimental-hydrate-support.js',
|
|
|
|
],
|
|
|
|
exclude: ['@astrojs/lit/server.js'],
|
|
|
|
},
|
|
|
|
ssr: {
|
|
|
|
external: [
|
|
|
|
'lit-element/lit-element.js',
|
|
|
|
'@lit-labs/ssr/lib/install-global-dom-shim.js',
|
|
|
|
'@lit-labs/ssr/lib/render-lit-html.js',
|
|
|
|
'@lit-labs/ssr/lib/lit-element-renderer.js',
|
2022-06-21 12:32:05 +00:00
|
|
|
'@astrojs/lit/server.js'
|
2022-03-18 22:35:45 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function (): AstroIntegration {
|
|
|
|
return {
|
|
|
|
name: '@astrojs/lit',
|
|
|
|
hooks: {
|
|
|
|
'astro:config:setup': ({ updateConfig, addRenderer, injectScript }) => {
|
|
|
|
// Inject the necessary polyfills on every page (inlined for speed).
|
|
|
|
injectScript(
|
|
|
|
'head-inline',
|
|
|
|
readFileSync(new URL('../client-shim.min.js', import.meta.url), { encoding: 'utf-8' })
|
|
|
|
);
|
|
|
|
// Inject the hydration code, before a component is hydrated.
|
|
|
|
injectScript('before-hydration', `import '@astrojs/lit/hydration-support.js';`);
|
|
|
|
// Add the lit renderer so that Astro can understand lit components.
|
|
|
|
addRenderer({
|
|
|
|
name: '@astrojs/lit',
|
|
|
|
serverEntrypoint: '@astrojs/lit/server.js',
|
|
|
|
});
|
|
|
|
// Update the vite configuration.
|
|
|
|
updateConfig({
|
|
|
|
vite: getViteConfiguration(),
|
|
|
|
});
|
|
|
|
},
|
2022-04-21 15:12:21 +00:00
|
|
|
'astro:build:setup': ({ vite, target }) => {
|
|
|
|
if (target === 'server') {
|
2022-04-21 15:13:24 +00:00
|
|
|
if (!vite.ssr) {
|
2022-04-21 15:12:21 +00:00
|
|
|
vite.ssr = {};
|
|
|
|
}
|
2022-04-21 15:13:24 +00:00
|
|
|
if (!vite.ssr.noExternal) {
|
2022-04-21 15:12:21 +00:00
|
|
|
vite.ssr.noExternal = [];
|
|
|
|
}
|
2022-04-21 15:13:24 +00:00
|
|
|
if (Array.isArray(vite.ssr.noExternal)) {
|
|
|
|
vite.ssr.noExternal.push('lit');
|
2022-04-21 15:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-03-18 22:35:45 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|