From bec094156f01d292835e5faebc01c119e936b380 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Mon, 27 Jun 2022 18:05:53 -0400 Subject: [PATCH] fix: only add to ssr.noExternal if present in project --- packages/astro/src/core/create-vite.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 00e91ff6c9fb..dde97d13df5c 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -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, @@ -102,7 +117,7 @@ export async function createVite( ], }, ssr: { - noExternal: [...ALWAYS_NOEXTERNAL], + noExternal: getSsrNoExternalDeps(astroConfig.root), } };