From 7c5fcd2fa817472f480bbfbbc11b9ed71a7210ab Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Thu, 22 Feb 2024 18:36:06 +0800 Subject: [PATCH] Improve `optimizeDeps.entries` to avoid server endpoints (#10143) --- .changeset/funny-bananas-switch.md | 5 +++++ packages/astro/src/core/create-vite.ts | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 .changeset/funny-bananas-switch.md diff --git a/.changeset/funny-bananas-switch.md b/.changeset/funny-bananas-switch.md new file mode 100644 index 000000000000..665c32804aa4 --- /dev/null +++ b/.changeset/funny-bananas-switch.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Improves the default `optimizeDeps.entries` Vite config to avoid globbing server endpoints, and respect the `srcDir` option diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index b2de6afb5c3b..662644fd3762 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -1,5 +1,6 @@ import nodeFs from 'node:fs'; import { fileURLToPath } from 'node:url'; +import glob from 'fast-glob'; import * as vite from 'vite'; import { crawlFrameworkPkgs } from 'vitefu'; 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 const commonConfig: vite.InlineConfig = { // 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), appType: 'custom', 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'], }, plugins: [