@altano/astro-prettier-response 2.0.0 → 3.0.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.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { z } from "astro/zod";
2
2
  import { AstroIntegration } from "astro";
3
-
4
3
  //#region src/integration.d.ts
5
4
  declare const integration: (options?: {
6
5
  disableMinifiers?: boolean | undefined;
package/dist/index.js CHANGED
@@ -1,9 +1,33 @@
1
1
  import { runtimeLogger } from "@inox-tools/runtime-logger";
2
- import { addVirtualImports, createResolver, defineIntegration } from "astro-integration-kit";
3
2
  import { z } from "astro/zod";
4
3
  //#region src/config.ts
5
4
  const ConfigSchema = z.object({
5
+ /**
6
+ * Astro/Vite will minify your HTML/JS/CSS by default. Astro lightly
7
+ * compresses your HTML (not a full minifier) while Vite will fully compress
8
+ * your CSS/JS. Since we're going for prettiest output here, it makes sense
9
+ * for this integration to fully disable such minification.
10
+ *
11
+ * If you set this to false, and you use the default Astro config with
12
+ * minification enabled, you will still get your output formatted by
13
+ * Prettier, but it will be minified output that is formatted. So, for
14
+ * example, most of the variable names in your JS will be one character
15
+ * letters.
16
+ *
17
+ * @defaultValue true
18
+ */
6
19
  disableMinifiers: z.boolean().default(true),
20
+ /**
21
+ * Install `@prettier/plugin-xml` package if you enable this
22
+ *
23
+ * Use the official Prettier plugin to format xml as well. Sitemaps and rss
24
+ * feeds are xml, so this would probably do something on your stock Astro
25
+ * site. Note: the Prettier output is only marginally better because xml is
26
+ * very whitespace sensitive and so Prettier's formatting is very strict and
27
+ * not that much prettier.
28
+ *
29
+ * @defaultValue false
30
+ */
7
31
  formatXml: z.boolean().default(false)
8
32
  }).strict().optional().default({
9
33
  disableMinifiers: true,
@@ -11,19 +35,35 @@ const ConfigSchema = z.object({
11
35
  });
12
36
  //#endregion
13
37
  //#region src/integration.ts
14
- const integration = defineIntegration({
15
- name: "astro-prettier-response",
16
- optionsSchema: ConfigSchema,
17
- setup({ options, name }) {
18
- const { resolve } = createResolver(import.meta.url);
19
- return { hooks: { "astro:config:setup": (params) => {
20
- addVirtualImports(params, {
21
- name,
22
- imports: { "virtual:astro-prettier-response/config": `export default ${JSON.stringify(ConfigSchema.parse(options))}` }
23
- });
24
- runtimeLogger(params, { name: "astro-prettier-response" });
38
+ const VIRTUAL_MODULE_ID = "virtual:astro-prettier-response/config";
39
+ const RESOLVED_VIRTUAL_MODULE_ID = `\0${VIRTUAL_MODULE_ID}`;
40
+ /**
41
+ * A minimal Vite plugin that serves the resolved integration config as a
42
+ * virtual module. Replaces `addVirtualImports` from the (deprecated)
43
+ * astro-integration-kit. Typed structurally (rather than importing Vite's
44
+ * `Plugin`) so this production module doesn't depend on the `vite` dev
45
+ * dependency.
46
+ */
47
+ function virtualConfigPlugin(moduleContents) {
48
+ return {
49
+ name: "astro-prettier-response-virtual-config",
50
+ resolveId(id) {
51
+ return id === VIRTUAL_MODULE_ID ? RESOLVED_VIRTUAL_MODULE_ID : void 0;
52
+ },
53
+ load(id) {
54
+ return id === RESOLVED_VIRTUAL_MODULE_ID ? moduleContents : void 0;
55
+ }
56
+ };
57
+ }
58
+ const integration = (options) => {
59
+ const resolvedOptions = ConfigSchema.parse(options);
60
+ return {
61
+ name: "astro-prettier-response",
62
+ hooks: { "astro:config:setup": (params) => {
25
63
  const { config, updateConfig, addMiddleware, logger } = params;
26
- if (options.disableMinifiers) {
64
+ updateConfig({ vite: { plugins: [virtualConfigPlugin(`export default ${JSON.stringify(resolvedOptions)}`)] } });
65
+ runtimeLogger(params, { name: "astro-prettier-response" });
66
+ if (resolvedOptions.disableMinifiers) {
27
67
  let didConfigOverride = false;
28
68
  if (config.compressHTML !== false) {
29
69
  didConfigOverride = true;
@@ -49,12 +89,12 @@ const integration = defineIntegration({
49
89
  if (didConfigOverride) logger.info(`Disabling minification of html/css/js in Astro config (https://github.com/altano/npm-packages/tree/main/packages/astro-prettier-response#config-overrides)`);
50
90
  }
51
91
  addMiddleware({
52
- entrypoint: resolve("./middleware/index.js"),
92
+ entrypoint: new URL("./middleware/index.js", import.meta.url),
53
93
  order: "post"
54
94
  });
55
- } } };
56
- }
57
- });
95
+ } }
96
+ };
97
+ };
58
98
  //#endregion
59
99
  export { integration as default };
60
100
 
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/config.ts","../src/integration.ts"],"sourcesContent":["import { z } from \"astro/zod\";\n\nexport const ConfigSchema: z.ZodDefault<\n z.ZodOptional<\n z.ZodObject<\n {\n disableMinifiers: z.ZodDefault<z.ZodBoolean>;\n formatXml: z.ZodDefault<z.ZodBoolean>;\n },\n z.core.$strict\n >\n >\n> = z\n .object({\n /**\n * Astro/Vite will minify your HTML/JS/CSS by default. Astro lightly\n * compresses your HTML (not a full minifier) while Vite will fully compress\n * your CSS/JS. Since we're going for prettiest output here, it makes sense\n * for this integration to fully disable such minification.\n *\n * If you set this to false, and you use the default Astro config with\n * minification enabled, you will still get your output formatted by\n * Prettier, but it will be minified output that is formatted. So, for\n * example, most of the variable names in your JS will be one character\n * letters.\n *\n * @defaultValue true\n */\n disableMinifiers: z.boolean().default(true),\n /**\n * Install `@prettier/plugin-xml` package if you enable this\n *\n * Use the official Prettier plugin to format xml as well. Sitemaps and rss\n * feeds are xml, so this would probably do something on your stock Astro\n * site. Note: the Prettier output is only marginally better because xml is\n * very whitespace sensitive and so Prettier's formatting is very strict and\n * not that much prettier.\n *\n * @defaultValue false\n */\n formatXml: z.boolean().default(false),\n })\n .strict()\n .optional()\n .default({ disableMinifiers: true, formatXml: false });\n\nexport type AstroPrettierResponseConfig = z.infer<typeof ConfigSchema>;\n","import { runtimeLogger } from \"@inox-tools/runtime-logger\";\nimport {\n addVirtualImports,\n createResolver,\n defineIntegration,\n} from \"astro-integration-kit\";\nimport { ConfigSchema } from \"./config.js\";\nimport type { AstroIntegration } from \"astro\";\n\nconst integration: (options?: {\n disableMinifiers?: boolean | undefined;\n formatXml?: boolean | undefined;\n}) => AstroIntegration & {} = defineIntegration({\n name: \"astro-prettier-response\",\n optionsSchema: ConfigSchema,\n setup({ options, name }) {\n const { resolve } = createResolver(import.meta.url);\n\n return {\n hooks: {\n \"astro:config:setup\": (params) => {\n addVirtualImports(params, {\n name,\n imports: {\n \"virtual:astro-prettier-response/config\": `export default ${JSON.stringify(ConfigSchema.parse(options))}`,\n },\n });\n\n runtimeLogger(params, { name: \"astro-prettier-response\" });\n\n const { config, updateConfig, addMiddleware, logger } = params;\n\n if (options.disableMinifiers) {\n let didConfigOverride = false;\n\n if (config.compressHTML !== false) {\n didConfigOverride = true;\n updateConfig({\n compressHTML: false,\n });\n }\n\n if (config?.vite?.build?.minify !== false) {\n didConfigOverride = true;\n updateConfig({\n vite: {\n build: {\n minify: false,\n },\n },\n });\n }\n\n if (config?.vite?.build?.cssMinify !== false) {\n didConfigOverride = true;\n updateConfig({\n vite: {\n build: {\n cssMinify: false,\n },\n },\n });\n }\n\n // Astro 6 hardcodes minify: true for the client environment,\n // ignoring vite.build.minify. Use a plugin to override it.\n if (\n config?.vite?.environments?.[\"client\"]?.build?.minify !== false\n ) {\n didConfigOverride = true;\n updateConfig({\n vite: {\n plugins: [\n {\n name: \"astro-prettier-response-disable-client-minify\",\n config() {\n return {\n environments: {\n client: {\n build: {\n minify: false,\n },\n },\n },\n };\n },\n },\n ],\n },\n });\n }\n\n if (didConfigOverride) {\n logger.info(\n `Disabling minification of html/css/js in Astro config (https://github.com/altano/npm-packages/tree/main/packages/astro-prettier-response#config-overrides)`,\n );\n }\n }\n\n addMiddleware({\n entrypoint: resolve(\"./middleware/index.js\"),\n order: \"post\",\n });\n },\n },\n };\n },\n});\n\nexport default integration;\n"],"mappings":";;;;AAEA,MAAa,eAUT,EACD,OAAO;CAeN,kBAAkB,EAAE,SAAS,CAAC,QAAQ,KAAK;CAY3C,WAAW,EAAE,SAAS,CAAC,QAAQ,MAAM;CACtC,CAAC,CACD,QAAQ,CACR,UAAU,CACV,QAA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integration.ts"],"sourcesContent":["import { z } from \"astro/zod\";\n\nexport const ConfigSchema: z.ZodDefault<\n z.ZodOptional<\n z.ZodObject<\n {\n disableMinifiers: z.ZodDefault<z.ZodBoolean>;\n formatXml: z.ZodDefault<z.ZodBoolean>;\n },\n z.core.$strict\n >\n >\n> = z\n .object({\n /**\n * Astro/Vite will minify your HTML/JS/CSS by default. Astro lightly\n * compresses your HTML (not a full minifier) while Vite will fully compress\n * your CSS/JS. Since we're going for prettiest output here, it makes sense\n * for this integration to fully disable such minification.\n *\n * If you set this to false, and you use the default Astro config with\n * minification enabled, you will still get your output formatted by\n * Prettier, but it will be minified output that is formatted. So, for\n * example, most of the variable names in your JS will be one character\n * letters.\n *\n * @defaultValue true\n */\n disableMinifiers: z.boolean().default(true),\n /**\n * Install `@prettier/plugin-xml` package if you enable this\n *\n * Use the official Prettier plugin to format xml as well. Sitemaps and rss\n * feeds are xml, so this would probably do something on your stock Astro\n * site. Note: the Prettier output is only marginally better because xml is\n * very whitespace sensitive and so Prettier's formatting is very strict and\n * not that much prettier.\n *\n * @defaultValue false\n */\n formatXml: z.boolean().default(false),\n })\n .strict()\n .optional()\n .default({ disableMinifiers: true, formatXml: false });\n\nexport type AstroPrettierResponseConfig = z.infer<typeof ConfigSchema>;\n","import { runtimeLogger } from \"@inox-tools/runtime-logger\";\nimport { ConfigSchema } from \"./config.js\";\nimport type { AstroIntegration } from \"astro\";\n\nconst VIRTUAL_MODULE_ID = \"virtual:astro-prettier-response/config\";\nconst RESOLVED_VIRTUAL_MODULE_ID = `\\0${VIRTUAL_MODULE_ID}`;\n\n/**\n * A minimal Vite plugin that serves the resolved integration config as a\n * virtual module. Replaces `addVirtualImports` from the (deprecated)\n * astro-integration-kit. Typed structurally (rather than importing Vite's\n * `Plugin`) so this production module doesn't depend on the `vite` dev\n * dependency.\n */\nexport function virtualConfigPlugin(moduleContents: string): {\n name: string;\n resolveId(id: string): string | undefined;\n load(id: string): string | undefined;\n} {\n return {\n name: \"astro-prettier-response-virtual-config\",\n resolveId(id) {\n return id === VIRTUAL_MODULE_ID ? RESOLVED_VIRTUAL_MODULE_ID : undefined;\n },\n load(id) {\n return id === RESOLVED_VIRTUAL_MODULE_ID ? moduleContents : undefined;\n },\n };\n}\n\nconst integration: (options?: {\n disableMinifiers?: boolean | undefined;\n formatXml?: boolean | undefined;\n}) => AstroIntegration & {} = (options) => {\n const resolvedOptions = ConfigSchema.parse(options);\n\n return {\n name: \"astro-prettier-response\",\n hooks: {\n \"astro:config:setup\": (params) => {\n const { config, updateConfig, addMiddleware, logger } = params;\n\n updateConfig({\n vite: {\n plugins: [\n virtualConfigPlugin(\n `export default ${JSON.stringify(resolvedOptions)}`,\n ),\n ],\n },\n });\n\n runtimeLogger(params, { name: \"astro-prettier-response\" });\n\n if (resolvedOptions.disableMinifiers) {\n let didConfigOverride = false;\n\n if (config.compressHTML !== false) {\n didConfigOverride = true;\n updateConfig({\n compressHTML: false,\n });\n }\n\n if (config?.vite?.build?.minify !== false) {\n didConfigOverride = true;\n updateConfig({\n vite: {\n build: {\n minify: false,\n },\n },\n });\n }\n\n if (config?.vite?.build?.cssMinify !== false) {\n didConfigOverride = true;\n updateConfig({\n vite: {\n build: {\n cssMinify: false,\n },\n },\n });\n }\n\n // Astro 6 hardcodes minify: true for the client environment,\n // ignoring vite.build.minify. Use a plugin to override it.\n if (config?.vite?.environments?.[\"client\"]?.build?.minify !== false) {\n didConfigOverride = true;\n updateConfig({\n vite: {\n plugins: [\n {\n name: \"astro-prettier-response-disable-client-minify\",\n config() {\n return {\n environments: {\n client: {\n build: {\n minify: false,\n },\n },\n },\n };\n },\n },\n ],\n },\n });\n }\n\n if (didConfigOverride) {\n logger.info(\n `Disabling minification of html/css/js in Astro config (https://github.com/altano/npm-packages/tree/main/packages/astro-prettier-response#config-overrides)`,\n );\n }\n }\n\n addMiddleware({\n entrypoint: new URL(\"./middleware/index.js\", import.meta.url),\n order: \"post\",\n });\n },\n },\n };\n};\n\nexport default integration;\n"],"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AAI,QAAQ,MAAM,eAAe,SAAS,EAAE,OAAO,WAAW,OAAO;KACnE,oBAAoB;KACpB,aAAa,EACX,MAAM,EACJ,SAAS,CACP;MACE,MAAM;MACN,SAAS;OACP,OAAO,EACL,cAAc,EACZ,QAAQ,EACN,OAAO,EACL,QAAQ,MACV,EACF,EACF,EACF;MACF;KACF,CACF,EACF,EACF,CAAC;IACH;IAEA,IAAI,mBACF,OAAO,KACL,4JACF;GAEJ;GAEA,cAAc;IACZ,YAAY,IAAI,IAAI,yBAAyB,OAAO,KAAK,GAAG;IAC5D,OAAO;GACT,CAAC;EACH,EACF;CACF;AACF"}
@@ -1,5 +1,4 @@
1
1
  import { MiddlewareHandler } from "astro";
2
-
3
2
  //#region src/middleware/index.d.ts
4
3
  declare const onRequest: MiddlewareHandler;
5
4
  //#endregion
@@ -50,7 +50,7 @@ async function formatOrReturn(filepath, source) {
50
50
  }
51
51
  //#endregion
52
52
  //#region src/middleware/index.ts
53
- const onRequest = defineMiddleware(async (context, next) => {
53
+ const onRequest = defineMiddleware(async (_context, next) => {
54
54
  const response = await next();
55
55
  if (!response.ok && !is400(response.status)) {
56
56
  logger.debug(`NOT formatting (status is not 2xx or 4xx)`);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../src/middleware/is400.ts","../../src/middleware/filename.ts","../../src/middleware/prettier.ts","../../src/middleware/format.ts","../../src/middleware/index.ts"],"sourcesContent":["export function is400(status: number): boolean {\n return status >= 400 && status < 500;\n}\n","import mime from \"mime-types\";\nimport { is400 } from \"./is400.js\";\n\nexport function synthesizeFilename(response: Response): string | null {\n const contentType = response.headers.get(\"content-type\");\n // I don't know how to produce a null content-type in testing\n /* v8 ignore next 8 */\n if (contentType == null) {\n if (is400(response.status)) {\n // let's just say all 400 responses with no Content-Type are html?\n return \"index.html\";\n }\n\n return null;\n }\n\n // We have a content-type, so derive the filename from that\n const extensionFromContentType = mime.extension(contentType);\n if (!extensionFromContentType) {\n // I'm pretty sure everything that Prettier can parse has a mime-type in the\n // mime-types db, so this is probably something we don't know anything\n // about. Let's just bail.\n return null;\n }\n\n return `file.${extensionFromContentType}`;\n}\n","import prettier, { type Options } from \"prettier\";\nimport config from \"virtual:astro-prettier-response/config\";\n\nfunction enhanceOptions(options: Options = {}): Options {\n if (!config.formatXml) {\n return options;\n }\n\n const givenPlugins = options.plugins ?? [];\n return {\n ...options,\n plugins: [...givenPlugins, \"@prettier/plugin-xml\"],\n };\n}\n\nexport async function format(\n source: string,\n options?: Options,\n): Promise<string> {\n return prettier.format(source, enhanceOptions(options));\n}\n\nexport async function getFileInfo(\n source: string,\n options?: Options,\n): Promise<prettier.FileInfoResult> {\n return prettier.getFileInfo(source, enhanceOptions(options));\n}\n","import { logger } from \"@it-astro:logger:astro-prettier-response\";\nimport { format } from \"./prettier.js\";\n\nexport async function formatOrReturn(\n filepath: string,\n source: string,\n): Promise<string> {\n try {\n const result = await format(source, {\n filepath: filepath,\n });\n logger.debug(`successfully formatted`);\n return result;\n } catch (e: unknown) {\n logger.error(`failed to format`);\n return source;\n }\n}\n","import { defineMiddleware } from \"astro/middleware\";\nimport { synthesizeFilename } from \"./filename.js\";\nimport { formatOrReturn } from \"./format.js\";\nimport { getFileInfo } from \"./prettier.js\";\nimport { is400 } from \"./is400.js\";\nimport { logger } from \"@it-astro:logger:astro-prettier-response\";\nimport type { MiddlewareHandler } from \"astro\";\n\nexport const onRequest: MiddlewareHandler = defineMiddleware(\n async (context, next) => {\n const response = await next();\n\n // Only attempt formatting on Responses that are OK or have a 400 error (e.g.\n // redirect pages or 404 pages)\n if (!response.ok && !is400(response.status)) {\n logger.debug(`NOT formatting (status is not 2xx or 4xx)`);\n return response;\n }\n\n const filename = synthesizeFilename(response);\n if (filename == null) {\n logger.debug(`NOT formatting (couldn't determine filename/extension)`);\n return response;\n }\n const fileInfo = await getFileInfo(filename);\n if (fileInfo?.inferredParser == null) {\n logger.debug(\n `NOT formatting (Prettier has no parser for this file type)`,\n );\n return response;\n }\n\n const clone = response.clone();\n const source = await clone.text();\n const processedSource = await formatOrReturn(filename, source);\n\n return new Response(processedSource, {\n status: response.status,\n headers: clone.headers,\n statusText: clone.statusText,\n });\n },\n);\n"],"mappings":";;;;;;AAAA,SAAgB,MAAM,QAAyB;AAC7C,QAAO,UAAU,OAAO,SAAS;;;;ACEnC,SAAgB,mBAAmB,UAAmC;CACpE,MAAM,cAAc,SAAS,QAAQ,IAAI,eAAe;;AAGxD,KAAI,eAAe,MAAM;AACvB,MAAI,MAAM,SAAS,OAAO,CAExB,QAAO;AAGT,SAAO;;CAIT,MAAM,2BAA2B,KAAK,UAAU,YAAY;AAC5D,KAAI,CAAC,yBAIH,QAAO;AAGT,QAAO,QAAQ;;;;ACtBjB,SAAS,eAAe,UAAmB,EAAE,EAAW;AACtD,KAAI,CAAC,OAAO,UACV,QAAO;CAGT,MAAM,eAAe,QAAQ,WAAW,EAAE;AAC1C,QAAO;EACL,GAAG;EACH,SAAS,CAAC,GAAG,cAAc,uBAAuB;EACnD;;AAGH,eAAsB,OACpB,QACA,SACiB;AACjB,QAAO,SAAS,OAAO,QAAQ,eAAe,QAAQ,CAAC;;AAGzD,eAAsB,YACpB,QACA,SACkC;AAClC,QAAO,SAAS,YAAY,QAAQ,eAAe,QAAQ,CAAC;;;;ACvB9D,eAAsB,eACpB,UACA,QACiB;AACjB,KAAI;EACF,MAAM,SAAS,MAAM,OAAO,QAAQ,EACxB,UACX,CAAC;AACF,SAAO,MAAM,yBAAyB;AACtC,SAAO;UACA,GAAY;AACnB,SAAO,MAAM,mBAAmB;AAChC,SAAO;;;;;ACPX,MAAa,YAA+B,iBAC1C,OAAO,SAAS,SAAS;CACvB,MAAM,WAAW,MAAM,MAAM;AAI7B,KAAI,CAAC,SAAS,MAAM,CAAC,MAAM,SAAS,OAAO,EAAE;AAC3C,SAAO,MAAM,4CAA4C;AACzD,SAAO;;CAGT,MAAM,WAAW,mBAAmB,SAAS;AAC7C,KAAI,YAAY,MAAM;AACpB,SAAO,MAAM,yDAAyD;AACtE,SAAO;;AAGT,MADiB,MAAM,YAAY,SAAS,GAC9B,kBAAkB,MAAM;AACpC,SAAO,MACL,6DACD;AACD,SAAO;;CAGT,MAAM,QAAQ,SAAS,OAAO;CAE9B,MAAM,kBAAkB,MAAM,eAAe,UAD9B,MAAM,MAAM,MAAM,CAC6B;AAE9D,QAAO,IAAI,SAAS,iBAAiB;EACnC,QAAQ,SAAS;EACjB,SAAS,MAAM;EACf,YAAY,MAAM;EACnB,CAAC;EAEL"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/middleware/is400.ts","../../src/middleware/filename.ts","../../src/middleware/prettier.ts","../../src/middleware/format.ts","../../src/middleware/index.ts"],"sourcesContent":["export function is400(status: number): boolean {\n return status >= 400 && status < 500;\n}\n","import mime from \"mime-types\";\nimport { is400 } from \"./is400.js\";\n\nexport function synthesizeFilename(response: Response): string | null {\n const contentType = response.headers.get(\"content-type\");\n // I don't know how to produce a null content-type in testing\n /* v8 ignore next 8 */\n if (contentType == null) {\n if (is400(response.status)) {\n // let's just say all 400 responses with no Content-Type are html?\n return \"index.html\";\n }\n\n return null;\n }\n\n // We have a content-type, so derive the filename from that\n const extensionFromContentType = mime.extension(contentType);\n if (!extensionFromContentType) {\n // I'm pretty sure everything that Prettier can parse has a mime-type in the\n // mime-types db, so this is probably something we don't know anything\n // about. Let's just bail.\n return null;\n }\n\n return `file.${extensionFromContentType}`;\n}\n","import prettier, { type Options } from \"prettier\";\nimport config from \"virtual:astro-prettier-response/config\";\n\nfunction enhanceOptions(options: Options = {}): Options {\n if (!config.formatXml) {\n return options;\n }\n\n const givenPlugins = options.plugins ?? [];\n return {\n ...options,\n plugins: [...givenPlugins, \"@prettier/plugin-xml\"],\n };\n}\n\nexport async function format(\n source: string,\n options?: Options,\n): Promise<string> {\n return prettier.format(source, enhanceOptions(options));\n}\n\nexport async function getFileInfo(\n source: string,\n options?: Options,\n): Promise<prettier.FileInfoResult> {\n return prettier.getFileInfo(source, enhanceOptions(options));\n}\n","import { logger } from \"@it-astro:logger:astro-prettier-response\";\nimport { format } from \"./prettier.js\";\n\nexport async function formatOrReturn(\n filepath: string,\n source: string,\n): Promise<string> {\n try {\n const result = await format(source, {\n filepath: filepath,\n });\n logger.debug(`successfully formatted`);\n return result;\n } catch (e: unknown) {\n logger.error(`failed to format`);\n return source;\n }\n}\n","import { defineMiddleware } from \"astro/middleware\";\nimport { synthesizeFilename } from \"./filename.js\";\nimport { formatOrReturn } from \"./format.js\";\nimport { getFileInfo } from \"./prettier.js\";\nimport { is400 } from \"./is400.js\";\nimport { logger } from \"@it-astro:logger:astro-prettier-response\";\nimport type { MiddlewareHandler } from \"astro\";\n\nexport const onRequest: MiddlewareHandler = defineMiddleware(\n async (_context, next) => {\n const response = await next();\n\n // Only attempt formatting on Responses that are OK or have a 400 error (e.g.\n // redirect pages or 404 pages)\n if (!response.ok && !is400(response.status)) {\n logger.debug(`NOT formatting (status is not 2xx or 4xx)`);\n return response;\n }\n\n const filename = synthesizeFilename(response);\n if (filename == null) {\n logger.debug(`NOT formatting (couldn't determine filename/extension)`);\n return response;\n }\n const fileInfo = await getFileInfo(filename);\n if (fileInfo?.inferredParser == null) {\n logger.debug(\n `NOT formatting (Prettier has no parser for this file type)`,\n );\n return response;\n }\n\n const clone = response.clone();\n const source = await clone.text();\n const processedSource = await formatOrReturn(filename, source);\n\n return new Response(processedSource, {\n status: response.status,\n headers: clone.headers,\n statusText: clone.statusText,\n });\n },\n);\n"],"mappings":";;;;;;AAAA,SAAgB,MAAM,QAAyB;CAC7C,OAAO,UAAU,OAAO,SAAS;AACnC;;;ACCA,SAAgB,mBAAmB,UAAmC;CACpE,MAAM,cAAc,SAAS,QAAQ,IAAI,cAAc;;CAGvD,IAAI,eAAe,MAAM;EACvB,IAAI,MAAM,SAAS,MAAM,GAEvB,OAAO;EAGT,OAAO;CACT;CAGA,MAAM,2BAA2B,KAAK,UAAU,WAAW;CAC3D,IAAI,CAAC,0BAIH,OAAO;C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astro-prettier-response",
3
- "version": "2.0.0",
3
+ "version": "3.0.0",
4
4
  "description": "Prettier html/css/js for your Astro site",
5
5
  "keywords": [
6
6
  "astro",
@@ -16,7 +16,7 @@
16
16
  "license": "ISC",
17
17
  "author": {
18
18
  "name": "Alan Norbauer",
19
- "email": "altano@gmail.com",
19
+ "email": "alan@norbauer.com",
20
20
  "url": "https://alan.norbauer.com"
21
21
  },
22
22
  "sideEffects": false,
@@ -31,32 +31,31 @@
31
31
  "dist/**/*.d.ts"
32
32
  ],
33
33
  "dependencies": {
34
- "@inox-tools/runtime-logger": "^0.8.1",
35
- "astro-integration-kit": "^0.20.0",
34
+ "@inox-tools/runtime-logger": "^2.0.0",
36
35
  "mime-types": "^3.0.2"
37
36
  },
38
37
  "devDependencies": {
39
- "@astrojs/check": "^0.9.8",
40
- "@inox-tools/astro-tests": "^0.9.1",
38
+ "@astrojs/check": "^0.9.9",
39
+ "@inox-tools/astro-tests": "^2.0.0",
41
40
  "@prettier/plugin-xml": "^3.4.1",
42
- "@types/mime-types": "^2.1.4",
43
- "@types/node": "^25.5.0",
44
- "@vitest/coverage-v8": "^4.1.0",
45
- "astro": "^6.0.8",
41
+ "@types/mime-types": "^3.0.1",
42
+ "@types/node": "^26.1.1",
43
+ "@vitest/coverage-v8": "^4.1.10",
44
+ "astro": "^7.1.3",
46
45
  "depcheck": "^1.4.7",
47
- "prettier": "^3.8.1",
48
- "tsdown": "^0.21.4",
49
- "typescript": "^5.9.3",
50
- "vite": "^7.3.1",
51
- "vitest": "^4.1.0",
46
+ "prettier": "3.9.6",
47
+ "tsdown": "^0.22.13",
48
+ "typescript": "^6.0.3",
49
+ "vite": "^8.1.5",
50
+ "vitest": "^4.1.10",
52
51
  "zx": "^8.8.5",
53
52
  "@altano/build-config": "0.0.1",
54
53
  "@altano/testing": "0.0.1",
55
- "@altano/tiny-async-pool": "4.0.2",
54
+ "@altano/tiny-async-pool": "4.0.3",
56
55
  "@altano/tsconfig": "0.0.1"
57
56
  },
58
57
  "peerDependencies": {
59
- "astro": "6.x.x",
58
+ "astro": "7.x.x",
60
59
  "prettier": "3.x.x"
61
60
  },
62
61
  "optionalDependencies": {
@@ -66,14 +65,15 @@
66
65
  "access": "public"
67
66
  },
68
67
  "scripts": {
69
- "build": "tsdown --config-loader unrun",
68
+ "build": "tsdown",
70
69
  "check:types:astro": "node --experimental-strip-types ./scripts/check-fixtures.ts",
71
70
  "check:types:src": "tsc --noEmit",
72
71
  "check:types:test": "tsc --noEmit --project ./tests/tsconfig.json",
73
72
  "check:unused-dependencies": "depcheck",
74
73
  "clean": "rm -rf .turbo && rm -rf .tsbuildinfo && rm -rf node_modules && rm -rf dist",
75
74
  "dev": "tsdown --watch",
76
- "format": "prettier --write src",
75
+ "format": "prettier --check .",
76
+ "format:fix": "prettier --write .",
77
77
  "lint": "eslint",
78
78
  "lint:fix": "TIMING=1 eslint src/**/*.ts* --fix",
79
79
  "lint:timing": "TIMING=1 eslint src/**/*.ts*",
Binary file