@cardenelabs/dragon 0.40.0 → 0.41.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,6 @@
1
1
  {
2
2
  "name": "@cardenelabs/dragon",
3
- "version": "0.40.0",
3
+ "version": "0.41.0",
4
4
  "description": "Dragon — 動く図を、人は行ごとの記法で、AI は JSON で書く。時系列図と構成図を段ごとに動かし、mp4・GIF・HTML・PNG に書き出す。",
5
5
  "license": "MIT",
6
6
  "author": "cardene777",
@@ -52,6 +52,16 @@
52
52
  "types": "./dist/export.d.cts",
53
53
  "default": "./dist/export.cjs"
54
54
  }
55
+ },
56
+ "./node": {
57
+ "import": {
58
+ "types": "./dist/node.d.ts",
59
+ "default": "./dist/node.js"
60
+ },
61
+ "require": {
62
+ "types": "./dist/node.d.cts",
63
+ "default": "./dist/node.cjs"
64
+ }
55
65
  }
56
66
  },
57
67
  "files": [
package/src/index.ts CHANGED
@@ -3,7 +3,8 @@
3
3
  *
4
4
  * 図の原本は JSON で、人は行の頭の英語の語で行の種類を決める dragon記法で書く。記法と JSON は行き来でき、
5
5
  * JSON は JSON Schema 1 本で検査する。原本から図を描き、頁に置いて段ごとに動かす。
6
- * 頁に字を読み込むのは別の入口 `@cardenelabs/dragon/fonts`。
6
+ * 頁に字を読み込むのは別の入口 `@cardenelabs/dragon/fonts`、原本の file を移すのは Node でだけ動く入口 `@cardenelabs/dragon/node`。
7
+ * この入口は頁を束ねる道具が読むため、Node の組み込みの module を読む部品を置かない (#3206)。
7
8
  */
8
9
  export { extractDiagrams, type Fence } from "./markdown";
9
10
  export { type LineError, type ParseOptions, type Parsed, parseNotation } from "./parser";
@@ -27,7 +28,7 @@ export {
27
28
  validateDiagram,
28
29
  type ViewMode,
29
30
  } from "./schema";
30
- export { FORMAT_VERSION, type Migrated, migrateFile, type Read, readDiagram } from "./version";
31
+ export { FORMAT_VERSION, type Read, readDiagram } from "./version";
31
32
  export type {
32
33
  Layout,
33
34
  Point,
package/src/node.ts ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Node でだけ動く部品の入口 (#3206)。
3
+ *
4
+ * 原本の file を読み書きする部品は、頁で描く入口 (`index.ts`) に置かない。
5
+ * 頁を束ねる道具 (Next.js の Turbopack など) は、呼ぶかどうかに関わらず入口の file の読み込みを全て解くため、
6
+ * 頁の入口に Node の組み込みの module を読む部品が 1 つでも在ると、ブラウザ向けの build が止まる。
7
+ */
8
+ import { readFile } from "node:fs/promises";
9
+ import { FORMAT_VERSION, readDiagram } from "./version";
10
+
11
+ export interface Migrated {
12
+ changed: boolean;
13
+ from: number;
14
+ to: number;
15
+ }
16
+
17
+ /** 原本の file を今の版へ移す。今の版の file には触れない。 */
18
+ export async function migrateFile(path: string): Promise<Migrated> {
19
+ const input: unknown = JSON.parse(await readFile(path, "utf8"));
20
+ const read = readDiagram(input);
21
+ if (!read.ok) throw new Error(`${path} を移せない: ${read.errors.map((error) => `${error.pointer} ${error.message}`).join(" / ")}`);
22
+ return { changed: false, from: read.diagram.version, to: FORMAT_VERSION };
23
+ }
package/src/version.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * 原本の形式の版。
3
3
  *
4
4
  * 道具より新しい版の図は描かずに断る。古い版の図は、版を 1 つずつ上げる変換を通して読み、
5
- * 原本の file を書き換えるのは移行の命令 (`migrateFile`) だけにする。
5
+ * 原本の file を書き換えるのは移行の命令 (`migrateFile`、Node でだけ動く入口 `node.ts`) だけにする。
6
6
  * 今は版 1 しか無いため、版を上げる変換はまだ持たない。版 2 を作る時に、版 1 から 2 への変換と
7
7
  * 古い版を読んだ時の警告をここに足す。
8
8
  */
@@ -15,12 +15,6 @@ export const FORMAT_VERSION = 1;
15
15
 
16
16
  export type Read = { ok: true; diagram: Diagram; warnings: string[] } | { ok: false; errors: FieldError[] };
17
17
 
18
- export interface Migrated {
19
- changed: boolean;
20
- from: number;
21
- to: number;
22
- }
23
-
24
18
  function newerThanTool(input: unknown): number | undefined {
25
19
  if (typeof input !== "object" || input === null) return undefined;
26
20
  const version = (input as { version?: unknown }).version;
@@ -40,16 +34,3 @@ export function readDiagram(input: unknown, options: CheckOptions = {}): Read {
40
34
  if (!checked.ok) return checked;
41
35
  return { ok: true, diagram: input as Diagram, warnings: [] };
42
36
  }
43
-
44
- /**
45
- * 原本の file を今の版へ移す。今の版の file には触れない。
46
- *
47
- * file を読むので、画面で描く入口からは呼ばない。node の file の命令は呼んだ時に初めて読み込む。
48
- */
49
- export async function migrateFile(path: string): Promise<Migrated> {
50
- const { readFile } = await import("node:fs/promises");
51
- const input: unknown = JSON.parse(await readFile(path, "utf8"));
52
- const read = readDiagram(input);
53
- if (!read.ok) throw new Error(`${path} を移せない: ${read.errors.map((error) => `${error.pointer} ${error.message}`).join(" / ")}`);
54
- return { changed: false, from: read.diagram.version, to: FORMAT_VERSION };
55
- }