Skip to content

Commit

Permalink
fix: only add to ssr.noExternal if present in project
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Jun 28, 2022
1 parent 7645cda commit bec0941
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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,
Expand Down Expand Up @@ -102,7 +117,7 @@ export async function createVite(
],
},
ssr: {
noExternal: [...ALWAYS_NOEXTERNAL],
noExternal: getSsrNoExternalDeps(astroConfig.root),
}
};

Expand Down

0 comments on commit bec0941

Please sign in to comment.