astro 2.9.6 → 2.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/components/ViewTransitions.astro +65 -12
  2. package/dist/@types/astro.d.ts +13 -1
  3. package/dist/assets/vite-plugin-assets.js +1 -1
  4. package/dist/cli/add/index.d.ts +1 -3
  5. package/dist/cli/add/index.js +11 -4
  6. package/dist/cli/build/index.d.ts +1 -3
  7. package/dist/cli/build/index.js +19 -9
  8. package/dist/cli/check/index.d.ts +4 -9
  9. package/dist/cli/check/index.js +23 -11
  10. package/dist/cli/dev/index.d.ts +1 -3
  11. package/dist/cli/dev/index.js +24 -20
  12. package/dist/cli/flags.d.ts +9 -0
  13. package/dist/cli/flags.js +40 -0
  14. package/dist/cli/index.js +7 -14
  15. package/dist/cli/info/index.js +4 -6
  16. package/dist/cli/preview/index.d.ts +1 -3
  17. package/dist/cli/preview/index.js +21 -5
  18. package/dist/cli/sync/index.d.ts +1 -3
  19. package/dist/cli/sync/index.js +17 -8
  20. package/dist/cli/throw-and-exit.js +3 -0
  21. package/dist/config/index.js +2 -2
  22. package/dist/content/vite-plugin-content-virtual-mod.js +1 -1
  23. package/dist/core/app/index.d.ts +6 -1
  24. package/dist/core/app/index.js +84 -62
  25. package/dist/core/build/index.d.ts +2 -7
  26. package/dist/core/build/index.js +18 -20
  27. package/dist/core/compile/compile.js +1 -0
  28. package/dist/core/config/config.d.ts +6 -22
  29. package/dist/core/config/config.js +55 -54
  30. package/dist/core/config/index.d.ts +3 -2
  31. package/dist/core/config/index.js +6 -14
  32. package/dist/core/config/logging.d.ts +3 -0
  33. package/dist/core/config/logging.js +12 -0
  34. package/dist/core/config/settings.d.ts +1 -2
  35. package/dist/core/config/settings.js +0 -9
  36. package/dist/core/constants.js +1 -1
  37. package/dist/core/dev/container.d.ts +6 -17
  38. package/dist/core/dev/container.js +8 -32
  39. package/dist/core/dev/dev.d.ts +2 -12
  40. package/dist/core/dev/dev.js +12 -43
  41. package/dist/core/dev/index.d.ts +1 -1
  42. package/dist/core/dev/index.js +1 -3
  43. package/dist/core/dev/restart.d.ts +9 -18
  44. package/dist/core/dev/restart.js +49 -74
  45. package/dist/core/errors/errors.d.ts +10 -0
  46. package/dist/core/errors/errors.js +10 -1
  47. package/dist/core/messages.js +2 -2
  48. package/dist/core/preview/index.d.ts +2 -9
  49. package/dist/core/preview/index.js +12 -21
  50. package/dist/core/sync/index.d.ts +14 -10
  51. package/dist/core/sync/index.js +19 -20
  52. package/dist/core/util.js +2 -2
  53. package/dist/runtime/server/astro-island.js +16 -1
  54. package/dist/runtime/server/astro-island.prebuilt.d.ts +1 -1
  55. package/dist/runtime/server/astro-island.prebuilt.js +1 -1
  56. package/dist/runtime/server/hydration.js +9 -0
  57. package/dist/runtime/server/render/component.js +11 -8
  58. package/dist/runtime/server/transition.d.ts +1 -0
  59. package/dist/runtime/server/transition.js +1 -0
  60. package/dist/vite-plugin-astro-postprocess/index.js +1 -1
  61. package/dist/vite-plugin-env/index.js +1 -1
  62. package/dist/vite-plugin-html/transform/index.js +1 -1
  63. package/dist/vite-plugin-scanner/index.js +4 -1
  64. package/dist/vite-plugin-scripts/page-ssr.js +1 -1
  65. package/package.json +3 -3
  66. package/dist/cli/load-settings.d.ts +0 -15
  67. package/dist/cli/load-settings.js +0 -39
@@ -34,6 +34,7 @@ const { fallback = 'animate' } = Astro.props as Props;
34
34
  !!document.querySelector('[name="astro-view-transitions-enabled"]');
35
35
  const triggerEvent = (name: Events) => document.dispatchEvent(new Event(name));
36
36
  const onload = () => triggerEvent('astro:load');
37
+ const PERSIST_ATTR = 'data-astro-transition-persist';
37
38
 
38
39
  const throttle = (cb: (...args: any[]) => any, delay: number) => {
39
40
  let wait = false;
@@ -86,8 +87,50 @@ const { fallback = 'animate' } = Astro.props as Props;
86
87
  async function updateDOM(dir: Direction, html: string, state?: State, fallback?: Fallback) {
87
88
  const doc = parser.parseFromString(html, 'text/html');
88
89
  doc.documentElement.dataset.astroTransition = dir;
90
+
91
+ // Check for a head element that should persist, either because it has the data
92
+ // attribute or is a link el.
93
+ const persistedHeadElement = (el: Element): Element | null => {
94
+ const id = el.getAttribute(PERSIST_ATTR);
95
+ const newEl = id && doc.head.querySelector(`[${PERSIST_ATTR}="${id}"]`);
96
+ if (newEl) {
97
+ return newEl;
98
+ }
99
+ if (el.matches('link[rel=stylesheet]')) {
100
+ const href = el.getAttribute('href');
101
+ return doc.head.querySelector(`link[rel=stylesheet][href="${href}"]`);
102
+ }
103
+ return null;
104
+ };
105
+
89
106
  const swap = () => {
90
- document.documentElement.replaceWith(doc.documentElement);
107
+ // Swap head
108
+ for (const el of Array.from(document.head.children)) {
109
+ const newEl = persistedHeadElement(el);
110
+ // If the element exists in the document already, remove it
111
+ // from the new document and leave the current node alone
112
+ if (newEl) {
113
+ newEl.remove();
114
+ } else {
115
+ // Otherwise remove the element in the head. It doesn't exist in the new page.
116
+ el.remove();
117
+ }
118
+ }
119
+ // Everything left in the new head is new, append it all.
120
+ document.head.append(...doc.head.children);
121
+
122
+ // Move over persist stuff in the body
123
+ const oldBody = document.body;
124
+ document.body.replaceWith(doc.body);
125
+ for (const el of oldBody.querySelectorAll(`[${PERSIST_ATTR}]`)) {
126
+ const id = el.getAttribute(PERSIST_ATTR);
127
+ const newEl = document.querySelector(`[${PERSIST_ATTR}="${id}"]`);
128
+ if (newEl) {
129
+ // The element exists in the new page, replace it with the element
130
+ // from the old page so that state is preserved.
131
+ newEl.replaceWith(el);
132
+ }
133
+ }
91
134
 
92
135
  if (state?.scrollY != null) {
93
136
  scrollTo(0, state.scrollY);
@@ -97,17 +140,27 @@ const { fallback = 'animate' } = Astro.props as Props;
97
140
  };
98
141
 
99
142
  // Wait on links to finish, to prevent FOUC
100
- const links = Array.from(doc.querySelectorAll('head link[rel=stylesheet]')).map(
101
- (link) =>
102
- new Promise((resolve) => {
103
- const c = link.cloneNode();
104
- ['load', 'error'].forEach((evName) => c.addEventListener(evName, resolve));
105
- document.head.append(c);
106
- })
107
- );
108
- if (links.length) {
109
- await Promise.all(links);
143
+ const links: Promise<any>[] = [];
144
+ for (const el of doc.querySelectorAll('head link[rel=stylesheet]')) {
145
+ // Do not preload links that are already on the page.
146
+ if (
147
+ !document.querySelector(
148
+ `[${PERSIST_ATTR}="${el.getAttribute(PERSIST_ATTR)}"], link[rel=stylesheet]`
149
+ )
150
+ ) {
151
+ const c = document.createElement('link');
152
+ c.setAttribute('rel', 'preload');
153
+ c.setAttribute('as', 'style');
154
+ c.setAttribute('href', el.getAttribute('href')!);
155
+ links.push(
156
+ new Promise<any>((resolve) => {
157
+ ['load', 'error'].forEach((evName) => c.addEventListener(evName, resolve));
158
+ document.head.append(c);
159
+ })
160
+ );
161
+ }
110
162
  }
163
+ links.length && (await Promise.all(links));
111
164
 
112
165
  if (fallback === 'animate') {
113
166
  let isAnimating = false;
@@ -187,7 +240,7 @@ const { fallback = 'animate' } = Astro.props as Props;
187
240
  transitionEnabledOnThisPage()
188
241
  ) {
189
242
  ev.preventDefault();
190
- navigate('forward', link.href);
243
+ navigate('forward', link.href, { index: currentHistoryIndex, scrollY: 0 });
191
244
  currentHistoryIndex++;
192
245
  const newState: State = { index: currentHistoryIndex, scrollY };
193
246
  persistState({ index: currentHistoryIndex - 1, scrollY });
@@ -14,7 +14,7 @@ import type { PageBuildData } from '../core/build/types';
14
14
  import type { AstroConfigSchema } from '../core/config';
15
15
  import type { AstroTimer } from '../core/config/timer';
16
16
  import type { AstroCookies } from '../core/cookies';
17
- import type { LogOptions } from '../core/logger/core';
17
+ import type { LogOptions, LoggerLevel } from '../core/logger/core';
18
18
  import type { AstroComponentFactory, AstroComponentInstance } from '../runtime/server';
19
19
  import type { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './../core/constants.js';
20
20
  export type { MarkdownHeading, MarkdownMetadata, MarkdownRenderingResult, RehypePlugins, RemarkPlugins, ShikiConfig, } from '@astrojs/markdown-remark';
@@ -55,6 +55,7 @@ export interface AstroBuiltinAttributes {
55
55
  'is:raw'?: boolean;
56
56
  'transition:animate'?: 'morph' | 'slide' | 'fade' | TransitionDirectionalAnimations;
57
57
  'transition:name'?: string;
58
+ 'transition:persist'?: boolean | string;
58
59
  }
59
60
  export interface AstroDefineVarsAttribute {
60
61
  'define:vars'?: any;
@@ -1227,6 +1228,17 @@ export interface InjectedRoute {
1227
1228
  export interface AstroConfig extends z.output<typeof AstroConfigSchema> {
1228
1229
  integrations: AstroIntegration[];
1229
1230
  }
1231
+ export interface AstroInlineConfig extends AstroUserConfig, AstroInlineOnlyConfig {
1232
+ }
1233
+ export interface AstroInlineOnlyConfig {
1234
+ configFile?: string | false;
1235
+ mode?: RuntimeMode;
1236
+ logLevel?: LoggerLevel;
1237
+ /**
1238
+ * @internal for testing only
1239
+ */
1240
+ logging?: LogOptions;
1241
+ }
1230
1242
  export type ContentEntryModule = {
1231
1243
  id: string;
1232
1244
  collection: string;
@@ -122,7 +122,7 @@ function assets({
122
122
  if (s) {
123
123
  return {
124
124
  code: s.toString(),
125
- map: resolvedConfig.build.sourcemap ? s.generateMap({ hires: true }) : null
125
+ map: resolvedConfig.build.sourcemap ? s.generateMap({ hires: "boundary" }) : null
126
126
  };
127
127
  } else {
128
128
  return null;
@@ -1,7 +1,5 @@
1
1
  import type yargs from 'yargs-parser';
2
- import { type LogOptions } from '../../core/logger/core.js';
3
2
  interface AddOptions {
4
- logging: LogOptions;
5
3
  flags: yargs.Arguments;
6
4
  }
7
5
  interface IntegrationInfo {
@@ -10,6 +8,6 @@ interface IntegrationInfo {
10
8
  dependencies: [name: string, version: string][];
11
9
  type: 'integration' | 'adapter';
12
10
  }
13
- export declare function add(names: string[], { flags, logging }: AddOptions): Promise<void>;
11
+ export declare function add(names: string[], { flags }: AddOptions): Promise<void>;
14
12
  export declare function validateIntegrations(integrations: string[]): Promise<IntegrationInfo[]>;
15
13
  export {};
@@ -8,7 +8,7 @@ import { fileURLToPath, pathToFileURL } from "node:url";
8
8
  import ora from "ora";
9
9
  import preferredPM from "preferred-pm";
10
10
  import prompts from "prompts";
11
- import { loadTSConfig, resolveConfigPath } from "../../core/config/index.js";
11
+ import { loadTSConfig, resolveConfigPath, resolveRoot } from "../../core/config/index.js";
12
12
  import {
13
13
  defaultTSConfig,
14
14
  presets,
@@ -21,6 +21,7 @@ import { appendForwardSlash } from "../../core/path.js";
21
21
  import { apply as applyPolyfill } from "../../core/polyfill.js";
22
22
  import { parseNpmName } from "../../core/util.js";
23
23
  import { eventCliSession, telemetry } from "../../events/index.js";
24
+ import { createLoggingFromFlags } from "../flags.js";
24
25
  import { generate, parse, t, visit } from "./babel.js";
25
26
  import { ensureImport } from "./imports.js";
26
27
  import { wrapDefaultExport } from "./wrapper.js";
@@ -66,7 +67,7 @@ async function getRegistry() {
66
67
  return "https://registry.npmjs.org";
67
68
  }
68
69
  }
69
- async function add(names, { flags, logging }) {
70
+ async function add(names, { flags }) {
70
71
  var _a;
71
72
  telemetry.record(eventCliSession("add"));
72
73
  applyPolyfill();
@@ -109,10 +110,12 @@ async function add(names, { flags, logging }) {
109
110
  return;
110
111
  }
111
112
  const cwd = flags.root;
113
+ const logging = createLoggingFromFlags(flags);
112
114
  const integrationNames = names.map((name) => ALIASES.has(name) ? ALIASES.get(name) : name);
113
115
  const integrations = await validateIntegrations(integrationNames);
114
116
  let installResult = await tryToInstallIntegrations({ integrations, cwd, flags, logging });
115
- const root = pathToFileURL(cwd ? path.resolve(cwd) : process.cwd());
117
+ const rootPath = resolveRoot(cwd);
118
+ const root = pathToFileURL(rootPath);
116
119
  root.href = appendForwardSlash(root.href);
117
120
  switch (installResult) {
118
121
  case 1 /* updated */: {
@@ -170,7 +173,11 @@ async function add(names, { flags, logging }) {
170
173
  throw createPrettyError(new Error(`Unable to install dependencies`));
171
174
  }
172
175
  }
173
- const rawConfigPath = await resolveConfigPath({ cwd, flags, fs: fsMod });
176
+ const rawConfigPath = await resolveConfigPath({
177
+ root: rootPath,
178
+ configFile: flags.config,
179
+ fs: fsMod
180
+ });
174
181
  let configURL = rawConfigPath ? pathToFileURL(rawConfigPath) : void 0;
175
182
  if (configURL) {
176
183
  debug("add", `Found config at ${configURL}`);
@@ -1,8 +1,6 @@
1
1
  import type yargs from 'yargs-parser';
2
- import type { LogOptions } from '../../core/logger/core.js';
3
2
  interface BuildOptions {
4
3
  flags: yargs.Arguments;
5
- logging: LogOptions;
6
4
  }
7
- export declare function build({ flags, logging }: BuildOptions): Promise<void>;
5
+ export declare function build({ flags }: BuildOptions): Promise<void>;
8
6
  export {};
@@ -1,14 +1,24 @@
1
1
  import _build from "../../core/build/index.js";
2
- import { loadSettings } from "../load-settings.js";
3
- async function build({ flags, logging }) {
4
- const settings = await loadSettings({ cmd: "build", flags, logging });
5
- if (!settings)
2
+ import { printHelp } from "../../core/messages.js";
3
+ import { flagsToAstroInlineConfig } from "../flags.js";
4
+ async function build({ flags }) {
5
+ if ((flags == null ? void 0 : flags.help) || (flags == null ? void 0 : flags.h)) {
6
+ printHelp({
7
+ commandName: "astro build",
8
+ usage: "[...flags]",
9
+ tables: {
10
+ Flags: [
11
+ ["--drafts", `Include Markdown draft pages in the build.`],
12
+ ["--help (-h)", "See all available flags."]
13
+ ]
14
+ },
15
+ description: `Builds your site for deployment.`
16
+ });
6
17
  return;
7
- await _build(settings, {
8
- flags,
9
- logging,
10
- teardownCompiler: true,
11
- mode: flags.mode
18
+ }
19
+ const inlineConfig = flagsToAstroInlineConfig(flags);
20
+ await _build(inlineConfig, {
21
+ teardownCompiler: true
12
22
  });
13
23
  }
14
24
  export {
@@ -4,16 +4,12 @@ import fs from 'node:fs';
4
4
  import type { Arguments as Flags } from 'yargs-parser';
5
5
  import type { AstroSettings } from '../../@types/astro';
6
6
  import type { LogOptions } from '../../core/logger/core.js';
7
- import type { ProcessExit, SyncOptions } from '../../core/sync';
7
+ import type { syncInternal } from '../../core/sync';
8
8
  export type CheckPayload = {
9
9
  /**
10
10
  * Flags passed via CLI
11
11
  */
12
12
  flags: Flags;
13
- /**
14
- * Logging options
15
- */
16
- logging: LogOptions;
17
13
  };
18
14
  /**
19
15
  *
@@ -43,13 +39,12 @@ export declare enum CheckResult {
43
39
  *
44
40
  * @param {CheckPayload} options Options passed {@link AstroChecker}
45
41
  * @param {Flags} options.flags Flags coming from the CLI
46
- * @param {LogOptions} options.logging Logging options
47
42
  */
48
- export declare function check({ logging, flags }: CheckPayload): Promise<AstroChecker | undefined>;
43
+ export declare function check({ flags }: CheckPayload): Promise<AstroChecker | undefined>;
49
44
  type CheckerConstructor = {
50
45
  diagnosticChecker: AstroCheck;
51
46
  isWatchMode: boolean;
52
- syncCli: (settings: AstroSettings, options: SyncOptions) => Promise<ProcessExit>;
47
+ syncInternal: typeof syncInternal;
53
48
  settings: Readonly<AstroSettings>;
54
49
  logging: Readonly<LogOptions>;
55
50
  fileSystem: typeof fs;
@@ -62,7 +57,7 @@ type CheckerConstructor = {
62
57
  */
63
58
  export declare class AstroChecker {
64
59
  #private;
65
- constructor({ diagnosticChecker, isWatchMode, syncCli, settings, fileSystem, logging, }: CheckerConstructor);
60
+ constructor({ diagnosticChecker, isWatchMode, syncInternal, settings, fileSystem, logging, }: CheckerConstructor);
66
61
  /**
67
62
  * Check all `.astro` files once and then finishes the operation.
68
63
  */
@@ -9,9 +9,14 @@ import fs from "node:fs";
9
9
  import { join } from "node:path";
10
10
  import { fileURLToPath, pathToFileURL } from "node:url";
11
11
  import ora from "ora";
12
+ import { resolveConfig } from "../../core/config/config.js";
13
+ import { createNodeLogging } from "../../core/config/logging.js";
14
+ import { createSettings } from "../../core/config/settings.js";
12
15
  import { debug, info } from "../../core/logger/core.js";
13
16
  import { printHelp } from "../../core/messages.js";
14
- import { loadSettings } from "../load-settings.js";
17
+ import { eventCliSession, telemetry } from "../../events/index.js";
18
+ import { runHookConfigSetup } from "../../integrations/index.js";
19
+ import { flagsToAstroInlineConfig } from "../flags.js";
15
20
  import { printDiagnostic } from "./print.js";
16
21
  var CheckResult = /* @__PURE__ */ ((CheckResult2) => {
17
22
  CheckResult2[CheckResult2["ExitWithSuccess"] = 0] = "ExitWithSuccess";
@@ -20,7 +25,7 @@ var CheckResult = /* @__PURE__ */ ((CheckResult2) => {
20
25
  return CheckResult2;
21
26
  })(CheckResult || {});
22
27
  const ASTRO_GLOB_PATTERN = "**/*.astro";
23
- async function check({ logging, flags }) {
28
+ async function check({ flags }) {
24
29
  if (flags.help || flags.h) {
25
30
  printHelp({
26
31
  commandName: "astro check",
@@ -35,16 +40,18 @@ async function check({ logging, flags }) {
35
40
  });
36
41
  return;
37
42
  }
38
- const settings = await loadSettings({ cmd: "check", flags, logging });
39
- if (!settings)
40
- return;
43
+ const inlineConfig = flagsToAstroInlineConfig(flags);
44
+ const logging = createNodeLogging(inlineConfig);
45
+ const { userConfig, astroConfig } = await resolveConfig(inlineConfig, "check");
46
+ telemetry.record(eventCliSession("check", userConfig, flags));
47
+ const settings = createSettings(astroConfig, fileURLToPath(astroConfig.root));
41
48
  const checkFlags = parseFlags(flags);
42
49
  if (checkFlags.watch) {
43
50
  info(logging, "check", "Checking files in watch mode");
44
51
  } else {
45
52
  info(logging, "check", "Checking files");
46
53
  }
47
- const { syncCli } = await import("../../core/sync/index.js");
54
+ const { syncInternal } = await import("../../core/sync/index.js");
48
55
  const root = settings.config.root;
49
56
  const require2 = createRequire(import.meta.url);
50
57
  const diagnosticChecker = new AstroCheck(
@@ -54,7 +61,7 @@ async function check({ logging, flags }) {
54
61
  })
55
62
  );
56
63
  return new AstroChecker({
57
- syncCli,
64
+ syncInternal,
58
65
  settings,
59
66
  fileSystem: fs,
60
67
  logging,
@@ -65,7 +72,7 @@ async function check({ logging, flags }) {
65
72
  class AstroChecker {
66
73
  #diagnosticsChecker;
67
74
  #shouldWatch;
68
- #syncCli;
75
+ #syncInternal;
69
76
  #settings;
70
77
  #logging;
71
78
  #fs;
@@ -75,14 +82,14 @@ class AstroChecker {
75
82
  constructor({
76
83
  diagnosticChecker,
77
84
  isWatchMode,
78
- syncCli,
85
+ syncInternal,
79
86
  settings,
80
87
  fileSystem,
81
88
  logging
82
89
  }) {
83
90
  this.#diagnosticsChecker = diagnosticChecker;
84
91
  this.#shouldWatch = isWatchMode;
85
- this.#syncCli = syncCli;
92
+ this.#syncInternal = syncInternal;
86
93
  this.#logging = logging;
87
94
  this.#settings = settings;
88
95
  this.#fs = fileSystem;
@@ -131,7 +138,12 @@ class AstroChecker {
131
138
  * @param openDocuments Whether the operation should open all `.astro` files
132
139
  */
133
140
  async #checkAllFiles(openDocuments) {
134
- const processExit = await this.#syncCli(this.#settings, {
141
+ const syncSettings = await runHookConfigSetup({
142
+ settings: this.#settings,
143
+ logging: this.#logging,
144
+ command: "build"
145
+ });
146
+ const processExit = await this.#syncInternal(syncSettings, {
135
147
  logging: this.#logging,
136
148
  fs: this.#fs
137
149
  });
@@ -1,8 +1,6 @@
1
1
  import type yargs from 'yargs-parser';
2
- import { type LogOptions } from '../../core/logger/core.js';
3
2
  interface DevOptions {
4
3
  flags: yargs.Arguments;
5
- logging: LogOptions;
6
4
  }
7
- export declare function dev({ flags, logging }: DevOptions): Promise<import("../../core/dev/dev.js").DevServer | undefined>;
5
+ export declare function dev({ flags }: DevOptions): Promise<import("../../core/dev/dev.js").DevServer | undefined>;
8
6
  export {};
@@ -1,25 +1,29 @@
1
- import fs from "node:fs";
2
- import { resolveConfigPath, resolveFlags } from "../../core/config/index.js";
1
+ import { cyan } from "kleur/colors";
3
2
  import devServer from "../../core/dev/index.js";
4
- import { info } from "../../core/logger/core.js";
5
- import { handleConfigError, loadSettings } from "../load-settings.js";
6
- async function dev({ flags, logging }) {
7
- const settings = await loadSettings({ cmd: "dev", flags, logging });
8
- if (!settings)
3
+ import { printHelp } from "../../core/messages.js";
4
+ import { flagsToAstroInlineConfig } from "../flags.js";
5
+ async function dev({ flags }) {
6
+ if (flags.help || flags.h) {
7
+ printHelp({
8
+ commandName: "astro dev",
9
+ usage: "[...flags]",
10
+ tables: {
11
+ Flags: [
12
+ ["--port", `Specify which port to run on. Defaults to 3000.`],
13
+ ["--host", `Listen on all addresses, including LAN and public addresses.`],
14
+ ["--host <custom-address>", `Expose on a network IP address at <custom-address>`],
15
+ ["--open", "Automatically open the app in the browser on server start"],
16
+ ["--help (-h)", "See all available flags."]
17
+ ]
18
+ },
19
+ description: `Check ${cyan(
20
+ "https://docs.astro.build/en/reference/cli-reference/#astro-dev"
21
+ )} for more information.`
22
+ });
9
23
  return;
10
- const root = flags.root;
11
- const configFlag = resolveFlags(flags).config;
12
- const configFlagPath = configFlag ? await resolveConfigPath({ cwd: root, flags, fs }) : void 0;
13
- return await devServer(settings, {
14
- configFlag,
15
- configFlagPath,
16
- flags,
17
- logging,
18
- handleConfigError(e) {
19
- handleConfigError(e, { cmd: "dev", cwd: root, flags, logging });
20
- info(logging, "astro", "Continuing with previous valid configuration\n");
21
- }
22
- });
24
+ }
25
+ const inlineConfig = flagsToAstroInlineConfig(flags);
26
+ return await devServer(inlineConfig);
23
27
  }
24
28
  export {
25
29
  dev
@@ -0,0 +1,9 @@
1
+ import type { Arguments as Flags } from 'yargs-parser';
2
+ import type { AstroInlineConfig } from '../@types/astro.js';
3
+ import type { LogOptions } from '../core/logger/core.js';
4
+ export declare function flagsToAstroInlineConfig(flags: Flags): AstroInlineConfig;
5
+ /**
6
+ * The `logging` is usually created from an `AstroInlineConfig`, but some flows like `add`
7
+ * doesn't read the AstroConfig directly, so we create a `logging` object from the CLI flags instead.
8
+ */
9
+ export declare function createLoggingFromFlags(flags: Flags): LogOptions;
@@ -0,0 +1,40 @@
1
+ import { nodeLogDestination } from "../core/logger/node.js";
2
+ function flagsToAstroInlineConfig(flags) {
3
+ return {
4
+ // Inline-only configs
5
+ configFile: typeof flags.config === "string" ? flags.config : void 0,
6
+ mode: typeof flags.mode === "string" ? flags.mode : void 0,
7
+ logLevel: flags.verbose ? "debug" : flags.silent ? "silent" : void 0,
8
+ // Astro user configs
9
+ root: typeof flags.root === "string" ? flags.root : void 0,
10
+ site: typeof flags.site === "string" ? flags.site : void 0,
11
+ base: typeof flags.base === "string" ? flags.base : void 0,
12
+ markdown: {
13
+ drafts: typeof flags.drafts === "boolean" ? flags.drafts : void 0
14
+ },
15
+ server: {
16
+ port: typeof flags.port === "number" ? flags.port : void 0,
17
+ host: typeof flags.host === "string" || typeof flags.host === "boolean" ? flags.host : void 0,
18
+ open: typeof flags.open === "boolean" ? flags.open : void 0
19
+ },
20
+ experimental: {
21
+ assets: typeof flags.experimentalAssets === "boolean" ? flags.experimentalAssets : void 0
22
+ }
23
+ };
24
+ }
25
+ function createLoggingFromFlags(flags) {
26
+ const logging = {
27
+ dest: nodeLogDestination,
28
+ level: "info"
29
+ };
30
+ if (flags.verbose) {
31
+ logging.level = "debug";
32
+ } else if (flags.silent) {
33
+ logging.level = "silent";
34
+ }
35
+ return logging;
36
+ }
37
+ export {
38
+ createLoggingFromFlags,
39
+ flagsToAstroInlineConfig
40
+ };
package/dist/cli/index.js CHANGED
@@ -82,16 +82,9 @@ async function runCommand(cmd, flags) {
82
82
  return;
83
83
  }
84
84
  }
85
- const { enableVerboseLogging, nodeLogDestination } = await import("../core/logger/node.js");
86
- const logging = {
87
- dest: nodeLogDestination,
88
- level: "info"
89
- };
90
85
  if (flags.verbose) {
91
- logging.level = "debug";
86
+ const { enableVerboseLogging } = await import("../core/logger/node.js");
92
87
  enableVerboseLogging();
93
- } else if (flags.silent) {
94
- logging.level = "silent";
95
88
  }
96
89
  if (!process.env.NODE_ENV) {
97
90
  process.env.NODE_ENV = cmd === "dev" ? "development" : "production";
@@ -100,12 +93,12 @@ async function runCommand(cmd, flags) {
100
93
  case "add": {
101
94
  const { add } = await import("./add/index.js");
102
95
  const packages = flags._.slice(3);
103
- await add(packages, { flags, logging });
96
+ await add(packages, { flags });
104
97
  return;
105
98
  }
106
99
  case "dev": {
107
100
  const { dev } = await import("./dev/index.js");
108
- const server = await dev({ flags, logging });
101
+ const server = await dev({ flags });
109
102
  if (server) {
110
103
  return await new Promise(() => {
111
104
  });
@@ -114,12 +107,12 @@ async function runCommand(cmd, flags) {
114
107
  }
115
108
  case "build": {
116
109
  const { build } = await import("./build/index.js");
117
- await build({ flags, logging });
110
+ await build({ flags });
118
111
  return;
119
112
  }
120
113
  case "preview": {
121
114
  const { preview } = await import("./preview/index.js");
122
- const server = await preview({ flags, logging });
115
+ const server = await preview({ flags });
123
116
  if (server) {
124
117
  return await server.closed();
125
118
  }
@@ -127,7 +120,7 @@ async function runCommand(cmd, flags) {
127
120
  }
128
121
  case "check": {
129
122
  const { check } = await import("./check/index.js");
130
- const checkServer = await check({ flags, logging });
123
+ const checkServer = await check({ flags });
131
124
  if (checkServer) {
132
125
  if (checkServer.isWatchMode) {
133
126
  await checkServer.watch();
@@ -142,7 +135,7 @@ async function runCommand(cmd, flags) {
142
135
  }
143
136
  case "sync": {
144
137
  const { sync } = await import("./sync/index.js");
145
- const exitCode = await sync({ flags, logging });
138
+ const exitCode = await sync({ flags });
146
139
  return process.exit(exitCode);
147
140
  }
148
141
  }
@@ -1,10 +1,12 @@
1
1
  import * as colors from "kleur/colors";
2
2
  import { arch, platform } from "node:os";
3
3
  import whichPm from "which-pm";
4
- import { openConfig } from "../../core/config/index.js";
4
+ import { resolveConfig } from "../../core/config/index.js";
5
5
  import { ASTRO_VERSION } from "../../core/constants.js";
6
+ import { flagsToAstroInlineConfig } from "../flags.js";
6
7
  async function printInfo({ flags }) {
7
8
  var _a;
9
+ const inlineConfig = flagsToAstroInlineConfig(flags);
8
10
  const packageManager = await whichPm(process.cwd());
9
11
  let adapter = "Couldn't determine.";
10
12
  let integrations = [];
@@ -14,11 +16,7 @@ async function printInfo({ flags }) {
14
16
  console.log(`${colors.bold(label)}` + " ".repeat(padding) + `${colors.green(value)}`);
15
17
  }
16
18
  try {
17
- const { userConfig } = await openConfig({
18
- cwd: flags.root,
19
- flags,
20
- cmd: "info"
21
- });
19
+ const { userConfig } = await resolveConfig(inlineConfig, "info");
22
20
  if ((_a = userConfig.adapter) == null ? void 0 : _a.name) {
23
21
  adapter = userConfig.adapter.name;
24
22
  }
@@ -1,8 +1,6 @@
1
1
  import type yargs from 'yargs-parser';
2
- import type { LogOptions } from '../../core/logger/core.js';
3
2
  interface PreviewOptions {
4
3
  flags: yargs.Arguments;
5
- logging: LogOptions;
6
4
  }
7
- export declare function preview({ flags, logging }: PreviewOptions): Promise<import("../../@types/astro.js").PreviewServer | undefined>;
5
+ export declare function preview({ flags }: PreviewOptions): Promise<import("../../@types/astro.js").PreviewServer | undefined>;
8
6
  export {};
@@ -1,10 +1,26 @@
1
+ import { cyan } from "kleur/colors";
2
+ import { printHelp } from "../../core/messages.js";
1
3
  import previewServer from "../../core/preview/index.js";
2
- import { loadSettings } from "../load-settings.js";
3
- async function preview({ flags, logging }) {
4
- const settings = await loadSettings({ cmd: "preview", flags, logging });
5
- if (!settings)
4
+ import { flagsToAstroInlineConfig } from "../flags.js";
5
+ async function preview({ flags }) {
6
+ if ((flags == null ? void 0 : flags.help) || (flags == null ? void 0 : flags.h)) {
7
+ printHelp({
8
+ commandName: "astro preview",
9
+ usage: "[...flags]",
10
+ tables: {
11
+ Flags: [
12
+ ["--open", "Automatically open the app in the browser on server start"],
13
+ ["--help (-h)", "See all available flags."]
14
+ ]
15
+ },
16
+ description: `Starts a local server to serve your static dist/ directory. Check ${cyan(
17
+ "https://docs.astro.build/en/reference/cli-reference/#astro-preview"
18
+ )} for more information.`
19
+ });
6
20
  return;
7
- return await previewServer(settings, { flags, logging });
21
+ }
22
+ const inlineConfig = flagsToAstroInlineConfig(flags);
23
+ return await previewServer(inlineConfig);
8
24
  }
9
25
  export {
10
26
  preview