miral 0.0.5 → 0.0.6

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.
Files changed (3) hide show
  1. package/README.md +17 -2
  2. package/dist/cli.js +63 -28
  3. package/package.json +14 -3
package/README.md CHANGED
@@ -27,8 +27,7 @@ npx miral build && npx -y fotone --port 9000 ./dist
27
27
 
28
28
  Open `http://localhost:9000` for development. Serve the generated `dist/` directory with any static HTTP server.
29
29
  Asset URLs are relative, so the catalog can also run under a subdirectory.
30
- Use `--port` for a different development port or `--outDir` for a different build directory.
31
- Both commands accept an optional directory to scan.
30
+ Install `react` and `react-dom` for React catalogs, or `preact` for Preact catalogs.
32
31
 
33
32
  ## Add components
34
33
 
@@ -66,12 +65,27 @@ export const Disabled = () => <Button disabled/>;
66
65
  examples.
67
66
  - `section` must be a string literal. Omit it to use `Elements`.
68
67
 
68
+ ## CLI reference
69
+
70
+ ```sh
71
+ miral dev [directory] [--port <port>] [--framework react|preact]
72
+ miral build [directory] [--outDir <path>] [--framework react|preact]
73
+ ```
74
+
75
+ | Argument | Command | Behavior |
76
+ | --- | --- | --- |
77
+ | `directory` | Both | Directory to scan. Defaults to the current directory. |
78
+ | `--framework` | Both | Overrides the config for this run. Defaults to `react`. |
79
+ | `--port` | `dev` | Development server port. Defaults to `9000`. |
80
+ | `--outDir` | `build` | Output directory relative to the working directory. Defaults to `dist`. |
81
+
69
82
  ## Configuration reference
70
83
 
71
84
  Place an optional `miral.config.ts` in the directory Miral scans. All options are optional.
72
85
 
73
86
  ```ts
74
87
  export default {
88
+ framework: "preact",
75
89
  title: "My UI kit",
76
90
  assetsDir: "./assets",
77
91
  order: ["Controls", "Components"],
@@ -81,6 +95,7 @@ export default {
81
95
 
82
96
  | Option | Type | Behavior |
83
97
  |------------------|----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
98
+ | `framework` | String literal | Selects `"react"` (default) or `"preact"`. Restart the development server after changing it. |
84
99
  | `title` | `string` | Sets the sidebar heading and browser tab title, for example `My UI kit \| Miral`. If empty or omitted, the sidebar has no heading and the tab shows `Miral`. |
85
100
  | `assetsDir` | String literal | Path relative to `miral.config.ts`. Adds an Assets entry at the end of the sidebar. Files in this directory and its subdirectories appear by filename, with previews for images. |
86
101
  | `order` | `string[]` | Lists sidebar sections in display order. Remaining sections follow alphabetically. |
package/dist/cli.js CHANGED
@@ -1,20 +1,66 @@
1
1
  #!/usr/bin/env node
2
+ import { existsSync } from "node:fs";
2
3
  import { readFile } from "node:fs/promises";
3
4
  import path from "node:path";
4
5
  import { fileURLToPath } from "node:url";
5
6
  import { parseArgs } from "node:util";
6
7
  import tailwind from "@tailwindcss/vite";
7
- import react from "@vitejs/plugin-react";
8
8
  import { build, createServer, parseSync, } from "vite";
9
9
  const { positionals: [command, directory], values, } = parseArgs({
10
10
  allowPositionals: true,
11
- options: { port: { type: "string" }, outDir: { type: "string" } },
11
+ options: {
12
+ port: { type: "string" },
13
+ outDir: { type: "string" },
14
+ framework: { type: "string" },
15
+ },
12
16
  });
13
17
  if (!["dev", "build"].includes(command)) {
14
- console.error("Usage: miral dev [directory] [--port 9000] | miral build [directory] [--outDir dist]");
18
+ console.error("Usage: miral dev [directory] [--port 9000] [--framework react|preact] | miral build [directory] [--outDir dist] [--framework react|preact]");
15
19
  process.exit(1);
16
20
  }
21
+ function configValue(source, id, name) {
22
+ const { program } = parseSync(id, source);
23
+ const config = program.body.find((node) => node.type === "ExportDefaultDeclaration");
24
+ let object = config?.declaration;
25
+ while (object?.type === "TSAsExpression" ||
26
+ object?.type === "TSSatisfiesExpression")
27
+ object = object.expression;
28
+ if (object?.type !== "ObjectExpression")
29
+ return;
30
+ const property = object.properties.find((item) => {
31
+ if (item.type !== "Property")
32
+ return false;
33
+ const key = item.key;
34
+ return ((key.type === "Identifier" && key.name === name) ||
35
+ (key.type === "Literal" && key.value === name));
36
+ });
37
+ if (property?.type !== "Property")
38
+ return;
39
+ const value = property.value.type === "Literal" ? property.value.value : null;
40
+ return value;
41
+ }
17
42
  const root = path.resolve(directory ?? ".");
43
+ const configPath = path.join(root, "miral.config.ts");
44
+ let framework = values.framework ?? "react";
45
+ if (values.framework === undefined && existsSync(configPath)) {
46
+ const source = await readFile(configPath, "utf8");
47
+ const configured = configValue(source, configPath, "framework");
48
+ if (configured !== undefined)
49
+ framework = configured;
50
+ }
51
+ if (framework !== "react" && framework !== "preact") {
52
+ console.error('Expected framework to be "react" or "preact".');
53
+ process.exit(1);
54
+ }
55
+ const preset = framework === "preact"
56
+ ? await import("nano-ssg/preact")
57
+ : await import("nano-ssg/react");
58
+ const frameworkPlugins = (preset.default.plugins ?? [])
59
+ .flat()
60
+ .filter((plugin) => plugin &&
61
+ typeof plugin === "object" &&
62
+ "name" in plugin &&
63
+ /^(vite:react|preact:|vite:preact|prefresh$)/.test(plugin.name));
18
64
  const htmlId = path.join(root, "index.html");
19
65
  const extension = import.meta.filename.endsWith(".ts") ? "tsx" : "js";
20
66
  const entry = `/@miral/Viewer.${extension}`;
@@ -52,24 +98,9 @@ const viewer = {
52
98
  },
53
99
  transform(source, id) {
54
100
  if (id === path.join(root, "miral.config.ts")) {
55
- const { program } = parseSync(id, source);
56
- const config = program.body.find((node) => node.type === "ExportDefaultDeclaration");
57
- let object = config?.declaration;
58
- while (object?.type === "TSAsExpression" ||
59
- object?.type === "TSSatisfiesExpression")
60
- object = object.expression;
61
- if (object?.type !== "ObjectExpression")
101
+ const directory = configValue(source, id, "assetsDir");
102
+ if (directory === undefined)
62
103
  return;
63
- const property = object.properties.find((item) => {
64
- if (item.type !== "Property")
65
- return false;
66
- const key = item.key;
67
- return ((key.type === "Identifier" && key.name === "assetsDir") ||
68
- (key.type === "Literal" && key.value === "assetsDir"));
69
- });
70
- if (property?.type !== "Property")
71
- return;
72
- const directory = property.value.type === "Literal" && property.value.value;
73
104
  if (typeof directory !== "string" || !directory.trim())
74
105
  this.error("Use a directory path string literal for assetsDir in miral.config.ts.");
75
106
  const pattern = path.posix.join(directory, "**/*");
@@ -146,17 +177,21 @@ const config = {
146
177
  root,
147
178
  base: "./",
148
179
  configFile: false,
149
- plugins: [viewer, react(), tailwind({ optimize: false })],
180
+ plugins: [viewer, ...frameworkPlugins, tailwind({ optimize: false })],
150
181
  optimizeDeps: {
151
182
  entries: ["**/*.mira.tsx", "!**/dist/**"],
152
- include: [
153
- "react",
154
- "react-dom/client",
155
- "react/jsx-runtime",
156
- "react/jsx-dev-runtime",
157
- ],
183
+ include: framework === "preact"
184
+ ? ["preact", "preact/hooks", "preact/compat", "preact/compat/client"]
185
+ : [
186
+ "react",
187
+ "react-dom/client",
188
+ "react/jsx-runtime",
189
+ "react/jsx-dev-runtime",
190
+ ],
191
+ },
192
+ resolve: {
193
+ dedupe: framework === "preact" ? ["preact"] : ["react", "react-dom"],
158
194
  },
159
- resolve: { dedupe: ["react", "react-dom"] },
160
195
  };
161
196
  if (command === "build") {
162
197
  await build({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "miral",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "Like Storybook, but much simpler.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -21,23 +21,34 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@tailwindcss/vite": "^4.3.5",
24
- "@vitejs/plugin-react": "^6.3.0",
25
24
  "nano-ssg": "0.0.8",
26
25
  "tailwindcss": "^4.3.5",
27
26
  "vite": "^8.4.0"
28
27
  },
29
28
  "peerDependencies": {
29
+ "preact": "^11.2.0",
30
30
  "react": "^19.3.1",
31
31
  "react-dom": "^19.3.1"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@biomejs/biome": "^2.6.9",
35
- "@types/node": "^24.19.4",
35
+ "@types/node": "^26.6.11",
36
36
  "@types/react": "^19.3.3",
37
37
  "@types/react-dom": "^19.3.0",
38
38
  "oxlint": "^1.95.0",
39
39
  "oxlint-better": "^1.0.8",
40
40
  "typescript": "~7.0.2",
41
41
  "zod": "^4.6.7"
42
+ },
43
+ "peerDependenciesMeta": {
44
+ "react": {
45
+ "optional": true
46
+ },
47
+ "react-dom": {
48
+ "optional": true
49
+ },
50
+ "preact": {
51
+ "optional": true
52
+ }
42
53
  }
43
54
  }