diff options
author | 2024-02-22 18:36:06 +0800 | |
---|---|---|
committer | 2024-02-22 18:36:06 +0800 | |
commit | 7c5fcd2fa817472f480bbfbbc11b9ed71a7210ab (patch) | |
tree | 2700c03f2f1da30ea0e38a2a67b438f7820a13e0 | |
parent | 2c2519204a26a522cdaa4e17cac3aa4dbcb66056 (diff) | |
download | astro-7c5fcd2fa817472f480bbfbbc11b9ed71a7210ab.tar.gz astro-7c5fcd2fa817472f480bbfbbc11b9ed71a7210ab.tar.zst astro-7c5fcd2fa817472f480bbfbbc11b9ed71a7210ab.zip |
Improve `optimizeDeps.entries` to avoid server endpoints (#10143)
-rw-r--r-- | .changeset/funny-bananas-switch.md | 5 | ||||
-rw-r--r-- | packages/astro/src/core/create-vite.ts | 11 |
2 files changed, 15 insertions, 1 deletions
diff --git a/.changeset/funny-bananas-switch.md b/.changeset/funny-bananas-switch.md new file mode 100644 index 000000000..665c32804 --- /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 b2de6afb5..662644fd3 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: [ |