hail mary: destroy all ssr external / noexternal!
This commit is contained in:
parent
257bdbf421
commit
90df3c9a85
1 changed files with 0 additions and 115 deletions
|
@ -15,28 +15,6 @@ import jsxVitePlugin from '../vite-plugin-jsx/index.js';
|
||||||
import markdownVitePlugin from '../vite-plugin-markdown/index.js';
|
import markdownVitePlugin from '../vite-plugin-markdown/index.js';
|
||||||
import astroScriptsPlugin from '../vite-plugin-scripts/index.js';
|
import astroScriptsPlugin from '../vite-plugin-scripts/index.js';
|
||||||
|
|
||||||
// Some packages are just external, and that’s the way it goes.
|
|
||||||
const ALWAYS_EXTERNAL = new Set([
|
|
||||||
...builtinModules.map((name) => `node:${name}`),
|
|
||||||
'@sveltejs/vite-plugin-svelte',
|
|
||||||
'micromark-util-events-to-acorn',
|
|
||||||
'@astrojs/markdown-remark',
|
|
||||||
// in-lined for markdown modules
|
|
||||||
'github-slugger',
|
|
||||||
'node-fetch',
|
|
||||||
'prismjs',
|
|
||||||
'shiki',
|
|
||||||
'unified',
|
|
||||||
'whatwg-url',
|
|
||||||
]);
|
|
||||||
const ALWAYS_NOEXTERNAL = new Set([
|
|
||||||
// This is only because Vite's native ESM doesn't resolve "exports" correctly.
|
|
||||||
'astro',
|
|
||||||
// Handle recommended nanostores. Only @nanostores/preact is required from our testing!
|
|
||||||
// Full explanation and related bug report: https://github.com/withastro/astro/pull/3667
|
|
||||||
'@nanostores/preact',
|
|
||||||
]);
|
|
||||||
|
|
||||||
// note: ssr is still an experimental API hence the type omission from `vite`
|
// note: ssr is still an experimental API hence the type omission from `vite`
|
||||||
export type ViteConfigWithSSR = vite.InlineConfig & { ssr?: vite.SSROptions };
|
export type ViteConfigWithSSR = vite.InlineConfig & { ssr?: vite.SSROptions };
|
||||||
|
|
||||||
|
@ -51,8 +29,6 @@ export async function createVite(
|
||||||
commandConfig: ViteConfigWithSSR,
|
commandConfig: ViteConfigWithSSR,
|
||||||
{ astroConfig, logging, mode }: CreateViteOptions
|
{ astroConfig, logging, mode }: CreateViteOptions
|
||||||
): Promise<ViteConfigWithSSR> {
|
): Promise<ViteConfigWithSSR> {
|
||||||
// Scan for any third-party Astro packages. Vite needs these to be passed to `ssr.noExternal`.
|
|
||||||
const astroPackages = await getAstroPackages(astroConfig);
|
|
||||||
// Start with the Vite configuration that Astro core needs
|
// Start with the Vite configuration that Astro core needs
|
||||||
const commonConfig: ViteConfigWithSSR = {
|
const commonConfig: ViteConfigWithSSR = {
|
||||||
cacheDir: fileURLToPath(new URL('./node_modules/.vite/', astroConfig.root)), // using local caches allows Astro to be used in monorepos, etc.
|
cacheDir: fileURLToPath(new URL('./node_modules/.vite/', astroConfig.root)), // using local caches allows Astro to be used in monorepos, etc.
|
||||||
|
@ -115,11 +91,6 @@ export async function createVite(
|
||||||
],
|
],
|
||||||
conditions: ['astro'],
|
conditions: ['astro'],
|
||||||
},
|
},
|
||||||
// Note: SSR API is in beta (https://vitejs.dev/guide/ssr.html)
|
|
||||||
ssr: {
|
|
||||||
external: [...ALWAYS_EXTERNAL],
|
|
||||||
noExternal: [...ALWAYS_NOEXTERNAL, ...astroPackages],
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Merge configs: we merge vite configuration objects together in the following order,
|
// Merge configs: we merge vite configuration objects together in the following order,
|
||||||
|
@ -153,89 +124,3 @@ function sortPlugins(result: ViteConfigWithSSR) {
|
||||||
result.plugins?.splice(mdxPluginIndex, 1);
|
result.plugins?.splice(mdxPluginIndex, 1);
|
||||||
result.plugins?.splice(jsxPluginIndex, 0, mdxPlugin);
|
result.plugins?.splice(jsxPluginIndex, 0, mdxPlugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scans `projectRoot` for third-party Astro packages that could export an `.astro` file
|
|
||||||
// `.astro` files need to be built by Vite, so these should use `noExternal`
|
|
||||||
async function getAstroPackages({ root }: AstroConfig): Promise<string[]> {
|
|
||||||
const pkgUrl = new URL('./package.json', root);
|
|
||||||
const pkgPath = fileURLToPath(pkgUrl);
|
|
||||||
if (!fs.existsSync(pkgPath)) return [];
|
|
||||||
|
|
||||||
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
||||||
|
|
||||||
const deps = [...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.devDependencies || {})];
|
|
||||||
|
|
||||||
return deps.filter((dep) => {
|
|
||||||
// Attempt: package is common and not Astro. ❌ Skip these for perf
|
|
||||||
if (isCommonNotAstro(dep)) return false;
|
|
||||||
// Attempt: package is named `astro-something`. ✅ Likely a community package
|
|
||||||
if (/^astro\-/.test(dep)) return true;
|
|
||||||
const depPkgUrl = new URL(`./node_modules/${dep}/package.json`, root);
|
|
||||||
const depPkgPath = fileURLToPath(depPkgUrl);
|
|
||||||
if (!fs.existsSync(depPkgPath)) return false;
|
|
||||||
|
|
||||||
const {
|
|
||||||
dependencies = {},
|
|
||||||
peerDependencies = {},
|
|
||||||
keywords = [],
|
|
||||||
} = JSON.parse(fs.readFileSync(depPkgPath, 'utf-8'));
|
|
||||||
// Attempt: package relies on `astro`. ✅ Definitely an Astro package
|
|
||||||
if (peerDependencies.astro || dependencies.astro) return true;
|
|
||||||
// Attempt: package is tagged with `astro` or `astro-component`. ✅ Likely a community package
|
|
||||||
if (keywords.includes('astro') || keywords.includes('astro-component')) return true;
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const COMMON_DEPENDENCIES_NOT_ASTRO = [
|
|
||||||
'autoprefixer',
|
|
||||||
'react',
|
|
||||||
'react-dom',
|
|
||||||
'preact',
|
|
||||||
'preact-render-to-string',
|
|
||||||
'vue',
|
|
||||||
'svelte',
|
|
||||||
'solid-js',
|
|
||||||
'lit',
|
|
||||||
'cookie',
|
|
||||||
'dotenv',
|
|
||||||
'esbuild',
|
|
||||||
'eslint',
|
|
||||||
'jest',
|
|
||||||
'postcss',
|
|
||||||
'prettier',
|
|
||||||
'astro',
|
|
||||||
'tslib',
|
|
||||||
'typescript',
|
|
||||||
'vite',
|
|
||||||
];
|
|
||||||
|
|
||||||
const COMMON_PREFIXES_NOT_ASTRO = [
|
|
||||||
'@webcomponents/',
|
|
||||||
'@fontsource/',
|
|
||||||
'@postcss-plugins/',
|
|
||||||
'@rollup/',
|
|
||||||
'@astrojs/renderer-',
|
|
||||||
'@types/',
|
|
||||||
'@typescript-eslint/',
|
|
||||||
'eslint-',
|
|
||||||
'jest-',
|
|
||||||
'postcss-plugin-',
|
|
||||||
'prettier-plugin-',
|
|
||||||
'remark-',
|
|
||||||
'rehype-',
|
|
||||||
'rollup-plugin-',
|
|
||||||
'vite-plugin-',
|
|
||||||
];
|
|
||||||
|
|
||||||
function isCommonNotAstro(dep: string): boolean {
|
|
||||||
return (
|
|
||||||
COMMON_DEPENDENCIES_NOT_ASTRO.includes(dep) ||
|
|
||||||
COMMON_PREFIXES_NOT_ASTRO.some(
|
|
||||||
(prefix) =>
|
|
||||||
prefix.startsWith('@')
|
|
||||||
? dep.startsWith(prefix)
|
|
||||||
: dep.substring(dep.lastIndexOf('/') + 1).startsWith(prefix) // check prefix omitting @scope/
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue