Skip to content

Commit

Permalink
feat: add new --viteMode cli flag for dev and build commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksim Nazarev authored and acupofspirt committed Apr 25, 2024
1 parent a10ed2a commit 6be3432
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/bright-zebras-end.md
@@ -0,0 +1,6 @@
---
"astro": minor
"@astrojs/vercel": patch
---

add new --viteMode cli flag for dev and build commands
7 changes: 7 additions & 0 deletions packages/astro/src/@types/astro.ts
Expand Up @@ -1992,6 +1992,13 @@ export interface AstroInlineOnlyConfig {
* The mode used when building your site to generate either "development" or "production" code.
*/
mode?: RuntimeMode;
/**
* The mode used by Vite. It also defines `import.meta.env.MODE` value.
* If it is unset the default value will be `'development'` for dev command and `'production'` for build command.
*
* @see [Vite mode documentation](https://vitejs.dev/guide/env-and-mode#modes)
*/
viteMode?: string;
/**
* The logging level to filter messages logged by Astro.
* - "debug": Log everything, including noisy debugging diagnostics.
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/cli/build/index.ts
Expand Up @@ -15,6 +15,7 @@ export async function build({ flags }: BuildOptions) {
tables: {
Flags: [
['--outDir <directory>', `Specify the output directory for the build.`],
['--viteMode <mode>', `Specify mode for Vite config.`],
['--help (-h)', 'See all available flags.'],
],
},
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/cli/dev/index.ts
Expand Up @@ -19,6 +19,7 @@ export async function dev({ flags }: DevOptions) {
['--host', `Listen on all addresses, including LAN and public addresses.`],
['--host <custom-address>', `Expose on a network IP address at <custom-address>`],
['--open', 'Automatically open the app in the browser on server start'],
['--viteMode <mode>', `Specify mode for Vite config.`],
['--help (-h)', 'See all available flags.'],
],
},
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/cli/flags.ts
Expand Up @@ -8,6 +8,7 @@ export function flagsToAstroInlineConfig(flags: Flags): AstroInlineConfig {
// Inline-only configs
configFile: typeof flags.config === 'string' ? flags.config : undefined,
mode: typeof flags.mode === 'string' ? (flags.mode as AstroInlineConfig['mode']) : undefined,
viteMode: typeof flags.viteMode === 'string' ? flags.viteMode : undefined,
logLevel: flags.verbose ? 'debug' : flags.silent ? 'silent' : undefined,

// Astro user configs
Expand Down
Expand Up @@ -46,7 +46,7 @@ export function astroContentVirtualModPlugin({
name: 'astro-content-virtual-mod-plugin',
enforce: 'pre',
configResolved(config) {
IS_DEV = config.mode === 'development';
IS_DEV = config.command !== 'build';
},
resolveId(id) {
if (id === VIRTUAL_MODULE_ID) {
Expand Down
8 changes: 7 additions & 1 deletion packages/astro/src/core/build/index.ts
Expand Up @@ -83,19 +83,22 @@ export default async function build(
...options,
logger,
mode: inlineConfig.mode,
viteMode: inlineConfig.viteMode,
});
await builder.run();
}

interface AstroBuilderOptions extends BuildOptions {
logger: Logger;
mode?: RuntimeMode;
viteMode?: string
}

class AstroBuilder {
private settings: AstroSettings;
private logger: Logger;
private mode: RuntimeMode = 'production';
private viteMode = 'production'
private origin: string;
private manifest: ManifestData;
private timer: Record<string, number>;
Expand All @@ -105,6 +108,9 @@ class AstroBuilder {
if (options.mode) {
this.mode = options.mode;
}
if (options.viteMode) {
this.viteMode = options.viteMode;
}
this.settings = settings;
this.logger = options.logger;
this.teardownCompiler = options.teardownCompiler ?? true;
Expand Down Expand Up @@ -134,7 +140,7 @@ class AstroBuilder {

const viteConfig = await createVite(
{
mode: this.mode,
mode: this.viteMode,
server: {
hmr: false,
middlewareMode: true,
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/config/config.ts
Expand Up @@ -165,12 +165,13 @@ function splitInlineConfig(inlineConfig: AstroInlineConfig): {
inlineUserConfig: AstroUserConfig;
inlineOnlyConfig: AstroInlineOnlyConfig;
} {
const { configFile, mode, logLevel, ...inlineUserConfig } = inlineConfig;
const { configFile, mode, viteMode, logLevel, ...inlineUserConfig } = inlineConfig;
return {
inlineUserConfig,
inlineOnlyConfig: {
configFile,
mode,
viteMode,
logLevel,
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/dev/container.ts
Expand Up @@ -68,7 +68,7 @@ export async function createContainer({

const viteConfig = await createVite(
{
mode: 'development',
mode: inlineConfig?.viteMode || 'development',
server: { host, headers, open },
optimizeDeps: {
include: rendererClientEntries,
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/test/fixtures/set-html/src/pages/fetch.astro
@@ -1,6 +1,6 @@
---
// This is a dev only test
const mode = import.meta.env.MODE;
const isDev = import.meta.env.DEV;
---
<html>
<head>
Expand All @@ -9,7 +9,7 @@ const mode = import.meta.env.MODE;
<body>
<h1>Testing</h1>
<div id="fetch" set:html={
mode === 'development' ?
isDev ?
// @ts-ignore
TEST_FETCH(fetch, '/test.html') :
'build mode'
Expand Down
18 changes: 18 additions & 0 deletions packages/astro/test/units/dev/dev.test.js
Expand Up @@ -273,4 +273,22 @@ describe('dev container', () => {
assert.equal(r.res.statusCode, 200);
});
});

it('overrides Vite mode when --viteMode flag is specified', async () => {
await runInContainer({ inlineConfig: { viteMode: 'production' } }, (container) => {
assert.equal(container.viteServer.config.mode, 'production');
});
await runInContainer({ inlineConfig: { viteMode: 'development' } }, (container) => {
assert.equal(container.viteServer.config.mode, 'development');
});
await runInContainer({ inlineConfig: { viteMode: 'staging' } }, (container) => {
assert.equal(container.viteServer.config.mode, 'staging');
});
});

it('uses "development" as Vite mode when --viteMode flag is not specified', async () => {
await runInContainer({}, (container) => {
assert.equal(container.viteServer.config.mode, 'development');
});
});
});
Expand Up @@ -4,7 +4,7 @@ export default function() {
let player = undefined;
// This is tested in dev mode, so make it work during the build to prevent
// breaking other tests.
if(import.meta.env.MODE === 'production') {
if(import.meta.env.PROD) {
player = {};
}
const [] = useState(player.currentTime || null);
Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/vercel/src/speed-insights.ts
Expand Up @@ -58,8 +58,8 @@ function collectWebVitals() {
}
}

const mode = (import.meta as any).env.MODE as 'development' | 'production';
const isProduction = (import.meta as any).env.PROD;

if (mode === 'production') {
if (isProduction) {
collectWebVitals();
}

0 comments on commit 6be3432

Please sign in to comment.