@macroforge/vite-plugin 0.1.39 → 0.1.42

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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAqJ9B;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,uBAAuB;IACtC;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAEhD;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAEhD;;;;;;;;;OASG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IAExB;;;;;;;;;;;;;;;OAeG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;;;;;;;;OASG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IAEvB;;;;;;;;;;;;;;;OAeG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B;AA6TD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,iBAAS,gBAAgB,CAAC,OAAO,GAAE,uBAA4B,GAAG,MAAM,CAuTvE;AAED,eAAe,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AA4D9B;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,uBAAuB;IACtC;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAEhD;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAEhD;;;;;;;;;OASG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IAExB;;;;;;;;;;;;;;;OAeG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;;;;;;;;OASG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IAEvB;;;;;;;;;;;;;;;OAeG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B;AA0OD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,iBAAS,gBAAgB,CAAC,OAAO,GAAE,uBAA4B,GAAG,MAAM,CAwUvE;AAED,eAAe,gBAAgB,CAAC"}
package/dist/index.js CHANGED
@@ -29,6 +29,7 @@
29
29
  import { createRequire } from "module";
30
30
  import * as fs from "fs";
31
31
  import * as path from "path";
32
+ import { collectExternalDecoratorModules, loadMacroConfig, } from "@macroforge/shared";
32
33
  const moduleRequire = createRequire(import.meta.url);
33
34
  let tsModule;
34
35
  try {
@@ -40,92 +41,6 @@ catch (error) {
40
41
  }
41
42
  const compilerOptionsCache = new Map();
42
43
  let cachedRequire;
43
- /**
44
- * Cache for external macro package manifests.
45
- * Maps package path to its manifest (or null if failed to load).
46
- */
47
- const externalManifestCache = new Map();
48
- /**
49
- * Parses macro import comments from TypeScript code.
50
- *
51
- * @remarks
52
- * Extracts macro names mapped to their source module paths from
53
- * `/** import macro { ... } from "package" * /` comments.
54
- *
55
- * @param text - The TypeScript source code to parse
56
- * @returns Map of macro names to their module paths
57
- *
58
- * @example
59
- * ```typescript
60
- * const text = `/** import macro {JSON, FieldController} from "@playground/macro"; * /`;
61
- * parseMacroImportComments(text);
62
- * // => Map { "JSON" => "@playground/macro", "FieldController" => "@playground/macro" }
63
- * ```
64
- *
65
- * @internal
66
- */
67
- function parseMacroImportComments(text) {
68
- const imports = new Map();
69
- const pattern = /\/\*\*\s*import\s+macro\s*\{([^}]+)\}\s*from\s*["']([^"']+)["']/gi;
70
- let match;
71
- while ((match = pattern.exec(text)) !== null) {
72
- const names = match[1]
73
- .split(",")
74
- .map((n) => n.trim())
75
- .filter(Boolean);
76
- const modulePath = match[2];
77
- for (const name of names) {
78
- imports.set(name, modulePath);
79
- }
80
- }
81
- return imports;
82
- }
83
- /**
84
- * Attempts to load the manifest from an external macro package.
85
- *
86
- * External macro packages (like `@playground/macro`) export their own
87
- * `__macroforgeGetManifest()` function that provides macro metadata
88
- * including descriptions.
89
- *
90
- * @param modulePath - The package path (e.g., "@playground/macro")
91
- * @returns The macro manifest, or null if loading failed
92
- *
93
- * @internal
94
- */
95
- function getExternalManifest(modulePath) {
96
- if (externalManifestCache.has(modulePath)) {
97
- return externalManifestCache.get(modulePath) ?? null;
98
- }
99
- try {
100
- const pkg = moduleRequire(modulePath);
101
- if (typeof pkg.__macroforgeGetManifest === "function") {
102
- const manifest = pkg.__macroforgeGetManifest();
103
- externalManifestCache.set(modulePath, manifest);
104
- return manifest;
105
- }
106
- }
107
- catch {
108
- // Package not found or doesn't export manifest
109
- }
110
- externalManifestCache.set(modulePath, null);
111
- return null;
112
- }
113
- /**
114
- * Collects decorator modules from external macro packages referenced in the code.
115
- *
116
- * @param code - The TypeScript source code to scan
117
- * @returns Array of decorator module names from external packages
118
- *
119
- * @internal
120
- */
121
- function collectExternalDecoratorModules(code) {
122
- const imports = parseMacroImportComments(code);
123
- const modulePaths = [...new Set(imports.values())];
124
- return modulePaths.flatMap((modulePath) => {
125
- const manifest = getExternalManifest(modulePath);
126
- return manifest?.decorators.map((d) => d.module) ?? [];
127
- });
128
- }
129
44
  /**
130
45
  * Ensures that `require()` is available in the current execution context.
131
46
  *
@@ -155,53 +70,6 @@ async function ensureRequire() {
155
70
  }
156
71
  return cachedRequire;
157
72
  }
158
- /**
159
- * Loads Macroforge configuration from `macroforge.json`.
160
- *
161
- * @remarks
162
- * Searches for `macroforge.json` starting from `projectRoot` and traversing up the
163
- * directory tree until found or the filesystem root is reached. This allows monorepo
164
- * setups where the config may be at the workspace root rather than the package root.
165
- *
166
- * If the config file is found but cannot be parsed (invalid JSON), returns the
167
- * default configuration rather than throwing an error.
168
- *
169
- * @param projectRoot - The directory to start searching from (usually Vite's resolved root)
170
- *
171
- * @returns The loaded configuration, or a default config if no file is found
172
- *
173
- * @example
174
- * ```typescript
175
- * // macroforge.json
176
- * {
177
- * "keepDecorators": true
178
- * }
179
- * ```
180
- *
181
- * @internal
182
- */
183
- function loadMacroConfig(projectRoot) {
184
- let current = projectRoot;
185
- const fallback = { keepDecorators: false };
186
- while (true) {
187
- const candidate = path.join(current, "macroforge.json");
188
- if (fs.existsSync(candidate)) {
189
- try {
190
- const raw = fs.readFileSync(candidate, "utf8");
191
- const parsed = JSON.parse(raw);
192
- return { keepDecorators: Boolean(parsed.keepDecorators) };
193
- }
194
- catch {
195
- return fallback;
196
- }
197
- }
198
- const parent = path.dirname(current);
199
- if (parent === current)
200
- break;
201
- current = parent;
202
- }
203
- return fallback;
204
- }
205
73
  /**
206
74
  * Retrieves and normalizes TypeScript compiler options for declaration emission.
207
75
  *
@@ -576,8 +444,7 @@ function napiMacrosPlugin(options = {}) {
576
444
  */
577
445
  configResolved(config) {
578
446
  projectRoot = config.root;
579
- macroConfig = loadMacroConfig(projectRoot);
580
- // Load the Rust binary
447
+ // Load the Rust binary first
581
448
  try {
582
449
  rustTransformer = moduleRequire("macroforge");
583
450
  }
@@ -585,6 +452,11 @@ function napiMacrosPlugin(options = {}) {
585
452
  console.warn("[@macroforge/vite-plugin] Rust binary not found. Please run `npm run build:rust` first.");
586
453
  console.warn(error);
587
454
  }
455
+ // Load config (passing Rust transformer for foreign type parsing)
456
+ macroConfig = loadMacroConfig(projectRoot, rustTransformer?.loadConfig);
457
+ if (macroConfig.hasForeignTypes) {
458
+ console.log("[@macroforge/vite-plugin] Loaded config with foreign types from:", macroConfig.configPath);
459
+ }
588
460
  },
589
461
  /**
590
462
  * Transform hook for processing TypeScript files through the macro expander.
@@ -631,11 +503,12 @@ function napiMacrosPlugin(options = {}) {
631
503
  }
632
504
  try {
633
505
  // Collect external decorator modules from macro imports
634
- const externalDecoratorModules = collectExternalDecoratorModules(code);
506
+ const externalDecoratorModules = collectExternalDecoratorModules(code, moduleRequire);
635
507
  // Perform macro expansion via the Rust binary
636
508
  const result = rustTransformer.expandSync(code, id, {
637
509
  keepDecorators: macroConfig.keepDecorators,
638
510
  externalDecoratorModules,
511
+ configPath: macroConfig.configPath,
639
512
  });
640
513
  // Report diagnostics from macro expansion
641
514
  for (const diag of result.diagnostics) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@macroforge/vite-plugin",
3
- "version": "0.1.39",
3
+ "version": "0.1.42",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -18,7 +18,8 @@
18
18
  "test": "npm run build && node --test tests/**/*.test.js"
19
19
  },
20
20
  "dependencies": {
21
- "macroforge": "^0.1.39"
21
+ "@macroforge/shared": "^0.1.42",
22
+ "macroforge": "^0.1.42"
22
23
  },
23
24
  "devDependencies": {
24
25
  "typescript": "^5.9.3",