astro 1.4.6 → 1.5.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 (43) hide show
  1. package/components/Code.astro +1 -1
  2. package/dist/@types/astro.d.ts +201 -29
  3. package/dist/cli/index.js +35 -36
  4. package/dist/config/index.js +2 -7
  5. package/dist/core/add/index.js +140 -23
  6. package/dist/core/build/index.js +3 -3
  7. package/dist/core/build/vite-plugin-css.js +6 -17
  8. package/dist/core/config/config.d.ts +1 -1
  9. package/dist/core/config/config.js +6 -2
  10. package/dist/core/config/index.d.ts +1 -1
  11. package/dist/core/config/index.js +2 -1
  12. package/dist/core/config/schema.d.ts +139 -1
  13. package/dist/core/config/schema.js +24 -2
  14. package/dist/core/config/settings.d.ts +1 -7
  15. package/dist/core/config/settings.js +7 -4
  16. package/dist/core/config/tsconfig.d.ts +10 -1
  17. package/dist/core/config/tsconfig.js +73 -7
  18. package/dist/core/constants.d.ts +1 -0
  19. package/dist/core/constants.js +4 -0
  20. package/dist/core/cookies/cookies.d.ts +1 -1
  21. package/dist/core/dev/index.js +7 -2
  22. package/dist/core/endpoint/dev/index.js +4 -1
  23. package/dist/core/endpoint/index.d.ts +1 -1
  24. package/dist/core/endpoint/index.js +44 -4
  25. package/dist/core/messages.js +2 -2
  26. package/dist/core/preview/index.d.ts +2 -11
  27. package/dist/core/preview/index.js +31 -125
  28. package/dist/core/preview/static-preview-server.d.ts +17 -0
  29. package/dist/core/preview/static-preview-server.js +127 -0
  30. package/dist/core/render/core.d.ts +1 -1
  31. package/dist/core/render/result.js +2 -2
  32. package/dist/core/util.d.ts +3 -14
  33. package/dist/core/util.js +4 -2
  34. package/dist/events/index.js +1 -1
  35. package/dist/integrations/index.d.ts +3 -2
  36. package/dist/integrations/index.js +39 -2
  37. package/dist/runtime/server/astro-global.js +1 -1
  38. package/dist/runtime/server/hydration.js +1 -1
  39. package/dist/runtime/server/render/page.js +1 -0
  40. package/dist/vite-plugin-jsx/index.js +9 -5
  41. package/dist/vite-plugin-jsx/tag.d.ts +3 -2
  42. package/dist/vite-plugin-jsx/tag.js +10 -4
  43. package/package.json +4 -2
@@ -24,7 +24,7 @@ export interface Props {
24
24
  /**
25
25
  * Enable word wrapping.
26
26
  * - true: enabled.
27
- * - false: enabled.
27
+ * - false: disabled.
28
28
  * - null: All overflow styling removed. Code will overflow the element by default.
29
29
  *
30
30
  * @default false
@@ -60,8 +60,17 @@ export interface CLIFlags {
60
60
  drafts?: boolean;
61
61
  }
62
62
  export interface BuildConfig {
63
+ /**
64
+ * @deprecated Use config.build.client instead.
65
+ */
63
66
  client: URL;
67
+ /**
68
+ * @deprecated Use config.build.server instead.
69
+ */
64
70
  server: URL;
71
+ /**
72
+ * @deprecated Use config.build.serverEntry instead.
73
+ */
65
74
  serverEntry: string;
66
75
  }
67
76
  /**
@@ -69,7 +78,7 @@ export interface BuildConfig {
69
78
  *
70
79
  * [Astro reference](https://docs.astro.build/reference/api-reference/#astro-global)
71
80
  */
72
- export interface AstroGlobal<Props extends Record<string, any> = Record<string, any>> extends AstroGlobalPartial {
81
+ export interface AstroGlobal<Props extends Record<string, any> = Record<string, any>> extends AstroGlobalPartial, AstroSharedContext<Props> {
73
82
  /**
74
83
  * Canonical URL of the current page.
75
84
  * @deprecated Use `Astro.url` instead.
@@ -82,21 +91,13 @@ export interface AstroGlobal<Props extends Record<string, any> = Record<string,
82
91
  * ```
83
92
  */
84
93
  canonicalURL: URL;
85
- /** The address (usually IP address) of the user. Used with SSR only.
86
- *
87
- */
88
- clientAddress: string;
89
94
  /**
90
95
  * A full URL object of the request URL.
91
96
  * Equivalent to: `new URL(Astro.request.url)`
92
97
  *
93
98
  * [Astro reference](https://docs.astro.build/en/reference/api-reference/#url)
94
99
  */
95
- /**
96
- * Utility for getting and setting cookies values.
97
- */
98
- cookies: AstroCookies;
99
- url: URL;
100
+ url: AstroSharedContext['url'];
100
101
  /** Parameters passed to a dynamic page generated using [getStaticPaths](https://docs.astro.build/en/reference/api-reference/#getstaticpaths)
101
102
  *
102
103
  * Example usage:
@@ -113,9 +114,9 @@ export interface AstroGlobal<Props extends Record<string, any> = Record<string,
113
114
  * <h1>{id}</h1>
114
115
  * ```
115
116
  *
116
- * [Astro reference](https://docs.astro.build/en/reference/api-reference/#params)
117
+ * [Astro reference](https://docs.astro.build/en/reference/api-reference/#astroparams)
117
118
  */
118
- params: Params;
119
+ params: AstroSharedContext['params'];
119
120
  /** List of props passed to this component
120
121
  *
121
122
  * A common way to get specific props is through [destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment), ex:
@@ -125,7 +126,7 @@ export interface AstroGlobal<Props extends Record<string, any> = Record<string,
125
126
  *
126
127
  * [Astro reference](https://docs.astro.build/en/core-concepts/astro-components/#component-props)
127
128
  */
128
- props: Props;
129
+ props: AstroSharedContext<Props>['props'];
129
130
  /** Information about the current request. This is a standard [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object
130
131
  *
131
132
  * For example, to get a URL object of the current URL, you can use:
@@ -159,11 +160,11 @@ export interface AstroGlobal<Props extends Record<string, any> = Record<string,
159
160
  *
160
161
  * [Astro reference](https://docs.astro.build/en/guides/server-side-rendering/#astroredirect)
161
162
  */
162
- redirect(path: string): Response;
163
+ redirect: AstroSharedContext['redirect'];
163
164
  /**
164
165
  * The <Astro.self /> element allows a component to reference itself recursively.
165
166
  *
166
- * [Astro reference](https://docs.astro.build/en/guides/server-side-rendering/#astroself)
167
+ * [Astro reference](https://docs.astro.build/en/guides/api-reference/#astroself)
167
168
  */
168
169
  self: AstroComponentFactory;
169
170
  /** Utility functions for modifying an Astro component’s slotted children
@@ -354,6 +355,7 @@ export interface AstroUserConfig {
354
355
  * @name outDir
355
356
  * @type {string}
356
357
  * @default `"./dist"`
358
+ * @see build.server
357
359
  * @description Set the directory that `astro build` writes your final build to.
358
360
  *
359
361
  * The value can be either an absolute file system path or a path relative to the project root.
@@ -384,22 +386,13 @@ export interface AstroUserConfig {
384
386
  * @name base
385
387
  * @type {string}
386
388
  * @description
387
- * The base path you're deploying to. Astro will match this pathname during development so that your development experience matches your build environment as closely as possible. In the example below, `astro dev` will start your server at `/docs`.
389
+ * The base path to deploy to. Astro will build your pages and assets using this path as the root. Currently, this has no effect during development.
388
390
  *
389
391
  * ```js
390
392
  * {
391
393
  * base: '/docs'
392
394
  * }
393
395
  * ```
394
- *
395
- * When using this option, you should mind that all of your imports will be affected. In this example, all of the imports including static resources and codes should add a prefix `/docs/`.
396
- *
397
- * For example, if you want to use a image in your Astro component, you need to change it from '/someimg.png' into '/docs/someimg.png'.
398
- *
399
- * ```astro
400
- * <!-- <img src="/someimg.png"> is not correct. -->
401
- * <img src="/docs/someimg.png">
402
- * ```
403
396
  */
404
397
  base?: string;
405
398
  /**
@@ -502,6 +495,68 @@ export interface AstroUserConfig {
502
495
  * This means that when you create relative URLs using `new URL('./relative', Astro.url)`, you will get consistent behavior between dev and build.
503
496
  */
504
497
  format?: 'file' | 'directory';
498
+ /**
499
+ * @docs
500
+ * @name build.client
501
+ * @type {string}
502
+ * @default `'./dist/client'`
503
+ * @description
504
+ * Controls the output directory of your client-side CSS and JavaScript when `output: 'server'` only.
505
+ * `outDir` controls where the code is built to.
506
+ *
507
+ * This value is relative to the `outDir`.
508
+ *
509
+ * ```js
510
+ * {
511
+ * output: 'server',
512
+ * build: {
513
+ * client: './client'
514
+ * }
515
+ * }
516
+ * ```
517
+ */
518
+ client?: string;
519
+ /**
520
+ * @docs
521
+ * @name build.server
522
+ * @type {string}
523
+ * @default `'./dist/server'`
524
+ * @description
525
+ * Controls the output directory of server JavaScript when building to SSR.
526
+ *
527
+ * This value is relative to the `outDir`.
528
+ *
529
+ * ```js
530
+ * {
531
+ * build: {
532
+ * server: './server'
533
+ * }
534
+ * }
535
+ * ```
536
+ */
537
+ server?: string;
538
+ /**
539
+ * @docs
540
+ * @name build.serverEntry
541
+ * @type {string}
542
+ * @default `'entry.mjs'`
543
+ * @description
544
+ * Specifies the file name of the server entrypoint when building to SSR.
545
+ * This entrypoint is usually dependent on which host you are deploying to and
546
+ * will be set by your adapter for you.
547
+ *
548
+ * Note that it is recommended that this file ends with `.mjs` so that the runtime
549
+ * detects that the file is a JavaScript module.
550
+ *
551
+ * ```js
552
+ * {
553
+ * build: {
554
+ * serverEntry: 'main.mjs'
555
+ * }
556
+ * }
557
+ * ```
558
+ */
559
+ serverEntry?: string;
505
560
  };
506
561
  /**
507
562
  * @docs
@@ -829,6 +884,7 @@ export interface AstroSettings {
829
884
  }[];
830
885
  tsConfig: TsConfigJson | undefined;
831
886
  tsConfigPath: string | undefined;
887
+ watchFiles: string[];
832
888
  }
833
889
  export declare type AsyncRendererComponentFn<U> = (Component: any, props: any, slots: Record<string, string>, metadata?: AstroComponentMetadata) => Promise<U>;
834
890
  /** Generic interface for a component (Astro, Svelte, React, etc.) */
@@ -978,19 +1034,116 @@ export interface Page<T = any> {
978
1034
  }
979
1035
  export declare type PaginateFunction = (data: any[], args?: PaginateOptions) => GetStaticPathsResult;
980
1036
  export declare type Params = Record<string, string | number | undefined>;
981
- export declare type Props = Record<string, unknown>;
982
1037
  export interface AstroAdapter {
983
1038
  name: string;
984
1039
  serverEntrypoint?: string;
1040
+ previewEntrypoint?: string;
985
1041
  exports?: string[];
986
1042
  args?: any;
987
1043
  }
988
1044
  declare type Body = string;
989
- export interface APIContext {
1045
+ interface AstroSharedContext<Props extends Record<string, any> = Record<string, any>> {
1046
+ /**
1047
+ * The address (usually IP address) of the user. Used with SSR only.
1048
+ */
1049
+ clientAddress: string;
1050
+ /**
1051
+ * Utility for getting and setting the values of cookies.
1052
+ */
990
1053
  cookies: AstroCookies;
991
- params: Params;
1054
+ /**
1055
+ * Information about the current request. This is a standard [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object
1056
+ */
992
1057
  request: Request;
1058
+ /**
1059
+ * A full URL object of the request URL.
1060
+ */
1061
+ url: URL;
1062
+ /**
1063
+ * Route parameters for this request if this is a dynamic route.
1064
+ */
1065
+ params: Params;
1066
+ /**
1067
+ * List of props returned for this path by `getStaticPaths` (**Static Only**).
1068
+ */
1069
+ props: Props;
1070
+ /**
1071
+ * Redirect to another page (**SSR Only**).
1072
+ */
1073
+ redirect(path: string, status?: 301 | 302 | 308): Response;
1074
+ }
1075
+ export interface APIContext<Props extends Record<string, any> = Record<string, any>> extends AstroSharedContext<Props> {
1076
+ site: URL | undefined;
1077
+ generator: string;
1078
+ /**
1079
+ * A full URL object of the request URL.
1080
+ * Equivalent to: `new URL(request.url)`
1081
+ */
1082
+ url: AstroSharedContext['url'];
1083
+ /**
1084
+ * Parameters matching the page’s dynamic route pattern.
1085
+ * In static builds, this will be the `params` generated by `getStaticPaths`.
1086
+ * In SSR builds, this can be any path segments matching the dynamic route pattern.
1087
+ *
1088
+ * Example usage:
1089
+ * ```ts
1090
+ * export function getStaticPaths() {
1091
+ * return [
1092
+ * { params: { id: '0' }, props: { name: 'Sarah' } },
1093
+ * { params: { id: '1' }, props: { name: 'Chris' } },
1094
+ * { params: { id: '2' }, props: { name: 'Fuzzy' } },
1095
+ * ];
1096
+ * }
1097
+ *
1098
+ * export async function get({ params }) {
1099
+ * return {
1100
+ * body: `Hello user ${params.id}!`,
1101
+ * }
1102
+ * }
1103
+ * ```
1104
+ *
1105
+ * [context reference](https://docs.astro.build/en/guides/api-reference/#contextparams)
1106
+ */
1107
+ params: AstroSharedContext['params'];
1108
+ /**
1109
+ * List of props passed from `getStaticPaths`. Only available to static builds.
1110
+ *
1111
+ * Example usage:
1112
+ * ```ts
1113
+ * export function getStaticPaths() {
1114
+ * return [
1115
+ * { params: { id: '0' }, props: { name: 'Sarah' } },
1116
+ * { params: { id: '1' }, props: { name: 'Chris' } },
1117
+ * { params: { id: '2' }, props: { name: 'Fuzzy' } },
1118
+ * ];
1119
+ * }
1120
+ *
1121
+ * export function get({ props }) {
1122
+ * return {
1123
+ * body: `Hello ${props.name}!`,
1124
+ * }
1125
+ * }
1126
+ * ```
1127
+ *
1128
+ * [context reference](https://docs.astro.build/en/guides/api-reference/#contextprops)
1129
+ */
1130
+ props: AstroSharedContext<Props>['props'];
1131
+ /**
1132
+ * Redirect to another page. Only available in SSR builds.
1133
+ *
1134
+ * Example usage:
1135
+ * ```ts
1136
+ * // src/pages/secret.ts
1137
+ * export function get({ redirect }) {
1138
+ * return redirect('/login');
1139
+ * }
1140
+ * ```
1141
+ *
1142
+ * [context reference](https://docs.astro.build/en/guides/api-reference/#contextredirect)
1143
+ */
1144
+ redirect: AstroSharedContext['redirect'];
993
1145
  }
1146
+ export declare type Props = Record<string, unknown>;
994
1147
  export interface EndpointOutput {
995
1148
  body: Body;
996
1149
  encoding?: BufferEncoding;
@@ -1028,9 +1181,11 @@ export interface AstroIntegration {
1028
1181
  hooks: {
1029
1182
  'astro:config:setup'?: (options: {
1030
1183
  config: AstroConfig;
1031
- command: 'dev' | 'build';
1184
+ command: 'dev' | 'build' | 'preview';
1185
+ isRestart: boolean;
1032
1186
  updateConfig: (newConfig: Record<string, any>) => void;
1033
1187
  addRenderer: (renderer: AstroRenderer) => void;
1188
+ addWatchFile: (path: URL | string) => void;
1034
1189
  injectScript: (stage: InjectedScriptStage, content: string) => void;
1035
1190
  injectRoute: (injectRoute: InjectedRoute) => void;
1036
1191
  }) => void | Promise<void>;
@@ -1119,3 +1274,20 @@ export interface SSRResult {
1119
1274
  export declare type MarkdownAstroData = {
1120
1275
  frontmatter: object;
1121
1276
  };
1277
+ export interface PreviewServer {
1278
+ host?: string;
1279
+ port: number;
1280
+ closed(): Promise<void>;
1281
+ stop(): Promise<void>;
1282
+ }
1283
+ export interface PreviewServerParams {
1284
+ outDir: URL;
1285
+ client: URL;
1286
+ serverEntrypoint: URL;
1287
+ host: string | undefined;
1288
+ port: number;
1289
+ }
1290
+ export declare type CreatePreviewServer = (params: PreviewServerParams) => PreviewServer | Promise<PreviewServer>;
1291
+ export interface PreviewModule {
1292
+ default: CreatePreviewServer;
1293
+ }
package/dist/cli/index.js CHANGED
@@ -7,12 +7,12 @@ import add from "../core/add/index.js";
7
7
  import build from "../core/build/index.js";
8
8
  import {
9
9
  createSettings,
10
- loadTSConfig,
11
10
  openConfig,
12
11
  resolveConfigPath,
13
12
  resolveFlags,
14
13
  resolveRoot
15
14
  } from "../core/config/index.js";
15
+ import { ASTRO_VERSION } from "../core/constants.js";
16
16
  import devServer from "../core/dev/index.js";
17
17
  import { collectErrorMetadata } from "../core/errors.js";
18
18
  import { debug, error, info } from "../core/logger/core.js";
@@ -20,7 +20,7 @@ import { enableVerboseLogging, nodeLogDestination } from "../core/logger/node.js
20
20
  import { formatConfigErrorMessage, formatErrorMessage, printHelp } from "../core/messages.js";
21
21
  import { appendForwardSlash } from "../core/path.js";
22
22
  import preview from "../core/preview/index.js";
23
- import { ASTRO_VERSION, createSafeError } from "../core/util.js";
23
+ import { createSafeError } from "../core/util.js";
24
24
  import * as event from "../events/index.js";
25
25
  import { eventConfigError, eventError, telemetry } from "../events/index.js";
26
26
  import { check } from "./check/index.js";
@@ -132,12 +132,7 @@ async function runCommand(cmd, flags) {
132
132
  if (!initialAstroConfig)
133
133
  return;
134
134
  telemetry.record(event.eventCliSession(cmd, initialUserConfig, flags));
135
- let initialTsConfig = loadTSConfig(root);
136
- let settings = createSettings({
137
- config: initialAstroConfig,
138
- tsConfig: initialTsConfig == null ? void 0 : initialTsConfig.config,
139
- tsConfigPath: initialTsConfig == null ? void 0 : initialTsConfig.path
140
- });
135
+ let settings = createSettings(initialAstroConfig, root);
141
136
  switch (cmd) {
142
137
  case "dev": {
143
138
  async function startDevServer({ isRestart = false } = {}) {
@@ -147,35 +142,39 @@ async function runCommand(cmd, flags) {
147
142
  const configFlagPath = configFlag ? await resolveConfigPath({ cwd: root, flags }) : void 0;
148
143
  const resolvedRoot = appendForwardSlash(resolveRoot(root));
149
144
  const handleServerRestart = (logMsg) => async function(changedFile) {
150
- if (!restartInFlight && (configFlag ? configFlagPath && normalizePath(configFlagPath) === normalizePath(changedFile) : new RegExp(
145
+ if (restartInFlight)
146
+ return;
147
+ let shouldRestart = false;
148
+ shouldRestart = configFlag ? !!configFlagPath && normalizePath(configFlagPath) === normalizePath(changedFile) : new RegExp(
151
149
  `${normalizePath(resolvedRoot)}.*astro.config.((mjs)|(cjs)|(js)|(ts))$`
152
- ).test(normalizePath(changedFile)))) {
153
- restartInFlight = true;
154
- console.clear();
155
- try {
156
- const newConfig = await openConfig({
157
- cwd: root,
158
- flags,
159
- cmd,
160
- logging,
161
- isConfigReload: true
162
- });
163
- info(logging, "astro", logMsg + "\n");
164
- let astroConfig = newConfig.astroConfig;
165
- let tsconfig = loadTSConfig(root);
166
- settings = createSettings({
167
- config: astroConfig,
168
- tsConfig: tsconfig == null ? void 0 : tsconfig.config,
169
- tsConfigPath: tsconfig == null ? void 0 : tsconfig.path
170
- });
171
- await stop();
172
- await startDevServer({ isRestart: true });
173
- } catch (e) {
174
- await handleConfigError(e, { cwd: root, flags, logging });
175
- await stop();
176
- info(logging, "astro", "Continuing with previous valid configuration\n");
177
- await startDevServer({ isRestart: true });
178
- }
150
+ ).test(normalizePath(changedFile));
151
+ if (!shouldRestart && settings.watchFiles.length > 0) {
152
+ shouldRestart = settings.watchFiles.some(
153
+ (path) => normalizePath(path) === normalizePath(changedFile)
154
+ );
155
+ }
156
+ if (!shouldRestart)
157
+ return;
158
+ restartInFlight = true;
159
+ console.clear();
160
+ try {
161
+ const newConfig = await openConfig({
162
+ cwd: root,
163
+ flags,
164
+ cmd,
165
+ logging,
166
+ isRestart: true
167
+ });
168
+ info(logging, "astro", logMsg + "\n");
169
+ let astroConfig = newConfig.astroConfig;
170
+ settings = createSettings(astroConfig, root);
171
+ await stop();
172
+ await startDevServer({ isRestart: true });
173
+ } catch (e) {
174
+ await handleConfigError(e, { cwd: root, flags, logging });
175
+ await stop();
176
+ info(logging, "astro", "Continuing with previous valid configuration\n");
177
+ await startDevServer({ isRestart: true });
179
178
  }
180
179
  };
181
180
  watcher.on("change", handleServerRestart("Configuration updated. Restarting..."));
@@ -7,7 +7,7 @@ function getViteConfig(inlineConfig) {
7
7
  const [
8
8
  { mergeConfig },
9
9
  { nodeLogDestination },
10
- { openConfig, createSettings, loadTSConfig },
10
+ { openConfig, createSettings },
11
11
  { createVite },
12
12
  { runHookConfigSetup, runHookConfigDone }
13
13
  ] = await Promise.all([
@@ -25,12 +25,7 @@ function getViteConfig(inlineConfig) {
25
25
  cmd,
26
26
  logging
27
27
  });
28
- const initialTsConfig = loadTSConfig(inlineConfig.root);
29
- const settings = createSettings({
30
- config,
31
- tsConfig: initialTsConfig == null ? void 0 : initialTsConfig.config,
32
- tsConfigPath: initialTsConfig == null ? void 0 : initialTsConfig.path
33
- });
28
+ const settings = createSettings(config, inlineConfig.root);
34
29
  await runHookConfigSetup({ settings, command: cmd, logging });
35
30
  const viteConfig = await createVite(
36
31
  {