0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Improve optimizeDeps.entries to avoid server endpoints (#10143)

This commit is contained in:
Bjorn Lu 2024-02-22 18:36:06 +08:00 committed by GitHub
parent 2c2519204a
commit 7c5fcd2fa8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Improves the default `optimizeDeps.entries` Vite config to avoid globbing server endpoints, and respect the `srcDir` option

View file

@ -1,5 +1,6 @@
import nodeFs from 'node:fs'; import nodeFs from 'node:fs';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import glob from 'fast-glob';
import * as vite from 'vite'; import * as vite from 'vite';
import { crawlFrameworkPkgs } from 'vitefu'; import { crawlFrameworkPkgs } from 'vitefu';
import type { AstroSettings } from '../@types/astro.js'; import type { AstroSettings } from '../@types/astro.js';
@ -103,6 +104,8 @@ export async function createVite(
}, },
}); });
const srcDirPattern = glob.convertPathToPattern(fileURLToPath(settings.config.srcDir));
// Start with the Vite configuration that Astro core needs // Start with the Vite configuration that Astro core needs
const commonConfig: vite.InlineConfig = { const commonConfig: vite.InlineConfig = {
// Tell Vite not to combine config from vite.config.js with our provided inline config // Tell Vite not to combine config from vite.config.js with our provided inline config
@ -112,7 +115,13 @@ export async function createVite(
customLogger: createViteLogger(logger, settings.config.vite.logLevel), customLogger: createViteLogger(logger, settings.config.vite.logLevel),
appType: 'custom', appType: 'custom',
optimizeDeps: { optimizeDeps: {
entries: ['src/**/*'], // Scan all files within `srcDir` except for known server-code (e.g endpoints)
entries: [
`${srcDirPattern}!(pages)/**/*`, // All files except for pages
`${srcDirPattern}pages/**/!(*.js|*.mjs|*.ts|*.mts)`, // All pages except for endpoints
`${srcDirPattern}pages/**/_*.{js,mjs,ts,mts}`, // Remaining JS/TS files prefixed with `_` (not endpoints)
`${srcDirPattern}pages/**/_*/**/*.{js,mjs,ts,mts}`, // Remaining JS/TS files within directories prefixed with `_` (not endpoints)
],
exclude: ['astro', 'node-fetch'], exclude: ['astro', 'node-fetch'],
}, },
plugins: [ plugins: [