@mono-lit/utility 0.0.3 → 0.0.5

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,4 +1,8 @@
1
+ import { c as pathAppRoots } from "./app-roots-LpDrHF_J.js";
2
+ import { l as srcDirForType } from "./create-config-DdL3Fh6T.js";
3
+ import { c as monoConfigFileFor, d as resolveFederatedRoots, n as extractConfig } from "./mono-alias-BU2B0tYL.js";
1
4
  import fs from "node:fs";
5
+ import { join } from "node:path";
2
6
 
3
7
  //#region src/vite/remote-matcher.ts
4
8
  /** `C:\a\B` -> `c:/a/b` — ids arrive slashed either way, and Windows is case-insensitive. */
@@ -68,6 +72,103 @@ function parsePageMetaFromFile(file) {
68
72
  }
69
73
  }
70
74
 
75
+ //#endregion
76
+ //#region src/vite/mono-lith.ts
77
+ const MONO_APPS_VIRTUAL_ID = "virtual:mono-apps";
78
+ const RESOLVED_VIRTUAL_ID = "\0virtual:mono-apps";
79
+ /** `C:\a\B` -> `c:/a/b` — chokidar and Vite slash paths differently, and Windows is case-insensitive. */
80
+ function normalizeFsPath(value) {
81
+ const slashed = value.replace(/\\/g, "/");
82
+ return process.platform === "win32" ? slashed.toLowerCase() : slashed;
83
+ }
84
+ /** Forward-slashed absolute path, safe inside a JS string literal. */
85
+ function importSpecifier(file) {
86
+ return JSON.stringify(file.replace(/\\/g, "/"));
87
+ }
88
+ function createMonoLithHooks(options = {}) {
89
+ const rootDir = options.dirname ?? process.cwd();
90
+ const appRoots = options.appRoots ?? resolveFederatedRoots({
91
+ dirname: rootDir,
92
+ ...options.appsDir ? { appsDir: options.appsDir } : {}
93
+ });
94
+ const siblings = pathAppRoots(appRoots);
95
+ const active = options.activeNames ? appRoots.filter((r) => options.activeNames.includes(r.name)) : appRoots;
96
+ const configFiles = [];
97
+ const ownName = extractConfig(rootDir).name ?? "<root>";
98
+ const ownConfig = monoConfigFileFor(rootDir);
99
+ if (ownConfig) configFiles.push({
100
+ name: ownName,
101
+ file: ownConfig
102
+ });
103
+ for (const root of active) {
104
+ const file = monoConfigFileFor(root.root);
105
+ if (file) configFiles.push({
106
+ name: root.name,
107
+ file
108
+ });
109
+ }
110
+ const configByPath = new Map(configFiles.map((c) => [normalizeFsPath(c.file), c.name]));
111
+ const load = (id) => {
112
+ if (id !== RESOLVED_VIRTUAL_ID) return void 0;
113
+ return [
114
+ "// Generated by @mono-lit/utility (virtual:mono-apps): one lazy import per",
115
+ "// federated app’s mono.config, whether it is a sibling or a clone.",
116
+ "export default {",
117
+ ...active.flatMap((r) => {
118
+ const file = monoConfigFileFor(r.root);
119
+ return file ? [` ${JSON.stringify(r.name)}: () => import(${importSpecifier(file)}),`] : [];
120
+ }),
121
+ "}",
122
+ ""
123
+ ].join("\n");
124
+ };
125
+ const configureServer = (server) => {
126
+ if (options.watchSiblings !== false) for (const sibling of siblings) server.watcher.add(join(sibling.root, srcDirForType(sibling.type ?? "vue")));
127
+ if (options.restartOnConfigChange === false) return;
128
+ for (const { file } of configFiles) server.watcher.add(file);
129
+ let restarting = false;
130
+ const onConfigEvent = (file) => {
131
+ const name = configByPath.get(normalizeFsPath(file));
132
+ if (!name || restarting) return;
133
+ restarting = true;
134
+ server.config.logger.info(`[mono] mono.config changed (${name}) — restarting`, { timestamp: true });
135
+ server.restart();
136
+ };
137
+ server.watcher.on("change", onConfigEvent);
138
+ server.watcher.on("add", onConfigEvent);
139
+ server.watcher.on("unlink", onConfigEvent);
140
+ };
141
+ return {
142
+ fsAllow: siblings.map((r) => r.root),
143
+ appRoots,
144
+ configureServer,
145
+ resolveId: (id) => id === "virtual:mono-apps" ? RESOLVED_VIRTUAL_ID : void 0,
146
+ load
147
+ };
148
+ }
149
+ /**
150
+ * Standalone plugin for a long-hand `vite.config.ts`. `monoRepo().vite()` /
151
+ * `mono.plugin` already include these hooks — do not register both.
152
+ *
153
+ * ```ts
154
+ * import { monoLith } from '@mono-lit/utility/vite'
155
+ * plugins: [VueRouter({ … }), vue(), monoLith({ dirname: __dirname })]
156
+ * ```
157
+ */
158
+ function monoLith(options = {}) {
159
+ const hooks = createMonoLithHooks(options);
160
+ return {
161
+ name: "mono-lith",
162
+ config() {
163
+ if (!hooks.fsAllow.length) return;
164
+ return { server: { fs: { allow: [options.dirname ?? process.cwd(), ...hooks.fsAllow] } } };
165
+ },
166
+ configureServer: hooks.configureServer,
167
+ resolveId: hooks.resolveId,
168
+ load: hooks.load
169
+ };
170
+ }
171
+
71
172
  //#endregion
72
173
  //#region src/composables/expose.ts
73
174
  /**
@@ -485,4 +586,4 @@ function unquote(valuePart) {
485
586
  }
486
587
 
487
588
  //#endregion
488
- export { defaultMonoExpose as a, parsePageMetaFromFile as c, monoRouterLinkToNuxtLink as i, remoteGate as l, monoNuxtLinkToRouterLink as n, sanitizeForExpose as o, monoRouterLink as r, parsePageMeta as s, monoNuxtLink as t };
589
+ export { defaultMonoExpose as a, createMonoLithHooks as c, parsePageMetaFromFile as d, remoteGate as f, monoRouterLinkToNuxtLink as i, monoLith as l, monoNuxtLinkToRouterLink as n, sanitizeForExpose as o, monoRouterLink as r, MONO_APPS_VIRTUAL_ID as s, monoNuxtLink as t, parsePageMeta as u };
@@ -1,4 +1,4 @@
1
- import { o as runMonoPrepare } from "./mono-tsconfig-D2qRL9_R.js";
1
+ import { s as runMonoPrepare } from "./mono-tsconfig-4R-pfXV3.js";
2
2
  import path from "node:path";
3
3
  import { defineCommand, runMain } from "citty";
4
4
 
@@ -21,6 +21,10 @@ const main = defineCommand({
21
21
  else console.log("[mono] tsconfig.json already extends ./.mono/tsconfig.json");
22
22
  if (result.removedKeys.length) console.log(`[mono] removed ${result.removedKeys.length} inline alias path(s) from tsconfig.json`);
23
23
  if (result.wiredApps.length) console.log(`[mono] wired ${result.wiredApps.length} cloned app tsconfig(s) -> ${monoRel}: ${result.wiredApps.join(", ")}`);
24
+ for (const app of result.hostedApps) {
25
+ const link = app.link === "skipped" ? "node_modules NOT linked" : `node_modules ${app.link}`;
26
+ console.log(`[mono] hosted app ${app.name}: ${link}, wrote ${path.relative(dirname, app.tsconfig)}`);
27
+ }
24
28
  }
25
29
  });
26
30
  runMain(main);
@@ -1,4 +1,4 @@
1
- import { J as loadSkillsEnv, K as isGenericAppName, O as resolveRemote, R as redactUrl, S as SkillsAccessError, U as ALLOWED_FLAGS, Y as resolveSkillTarget, _ as stagingRoot, b as resolveAppId, i as runSearch, n as runCheck, o as buildSavePlan, p as listSessions, q as loadSkillConfig, r as runRead, s as runSave, t as runSessionShow, w as ensureRepo, x as detectActor } from "./session-Bcig-rPi.js";
1
+ import { J as loadSkillsEnv, K as isGenericAppName, O as resolveRemote, R as redactUrl, S as SkillsAccessError, U as ALLOWED_FLAGS, Y as resolveSkillTarget, _ as stagingRoot, b as resolveAppId, i as runSearch, n as runCheck, o as buildSavePlan, p as listSessions, q as loadSkillConfig, r as runRead, s as runSave, t as runSessionShow, w as ensureRepo, x as detectActor } from "./session-CbSl2BMJ.js";
2
2
  import fs from "node:fs";
3
3
  import path from "node:path";
4
4
  import { defineCommand, runMain } from "citty";
@@ -1,5 +1,6 @@
1
- import { c as resolveAppRoots } from "./app-roots-CvJnmXcs.js";
2
- import { f as stripCommentsSafe, n as extractConfig, s as monoAlias } from "./mono-alias-DNDm-jB_.js";
1
+ import { r as hostedAppRoots, u as resolveAppRoots } from "./app-roots-LpDrHF_J.js";
2
+ import { l as srcDirForType } from "./create-config-DdL3Fh6T.js";
3
+ import { f as stripCommentsSafe, n as extractConfig, s as monoAlias } from "./mono-alias-BU2B0tYL.js";
3
4
  import fs from "node:fs";
4
5
  import path from "node:path";
5
6
  import { readTSConfig, writeTSConfig } from "pkg-types";
@@ -148,6 +149,7 @@ async function runMonoPrepare(opts) {
148
149
  }
149
150
  await writeTSConfig(rootTsconfigPath, root);
150
151
  const wiredApps = await wireClonedApps(dirname, monoTsconfigPath, writtenKeys);
152
+ const hostedApps = adoptHostedApps(dirname);
151
153
  const unpreparedApps = unpreparedPathApps(dirname);
152
154
  if (unpreparedApps.length) console.warn(`[mono] ${unpreparedApps.join(", ")}: tsconfig.json extends ./.mono/tsconfig.json but the file is missing — run \`mono prepare\` in that app.`);
153
155
  return {
@@ -158,9 +160,120 @@ async function runMonoPrepare(opts) {
158
160
  addedExtends,
159
161
  wiredApps,
160
162
  outsideProject,
161
- unpreparedApps
163
+ unpreparedApps,
164
+ hostedApps
162
165
  };
163
166
  }
167
+ /**
168
+ * Prepare every HOSTED (mini) app read in place through `apps[].path`, from the
169
+ * host's side — the mini has no `package.json`, so it can never run a
170
+ * `mono prepare` (or an install) of its own.
171
+ *
172
+ * 1. `<mini>/node_modules` becomes a link (a junction on Windows — no admin
173
+ * rights needed) to the host's `node_modules`. A sibling outside the host
174
+ * has no `node_modules` above it, so every bare import in its files —
175
+ * `vue`, `@mono-lit/utility/*` — fails in Vite AND in jiti (the host's c12
176
+ * loads the mini's `mono.config.ts`). One link fixes Vite, jiti, vue-tsc and
177
+ * the editor together, and because the realpaths ARE the host's, there is
178
+ * still exactly one copy of Vue. A real directory there is never touched.
179
+ * 2. `<mini>/.mono/tsconfig.json` extends the host's config (Nuxt host:
180
+ * `.nuxt/tsconfig.app.json`, Vite host: `tsconfig.json`), and — when the
181
+ * mini has none — `<mini>/tsconfig.json` extends that, so the editor
182
+ * type-checks the mini against the host's aliases and generated
183
+ * auto-import/route types.
184
+ *
185
+ * Unlike `wireClonedApps`' rule for full `path` apps, this DOES write into the
186
+ * sibling — but only files a mini gitignores (`node_modules`, `.mono/`, the
187
+ * generated `tsconfig.json`). Nothing it commits is ever edited. A clone under `.mono/apps` needs none
188
+ * of this: Node already walks up to the host's `node_modules`.
189
+ */
190
+ function adoptHostedApps(dirname) {
191
+ const host = extractConfig(dirname);
192
+ const roots = resolveAppRoots({
193
+ dirname,
194
+ appsDir: MONO_APPS_DIR,
195
+ apps: host.apps,
196
+ onMissingPath: "ignore",
197
+ listClones: false
198
+ });
199
+ const hostModules = path.join(dirname, "node_modules");
200
+ const base = host.type === "nuxt" ? path.join(dirname, ".nuxt", "tsconfig.app.json") : path.join(dirname, "tsconfig.json");
201
+ const prepared = [];
202
+ for (const { name, type, root } of hostedAppRoots(roots)) {
203
+ const link = linkHostModules(name, root, hostModules);
204
+ const miniMono = path.join(root, MONO_DIR);
205
+ if (!fs.existsSync(miniMono)) fs.mkdirSync(miniMono, { recursive: true });
206
+ const tsconfigPath = path.join(miniMono, "tsconfig.json");
207
+ const rel = (abs) => toRelativePosix(miniMono, abs);
208
+ const miniSrc = srcDirForType(type ?? host.type);
209
+ const include = host.type === "nuxt" ? [rel(path.join(dirname, ".nuxt", "nuxt.d.ts"))] : [`${rel(dirname)}/*.d.ts`, `${rel(path.join(dirname, srcDirForType(host.type)))}/**/*.d.ts`];
210
+ const json = {
211
+ extends: rel(base),
212
+ compilerOptions: { rootDir: rel(commonDir(dirname, root)) },
213
+ include: [
214
+ `../${miniSrc}/**/*`,
215
+ "../mono.config.ts",
216
+ "../*.d.ts",
217
+ ...include
218
+ ],
219
+ exclude: ["../node_modules", `${rel(hostModules)}`]
220
+ };
221
+ const text = `${JSON.stringify(json, null, 2)}\n`;
222
+ if (!fs.existsSync(tsconfigPath) || fs.readFileSync(tsconfigPath, "utf8") !== text) fs.writeFileSync(tsconfigPath, text);
223
+ const entryPath = path.join(root, "tsconfig.json");
224
+ if (!fs.existsSync(entryPath)) fs.writeFileSync(entryPath, `${JSON.stringify({ extends: MONO_TSCONFIG_REF }, null, 2)}\n`);
225
+ prepared.push({
226
+ name,
227
+ link,
228
+ tsconfig: tsconfigPath
229
+ });
230
+ }
231
+ return prepared;
232
+ }
233
+ /** Point `<root>/node_modules` at the host's. Never replaces a real directory. */
234
+ function linkHostModules(name, root, hostModules) {
235
+ if (!fs.existsSync(hostModules)) {
236
+ console.warn(`[mono] ${name}: the host has no node_modules yet — install the host first.`);
237
+ return "skipped";
238
+ }
239
+ const linkPath = path.join(root, "node_modules");
240
+ let stat;
241
+ try {
242
+ stat = fs.lstatSync(linkPath);
243
+ } catch {
244
+ stat = void 0;
245
+ }
246
+ if (stat) {
247
+ if (!stat.isSymbolicLink()) {
248
+ console.warn(`[mono] ${name}: ${linkPath} is a real directory — a hosted app uses the host's node_modules; delete it to let mono link them.`);
249
+ return "skipped";
250
+ }
251
+ let current = "";
252
+ try {
253
+ current = fs.realpathSync(linkPath);
254
+ } catch {}
255
+ if (current && samePath(current, fs.realpathSync(hostModules))) return "kept";
256
+ fs.unlinkSync(linkPath);
257
+ }
258
+ fs.symlinkSync(hostModules, linkPath, process.platform === "win32" ? "junction" : "dir");
259
+ return "created";
260
+ }
261
+ function samePath(a, b) {
262
+ const norm = (p) => {
263
+ const r = path.resolve(p);
264
+ return process.platform === "win32" ? r.toLowerCase() : r;
265
+ };
266
+ return norm(a) === norm(b);
267
+ }
268
+ /** Deepest directory containing both `a` and `b`. */
269
+ function commonDir(a, b) {
270
+ const pa = path.resolve(a).split(path.sep);
271
+ const pb = path.resolve(b).split(path.sep);
272
+ const eq = (x, y) => process.platform === "win32" ? x.toLowerCase() === y.toLowerCase() : x === y;
273
+ let i = 0;
274
+ while (i < pa.length && i < pb.length && eq(pa[i], pb[i])) i++;
275
+ return pa.slice(0, i).join(path.sep) || path.sep;
276
+ }
164
277
  /** Written alias keys whose target directory is not under `dirname`. */
165
278
  function pathsOutsideProject(dirname, paths) {
166
279
  const monoDir = path.resolve(dirname, MONO_DIR);
@@ -201,4 +314,4 @@ function unpreparedPathApps(dirname) {
201
314
  }
202
315
 
203
316
  //#endregion
204
- export { monoTsconfigPaths as a, buildMonoTsconfig as i, MONO_DIR as n, runMonoPrepare as o, MONO_TSCONFIG_REF as r, MONO_APPS_DIR as t };
317
+ export { buildMonoTsconfig as a, adoptHostedApps as i, MONO_DIR as n, monoTsconfigPaths as o, MONO_TSCONFIG_REF as r, runMonoPrepare as s, MONO_APPS_DIR as t };
@@ -1,5 +1,5 @@
1
- import { c as resolveAppRoots } from "./app-roots-CvJnmXcs.js";
2
- import { a as extractTemplate, n as extractConfig } from "./mono-alias-DNDm-jB_.js";
1
+ import { u as resolveAppRoots } from "./app-roots-LpDrHF_J.js";
2
+ import { a as extractTemplate, n as extractConfig } from "./mono-alias-BU2B0tYL.js";
3
3
  import { existsSync } from "node:fs";
4
4
  import { join, normalize, resolve } from "node:path";
5
5
 
@@ -1,5 +1,5 @@
1
1
  import "./create-config-B8IhX4ht.js";
2
- import "./app-roots-DK41xMWj.js";
2
+ import "./app-roots-BlcIuAan.js";
3
3
  //#region src/nuxt/nuxt-layers.d.ts
4
4
  interface MonoNuxtLayersOptions {
5
5
  /** Project root. `apps[].path` resolves against it. @default process.cwd() */
@@ -1,3 +1,3 @@
1
- import { n as monoNuxtLayers } from "./nuxt-layers-DP1m7f1-.js";
1
+ import { n as monoNuxtLayers } from "./nuxt-layers-BO0AGwA5.js";
2
2
 
3
3
  export { monoNuxtLayers };
package/dist/nuxt.js CHANGED
@@ -1,10 +1,10 @@
1
- import { o as pathAppRoots, t as appRootDirs } from "./app-roots-CvJnmXcs.js";
1
+ import { c as pathAppRoots, t as appRootDirs } from "./app-roots-LpDrHF_J.js";
2
2
  import { c as resolveMonoConfig, l as srcDirForType, o as resolveExtendsAppNames, s as resolveExtendsEcosystems } from "./create-config-DdL3Fh6T.js";
3
- import { n as mergeEcosystem, r as monoEcosystem, t as ecosystemSubAllowed } from "./merge-file-CtyKTu5L.js";
3
+ import { n as mergeEcosystem, r as monoEcosystem, t as ecosystemSubAllowed } from "./merge-file-DahuGcvr.js";
4
4
  import { t as getMonoConfig } from "./config-node-UzJrXg0Z.js";
5
- import { d as resolveFederatedRoots, n as extractConfig, o as formatMissingApps, s as monoAlias, t as assertAppSources, u as monoStubAliases } from "./mono-alias-DNDm-jB_.js";
6
- import { a as defaultMonoExpose, i as monoRouterLinkToNuxtLink, l as remoteGate, o as sanitizeForExpose, s as parsePageMeta } from "./mono-link-YlOQ2PVS.js";
7
- import { r as registerMonoNuxtLayers, t as monoLayerCandidates } from "./nuxt-layers-DP1m7f1-.js";
5
+ import { d as resolveFederatedRoots, n as extractConfig, o as formatMissingApps, s as monoAlias, t as assertAppSources, u as monoStubAliases } from "./mono-alias-BU2B0tYL.js";
6
+ import { a as defaultMonoExpose, c as createMonoLithHooks, f as remoteGate, i as monoRouterLinkToNuxtLink, o as sanitizeForExpose, u as parsePageMeta } from "./mono-link-URGX8xsT.js";
7
+ import { r as registerMonoNuxtLayers, t as monoLayerCandidates } from "./nuxt-layers-BO0AGwA5.js";
8
8
  import { existsSync, readFileSync, readdirSync, realpathSync, statSync } from "node:fs";
9
9
  import { isAbsolute, join, normalize, relative, resolve, sep } from "node:path";
10
10
  import { addComponentsDir, addImportsDir, addPlugin, addPluginTemplate, addRouteMiddleware, addTemplate, addVitePlugin, defineNuxtModule, extendPages, extendViteConfig } from "@nuxt/kit";
@@ -246,6 +246,18 @@ const monoNuxtModule = defineNuxtModule({
246
246
  const activeNames = resolveExtendsAppNames(rawConfig);
247
247
  const activeApps = hasExtends ? (rawConfig.apps ?? []).filter((a) => activeNames.includes(a.name)) : rawConfig.apps ?? [];
248
248
  const activeRoots = appRoots.filter((r) => activeApps.some((a) => a.name === r.name));
249
+ const lith = createMonoLithHooks({
250
+ dirname: rootDir,
251
+ appRoots,
252
+ ...hasExtends ? { activeNames } : {},
253
+ restartOnConfigChange: false
254
+ });
255
+ addVitePlugin({
256
+ name: "mono-nuxt-apps",
257
+ configureServer: lith.configureServer,
258
+ resolveId: lith.resolveId,
259
+ load: lith.load
260
+ });
249
261
  const extendsEcosystems = resolveExtendsEcosystems(rawConfig);
250
262
  const monoConfig = {
251
263
  ...resolveMonoConfig(rawConfig),