Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default NODE_ENV for JS API #9575

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/six-bottles-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Sets correct `process.env.NODE_ENV` default when using the JS API
3 changes: 2 additions & 1 deletion packages/astro/src/cli/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -92,6 +92,7 @@ async function getRegistry(): Promise<string> {
}

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));
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/cli/check/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof import('@astrojs/check')>(
Expand Down
5 changes: 0 additions & 5 deletions packages/astro/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -63,6 +64,7 @@ export default async function build(
inlineConfig: AstroInlineConfig,
options: BuildOptions = {}
): Promise<void> {
ensureProcessNodeEnv('production');
applyPolyfill();
const logger = createNodeLogger(inlineConfig);
const { userConfig, astroConfig } = await resolveConfig(inlineConfig, 'build');
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +26,7 @@ export interface DevServer {
* @experimental The JavaScript API is experimental
*/
export default async function dev(inlineConfig: AstroInlineConfig): Promise<DevServer> {
ensureProcessNodeEnv('development');
const devStart = performance.now();
await telemetry.record([]);

Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +19,7 @@ import { getResolvedHostForHttpServer } from './util.js';
* @experimental The JavaScript API is experimental
*/
export default async function preview(inlineConfig: AstroInlineConfig): Promise<PreviewServer> {
ensureProcessNodeEnv('production');
const logger = createNodeLogger(inlineConfig);
const { userConfig, astroConfig } = await resolveConfig(inlineConfig ?? {}, 'preview');
telemetry.record(eventCliSession('preview', userConfig));
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -41,6 +42,7 @@ export default async function sync(
inlineConfig: AstroInlineConfig,
options?: SyncOptions
): Promise<ProcessExit> {
ensureProcessNodeEnv('production');
const logger = createNodeLogger(inlineConfig);
const { userConfig, astroConfig } = await resolveConfig(inlineConfig ?? {}, 'sync');
telemetry.record(eventCliSession('sync', userConfig));
Expand Down
9 changes: 9 additions & 0 deletions packages/astro/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Loading