Skip to content

Commit

Permalink
Improve optimizeDeps.entries to avoid server endpoints (#10143)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Feb 22, 2024
1 parent 2c25192 commit 7c5fcd2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/funny-bananas-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Improves the default `optimizeDeps.entries` Vite config to avoid globbing server endpoints, and respect the `srcDir` option
11 changes: 10 additions & 1 deletion packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -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: [
Expand Down

0 comments on commit 7c5fcd2

Please sign in to comment.