@altano/astro-prettier-response 1.0.0 → 1.0.1
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
|
@@ -6,7 +6,6 @@ declare const integration: (options?: {
|
|
|
6
6
|
disableMinifiers?: boolean | undefined;
|
|
7
7
|
formatXml?: boolean | undefined;
|
|
8
8
|
}) => AstroIntegration & {};
|
|
9
|
-
|
|
10
9
|
//#endregion
|
|
11
10
|
//#region src/config.d.ts
|
|
12
11
|
declare const ConfigSchema: z.ZodDefault<z.ZodOptional<z.ZodObject<{
|
|
@@ -20,7 +19,6 @@ declare const ConfigSchema: z.ZodDefault<z.ZodOptional<z.ZodObject<{
|
|
|
20
19
|
formatXml?: boolean | undefined;
|
|
21
20
|
}>>>;
|
|
22
21
|
type AstroPrettierResponseConfig = z.infer<typeof ConfigSchema>;
|
|
23
|
-
|
|
24
22
|
//#endregion
|
|
25
23
|
export { AstroPrettierResponseConfig, integration as default };
|
|
26
24
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["ConfigSchema: z.ZodDefault<\n z.ZodOptional<\n z.ZodObject<\n {\n disableMinifiers: z.ZodDefault<z.ZodBoolean>;\n formatXml: z.ZodDefault<z.ZodBoolean>;\n },\n \"strict\",\n z.ZodTypeAny,\n {\n disableMinifiers: boolean;\n formatXml: boolean;\n },\n {\n disableMinifiers?: boolean | undefined;\n formatXml?: boolean | undefined;\n }\n >\n >\n>","integration: (options?: {\n disableMinifiers?: boolean | undefined;\n formatXml?: boolean | undefined;\n}) => AstroIntegration & {}"],"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 \"strict\",\n z.ZodTypeAny,\n {\n disableMinifiers: boolean;\n formatXml: boolean;\n },\n {\n disableMinifiers?: boolean | undefined;\n formatXml?: boolean | undefined;\n }\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({});\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 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,MAAaA,eAmBT,EACD,OAAO;CAeN,kBAAkB,EAAE,SAAS,CAAC,QAAQ,KAAK;CAY3C,WAAW,EAAE,SAAS,CAAC,QAAQ,MAAM;AACtC,EAAC,CACD,QAAQ,CACR,UAAU,CACV,QAAQ,CAAE,EAAC;;;;AC5Cd,MAAMC,cAGwB,kBAAkB;CAC9C,MAAM;CACN,eAAe;CACf,MAAM,EAAE,SAAS,MAAM,EAAE;EACvB,MAAM,EAAE,SAAS,GAAG,eAAe,OAAO,KAAK,IAAI;AAEnD,SAAO,EACL,OAAO,EACL,sBAAsB,CAAC,WAAW;AAChC,qBAAkB,QAAQ;IACxB;IACA,SAAS,EACP,2CAA2C,iBAAiB,KAAK,UAAU,aAAa,MAAM,QAAQ,CAAC,CAAC,EACzG;GACF,EAAC;AAEF,iBAAc,QAAQ,EAAE,MAAM,0BAA2B,EAAC;GAE1D,MAAM,EAAE,QAAQ,cAAc,eAAe,QAAQ,GAAG;AAExD,OAAI,QAAQ,kBAAkB;IAC5B,IAAI,oBAAoB;AAExB,QAAI,OAAO,iBAAiB,OAAO;AACjC,yBAAoB;AACpB,kBAAa,EACX,cAAc,MACf,EAAC;IACH;AAED,QAAI,QAAQ,MAAM,OAAO,WAAW,OAAO;AACzC,yBAAoB;AACpB,kBAAa,EACX,MAAM,EACJ,OAAO,EACL,QAAQ,MACT,EACF,EACF,EAAC;IACH;AAED,QAAI,QAAQ,MAAM,OAAO,cAAc,OAAO;AAC5C,yBAAoB;AACpB,kBAAa,EACX,MAAM,EACJ,OAAO,EACL,WAAW,MACZ,EACF,EACF,EAAC;IACH;AAED,QAAI,kBACF,QAAO,MACJ,4JACF;GAEJ;AAED,iBAAc;IACZ,YAAY,QAAQ,wBAAwB;IAC5C,OAAO;GACR,EAAC;EACH,EACF,EACF;CACF;AACF,EAAC;AAEF,0BAAe"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["status: number","response: Response","options: Options","source: string","options?: Options","filepath: string","source: string","e: unknown","onRequest: MiddlewareHandler"],"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,MAAMA,QAAyB;AAC7C,QAAO,UAAU,OAAO,SAAS;AAClC;;;;ACCD,SAAgB,mBAAmBC,UAAmC;CACpE,MAAM,cAAc,SAAS,QAAQ,IAAI,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AAe,UAAU,OAAO;AAE9D,QAAO,IAAI,SAAS,iBAAiB;EACnC,QAAQ,SAAS;EACjB,SAAS,MAAM;EACf,YAAY,MAAM;CACnB;AACF,EACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@altano/astro-prettier-response",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Prettier html/css/js for your Astro site",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astro",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"files": [
|
|
29
29
|
"dist/**/*.js",
|
|
30
|
+
"dist/**/*.js.map",
|
|
30
31
|
"dist/**/*.d.ts"
|
|
31
32
|
],
|
|
32
33
|
"dependencies": {
|
|
@@ -41,17 +42,17 @@
|
|
|
41
42
|
"@types/mime-types": "^2.1.4",
|
|
42
43
|
"@types/node": "^22.13.10",
|
|
43
44
|
"@vitest/coverage-v8": "^3.1.4",
|
|
44
|
-
"astro": "^5.
|
|
45
|
+
"astro": "^5.10.1",
|
|
45
46
|
"depcheck": "^1.4.7",
|
|
46
47
|
"prettier": "^3.5.3",
|
|
47
|
-
"tsdown": "^0.12.
|
|
48
|
+
"tsdown": "^0.12.9",
|
|
48
49
|
"typescript": "^5.8.2",
|
|
49
50
|
"vite": "^6.2.6",
|
|
50
51
|
"vitest": "^3.1.4",
|
|
51
52
|
"zx": "^8.5.2",
|
|
52
53
|
"@altano/build-config": "0.0.1",
|
|
53
54
|
"@altano/testing": "0.0.1",
|
|
54
|
-
"@altano/tiny-async-pool": "4.0.
|
|
55
|
+
"@altano/tiny-async-pool": "4.0.1",
|
|
55
56
|
"@altano/tsconfig": "0.0.1"
|
|
56
57
|
},
|
|
57
58
|
"peerDependencies": {
|