truecanvas 0.1.0 → 0.3.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.
@@ -0,0 +1,28 @@
1
+ import {
2
+ idOf,
3
+ parseModule,
4
+ walk
5
+ } from "./chunk-PBKX3SBC.js";
6
+
7
+ // src/core/inject.ts
8
+ import MagicString from "magic-string";
9
+ function injectIds(source, file) {
10
+ let ast;
11
+ try {
12
+ ast = parseModule(source);
13
+ } catch {
14
+ return source;
15
+ }
16
+ const s = new MagicString(source);
17
+ walk(ast, (node) => {
18
+ if (node.type !== "JSXOpeningElement") return;
19
+ if (node.attributes.some((a) => a.type === "JSXAttribute" && a.name.name === "data-tc")) return;
20
+ const id = file ? `${file}#${idOf(node)}` : idOf(node);
21
+ s.appendLeft((node.typeParameters ?? node.name).end, ` data-tc="${id}"`);
22
+ });
23
+ return s.toString();
24
+ }
25
+
26
+ export {
27
+ injectIds
28
+ };
@@ -1,9 +1,15 @@
1
1
  // src/core/catalog.ts
2
2
  import fs from "fs";
3
- import path from "path";
3
+ import path2 from "path";
4
4
  import { fileURLToPath } from "url";
5
5
  import { createRequire } from "module";
6
6
  import { globSync } from "tinyglobby";
7
+
8
+ // src/core/paths.ts
9
+ import path from "path";
10
+ var posix = (p) => p.split(path.sep).join("/");
11
+
12
+ // src/core/catalog.ts
7
13
  var require2 = createRequire(import.meta.url);
8
14
  var Catalog = class {
9
15
  constructor(root, globs, libraries = []) {
@@ -51,8 +57,8 @@ var Catalog = class {
51
57
  }
52
58
  };
53
59
  async function runAnalysis(root, globs, libraries) {
54
- const here = path.dirname(fileURLToPath(import.meta.url));
55
- const worker = [path.join(here, "catalog-worker.js"), path.join(here, "..", "catalog-worker.js")].find((p) => fs.existsSync(p));
60
+ const here = path2.dirname(fileURLToPath(import.meta.url));
61
+ const worker = [path2.join(here, "catalog-worker.js"), path2.join(here, "..", "catalog-worker.js")].find((p) => fs.existsSync(p));
56
62
  if (!worker) return analyzeComponents(root, globs, libraries);
57
63
  const { Worker } = await import("worker_threads");
58
64
  return new Promise((resolve, reject) => {
@@ -68,7 +74,7 @@ async function analyzeComponents(root, globs, libraries = []) {
68
74
  const options = compilerOptions(ts, root);
69
75
  const libEntries = /* @__PURE__ */ new Map();
70
76
  for (const lib of libraries) {
71
- const res = ts.resolveModuleName(lib, path.join(root, "index.ts"), options, ts.sys).resolvedModule;
77
+ const res = ts.resolveModuleName(lib, path2.join(root, "index.ts"), options, ts.sys).resolvedModule;
72
78
  if (res) libEntries.set(res.resolvedFileName, lib);
73
79
  }
74
80
  const program = ts.createProgram({ rootNames: [...files, ...libEntries.keys()], options });
@@ -91,7 +97,7 @@ async function analyzeComponents(root, globs, libraries = []) {
91
97
  if (!sig) continue;
92
98
  const ret = checker.typeToString(sig.getReturnType());
93
99
  if (!/Element|ReactNode|ReactPortal|null|any/.test(ret) || /^Promise</.test(ret)) continue;
94
- out.push(describe(ts, checker, sym, decl, sig, library ?? path.relative(root, file), library));
100
+ out.push(describe(ts, checker, sym, decl, sig, library ?? posix(path2.relative(root, file)), library));
95
101
  }
96
102
  }
97
103
  for (const spec of out) applyProfile(spec);
@@ -115,7 +121,7 @@ function applyProfile(spec) {
115
121
  async function attachPresets(root, specs) {
116
122
  const byLib = /* @__PURE__ */ new Map();
117
123
  for (const s of specs) if (s.library) byLib.set(s.library, [...byLib.get(s.library) ?? [], s]);
118
- const projectRequire = createRequire(path.join(root, "package.json"));
124
+ const projectRequire = createRequire(path2.join(root, "package.json"));
119
125
  for (const [lib, list] of byLib) {
120
126
  let mod;
121
127
  try {
@@ -148,7 +154,7 @@ function compilerOptions(ts, root) {
148
154
  const base = { jsx: ts.JsxEmit.ReactJSX, strict: true, skipLibCheck: true, noEmit: true, allowJs: false };
149
155
  if (!configPath) return base;
150
156
  const read = ts.readConfigFile(configPath, ts.sys.readFile);
151
- const parsed = ts.parseJsonConfigFileContent(read.config, ts.sys, path.dirname(configPath));
157
+ const parsed = ts.parseJsonConfigFileContent(read.config, ts.sys, path2.dirname(configPath));
152
158
  return { ...parsed.options, noEmit: true, skipLibCheck: true, incremental: false, plugins: [] };
153
159
  }
154
160
  function describe(ts, checker, sym, decl, sig, file, library) {
@@ -273,32 +279,43 @@ function readDefaults(ts, decl) {
273
279
  }
274
280
  function importSpecifier(root, canvasFile, componentFile) {
275
281
  if (!/\.(tsx|ts|jsx|js)$/.test(componentFile) && !componentFile.startsWith(".")) return componentFile;
276
- const abs = path.resolve(root, componentFile);
277
- const noExt = abs.replace(/\.(tsx|ts|jsx|js)$/, "").replace(/\/index$/, "");
282
+ const abs = path2.resolve(root, componentFile);
283
+ const noExt = posix(abs).replace(/\.(tsx|ts|jsx|js)$/, "").replace(/\/index$/, "");
278
284
  const alias = readAlias(root);
279
285
  if (alias) {
280
- const rel2 = path.relative(alias.dir, noExt);
281
- if (!rel2.startsWith("..")) return `${alias.prefix}${rel2}`;
286
+ const rel2 = path2.relative(alias.dir, noExt);
287
+ if (!rel2.startsWith("..")) return `${alias.prefix}${posix(rel2)}`;
282
288
  }
283
- let rel = path.relative(path.dirname(path.resolve(root, canvasFile)), noExt);
289
+ let rel = posix(path2.relative(path2.dirname(path2.resolve(root, canvasFile)), noExt));
284
290
  if (!rel.startsWith(".")) rel = `./${rel}`;
285
291
  return rel;
286
292
  }
293
+ function resolveAliasPath(root, spec) {
294
+ const alias = readAlias(root);
295
+ if (alias && spec.startsWith(alias.prefix)) return path2.join(alias.dir, spec.slice(alias.prefix.length));
296
+ return path2.resolve(root, spec);
297
+ }
287
298
  var aliasCache = null;
288
299
  function readAlias(root) {
289
- if (aliasCache?.root === root) return aliasCache.value;
300
+ const configFile = path2.join(root, "tsconfig.json");
301
+ let key = `${root}\0none`;
302
+ try {
303
+ key = `${root}\0${fs.statSync(configFile).mtimeMs}`;
304
+ } catch {
305
+ }
306
+ if (aliasCache?.key === key) return aliasCache.value;
290
307
  let value = null;
291
308
  try {
292
309
  const ts = require2("typescript");
293
310
  const configPath = ts.findConfigFile(root, ts.sys.fileExists, "tsconfig.json");
294
311
  if (configPath) {
295
312
  const read = ts.readConfigFile(configPath, ts.sys.readFile);
296
- const parsed = ts.parseJsonConfigFileContent(read.config, ts.sys, path.dirname(configPath));
313
+ const parsed = ts.parseJsonConfigFileContent(read.config, ts.sys, path2.dirname(configPath));
297
314
  const paths = parsed.options.paths ?? {};
298
- const base = parsed.options.pathsBasePath ?? parsed.options.baseUrl ?? path.dirname(configPath);
299
- for (const [key, targets] of Object.entries(paths)) {
300
- if (key.endsWith("/*") && targets[0]?.endsWith("/*")) {
301
- value = { prefix: key.slice(0, -1), dir: path.resolve(base, targets[0].slice(0, -2)) };
315
+ const base = parsed.options.pathsBasePath ?? parsed.options.baseUrl ?? path2.dirname(configPath);
316
+ for (const [k, targets] of Object.entries(paths)) {
317
+ if (k.endsWith("/*") && targets[0]?.endsWith("/*")) {
318
+ value = { prefix: k.slice(0, -1), dir: path2.resolve(base, targets[0].slice(0, -2)) };
302
319
  break;
303
320
  }
304
321
  }
@@ -306,12 +323,14 @@ function readAlias(root) {
306
323
  } catch {
307
324
  value = null;
308
325
  }
309
- aliasCache = { root, value };
326
+ aliasCache = { key, value };
310
327
  return value;
311
328
  }
312
329
 
313
330
  export {
331
+ posix,
314
332
  Catalog,
315
333
  analyzeComponents,
316
- importSpecifier
334
+ importSpecifier,
335
+ resolveAliasPath
317
336
  };