fix: only add to ssr.noExternal if present in project

This commit is contained in:
bholmesdev 2022-06-27 18:05:53 -04:00
parent 08820f17ff
commit e3c4baa4f5

View file

@ -14,6 +14,7 @@ import astroIntegrationsContainerPlugin from '../vite-plugin-integrations-contai
import jsxVitePlugin from '../vite-plugin-jsx/index.js';
import markdownVitePlugin from '../vite-plugin-markdown/index.js';
import astroScriptsPlugin from '../vite-plugin-scripts/index.js';
import { resolveDependency } from './util.js';
// note: ssr is still an experimental API hence the type omission from `vite`
export type ViteConfigWithSSR = vite.InlineConfig & { ssr?: vite.SSROptions };
@ -31,6 +32,20 @@ const ALWAYS_NOEXTERNAL = new Set([
'astro/components',
]);
function getSsrNoExternalDeps(projectRoot: URL): string[] {
let noExternalDeps = []
for (const dep of ALWAYS_NOEXTERNAL) {
try {
resolveDependency(dep, projectRoot)
noExternalDeps.push(dep)
} catch {
// ignore dependency if *not* installed / present in your project
// prevents hard error from Vite!
}
}
return noExternalDeps
}
/** Return a common starting point for all Vite actions */
export async function createVite(
commandConfig: ViteConfigWithSSR,
@ -103,7 +118,7 @@ export async function createVite(
conditions: ['astro'],
},
ssr: {
noExternal: [...ALWAYS_NOEXTERNAL],
noExternal: getSsrNoExternalDeps(astroConfig.root),
}
};