Skip to content

Commit 4f06150

Browse files
committed
Skip emitFile() in astro:scripts plugin during serve mode
Guard the buildStart hook in vite-plugin-scripts to skip this.emitFile() when Vite is running in serve mode. emitFile() is a Rollup build-time API that Vite stubs out during dev, producing a spurious warning: [WARN] [vite] [plugin:astro:scripts] context method emitFile() is not supported in serve mode. The fix captures the Vite command via the config hook and returns early from buildStart when command === 'serve'. Fixes #16026 Fixes #15975
1 parent b089b90 commit 4f06150

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes the `emitFile() is not supported in serve mode` warning that appears during `astro dev` when using integrations that inject before-hydration scripts (e.g. `@astrojs/react`)

packages/astro/src/vite-plugin-scripts/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Plugin as VitePlugin } from 'vite';
1+
import type { Plugin as VitePlugin, ConfigEnv } from 'vite';
22
import type { AstroSettings } from '../types/astro.js';
33
import type { InjectedScriptStage } from '../types/public/integrations.js';
44
import { ASTRO_VITE_ENVIRONMENT_NAMES } from '../core/constants.js';
@@ -14,8 +14,12 @@ export const PAGE_SCRIPT_ID = `${SCRIPT_ID_PREFIX}${'page' as InjectedScriptStag
1414
export const PAGE_SSR_SCRIPT_ID = `${SCRIPT_ID_PREFIX}${'page-ssr' as InjectedScriptStage}.js`;
1515

1616
export default function astroScriptsPlugin({ settings }: { settings: AstroSettings }): VitePlugin {
17+
let command: 'build' | 'serve';
1718
return {
1819
name: 'astro:scripts',
20+
config(_: any, env: ConfigEnv) {
21+
command = env.command;
22+
},
1923

2024
resolveId: {
2125
filter: {
@@ -58,6 +62,7 @@ export default function astroScriptsPlugin({ settings }: { settings: AstroSettin
5862
},
5963
},
6064
buildStart() {
65+
if (command === 'serve') return;
6166
const hasHydrationScripts = settings.scripts.some((s) => s.stage === 'before-hydration');
6267
if (
6368
hasHydrationScripts &&

0 commit comments

Comments
 (0)