Skip to content

Commit

Permalink
Adds astro cli dynamic imports (#5292)
Browse files Browse the repository at this point in the history
* change add, build, preview, devServer and telemtryHandler imports to dynamic

* update changelog for astro cli imports change
  • Loading branch information
MontelAle authored Nov 4, 2022
1 parent b4f77a6 commit 97e2b6a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-beds-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Changes slow astro cli imports to dynamic
15 changes: 10 additions & 5 deletions packages/astro/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,22 @@ import { pathToFileURL } from 'url';
import type { Arguments as Flags } from 'yargs-parser';
import yargs from 'yargs-parser';
import { z } from 'zod';
import add from '../core/add/index.js';
import build from '../core/build/index.js';
import {
createSettings,
openConfig,
resolveConfigPath,
resolveFlags,
} from '../core/config/index.js';
import { ASTRO_VERSION } from '../core/constants.js';
import devServer from '../core/dev/index.js';
import { collectErrorMetadata } from '../core/errors/dev/index.js';
import { createSafeError } from '../core/errors/index.js';
import { debug, error, info, LogOptions } from '../core/logger/core.js';
import { enableVerboseLogging, nodeLogDestination } from '../core/logger/node.js';
import { formatConfigErrorMessage, formatErrorMessage, printHelp } from '../core/messages.js';
import preview from '../core/preview/index.js';
import * as event from '../events/index.js';
import { eventConfigError, eventError, telemetry } from '../events/index.js';
import { check } from './check/index.js';
import { openInBrowser } from './open.js';
import * as telemetryHandler from './telemetry.js';

type Arguments = yargs.Arguments;
type CLICommand =
Expand Down Expand Up @@ -140,6 +135,8 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
//
switch (cmd) {
case 'add': {
const { default: add } = await import('../core/add/index.js');

telemetry.record(event.eventCliSession(cmd));
const packages = flags._.slice(3) as string[];
return await add(packages, { cwd: root, flags, logging, telemetry });
Expand All @@ -149,6 +146,8 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
return await openInBrowser('https://docs.astro.build/');
}
case 'telemetry': {
const telemetryHandler = await import('./telemetry.js');

// Do not track session start, since the user may be trying to enable,
// disable, or modify telemetry settings.
const subcommand = flags._[3]?.toString();
Expand All @@ -174,6 +173,8 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
// by the end of this switch statement.
switch (cmd) {
case 'dev': {
const { default: devServer } = await import('../core/dev/index.js');

const configFlag = resolveFlags(flags).config;
const configFlagPath = configFlag ? await resolveConfigPath({ cwd: root, flags }) : undefined;

Expand All @@ -191,6 +192,8 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
}

case 'build': {
const { default: build } = await import('../core/build/index.js');

return await build(settings, { ...flags, logging, telemetry });
}

Expand All @@ -200,6 +203,8 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
}

case 'preview': {
const { default: preview } = await import('../core/preview/index.js');

const server = await preview(settings, { logging, telemetry });
return await server.closed(); // keep alive until the server is closed
}
Expand Down

0 comments on commit 97e2b6a

Please sign in to comment.