@altano/astro-prettier-response 0.0.2 → 1.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/README.md +2 -2
- package/dist/index.d.ts +25 -2
- package/dist/index.js +47 -98
- package/dist/middleware/index.d.ts +7 -1
- package/dist/middleware/index.js +66 -74
- package/package.json +12 -13
- package/dist/config.d.ts +0 -38
- package/dist/integration.d.ts +0 -6
- package/dist/middleware/filename.d.ts +0 -2
- package/dist/middleware/format.d.ts +0 -2
- package/dist/middleware/is400.d.ts +0 -2
- package/dist/middleware/prettier.d.ts +0 -4
package/README.md
CHANGED
|
@@ -95,7 +95,7 @@ export default defineConfig({
|
|
|
95
95
|
});
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
-
And if you like the behavior but you just hate seeing this logged every time you build, then configure Astro to disable minification yourself and the integration will stop logging:
|
|
98
|
+
And if you like the default behavior but you just hate seeing this logged every time you build, then configure Astro to disable minification yourself and the integration will stop logging:
|
|
99
99
|
|
|
100
100
|
```js
|
|
101
101
|
import prettierResponse from "@altano/astro-prettier-response";
|
|
@@ -112,7 +112,7 @@ export default defineConfig({
|
|
|
112
112
|
});
|
|
113
113
|
```
|
|
114
114
|
|
|
115
|
-
This will
|
|
115
|
+
This will do exactly the same thing (disable minification) and will still run your output through Prettier, but the log line will go away.
|
|
116
116
|
|
|
117
117
|
## How This Integration Works
|
|
118
118
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { z } from "astro/zod";
|
|
2
|
+
import { AstroIntegration } from "astro";
|
|
3
|
+
|
|
4
|
+
//#region src/integration.d.ts
|
|
5
|
+
declare const integration: (options?: {
|
|
6
|
+
disableMinifiers?: boolean | undefined;
|
|
7
|
+
formatXml?: boolean | undefined;
|
|
8
|
+
}) => AstroIntegration & {};
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/config.d.ts
|
|
12
|
+
declare const ConfigSchema: z.ZodDefault<z.ZodOptional<z.ZodObject<{
|
|
13
|
+
disableMinifiers: z.ZodDefault<z.ZodBoolean>;
|
|
14
|
+
formatXml: z.ZodDefault<z.ZodBoolean>;
|
|
15
|
+
}, "strict", z.ZodTypeAny, {
|
|
16
|
+
disableMinifiers: boolean;
|
|
17
|
+
formatXml: boolean;
|
|
18
|
+
}, {
|
|
19
|
+
disableMinifiers?: boolean | undefined;
|
|
20
|
+
formatXml?: boolean | undefined;
|
|
21
|
+
}>>>;
|
|
22
|
+
type AstroPrettierResponseConfig = z.infer<typeof ConfigSchema>;
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
export { AstroPrettierResponseConfig, integration as default };
|
|
3
26
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,103 +1,52 @@
|
|
|
1
|
-
// src/integration.ts
|
|
2
1
|
import { runtimeLogger } from "@inox-tools/runtime-logger";
|
|
3
|
-
import {
|
|
4
|
-
addVirtualImports,
|
|
5
|
-
createResolver,
|
|
6
|
-
defineIntegration
|
|
7
|
-
} from "astro-integration-kit";
|
|
8
|
-
|
|
9
|
-
// src/config.ts
|
|
2
|
+
import { addVirtualImports, createResolver, defineIntegration } from "astro-integration-kit";
|
|
10
3
|
import { z } from "astro/zod";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
* for this integration to fully disable such minification.
|
|
17
|
-
*
|
|
18
|
-
* If you set this to false, and you use the default Astro config with
|
|
19
|
-
* minification enabled, you will still get your output formatted by
|
|
20
|
-
* Prettier, but it will be minified output that is formatted. So, for
|
|
21
|
-
* example, most of the variable names in your JS will be one character
|
|
22
|
-
* letters.
|
|
23
|
-
*
|
|
24
|
-
* @defaultValue true
|
|
25
|
-
*/
|
|
26
|
-
disableMinifiers: z.boolean().default(true),
|
|
27
|
-
/**
|
|
28
|
-
* Install `@prettier/plugin-xml` package if you enable this
|
|
29
|
-
*
|
|
30
|
-
* Use the official Prettier plugin to format xml as well. Sitemaps and rss
|
|
31
|
-
* feeds are xml, so this would probably do something on your stock Astro
|
|
32
|
-
* site. Note: the Prettier output is only marginally better because xml is
|
|
33
|
-
* very whitespace sensitive and so Prettier's formatting is very strict and
|
|
34
|
-
* not that much prettier.
|
|
35
|
-
*
|
|
36
|
-
* @defaultValue false
|
|
37
|
-
*/
|
|
38
|
-
formatXml: z.boolean().default(false)
|
|
4
|
+
|
|
5
|
+
//#region src/config.ts
|
|
6
|
+
const ConfigSchema = z.object({
|
|
7
|
+
disableMinifiers: z.boolean().default(true),
|
|
8
|
+
formatXml: z.boolean().default(false)
|
|
39
9
|
}).strict().optional().default({});
|
|
40
10
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
didConfigOverride = true;
|
|
78
|
-
updateConfig({
|
|
79
|
-
vite: {
|
|
80
|
-
build: {
|
|
81
|
-
cssMinify: false
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
if (didConfigOverride) {
|
|
87
|
-
logger.info(
|
|
88
|
-
`Disabling minification of html/css/js in Astro config (https://github.com/altano/npm-packages/tree/main/packages/astro-prettier-response#config-overrides)`
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
addMiddleware({
|
|
93
|
-
entrypoint: resolve("./middleware/index.js"),
|
|
94
|
-
order: "post"
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
}
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/integration.ts
|
|
13
|
+
const integration = defineIntegration({
|
|
14
|
+
name: "astro-prettier-response",
|
|
15
|
+
optionsSchema: ConfigSchema,
|
|
16
|
+
setup({ options, name }) {
|
|
17
|
+
const { resolve } = createResolver(import.meta.url);
|
|
18
|
+
return { hooks: { "astro:config:setup": (params) => {
|
|
19
|
+
addVirtualImports(params, {
|
|
20
|
+
name,
|
|
21
|
+
imports: { "virtual:astro-prettier-response/config": `export default ${JSON.stringify(ConfigSchema.parse(options))}` }
|
|
22
|
+
});
|
|
23
|
+
runtimeLogger(params, { name: "astro-prettier-response" });
|
|
24
|
+
const { config, updateConfig, addMiddleware, logger } = params;
|
|
25
|
+
if (options.disableMinifiers) {
|
|
26
|
+
let didConfigOverride = false;
|
|
27
|
+
if (config.compressHTML !== false) {
|
|
28
|
+
didConfigOverride = true;
|
|
29
|
+
updateConfig({ compressHTML: false });
|
|
30
|
+
}
|
|
31
|
+
if (config?.vite?.build?.minify !== false) {
|
|
32
|
+
didConfigOverride = true;
|
|
33
|
+
updateConfig({ vite: { build: { minify: false } } });
|
|
34
|
+
}
|
|
35
|
+
if (config?.vite?.build?.cssMinify !== false) {
|
|
36
|
+
didConfigOverride = true;
|
|
37
|
+
updateConfig({ vite: { build: { cssMinify: false } } });
|
|
38
|
+
}
|
|
39
|
+
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)`);
|
|
40
|
+
}
|
|
41
|
+
addMiddleware({
|
|
42
|
+
entrypoint: resolve("./middleware/index.js"),
|
|
43
|
+
order: "post"
|
|
44
|
+
});
|
|
45
|
+
} } };
|
|
46
|
+
}
|
|
100
47
|
});
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
48
|
+
var integration_default = integration;
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
export { integration_default as default };
|
|
52
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { MiddlewareHandler } from "astro";
|
|
2
|
+
|
|
3
|
+
//#region src/middleware/index.d.ts
|
|
4
|
+
declare const onRequest: MiddlewareHandler;
|
|
5
|
+
|
|
6
|
+
//#endregion
|
|
7
|
+
export { onRequest };
|
|
2
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/middleware/index.js
CHANGED
|
@@ -1,94 +1,86 @@
|
|
|
1
|
-
// src/middleware/index.ts
|
|
2
1
|
import { defineMiddleware } from "astro/middleware";
|
|
3
|
-
|
|
4
|
-
// src/middleware/filename.ts
|
|
5
2
|
import mime from "mime-types";
|
|
3
|
+
import { logger } from "@it-astro:logger:astro-prettier-response";
|
|
4
|
+
import prettier from "prettier";
|
|
5
|
+
import config from "virtual:astro-prettier-response/config";
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
//#region src/middleware/is400.ts
|
|
8
8
|
function is400(status) {
|
|
9
|
-
|
|
9
|
+
return status >= 400 && status < 500;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/middleware/filename.ts
|
|
13
14
|
function synthesizeFilename(response) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
return `file.${extensionFromContentType}`;
|
|
15
|
+
const contentType = response.headers.get("content-type");
|
|
16
|
+
/* v8 ignore next 8 */
|
|
17
|
+
if (contentType == null) {
|
|
18
|
+
if (is400(response.status)) return "index.html";
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
const extensionFromContentType = mime.extension(contentType);
|
|
22
|
+
if (!extensionFromContentType) return null;
|
|
23
|
+
return `file.${extensionFromContentType}`;
|
|
26
24
|
}
|
|
27
25
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
// src/middleware/prettier.ts
|
|
32
|
-
import prettier from "prettier";
|
|
33
|
-
import config from "virtual:astro-prettier-response/config";
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/middleware/prettier.ts
|
|
34
28
|
function enhanceOptions(options = {}) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
plugins: [...givenPlugins, "@prettier/plugin-xml"]
|
|
42
|
-
};
|
|
29
|
+
if (!config.formatXml) return options;
|
|
30
|
+
const givenPlugins = options.plugins ?? [];
|
|
31
|
+
return {
|
|
32
|
+
...options,
|
|
33
|
+
plugins: [...givenPlugins, "@prettier/plugin-xml"]
|
|
34
|
+
};
|
|
43
35
|
}
|
|
44
36
|
async function format(source, options) {
|
|
45
|
-
|
|
37
|
+
return prettier.format(source, enhanceOptions(options));
|
|
46
38
|
}
|
|
47
39
|
async function getFileInfo(source, options) {
|
|
48
|
-
|
|
40
|
+
return prettier.getFileInfo(source, enhanceOptions(options));
|
|
49
41
|
}
|
|
50
42
|
|
|
51
|
-
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/middleware/format.ts
|
|
52
45
|
async function formatOrReturn(filepath, source) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return source;
|
|
62
|
-
}
|
|
46
|
+
try {
|
|
47
|
+
const result = await format(source, { filepath });
|
|
48
|
+
logger.debug(`successfully formatted`);
|
|
49
|
+
return result;
|
|
50
|
+
} catch (e) {
|
|
51
|
+
logger.error(`failed to format`);
|
|
52
|
+
return source;
|
|
53
|
+
}
|
|
63
54
|
}
|
|
64
55
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/middleware/index.ts
|
|
58
|
+
const onRequest = defineMiddleware(async (context, next) => {
|
|
59
|
+
const response = await next();
|
|
60
|
+
if (!response.ok && !is400(response.status)) {
|
|
61
|
+
logger.debug(`NOT formatting (status is not 2xx or 4xx)`);
|
|
62
|
+
return response;
|
|
63
|
+
}
|
|
64
|
+
const filename = synthesizeFilename(response);
|
|
65
|
+
if (filename == null) {
|
|
66
|
+
logger.debug(`NOT formatting (couldn't determine filename/extension)`);
|
|
67
|
+
return response;
|
|
68
|
+
}
|
|
69
|
+
const fileInfo = await getFileInfo(filename);
|
|
70
|
+
if (fileInfo?.inferredParser == null) {
|
|
71
|
+
logger.debug(`NOT formatting (Prettier has no parser for this file type)`);
|
|
72
|
+
return response;
|
|
73
|
+
}
|
|
74
|
+
const clone = response.clone();
|
|
75
|
+
const source = await clone.text();
|
|
76
|
+
const processedSource = await formatOrReturn(filename, source);
|
|
77
|
+
return new Response(processedSource, {
|
|
78
|
+
status: response.status,
|
|
79
|
+
headers: clone.headers,
|
|
80
|
+
statusText: clone.statusText
|
|
81
|
+
});
|
|
91
82
|
});
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
};
|
|
83
|
+
|
|
84
|
+
//#endregion
|
|
85
|
+
export { onRequest };
|
|
86
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@altano/astro-prettier-response",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Prettier html/css/js for your Astro site",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astro",
|
|
@@ -40,19 +40,19 @@
|
|
|
40
40
|
"@prettier/plugin-xml": "^3.4.1",
|
|
41
41
|
"@types/mime-types": "^2.1.4",
|
|
42
42
|
"@types/node": "^22.13.10",
|
|
43
|
-
"@vitest/coverage-v8": "^3.1.
|
|
44
|
-
"astro": "^5.
|
|
43
|
+
"@vitest/coverage-v8": "^3.1.4",
|
|
44
|
+
"astro": "^5.8.0",
|
|
45
45
|
"depcheck": "^1.4.7",
|
|
46
46
|
"prettier": "^3.5.3",
|
|
47
|
-
"
|
|
47
|
+
"tsdown": "^0.12.3",
|
|
48
48
|
"typescript": "^5.8.2",
|
|
49
49
|
"vite": "^6.2.6",
|
|
50
|
-
"vitest": "^3.1.
|
|
50
|
+
"vitest": "^3.1.4",
|
|
51
51
|
"zx": "^8.5.2",
|
|
52
|
-
"@altano/build-config": "0.0.
|
|
53
|
-
"@altano/
|
|
54
|
-
"@altano/
|
|
55
|
-
"@altano/tsconfig": "0.0.
|
|
52
|
+
"@altano/build-config": "0.0.1",
|
|
53
|
+
"@altano/testing": "0.0.1",
|
|
54
|
+
"@altano/tiny-async-pool": "4.0.0",
|
|
55
|
+
"@altano/tsconfig": "0.0.1"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
|
58
58
|
"astro": "5.x.x",
|
|
@@ -65,14 +65,13 @@
|
|
|
65
65
|
"access": "public"
|
|
66
66
|
},
|
|
67
67
|
"scripts": {
|
|
68
|
-
"build": "
|
|
69
|
-
"
|
|
70
|
-
"check:types:astro-fixtures": "node --experimental-strip-types ./scripts/check-fixtures.ts",
|
|
68
|
+
"build": "tsdown",
|
|
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
|
-
"dev": "
|
|
74
|
+
"dev": "tsdown --watch",
|
|
76
75
|
"format": "prettier --write src",
|
|
77
76
|
"lint": "eslint",
|
|
78
77
|
"lint:fix": "TIMING=1 eslint src/**/*.ts* --fix",
|
package/dist/config.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { z } from "astro/zod";
|
|
2
|
-
export declare const ConfigSchema: z.ZodDefault<z.ZodOptional<z.ZodObject<{
|
|
3
|
-
/**
|
|
4
|
-
* Astro/Vite will minify your HTML/JS/CSS by default. Astro lightly
|
|
5
|
-
* compresses your HTML (not a full minifier) while Vite will fully compress
|
|
6
|
-
* your CSS/JS. Since we're going for prettiest output here, it makes sense
|
|
7
|
-
* for this integration to fully disable such minification.
|
|
8
|
-
*
|
|
9
|
-
* If you set this to false, and you use the default Astro config with
|
|
10
|
-
* minification enabled, you will still get your output formatted by
|
|
11
|
-
* Prettier, but it will be minified output that is formatted. So, for
|
|
12
|
-
* example, most of the variable names in your JS will be one character
|
|
13
|
-
* letters.
|
|
14
|
-
*
|
|
15
|
-
* @defaultValue true
|
|
16
|
-
*/
|
|
17
|
-
disableMinifiers: z.ZodDefault<z.ZodBoolean>;
|
|
18
|
-
/**
|
|
19
|
-
* Install `@prettier/plugin-xml` package if you enable this
|
|
20
|
-
*
|
|
21
|
-
* Use the official Prettier plugin to format xml as well. Sitemaps and rss
|
|
22
|
-
* feeds are xml, so this would probably do something on your stock Astro
|
|
23
|
-
* site. Note: the Prettier output is only marginally better because xml is
|
|
24
|
-
* very whitespace sensitive and so Prettier's formatting is very strict and
|
|
25
|
-
* not that much prettier.
|
|
26
|
-
*
|
|
27
|
-
* @defaultValue false
|
|
28
|
-
*/
|
|
29
|
-
formatXml: z.ZodDefault<z.ZodBoolean>;
|
|
30
|
-
}, "strict", z.ZodTypeAny, {
|
|
31
|
-
disableMinifiers: boolean;
|
|
32
|
-
formatXml: boolean;
|
|
33
|
-
}, {
|
|
34
|
-
disableMinifiers?: boolean | undefined;
|
|
35
|
-
formatXml?: boolean | undefined;
|
|
36
|
-
}>>>;
|
|
37
|
-
export type AstroPrettierResponseConfig = z.infer<typeof ConfigSchema>;
|
|
38
|
-
//# sourceMappingURL=config.d.ts.map
|
package/dist/integration.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import prettier, { type Options } from "prettier";
|
|
2
|
-
export declare function format(source: string, options?: Options): Promise<string>;
|
|
3
|
-
export declare function getFileInfo(source: string, options?: Options): Promise<prettier.FileInfoResult>;
|
|
4
|
-
//# sourceMappingURL=prettier.d.ts.map
|