@deepseek-ai/cordis-plugin-loader 1.0.6 → 1.0.7-alpha.1
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/README.md +10 -0
- package/lib/index.js +77 -9
- package/lib/types/config/entry.d.ts.map +1 -1
- package/lib/types/config/tree.d.ts.map +1 -1
- package/lib/types/resolve.d.ts +9 -0
- package/lib/types/resolve.d.ts.map +1 -0
- package/package.json +4 -4
- package/src/config/entry.ts +5 -5
- package/src/config/tree.ts +4 -1
- package/src/resolve.ts +75 -0
package/README.md
CHANGED
|
@@ -48,6 +48,16 @@ root.loader.update(id, { config: { enabled: false } })
|
|
|
48
48
|
|
|
49
49
|
For file-backed trees, use `@cordisjs/plugin-include`.
|
|
50
50
|
|
|
51
|
+
## Module resolution
|
|
52
|
+
|
|
53
|
+
Without Node module internals, Loader resolves bare specifiers, package imports and self-references in the nearest package scope containing the configuration file. Relative specifiers use the configuration URL directly, and `cordis:` builtins bypass filesystem resolution. Other specifiers, including `node:` builtins and absolute URLs, initialize an ignored `.cordis/resolve.mjs` helper in that scope to preserve native ESM import conditions.
|
|
54
|
+
|
|
55
|
+
If no scope exists or writing the helper files fails, Loader uses its native import fallback. This fallback emits no diagnostic of its own; project-only dependencies can remain unavailable, and the resulting import error can name the Loader module rather than the configuration project. Failure to import a written helper propagates to the caller without this fallback.
|
|
56
|
+
|
|
57
|
+
Initialization results are cached for the lifetime of the resolver module, including failed writes and rejected helper imports. Fixing the filesystem does not retry initialization for an already cached scope. Concurrent initialization through filesystem aliases of the same package can contend for the temporary helper files and cause a write failure.
|
|
58
|
+
|
|
59
|
+
Node module internals are classified by their available APIs, including the v1 API used by Node 24.0–24.11.
|
|
60
|
+
|
|
51
61
|
## Volatile configuration
|
|
52
62
|
|
|
53
63
|
`Entry.update` compares raw entry options strictly; when only `config` changed on an active fiber in an unchanged context, the pure `equalExceptVolatile` function in `config/diff.ts` compares the raw configs using Schemastery metadata. It ignores volatile fields at fixed object paths without executing expressions, validators or `internal/config` hooks. Object nodes use their declared defaults for null or absent values, including objects without volatile fields; equivalent defaults skip updates. Other ordinary values, parent expressions and unknown fields retain raw comparison. Recursive schema backedges use raw comparison. Entry passes the actual plugin schema; a non-Schemastery schema compares raw. Context changes, other option changes and pending fibers use ordinary updates. A volatile-only change retains the raw Fiber config for later activation, then `_commitVolatile` parses it through the fiber's `internal/config` hook and schema, commits the new values into the running references when every ordinary effective value still matches, and emits `loader/volatile-update` to the owning fiber with the changed paths. A changed ordinary effective value uses the ordinary remount with a debug log naming the entry; because strict comparison treats class instances other than URL, Date and RegExp by identity, an ordinary field whose transform builds a fresh instance on every parse always takes that remount. An invalid candidate is logged and leaves the running references unchanged. A throwing `loader/volatile-update` listener is logged and does not fail the update; the values are already committed. Group/Include retain child dispatch, and programmatic Fiber updates preserve saving and `noSave`; `simplify` is called with its schema receiver so saved config contains plain values.
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
|
-
import { Context, Inject, Service, composeError
|
|
2
|
+
import { Context, Inject, Service, composeError } from "@deepseek-ai/cordis";
|
|
3
3
|
import { deepEqual, defineProperty, isNonNullable, isNullable, updateVolatile, valueMap, volatileEntries } from "@deepseek-ai/cosmokit";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import fsPromises from "node:fs/promises";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import url from "node:url";
|
|
4
8
|
//#region lib/types/internal.js
|
|
5
9
|
/** Helpers for locating the current Node internal module loader. */
|
|
6
10
|
var ModuleLoader;
|
|
@@ -115,6 +119,67 @@ var Group = class extends EntryGroup {
|
|
|
115
119
|
}
|
|
116
120
|
};
|
|
117
121
|
//#endregion
|
|
122
|
+
//#region lib/types/resolve.js
|
|
123
|
+
var __rewriteRelativeImportExtension$1 = function(path, preserveJsx) {
|
|
124
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function(m, tsx, d, ext, cm) {
|
|
125
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : d + ext + "." + cm.toLowerCase() + "js";
|
|
126
|
+
});
|
|
127
|
+
return path;
|
|
128
|
+
};
|
|
129
|
+
const HELPER_FILES = {
|
|
130
|
+
".gitignore": "# created by cordis automatically\n*\n",
|
|
131
|
+
"resolve.mjs": "export default (specifier) => import.meta.resolve(specifier)\n"
|
|
132
|
+
};
|
|
133
|
+
const cache = Object.create(null);
|
|
134
|
+
/** Nearest ancestor of `dir` that owns a `package.json`, if any. */
|
|
135
|
+
function findScope(dir) {
|
|
136
|
+
while (true) {
|
|
137
|
+
if (fs.existsSync(path.join(dir, "package.json"))) return dir;
|
|
138
|
+
const parent = path.dirname(dir);
|
|
139
|
+
if (parent === dir) return;
|
|
140
|
+
dir = parent;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function resolveScope(baseUrl) {
|
|
144
|
+
if (!baseUrl) return;
|
|
145
|
+
let dir;
|
|
146
|
+
try {
|
|
147
|
+
dir = url.fileURLToPath(new URL(".", baseUrl));
|
|
148
|
+
} catch {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
return findScope(path.resolve(dir));
|
|
152
|
+
}
|
|
153
|
+
async function write(filename, content) {
|
|
154
|
+
const temp = `${filename}.${process.pid}`;
|
|
155
|
+
await fsPromises.writeFile(temp, content);
|
|
156
|
+
await fsPromises.rename(temp, filename);
|
|
157
|
+
}
|
|
158
|
+
async function _createResolve(scope) {
|
|
159
|
+
const dir = path.join(scope, ".cordis");
|
|
160
|
+
try {
|
|
161
|
+
await fsPromises.mkdir(dir, { recursive: true });
|
|
162
|
+
for (const [name, content] of Object.entries(HELPER_FILES)) await write(path.join(dir, name), content);
|
|
163
|
+
} catch {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const helperUrl = url.pathToFileURL(path.join(dir, "resolve.mjs")).href;
|
|
167
|
+
return (await import(__rewriteRelativeImportExtension$1(
|
|
168
|
+
/* @vite-ignore */
|
|
169
|
+
helperUrl
|
|
170
|
+
))).default;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Create or reuse a native ESM resolver anchored in the config project's package scope.
|
|
174
|
+
* @param baseUrl Config file URL; absent, non-file or unscoped URLs cannot supply a resolver.
|
|
175
|
+
* @returns The project resolver, or undefined when no package scope exists or its helper cannot be written.
|
|
176
|
+
*/
|
|
177
|
+
async function createResolve(baseUrl) {
|
|
178
|
+
const scope = resolveScope(baseUrl);
|
|
179
|
+
if (!scope) return;
|
|
180
|
+
return await (cache[scope] ??= _createResolve(scope));
|
|
181
|
+
}
|
|
182
|
+
//#endregion
|
|
118
183
|
//#region lib/types/config/tree.js
|
|
119
184
|
var __rewriteRelativeImportExtension = function(path, preserveJsx) {
|
|
120
185
|
if (typeof path === "string" && /^\.\.?\//.test(path)) return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function(m, tsx, d, ext, cm) {
|
|
@@ -220,10 +285,13 @@ var EntryTree = class EntryTree {
|
|
|
220
285
|
/* @vite-ignore */
|
|
221
286
|
new URL(name, this.ctx.baseUrl).href
|
|
222
287
|
));
|
|
223
|
-
else
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
288
|
+
else {
|
|
289
|
+
const resolve = await createResolve(this.ctx.baseUrl);
|
|
290
|
+
return await import(__rewriteRelativeImportExtension(
|
|
291
|
+
/* @vite-ignore */
|
|
292
|
+
resolve ? resolve(name) : name
|
|
293
|
+
));
|
|
294
|
+
}
|
|
227
295
|
}, getOuterStack);
|
|
228
296
|
}
|
|
229
297
|
};
|
|
@@ -380,7 +448,7 @@ var Entry = class Entry {
|
|
|
380
448
|
...legacy
|
|
381
449
|
}).filter((key) => !deepEqual(this.options[key], legacy[key], key === "config"));
|
|
382
450
|
const volatileOnly = changes.length === 1 && changes[0] === "config" && this.fiber.state === 2 && Object.getPrototypeOf(this.ctx) === this.parent.ctx && equalExceptVolatile(legacy.config, this.options.config, this.fiber.runtime?.Config);
|
|
383
|
-
if (volatileOnly) this.fiber.
|
|
451
|
+
if (volatileOnly) this.fiber.rawConfig = this.options.config;
|
|
384
452
|
const pending = volatileOnly && this._commitVolatile() ? [] : changes;
|
|
385
453
|
if (!pending.length && !force) return;
|
|
386
454
|
this.context.emit("loader/partial-dispose", this, legacy, true);
|
|
@@ -394,18 +462,18 @@ var Entry = class Entry {
|
|
|
394
462
|
*/
|
|
395
463
|
_commitVolatile() {
|
|
396
464
|
const fiber = this.fiber;
|
|
397
|
-
const refs = volatileEntries(fiber.
|
|
465
|
+
const refs = volatileEntries(fiber.effectiveConfig);
|
|
398
466
|
if (!refs.length) return true;
|
|
399
467
|
const raw = this.options.config;
|
|
400
468
|
let candidate;
|
|
401
469
|
try {
|
|
402
|
-
candidate =
|
|
470
|
+
candidate = fiber.resolveConfig(raw);
|
|
403
471
|
} catch (error) {
|
|
404
472
|
this.ctx.logger.warn("volatile config update failed for %C", this.options.id);
|
|
405
473
|
this.ctx.logger.warn(error);
|
|
406
474
|
return true;
|
|
407
475
|
}
|
|
408
|
-
if (!deepEqual(fiber.
|
|
476
|
+
if (!deepEqual(fiber.effectiveConfig, candidate, true)) {
|
|
409
477
|
this.ctx.logger.debug("ordinary config values of %C changed with its volatile values; applying the ordinary update", this.options.id);
|
|
410
478
|
return false;
|
|
411
479
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entry.d.ts","sourceRoot":"","sources":["../../../src/config/entry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAc,MAAM,
|
|
1
|
+
{"version":3,"file":"entry.d.ts","sourceRoot":"","sources":["../../../src/config/entry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAc,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAExE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAIrC,qEAAqE;AACrE,MAAM,WAAW,YAAY;IAC3B,kDAAkD;IAClD,EAAE,EAAE,MAAM,CAAA;IACV,mDAAmD;IACnD,IAAI,EAAE,MAAM,CAAA;IACZ,mCAAmC;IACnC,MAAM,CAAC,EAAE,GAAG,CAAA;IACZ,0CAA0C;IAC1C,KAAK,CAAC,EAAE,OAAO,GAAG,IAAI,CAAA;IACtB,wDAAwD;IACxD,QAAQ,CAAC,EAAE,OAAO,GAAG,IAAI,CAAA;IACzB,oEAAoE;IACpE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CACvB;AAmBD,wDAAwD;AACxD,qBAAa,KAAK;IAeG,MAAM,EAAE,MAAM;IAdjC,MAAM,CAAC,QAAQ,CAAC,GAAG,gBAA6B;IAEzC,GAAG,EAAE,OAAO,CAAA;IACZ,KAAK,CAAC,EAAE,KAAK,CAAA;IACpB,+FAA+F;IACxF,eAAe,EAAE,OAAO,CAAA;IACxB,MAAM,EAAG,UAAU,CAAA;IAEnB,OAAO,EAAS,YAAY,CAAA;IAC5B,QAAQ,CAAC,EAAE,UAAU,CAAA;IACrB,OAAO,CAAC,EAAE,SAAS,CAAA;IAE1B,SAAS,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;gBAEN,MAAM,EAAE,MAAM;IAKjC,IAAI,OAAO,IAAI,OAAO,CAErB;IAED,IAAI,EAAE,WAML;IAED,mEAAmE;IACnE,IAAI,QAAQ,YASX;IAED;;;OAGG;IACH,OAAO,CAAC,UAAU;IAMlB,QAAQ,CAAC,IAAI,EAAE,MAAM;IAIrB,OAAO,CAAC,aAAa;IAUf,OAAO;IAMb,iFAAiF;IAC3E,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC,EAAE,MAAM,UAAQ,EAAE,KAAK,UAAQ;IAyC1E;;;;OAIG;IACH,OAAO,CAAC,eAAe;IAmCvB,aAAa,iBAQZ;IAED,2EAA2E;IACrE,IAAI;YAaI,KAAK;CAgBpB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tree.d.ts","sourceRoot":"","sources":["../../../src/config/tree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC3D,OAAO,EAAiB,KAAK,IAAI,EAAE,MAAM,uBAAuB,CAAA;AAChE,OAAO,EAAE,KAAK,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAA;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"tree.d.ts","sourceRoot":"","sources":["../../../src/config/tree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC3D,OAAO,EAAiB,KAAK,IAAI,EAAE,MAAM,uBAAuB,CAAA;AAChE,OAAO,EAAE,KAAK,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAA;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAGvC,6EAA6E;AAC7E,8BAAsB,SAAS;IAC7B,MAAM,CAAC,QAAQ,CAAC,GAAG,OAAM;IAElB,GAAG,EAAE,OAAO,CAAA;IACZ,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,IAAI,EAAE,UAAU,CAAA;IAChB,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAAsB;gBAEnC,GAAG,EAAE,OAAO;IAOxB,IAAI,OAAO,IAAI,OAAO,CAErB;IAED,4DAA4D;IAC1D,OAAO,IAAI,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC;IAQzC,oEAAoE;IACpE,QAAQ;IAMR,qEAAqE;IAC/D,KAAK;IAQX,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC;IASvC,iFAAiF;IACjF,OAAO,CAAC,EAAE,EAAE,MAAM;IAalB,YAAY,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;IAO9B,2DAA2D;IACrD,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,EAAE,MAAM,GAAE,MAAM,GAAG,IAAW,EAAE,QAAQ,SAAW;IAOjG,sDAAsD;IACtD,MAAM,CAAC,EAAE,EAAE,MAAM;IAMjB,+DAA+D;IACzD,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,YAAY,EAAE,IAAI,GAAG,MAAM,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,EAAE,QAAQ,CAAC,EAAE,MAAM;IAc9G,oEAAoE;IACpE,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,MAAM,MAAM,EAAE;IAqBnD,iFAAiF;IACjF,QAAQ,CAAC,KAAK,IAAI,IAAI;CACvB"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Resolve a specifier to its URL in one configuration project. */
|
|
2
|
+
export type Resolve = (specifier: string) => string;
|
|
3
|
+
/**
|
|
4
|
+
* Create or reuse a native ESM resolver anchored in the config project's package scope.
|
|
5
|
+
* @param baseUrl Config file URL; absent, non-file or unscoped URLs cannot supply a resolver.
|
|
6
|
+
* @returns The project resolver, or undefined when no package scope exists or its helper cannot be written.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createResolve(baseUrl: string | undefined): Promise<Resolve | undefined>;
|
|
9
|
+
//# sourceMappingURL=resolve.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve.d.ts","sourceRoot":"","sources":["../../src/resolve.ts"],"names":[],"mappings":"AAMA,mEAAmE;AACnE,MAAM,MAAM,OAAO,GAAG,CAAC,SAAS,EAAE,MAAM,KAAK,MAAM,CAAA;AA0DnD;;;;GAIG;AACH,wBAAsB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,gCAI9D"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deepseek-ai/cordis-plugin-loader",
|
|
3
3
|
"description": "Plugin loader for cordis",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.7-alpha.1",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"author": "Shigma <shigma10826@gmail.com>",
|
|
31
31
|
"license": "MIT",
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@deepseek-ai/cordis": "~4.0.
|
|
33
|
+
"@deepseek-ai/cordis": "~4.0.6-alpha.1",
|
|
34
34
|
"node-addon-require-builtin": "^0.1.9"
|
|
35
35
|
},
|
|
36
36
|
"peerDependenciesMeta": {
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
}
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@deepseek-ai/cosmokit": "~1.8.
|
|
43
|
-
"@deepseek-ai/schemastery": "~3.18.
|
|
42
|
+
"@deepseek-ai/cosmokit": "~1.8.7-alpha.1",
|
|
43
|
+
"@deepseek-ai/schemastery": "~3.18.6-alpha.1"
|
|
44
44
|
}
|
|
45
45
|
}
|
package/src/config/entry.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Context, Fiber, FiberState, Inject
|
|
1
|
+
import { Context, Fiber, FiberState, Inject } from '@deepseek-ai/cordis'
|
|
2
2
|
import { deepEqual, isNullable, updateVolatile, volatileEntries, type Volatile } from '@deepseek-ai/cosmokit'
|
|
3
3
|
import { Loader } from '../index.ts'
|
|
4
4
|
import { EntryGroup } from './group.ts'
|
|
@@ -146,7 +146,7 @@ export class Entry {
|
|
|
146
146
|
const volatileOnly = changes.length === 1 && changes[0] === 'config'
|
|
147
147
|
&& this.fiber.state === FiberState.ACTIVE && Object.getPrototypeOf(this.ctx) === this.parent.ctx
|
|
148
148
|
&& equalExceptVolatile(legacy.config, this.options.config, this.fiber.runtime?.Config)
|
|
149
|
-
if (volatileOnly) this.fiber.
|
|
149
|
+
if (volatileOnly) this.fiber.rawConfig = this.options.config
|
|
150
150
|
const pending = volatileOnly && this._commitVolatile() ? [] : changes
|
|
151
151
|
if (!pending.length && !force) return
|
|
152
152
|
this.context.emit('loader/partial-dispose', this, legacy, true)
|
|
@@ -163,18 +163,18 @@ export class Entry {
|
|
|
163
163
|
*/
|
|
164
164
|
private _commitVolatile(): boolean {
|
|
165
165
|
const fiber = this.fiber!
|
|
166
|
-
const refs = volatileEntries(fiber.
|
|
166
|
+
const refs = volatileEntries(fiber.effectiveConfig)
|
|
167
167
|
if (!refs.length) return true
|
|
168
168
|
const raw = this.options.config
|
|
169
169
|
let candidate: unknown
|
|
170
170
|
try {
|
|
171
|
-
candidate =
|
|
171
|
+
candidate = fiber.resolveConfig(raw)
|
|
172
172
|
} catch (error) {
|
|
173
173
|
this.ctx.logger.warn('volatile config update failed for %C', this.options.id)
|
|
174
174
|
this.ctx.logger.warn(error)
|
|
175
175
|
return true
|
|
176
176
|
}
|
|
177
|
-
if (!deepEqual(fiber.
|
|
177
|
+
if (!deepEqual(fiber.effectiveConfig, candidate, true)) {
|
|
178
178
|
this.ctx.logger.debug('ordinary config values of %C changed with its volatile values; applying the ordinary update', this.options.id)
|
|
179
179
|
return false
|
|
180
180
|
}
|
package/src/config/tree.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { composeError, Context } from '@deepseek-ai/cordis'
|
|
|
2
2
|
import { isNonNullable, type Dict } from '@deepseek-ai/cosmokit'
|
|
3
3
|
import { Entry, type EntryOptions } from './entry.ts'
|
|
4
4
|
import { EntryGroup } from './group.ts'
|
|
5
|
+
import { createResolve } from '../resolve.ts'
|
|
5
6
|
|
|
6
7
|
/** Mutable tree of loader entries. Persistence is supplied by subclasses. */
|
|
7
8
|
export abstract class EntryTree {
|
|
@@ -123,7 +124,9 @@ export abstract class EntryTree {
|
|
|
123
124
|
} else if (name.startsWith('.')) {
|
|
124
125
|
return await import(/* @vite-ignore */new URL(name, this.ctx.baseUrl).href)
|
|
125
126
|
} else {
|
|
126
|
-
|
|
127
|
+
const resolve = await createResolve(this.ctx.baseUrl)
|
|
128
|
+
const url = resolve ? resolve(name) : name
|
|
129
|
+
return await import(/* @vite-ignore */ url)
|
|
127
130
|
}
|
|
128
131
|
}, getOuterStack)
|
|
129
132
|
}
|
package/src/resolve.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import fsPromises from 'node:fs/promises'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import url from 'node:url'
|
|
5
|
+
import type { Dict } from '@deepseek-ai/cosmokit'
|
|
6
|
+
|
|
7
|
+
/** Resolve a specifier to its URL in one configuration project. */
|
|
8
|
+
export type Resolve = (specifier: string) => string
|
|
9
|
+
|
|
10
|
+
// Order matters: the directory ignores itself before the helper appears in it.
|
|
11
|
+
const HELPER_FILES = {
|
|
12
|
+
'.gitignore': '# created by cordis automatically\n*\n',
|
|
13
|
+
// The one-argument form of `import.meta.resolve` anchors on this file, so
|
|
14
|
+
// resolution happens in the project scope. A resolve call also survives a
|
|
15
|
+
// bundling host, which transforms modules inside its root and redirects the
|
|
16
|
+
// `import()` in them to its own resolver.
|
|
17
|
+
'resolve.mjs': 'export default (specifier) => import.meta.resolve(specifier)\n',
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const cache: Dict<Promise<Resolve | undefined>> = Object.create(null)
|
|
21
|
+
|
|
22
|
+
/** Nearest ancestor of `dir` that owns a `package.json`, if any. */
|
|
23
|
+
function findScope(dir: string) {
|
|
24
|
+
while (true) {
|
|
25
|
+
if (fs.existsSync(path.join(dir, 'package.json'))) return dir
|
|
26
|
+
const parent = path.dirname(dir)
|
|
27
|
+
if (parent === dir) return
|
|
28
|
+
dir = parent
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function resolveScope(baseUrl: string | undefined) {
|
|
33
|
+
if (!baseUrl) return
|
|
34
|
+
let dir: string
|
|
35
|
+
try {
|
|
36
|
+
dir = url.fileURLToPath(new URL('.', baseUrl))
|
|
37
|
+
} catch {
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
return findScope(path.resolve(dir))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function write(filename: string, content: string) {
|
|
44
|
+
// Another process may be writing the same path; `rename` is atomic, so a
|
|
45
|
+
// concurrent reader only ever observes the complete helper.
|
|
46
|
+
const temp = `${filename}.${process.pid}`
|
|
47
|
+
await fsPromises.writeFile(temp, content)
|
|
48
|
+
await fsPromises.rename(temp, filename)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function _createResolve(scope: string): Promise<Resolve | undefined> {
|
|
52
|
+
const dir = path.join(scope, '.cordis')
|
|
53
|
+
try {
|
|
54
|
+
await fsPromises.mkdir(dir, { recursive: true })
|
|
55
|
+
for (const [name, content] of Object.entries(HELPER_FILES)) {
|
|
56
|
+
await write(path.join(dir, name), content)
|
|
57
|
+
}
|
|
58
|
+
} catch {
|
|
59
|
+
// Read-only filesystems are legitimate; the caller falls back to a native `import()`.
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
const helperUrl = url.pathToFileURL(path.join(dir, 'resolve.mjs')).href
|
|
63
|
+
return (await import(/* @vite-ignore */ helperUrl)).default
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Create or reuse a native ESM resolver anchored in the config project's package scope.
|
|
68
|
+
* @param baseUrl Config file URL; absent, non-file or unscoped URLs cannot supply a resolver.
|
|
69
|
+
* @returns The project resolver, or undefined when no package scope exists or its helper cannot be written.
|
|
70
|
+
*/
|
|
71
|
+
export async function createResolve(baseUrl: string | undefined) {
|
|
72
|
+
const scope = resolveScope(baseUrl)
|
|
73
|
+
if (!scope) return
|
|
74
|
+
return await (cache[scope] ??= _createResolve(scope))
|
|
75
|
+
}
|