astro 4.6.3 → 4.6.4

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.
@@ -108,9 +108,16 @@ const { fallback = 'animate' } = Astro.props;
108
108
  const form = el as HTMLFormElement;
109
109
  const submitter = ev.submitter;
110
110
  const formData = new FormData(form, submitter);
111
+ // form.action and form.method can point to an <input name="action"> or <input name="method">
112
+ // in which case should fallback to the form attribute
113
+ const formAction =
114
+ typeof form.action === 'string' ? form.action : form.getAttribute('action');
115
+ const formMethod =
116
+ typeof form.method === 'string' ? form.method : form.getAttribute('method');
111
117
  // Use the form action, if defined, otherwise fallback to current path.
112
- let action = submitter?.getAttribute('formaction') ?? form.action ?? location.pathname;
113
- const method = submitter?.getAttribute('formmethod') ?? form.method;
118
+ let action = submitter?.getAttribute('formaction') ?? formAction ?? location.pathname;
119
+ // Use the form method, if defined, otherwise fallback to "get"
120
+ const method = submitter?.getAttribute('formmethod') ?? formMethod ?? 'get';
114
121
 
115
122
  // the "dialog" method is a special keyword used within <dialog> elements
116
123
  // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-method
@@ -1,6 +1,6 @@
1
1
  import { imageConfig } from "astro:assets";
2
2
  import { isRemotePath } from "@astrojs/internal-helpers/path";
3
- import mime from "mime/lite.js";
3
+ import * as mime from "mrmime";
4
4
  import { getConfiguredImageService } from "../internal.js";
5
5
  import { etag } from "../utils/etag.js";
6
6
  import { isRemoteAllowed } from "../utils/remotePattern.js";
@@ -47,7 +47,7 @@ const GET = async ({ request }) => {
47
47
  return new Response(data, {
48
48
  status: 200,
49
49
  headers: {
50
- "Content-Type": mime.getType(format) ?? `image/${format}`,
50
+ "Content-Type": mime.lookup(format) ?? `image/${format}`,
51
51
  "Cache-Control": "public, max-age=31536000",
52
52
  ETag: etag(data.toString()),
53
53
  Date: (/* @__PURE__ */ new Date()).toUTCString()
@@ -4,7 +4,7 @@ import { fileURLToPath, pathToFileURL } from "node:url";
4
4
  import { assetsDir, imageConfig, outDir } from "astro:assets";
5
5
  import { isRemotePath, removeQueryString } from "@astrojs/internal-helpers/path";
6
6
  import { readFile } from "fs/promises";
7
- import mime from "mime/lite.js";
7
+ import * as mime from "mrmime";
8
8
  import { getConfiguredImageService } from "../internal.js";
9
9
  import { etag } from "../utils/etag.js";
10
10
  import { isRemoteAllowed } from "../utils/remotePattern.js";
@@ -83,7 +83,7 @@ const GET = async ({ request }) => {
83
83
  return new Response(data, {
84
84
  status: 200,
85
85
  headers: {
86
- "Content-Type": mime.getType(format) ?? `image/${format}`,
86
+ "Content-Type": mime.lookup(format) ?? `image/${format}`,
87
87
  "Cache-Control": "public, max-age=31536000",
88
88
  ETag: etag(data.toString()),
89
89
  Date: (/* @__PURE__ */ new Date()).toUTCString()
@@ -1,2 +1,7 @@
1
+ import type * as vite from 'vite';
1
2
  import type { ImageMetadata } from '../types.js';
2
- export declare function emitESMImage(id: string | undefined, watchMode: boolean, fileEmitter: any): Promise<ImageMetadata | undefined>;
3
+ type FileEmitter = vite.Rollup.EmitFile;
4
+ export declare function emitESMImage(id: string | undefined,
5
+ /** @deprecated */
6
+ _watchMode: boolean, fileEmitter?: FileEmitter): Promise<ImageMetadata | undefined>;
7
+ export {};
@@ -3,7 +3,7 @@ import path from "node:path";
3
3
  import { fileURLToPath, pathToFileURL } from "node:url";
4
4
  import { prependForwardSlash, slash } from "../../core/path.js";
5
5
  import { imageMetadata } from "./metadata.js";
6
- async function emitESMImage(id, watchMode, fileEmitter) {
6
+ async function emitESMImage(id, _watchMode, fileEmitter) {
7
7
  if (!id) {
8
8
  return void 0;
9
9
  }
@@ -24,16 +24,22 @@ async function emitESMImage(id, watchMode, fileEmitter) {
24
24
  writable: false,
25
25
  value: id
26
26
  });
27
- if (!watchMode) {
27
+ let isBuild = typeof fileEmitter === "function";
28
+ if (isBuild) {
28
29
  const pathname = decodeURI(url.pathname);
29
30
  const filename = path.basename(pathname, path.extname(pathname) + `.${fileMetadata.format}`);
30
- const handle = fileEmitter({
31
- name: filename,
32
- source: await fs.readFile(url),
33
- type: "asset"
34
- });
35
- emittedImage.src = `__ASTRO_ASSET_IMAGE__${handle}__`;
36
- } else {
31
+ try {
32
+ const handle = fileEmitter({
33
+ name: filename,
34
+ source: await fs.readFile(url),
35
+ type: "asset"
36
+ });
37
+ emittedImage.src = `__ASTRO_ASSET_IMAGE__${handle}__`;
38
+ } catch {
39
+ isBuild = false;
40
+ }
41
+ }
42
+ if (!isBuild) {
37
43
  url.searchParams.append("origWidth", fileMetadata.width.toString());
38
44
  url.searchParams.append("origHeight", fileMetadata.height.toString());
39
45
  url.searchParams.append("origFormat", fileMetadata.format);
@@ -69,6 +69,7 @@ function assets({
69
69
  mode
70
70
  }) {
71
71
  let resolvedConfig;
72
+ let shouldEmitFile = false;
72
73
  globalThis.astroAsset = {
73
74
  referencedImages: /* @__PURE__ */ new Set()
74
75
  };
@@ -155,6 +156,9 @@ function assets({
155
156
  {
156
157
  name: "astro:assets:esm",
157
158
  enforce: "pre",
159
+ config(_, env) {
160
+ shouldEmitFile = env.command === "build";
161
+ },
158
162
  configResolved(viteConfig) {
159
163
  resolvedConfig = viteConfig;
160
164
  },
@@ -169,7 +173,8 @@ function assets({
169
173
  if (!assetRegexEnds.test(id)) {
170
174
  return;
171
175
  }
172
- const imageMetadata = await emitESMImage(id, this.meta.watchMode, this.emitFile);
176
+ const emitFile = shouldEmitFile ? this.emitFile : void 0;
177
+ const imageMetadata = await emitESMImage(id, this.meta.watchMode, emitFile);
173
178
  if (!imageMetadata) {
174
179
  throw new AstroError({
175
180
  ...AstroErrorData.ImageNotFound,
@@ -27,8 +27,8 @@ function getViteConfig(inlineConfig) {
27
27
  level: "info"
28
28
  });
29
29
  const { astroConfig: config } = await resolveConfig({}, cmd);
30
- const settings = await createSettings(config, inlineConfig.root);
31
- await runHookConfigSetup({ settings, command: cmd, logger });
30
+ let settings = await createSettings(config, inlineConfig.root);
31
+ settings = await runHookConfigSetup({ settings, command: cmd, logger });
32
32
  const viteConfig = await createVite(
33
33
  {
34
34
  mode,
@@ -1,6 +1,6 @@
1
1
  import type { PluginContext } from 'rollup';
2
2
  import { z } from 'zod';
3
- export declare function createImage(pluginContext: PluginContext, entryFilePath: string): () => z.ZodEffects<z.ZodString, z.ZodNever | {
3
+ export declare function createImage(pluginContext: PluginContext, shouldEmitFile: boolean, entryFilePath: string): () => z.ZodEffects<z.ZodString, z.ZodNever | {
4
4
  ASTRO_ASSET: string;
5
5
  src: string;
6
6
  width: number;
@@ -1,13 +1,13 @@
1
1
  import { z } from "zod";
2
2
  import { emitESMImage } from "../assets/utils/emitAsset.js";
3
- function createImage(pluginContext, entryFilePath) {
3
+ function createImage(pluginContext, shouldEmitFile, entryFilePath) {
4
4
  return () => {
5
5
  return z.string().transform(async (imagePath, ctx) => {
6
6
  const resolvedFilePath = (await pluginContext.resolve(imagePath, entryFilePath))?.id;
7
7
  const metadata = await emitESMImage(
8
8
  resolvedFilePath,
9
9
  pluginContext.meta.watchMode,
10
- pluginContext.emitFile
10
+ shouldEmitFile ? pluginContext.emitFile : void 0
11
11
  );
12
12
  if (!metadata) {
13
13
  ctx.addIssue({
@@ -14,7 +14,7 @@ export declare function createGetCollection({ contentCollectionToEntryMap, dataC
14
14
  contentCollectionToEntryMap: CollectionToEntryMap;
15
15
  dataCollectionToEntryMap: CollectionToEntryMap;
16
16
  getRenderEntryImport: GetEntryImport;
17
- }): (collection: string, filter?: ((entry: any) => unknown) | undefined) => Promise<any[]>;
17
+ }): (collection: string, filter?: (entry: any) => unknown) => Promise<any[]>;
18
18
  export declare function createGetEntryBySlug({ getEntryImport, getRenderEntryImport, }: {
19
19
  getEntryImport: GetEntryImport;
20
20
  getRenderEntryImport: GetEntryImport;
@@ -99,7 +99,7 @@ export declare function getEntryData(entry: {
99
99
  collection: string;
100
100
  unvalidatedData: Record<string, unknown>;
101
101
  _internal: EntryInternal;
102
- }, collectionConfig: CollectionConfig, pluginContext: PluginContext): Promise<any>;
102
+ }, collectionConfig: CollectionConfig, shouldEmitFile: boolean, pluginContext: PluginContext): Promise<Record<string, unknown>>;
103
103
  export declare function getContentEntryExts(settings: Pick<AstroSettings, 'contentEntryTypes'>): string[];
104
104
  export declare function getDataEntryExts(settings: Pick<AstroSettings, 'dataEntryTypes'>): string[];
105
105
  export declare function getEntryConfigByExtMap<TEntryType extends ContentEntryType | DataEntryType>(entryTypes: TEntryType[]): Map<string, TEntryType>;
@@ -49,7 +49,7 @@ function parseEntrySlug({
49
49
  });
50
50
  }
51
51
  }
52
- async function getEntryData(entry, collectionConfig, pluginContext) {
52
+ async function getEntryData(entry, collectionConfig, shouldEmitFile, pluginContext) {
53
53
  let data;
54
54
  if (collectionConfig.type === "data") {
55
55
  data = entry.unvalidatedData;
@@ -60,7 +60,7 @@ async function getEntryData(entry, collectionConfig, pluginContext) {
60
60
  let schema = collectionConfig.schema;
61
61
  if (typeof schema === "function") {
62
62
  schema = schema({
63
- image: createImage(pluginContext, entry._internal.filePath)
63
+ image: createImage(pluginContext, shouldEmitFile, entry._internal.filePath)
64
64
  });
65
65
  }
66
66
  if (schema) {
@@ -44,9 +44,13 @@ function astroContentImportPlugin({
44
44
  const contentEntryConfigByExt = getEntryConfigByExtMap(settings.contentEntryTypes);
45
45
  const dataEntryConfigByExt = getEntryConfigByExtMap(settings.dataEntryTypes);
46
46
  const { contentDir } = contentPaths;
47
+ let shouldEmitFile = false;
47
48
  const plugins = [
48
49
  {
49
50
  name: "astro:content-imports",
51
+ config(_config, env) {
52
+ shouldEmitFile = env.command === "build";
53
+ },
50
54
  async transform(_, viteId) {
51
55
  if (hasContentFlag(viteId, DATA_FLAG)) {
52
56
  const fileId = viteId.split("?")[0] ?? viteId;
@@ -56,7 +60,8 @@ function astroContentImportPlugin({
56
60
  contentDir,
57
61
  config: settings.config,
58
62
  fs,
59
- pluginContext: this
63
+ pluginContext: this,
64
+ shouldEmitFile
60
65
  });
61
66
  const code = `
62
67
  export const id = ${JSON.stringify(id)};
@@ -77,7 +82,8 @@ export const _internal = {
77
82
  contentDir,
78
83
  config: settings.config,
79
84
  fs,
80
- pluginContext: this
85
+ pluginContext: this,
86
+ shouldEmitFile
81
87
  });
82
88
  const code = `
83
89
  export const id = ${JSON.stringify(id)};
@@ -158,6 +164,7 @@ async function getContentEntryModule(params) {
158
164
  const data = collectionConfig ? await getEntryData(
159
165
  { id, collection, _internal, unvalidatedData },
160
166
  collectionConfig,
167
+ params.shouldEmitFile,
161
168
  pluginContext
162
169
  ) : unvalidatedData;
163
170
  const contentEntryModule = {
@@ -182,6 +189,7 @@ async function getDataEntryModule(params) {
182
189
  const data = collectionConfig ? await getEntryData(
183
190
  { id, collection, _internal, unvalidatedData },
184
191
  collectionConfig,
192
+ params.shouldEmitFile,
185
193
  pluginContext
186
194
  ) : unvalidatedData;
187
195
  const dataEntryModule = {
@@ -1,3 +1,4 @@
1
+ /// <reference types="node" resolution-mode="require"/>
1
2
  import type { OutgoingHttpHeaders } from 'node:http';
2
3
  /**
3
4
  * Takes in a nullable WebAPI Headers object and produces a NodeJS OutgoingHttpHeaders object suitable for usage