diff --git a/.changeset/six-bottles-mix.md b/.changeset/six-bottles-mix.md new file mode 100644 index 000000000000..d010104a698f --- /dev/null +++ b/.changeset/six-bottles-mix.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Sets correct `process.env.NODE_ENV` default when using the JS API diff --git a/packages/astro/src/cli/add/index.ts b/packages/astro/src/cli/add/index.ts index 5b65106cca86..1240e67deed0 100644 --- a/packages/astro/src/cli/add/index.ts +++ b/packages/astro/src/cli/add/index.ts @@ -27,7 +27,7 @@ import * as msg from '../../core/messages.js'; import { printHelp } from '../../core/messages.js'; import { appendForwardSlash } from '../../core/path.js'; import { apply as applyPolyfill } from '../../core/polyfill.js'; -import { parseNpmName } from '../../core/util.js'; +import { ensureProcessNodeEnv, parseNpmName } from '../../core/util.js'; import { eventCliSession, telemetry } from '../../events/index.js'; import { createLoggerFromFlags, flagsToAstroInlineConfig } from '../flags.js'; import { generate, parse, t, visit } from './babel.js'; @@ -92,6 +92,7 @@ async function getRegistry(): Promise { } export async function add(names: string[], { flags }: AddOptions) { + ensureProcessNodeEnv('production'); const inlineConfig = flagsToAstroInlineConfig(flags); const { userConfig } = await resolveConfig(inlineConfig, 'add'); telemetry.record(eventCliSession('add', userConfig)); diff --git a/packages/astro/src/cli/check/index.ts b/packages/astro/src/cli/check/index.ts index 6ce3bf71ecb6..faf07b776b9f 100644 --- a/packages/astro/src/cli/check/index.ts +++ b/packages/astro/src/cli/check/index.ts @@ -2,8 +2,10 @@ import path from 'node:path'; import type { Arguments } from 'yargs-parser'; import { createLoggerFromFlags, flagsToAstroInlineConfig } from '../flags.js'; import { getPackage } from '../install-package.js'; +import { ensureProcessNodeEnv } from '../../core/util.js'; export async function check(flags: Arguments) { + ensureProcessNodeEnv('production'); const logger = createLoggerFromFlags(flags); const getPackageOpts = { skipAsk: flags.yes || flags.y, cwd: flags.root }; const checkPackage = await getPackage( diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts index 7ca4d21a9a7b..19347ebecd16 100644 --- a/packages/astro/src/cli/index.ts +++ b/packages/astro/src/cli/index.ts @@ -131,11 +131,6 @@ async function runCommand(cmd: string, flags: yargs.Arguments) { enableVerboseLogging(); } - // Start with a default NODE_ENV so Vite doesn't set an incorrect default when loading the Astro config - if (!process.env.NODE_ENV) { - process.env.NODE_ENV = cmd === 'dev' ? 'development' : 'production'; - } - const { notify } = await import('./telemetry/index.js'); await notify(); diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index 8cb53f3c7c3a..553497bc5b40 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -33,6 +33,7 @@ import { collectPagesData } from './page-data.js'; import { staticBuild, viteBuild } from './static-build.js'; import type { StaticBuildOptions } from './types.js'; import { getTimeStat } from './util.js'; +import { ensureProcessNodeEnv } from '../util.js'; export interface BuildOptions { /** @@ -63,6 +64,7 @@ export default async function build( inlineConfig: AstroInlineConfig, options: BuildOptions = {} ): Promise { + ensureProcessNodeEnv('production'); applyPolyfill(); const logger = createNodeLogger(inlineConfig); const { userConfig, astroConfig } = await resolveConfig(inlineConfig, 'build'); diff --git a/packages/astro/src/core/dev/dev.ts b/packages/astro/src/core/dev/dev.ts index c0c1b9b8ea64..d345c1db6145 100644 --- a/packages/astro/src/core/dev/dev.ts +++ b/packages/astro/src/core/dev/dev.ts @@ -10,6 +10,7 @@ import { telemetry } from '../../events/index.js'; import * as msg from '../messages.js'; import { startContainer } from './container.js'; import { createContainerWithAutomaticRestart } from './restart.js'; +import { ensureProcessNodeEnv } from '../util.js'; export interface DevServer { address: AddressInfo; @@ -25,6 +26,7 @@ export interface DevServer { * @experimental The JavaScript API is experimental */ export default async function dev(inlineConfig: AstroInlineConfig): Promise { + ensureProcessNodeEnv('development'); const devStart = performance.now(); await telemetry.record([]); diff --git a/packages/astro/src/core/preview/index.ts b/packages/astro/src/core/preview/index.ts index 0f65c0c858b7..d5faf8cf27f3 100644 --- a/packages/astro/src/core/preview/index.ts +++ b/packages/astro/src/core/preview/index.ts @@ -10,6 +10,7 @@ import { createNodeLogger } from '../config/logging.js'; import { createSettings } from '../config/settings.js'; import createStaticPreviewServer from './static-preview-server.js'; import { getResolvedHostForHttpServer } from './util.js'; +import { ensureProcessNodeEnv } from '../util.js'; /** * Starts a local server to serve your static dist/ directory. This command is useful for previewing @@ -18,6 +19,7 @@ import { getResolvedHostForHttpServer } from './util.js'; * @experimental The JavaScript API is experimental */ export default async function preview(inlineConfig: AstroInlineConfig): Promise { + ensureProcessNodeEnv('production'); const logger = createNodeLogger(inlineConfig); const { userConfig, astroConfig } = await resolveConfig(inlineConfig ?? {}, 'preview'); telemetry.record(eventCliSession('preview', userConfig)); diff --git a/packages/astro/src/core/sync/index.ts b/packages/astro/src/core/sync/index.ts index 966454845b30..bfaed6c2f9fd 100644 --- a/packages/astro/src/core/sync/index.ts +++ b/packages/astro/src/core/sync/index.ts @@ -17,6 +17,7 @@ import { createSettings } from '../config/settings.js'; import { createVite } from '../create-vite.js'; import { AstroError, AstroErrorData, createSafeError, isAstroError } from '../errors/index.js'; import type { Logger } from '../logger/core.js'; +import { ensureProcessNodeEnv } from '../util.js'; export type ProcessExit = 0 | 1; @@ -41,6 +42,7 @@ export default async function sync( inlineConfig: AstroInlineConfig, options?: SyncOptions ): Promise { + ensureProcessNodeEnv('production'); const logger = createNodeLogger(inlineConfig); const { userConfig, astroConfig } = await resolveConfig(inlineConfig ?? {}, 'sync'); telemetry.record(eventCliSession('sync', userConfig)); diff --git a/packages/astro/src/core/util.ts b/packages/astro/src/core/util.ts index bcfa9a511bbb..4db3549a2a6d 100644 --- a/packages/astro/src/core/util.ts +++ b/packages/astro/src/core/util.ts @@ -231,3 +231,12 @@ export function resolvePath(specifier: string, importer: string) { return specifier; } } + +/** + * Set a default NODE_ENV so Vite doesn't set an incorrect default when loading the Astro config + */ +export function ensureProcessNodeEnv(defaultNodeEnv: string) { + if (!process.env.NODE_ENV) { + process.env.NODE_ENV = defaultNodeEnv; + } +}