@kirana-style/astro 0.0.0-stage → 0.1.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/package.json CHANGED
@@ -1,6 +1,45 @@
1
1
  {
2
2
  "name": "@kirana-style/astro",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Kirana for Astro: the integration that compiles .astro components and finishes the stylesheet.",
5
+ "license": "MIT",
6
+ "author": "The Kirana authors",
7
+ "homepage": "https://kirana.style",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/kirana-style/kirana.git",
11
+ "directory": "packages/kirana/npm/astro"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/kirana-style/kirana/issues"
15
+ },
16
+ "type": "module",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./src/index.d.ts",
20
+ "default": "./src/index.js"
21
+ },
22
+ "./package.json": "./package.json"
23
+ },
24
+ "dependencies": {
25
+ "@kirana-style/compiler": "0.1.0",
26
+ "@kirana-style/core": "0.1.0",
27
+ "@kirana-style/vite": "0.1.0"
28
+ },
29
+ "peerDependencies": {
30
+ "astro": ">=5.0.0"
31
+ },
32
+ "files": [
33
+ "src",
34
+ "bin",
35
+ "*.d.ts",
36
+ "*.css",
37
+ "README.md",
38
+ "builders.json",
39
+ "schemas",
40
+ "gatsby-node.mjs",
41
+ "rules",
42
+ "recommended.json",
43
+ "strict.json"
44
+ ]
45
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,27 @@
1
+ import type { KiranaViteOptions } from "@kirana-style/vite";
2
+ export type KiranaAstroOptions = Omit<KiranaViteOptions, "renameStylesheet">;
3
+ /**
4
+ * Renames each filled stylesheet in `outDir` by its content hash and replaces its old name in every
5
+ * text file there. Returns the renames, old name to new.
6
+ */
7
+ export declare function renameStylesheets(outDir: string, filled: Iterable<string>): Promise<Map<string, string>>;
8
+ type Hooks = {
9
+ "astro:config:setup": (options: {
10
+ updateConfig: (config: {
11
+ vite: {
12
+ plugins: unknown[];
13
+ };
14
+ }) => void;
15
+ }) => void;
16
+ "astro:config:done": (options: {
17
+ config: {
18
+ outDir: URL;
19
+ };
20
+ }) => void;
21
+ "astro:build:done": () => Promise<void>;
22
+ };
23
+ export default function kirana(options?: KiranaAstroOptions): {
24
+ name: string;
25
+ hooks: Hooks;
26
+ };
27
+ export {};
package/src/index.js ADDED
@@ -0,0 +1,73 @@
1
+ /**
2
+ * The Astro integration (KRN-INT-004): `@kirana-style/vite` for Astro's Vite, which compiles `.astro`
3
+ * components as they are read (`design-notes/sfc-masking.md`), plus the one step Astro needs after
4
+ * its build. Astro links each page's stylesheet — or inlines a small one — by the name and content it
5
+ * recorded before the bundle was finished, so the Vite plugin fills the stylesheet in place, and once
6
+ * Astro has written every page this integration renames the file by its real content and rewrites
7
+ * every reference to it.
8
+ */
9
+ import { readFile, readdir, rename, writeFile } from "node:fs/promises";
10
+ import path from "node:path";
11
+ import { fileURLToPath } from "node:url";
12
+ import { renameAsset } from "@kirana-style/compiler/host";
13
+ import vite from "@kirana-style/vite";
14
+ /** Files whose text can name the stylesheet: pages, server chunks, manifests, other stylesheets. */
15
+ const TEXT = /\.(?:html|m?js|json|css)$/;
16
+ /** Every file under a directory. */
17
+ async function files(directory) {
18
+ const entries = await readdir(directory, {
19
+ recursive: true,
20
+ withFileTypes: true,
21
+ });
22
+ return entries
23
+ .filter((entry) => entry.isFile())
24
+ .map((entry) => path.join(entry.parentPath, entry.name));
25
+ }
26
+ /**
27
+ * Renames each filled stylesheet in `outDir` by its content hash and replaces its old name in every
28
+ * text file there. Returns the renames, old name to new.
29
+ */
30
+ export async function renameStylesheets(outDir, filled) {
31
+ const all = await files(outDir);
32
+ const renames = new Map();
33
+ for (const fileName of filled) {
34
+ const base = path.posix.basename(fileName);
35
+ for (const file of all.filter((candidate) => path.basename(candidate) === base)) {
36
+ const next = path.basename(renameAsset(base, await readFile(file, "utf8")));
37
+ if (next === base)
38
+ continue;
39
+ await rename(file, path.join(path.dirname(file), next));
40
+ renames.set(base, next);
41
+ }
42
+ }
43
+ if (renames.size === 0)
44
+ return renames;
45
+ for (const file of await files(outDir)) {
46
+ if (!TEXT.test(file))
47
+ continue;
48
+ const text = await readFile(file, "utf8");
49
+ let next = text;
50
+ for (const [from, to] of renames)
51
+ next = next.replaceAll(from, to);
52
+ if (next !== text)
53
+ await writeFile(file, next);
54
+ }
55
+ return renames;
56
+ }
57
+ export default function kirana(options = {}) {
58
+ const plugins = vite({ ...options, renameStylesheet: false });
59
+ const { filled } = plugins[0].api;
60
+ let outDir = "";
61
+ return {
62
+ name: "@kirana-style/astro",
63
+ hooks: {
64
+ "astro:config:setup": ({ updateConfig }) => updateConfig({ vite: { plugins } }),
65
+ "astro:config:done": ({ config }) => {
66
+ outDir = fileURLToPath(config.outDir);
67
+ },
68
+ "astro:build:done": async () => {
69
+ await renameStylesheets(outDir, filled);
70
+ },
71
+ },
72
+ };
73
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.