@mdsvex/language-core 0.1.0-next.1 → 0.1.0-next.3
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/dist/_svelte_shims.d.ts +1 -0
- package/dist/completions.d.ts +51 -0
- package/dist/config.d.ts +48 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +1254 -0
- package/dist/language_plugin.d.ts +53 -0
- package/dist/svelte_plugin.d.ts +14 -0
- package/package.json +16 -7
- package/CHANGELOG.md +0 -37
- package/src/_svelte_shims.ts +0 -1
- package/src/completions.ts +0 -242
- package/src/config.ts +0 -521
- package/src/index.ts +0 -31
- package/src/language_plugin.ts +0 -556
- package/src/svelte_plugin.ts +0 -183
- package/test/completions.spec.ts +0 -179
- package/test/config.spec.ts +0 -304
- package/test/language_plugin.spec.ts +0 -236
- package/test/language_service.spec.ts +0 -314
- package/test/ts_harness.ts +0 -168
- package/tsconfig.json +0 -11
package/src/config.ts
DELETED
|
@@ -1,521 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* the mdsvex config of a document without running vite, from the nearest
|
|
3
|
-
* manifest the plugin wrote or else the nearest mdsvex.config.json
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import * as fs from 'node:fs';
|
|
7
|
-
import * as path from 'node:path';
|
|
8
|
-
import { fileURLToPath } from 'node:url';
|
|
9
|
-
import type * as TS from 'typescript';
|
|
10
|
-
|
|
11
|
-
import { MANIFEST_PATH, MANIFEST_VERSION, module_script } from 'mdsvex';
|
|
12
|
-
import type {
|
|
13
|
-
CompileOptions,
|
|
14
|
-
ComponentMode,
|
|
15
|
-
HighlightConfig,
|
|
16
|
-
ManifestModule,
|
|
17
|
-
ManifestTemplate,
|
|
18
|
-
MdsvexManifest,
|
|
19
|
-
TemplateEntry,
|
|
20
|
-
} from 'mdsvex';
|
|
21
|
-
import { create_highlight } from 'mdsvex/highlight';
|
|
22
|
-
import { default_languages } from 'mdsvex/highlight/languages';
|
|
23
|
-
import type { PfmToSvelteOptions } from '@mdsvex/source-map/pfm-to-svelte';
|
|
24
|
-
|
|
25
|
-
export const CONFIG_FILE = 'mdsvex.config.json';
|
|
26
|
-
const COMPONENTS_ID = 'mdsvex:components';
|
|
27
|
-
const DIRECTIVES_ID = 'mdsvex:directives';
|
|
28
|
-
const TEMPLATE_ID = 'mdsvex:template/';
|
|
29
|
-
const TEMPLATE_DIRECTIVES_ID = 'mdsvex:template-directives/';
|
|
30
|
-
const DIRECTIVES_EXPORT = 'directives';
|
|
31
|
-
|
|
32
|
-
/** the shape of mdsvex.config.json, the vite plugin options that are data */
|
|
33
|
-
export interface JsonConfig {
|
|
34
|
-
extensions?: string[];
|
|
35
|
-
templates?: Record<
|
|
36
|
-
string,
|
|
37
|
-
string | { component: string; components?: string }
|
|
38
|
-
>;
|
|
39
|
-
components?: string | string[];
|
|
40
|
-
component_mode?: ComponentMode;
|
|
41
|
-
/** false renders code plain, as the highlight option of the plugin does */
|
|
42
|
-
highlight?: boolean;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface LoadedConfig {
|
|
46
|
-
/** the file read, the manifest or the json config */
|
|
47
|
-
file: string;
|
|
48
|
-
/** changes whenever the file does, for caches */
|
|
49
|
-
stamp: string;
|
|
50
|
-
manifest: MdsvexManifest;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** the options a document converts with, and a stamp to cache them by */
|
|
54
|
-
export interface DocumentOptions {
|
|
55
|
-
stamp: string;
|
|
56
|
-
options: PfmToSvelteOptions;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export interface ConfigLoader {
|
|
60
|
-
/** the nearest config of a file, null without one */
|
|
61
|
-
load(file: string): LoadedConfig | null;
|
|
62
|
-
options_for(file: string): DocumentOptions;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const posix = (p: string) => p.replace(/\\/g, '/');
|
|
66
|
-
|
|
67
|
-
export interface ConfigLoaderOptions {
|
|
68
|
-
/** reads the exports of templates a mdsvex.config.json names, without it they replace nothing */
|
|
69
|
-
typescript?: typeof TS;
|
|
70
|
-
/** called with each config file read, so a server can watch it */
|
|
71
|
-
on_read?: (file: string) => void;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export function create_config_loader(
|
|
75
|
-
loader_options: ConfigLoaderOptions = {}
|
|
76
|
-
): ConfigLoader {
|
|
77
|
-
const ts = loader_options.typescript;
|
|
78
|
-
const parsed = new Map<string, LoadedConfig & { mtime: number }>();
|
|
79
|
-
|
|
80
|
-
function read(file: string, json: boolean): LoadedConfig | null {
|
|
81
|
-
let stat: fs.Stats;
|
|
82
|
-
try {
|
|
83
|
-
stat = fs.statSync(file);
|
|
84
|
-
} catch {
|
|
85
|
-
return null;
|
|
86
|
-
}
|
|
87
|
-
const hit = parsed.get(file);
|
|
88
|
-
if (hit !== undefined && hit.mtime === stat.mtimeMs) return hit;
|
|
89
|
-
let manifest: MdsvexManifest | null = null;
|
|
90
|
-
try {
|
|
91
|
-
const raw = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
92
|
-
manifest = json
|
|
93
|
-
? from_json(raw, path.dirname(file), ts)
|
|
94
|
-
: raw.version === MANIFEST_VERSION
|
|
95
|
-
? raw
|
|
96
|
-
: null;
|
|
97
|
-
} catch {
|
|
98
|
-
manifest = null;
|
|
99
|
-
}
|
|
100
|
-
loader_options.on_read?.(file);
|
|
101
|
-
if (manifest === null) return null;
|
|
102
|
-
const loaded = {
|
|
103
|
-
file,
|
|
104
|
-
stamp: `${file}@${stat.mtimeMs}`,
|
|
105
|
-
manifest,
|
|
106
|
-
mtime: stat.mtimeMs,
|
|
107
|
-
};
|
|
108
|
-
parsed.set(file, loaded);
|
|
109
|
-
return loaded;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function load(file: string): LoadedConfig | null {
|
|
113
|
-
let dir = path.dirname(file);
|
|
114
|
-
for (;;) {
|
|
115
|
-
const found =
|
|
116
|
-
read(path.join(dir, MANIFEST_PATH), false) ??
|
|
117
|
-
read(path.join(dir, CONFIG_FILE), true);
|
|
118
|
-
if (found !== null) return found;
|
|
119
|
-
const up = path.dirname(dir);
|
|
120
|
-
if (up === dir) return null;
|
|
121
|
-
dir = up;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return {
|
|
126
|
-
load,
|
|
127
|
-
options_for(file) {
|
|
128
|
-
const config = load(file);
|
|
129
|
-
if (config === null) return { stamp: '', options: {} };
|
|
130
|
-
return {
|
|
131
|
-
stamp: config.stamp,
|
|
132
|
-
options: document_options(config.manifest, file),
|
|
133
|
-
};
|
|
134
|
-
},
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/** a specifier typescript resolves to file from the directory of doc */
|
|
139
|
-
function specifier_from(doc: string, file: string): string {
|
|
140
|
-
let rel = posix(path.relative(path.dirname(doc), file));
|
|
141
|
-
if (!rel.startsWith('.') && !path.isAbsolute(rel)) rel = './' + rel;
|
|
142
|
-
// a .ts import needs allowImportingTsExtensions, .js finds the .ts in every resolution mode
|
|
143
|
-
return rel.replace(/\.([mc]?)ts$/, (_, m) => `.${m}js`);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function sources(modules: ManifestModule[]) {
|
|
147
|
-
return modules.length === 0
|
|
148
|
-
? undefined
|
|
149
|
-
: modules.map((m) => ({ specifier: m.id, names: m.names }));
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/** the conversion options of doc under manifest */
|
|
153
|
-
export function document_options(
|
|
154
|
-
manifest: MdsvexManifest,
|
|
155
|
-
doc: string
|
|
156
|
-
): PfmToSvelteOptions {
|
|
157
|
-
const templates: Record<string, TemplateEntry> = {};
|
|
158
|
-
const files = new Map<string, (name: string) => string>();
|
|
159
|
-
for (const [name, t] of Object.entries(manifest.templates)) {
|
|
160
|
-
const extra = t.extra;
|
|
161
|
-
const names =
|
|
162
|
-
extra === null ? t.components : union(t.components, extra.names);
|
|
163
|
-
const entry: TemplateEntry = { specifier: t.id, components: names };
|
|
164
|
-
if (t.directives.length !== 0) entry.directives = sources(t.directives);
|
|
165
|
-
templates[name] = entry;
|
|
166
|
-
// the extra module wins a name both export, as the merging module of the plugin does
|
|
167
|
-
files.set(t.id, (n) =>
|
|
168
|
-
n !== 'default' && extra !== null && extra.names.includes(n)
|
|
169
|
-
? extra.file
|
|
170
|
-
: t.file
|
|
171
|
-
);
|
|
172
|
-
for (const d of t.directives) files.set(d.id, () => d.file);
|
|
173
|
-
}
|
|
174
|
-
for (const m of [...manifest.components, ...manifest.directives])
|
|
175
|
-
files.set(m.id, () => m.file);
|
|
176
|
-
|
|
177
|
-
const compile: CompileOptions = {
|
|
178
|
-
component_mode: manifest.component_mode,
|
|
179
|
-
components: sources(manifest.components),
|
|
180
|
-
directives: sources(manifest.directives),
|
|
181
|
-
highlight: highlight_of(manifest),
|
|
182
|
-
};
|
|
183
|
-
if (Object.keys(templates).length !== 0) compile.templates = templates;
|
|
184
|
-
if (manifest.select_template) {
|
|
185
|
-
// what the selector picked when the plugin last compiled the file
|
|
186
|
-
const picked = manifest.documents[posix(doc)];
|
|
187
|
-
compile.select_template = () => (picked === null ? false : picked);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return {
|
|
191
|
-
compile,
|
|
192
|
-
resolve(id, name) {
|
|
193
|
-
const file = files.get(id)?.(name);
|
|
194
|
-
return file === undefined ? undefined : specifier_from(doc, file);
|
|
195
|
-
},
|
|
196
|
-
// a parse plugin may handle a directive no component renders
|
|
197
|
-
report_directives: !manifest.directive_plugins,
|
|
198
|
-
lenient_frontmatter: manifest.frontmatter_parse,
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
let twinkleplop: HighlightConfig | null = null;
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* the highlighter documents compile with, twinkleplop as the plugin has it by
|
|
206
|
-
* default so live expressions in code are references, compile is synchronous
|
|
207
|
-
* so every language is loaded up front
|
|
208
|
-
*/
|
|
209
|
-
function highlight_of(manifest: MdsvexManifest): CompileOptions['highlight'] {
|
|
210
|
-
// a custom highlighter has no live expressions
|
|
211
|
-
if ((manifest.highlight ?? 'twinkleplop') !== 'twinkleplop') return undefined;
|
|
212
|
-
return (twinkleplop ??= create_highlight({ languages: default_languages }));
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function union(a: readonly string[], b: readonly string[]): string[] {
|
|
216
|
-
const out = a.slice();
|
|
217
|
-
for (const n of b) if (!out.includes(n)) out.push(n);
|
|
218
|
-
return out;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/** a manifest from mdsvex.config.json in dir, entries that do not resolve are left out */
|
|
222
|
-
export function from_json(
|
|
223
|
-
config: JsonConfig,
|
|
224
|
-
dir: string,
|
|
225
|
-
ts: typeof TS | undefined
|
|
226
|
-
): MdsvexManifest {
|
|
227
|
-
const extensions = (config.extensions ?? ['.svx']).map((e) =>
|
|
228
|
-
e.startsWith('.') ? e : '.' + e
|
|
229
|
-
);
|
|
230
|
-
const manifest: MdsvexManifest = {
|
|
231
|
-
version: MANIFEST_VERSION,
|
|
232
|
-
root: posix(dir),
|
|
233
|
-
extensions,
|
|
234
|
-
component_mode: config.component_mode ?? 'markdown',
|
|
235
|
-
frontmatter_parse: false,
|
|
236
|
-
directive_plugins: false,
|
|
237
|
-
select_template: false,
|
|
238
|
-
templates: {},
|
|
239
|
-
components: [],
|
|
240
|
-
directives: [],
|
|
241
|
-
documents: {},
|
|
242
|
-
highlight: config.highlight === false ? false : 'twinkleplop',
|
|
243
|
-
};
|
|
244
|
-
const from = path.join(dir, 'vite.config');
|
|
245
|
-
const scan = (file: string) => scan_exports(file, ts);
|
|
246
|
-
|
|
247
|
-
const written =
|
|
248
|
-
config.components === undefined
|
|
249
|
-
? []
|
|
250
|
-
: Array.isArray(config.components)
|
|
251
|
-
? config.components
|
|
252
|
-
: [config.components];
|
|
253
|
-
const many = written.length !== 1;
|
|
254
|
-
written.forEach((spec, i) => {
|
|
255
|
-
const file = resolve_specifier(spec, from);
|
|
256
|
-
if (file === null) return;
|
|
257
|
-
const own = scan(file);
|
|
258
|
-
const suffix = many ? '/' + i : '';
|
|
259
|
-
manifest.components.push({
|
|
260
|
-
id: COMPONENTS_ID + suffix,
|
|
261
|
-
file,
|
|
262
|
-
names: own.names,
|
|
263
|
-
});
|
|
264
|
-
const d = directives_of(own, file, DIRECTIVES_ID + suffix, scan);
|
|
265
|
-
if (d !== null) manifest.directives.push(d);
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
for (const [name, entry] of Object.entries(config.templates ?? {})) {
|
|
269
|
-
const pair = typeof entry === 'object';
|
|
270
|
-
const file = resolve_specifier(pair ? entry.component : entry, from);
|
|
271
|
-
if (file === null) continue;
|
|
272
|
-
const own = scan(file);
|
|
273
|
-
const template: ManifestTemplate = {
|
|
274
|
-
id: TEMPLATE_ID + name,
|
|
275
|
-
file,
|
|
276
|
-
components: own.names.filter((n) => n !== 'default'),
|
|
277
|
-
extra: null,
|
|
278
|
-
directives: [],
|
|
279
|
-
};
|
|
280
|
-
const base = TEMPLATE_DIRECTIVES_ID + name;
|
|
281
|
-
const mine = directives_of(own, file, base, scan);
|
|
282
|
-
if (mine !== null) template.directives.push(mine);
|
|
283
|
-
const extra_spec = pair ? entry.components : undefined;
|
|
284
|
-
const extra_file =
|
|
285
|
-
extra_spec === undefined ? null : resolve_specifier(extra_spec, from);
|
|
286
|
-
if (extra_file !== null) {
|
|
287
|
-
const more = scan(extra_file);
|
|
288
|
-
template.extra = {
|
|
289
|
-
id: TEMPLATE_ID + name,
|
|
290
|
-
file: extra_file,
|
|
291
|
-
names: more.names.filter((n) => n !== 'default'),
|
|
292
|
-
};
|
|
293
|
-
const theirs = directives_of(
|
|
294
|
-
more,
|
|
295
|
-
extra_file,
|
|
296
|
-
base + '/components',
|
|
297
|
-
scan
|
|
298
|
-
);
|
|
299
|
-
if (theirs !== null) template.directives.push(theirs);
|
|
300
|
-
}
|
|
301
|
-
manifest.templates[name] = template;
|
|
302
|
-
}
|
|
303
|
-
return manifest;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
interface Scanned {
|
|
307
|
-
names: string[];
|
|
308
|
-
/** the specifier of export * as directives from, null for none */
|
|
309
|
-
directives: string | null;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
function directives_of(
|
|
313
|
-
own: Scanned,
|
|
314
|
-
owner: string,
|
|
315
|
-
id: string,
|
|
316
|
-
scan: (file: string) => Scanned
|
|
317
|
-
): ManifestModule | null {
|
|
318
|
-
if (own.directives === null) return null;
|
|
319
|
-
const file = resolve_specifier(own.directives, owner);
|
|
320
|
-
if (file === null) return null;
|
|
321
|
-
return { id, file, names: scan(file).names };
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* the names a module exports without running it, of a svelte file its module
|
|
326
|
-
* script, read from the typescript syntax tree so it needs no vite
|
|
327
|
-
*/
|
|
328
|
-
export function scan_exports(file: string, ts: typeof TS | undefined): Scanned {
|
|
329
|
-
const none: Scanned = { names: [], directives: null };
|
|
330
|
-
if (ts === undefined) return none;
|
|
331
|
-
let code: string | null;
|
|
332
|
-
try {
|
|
333
|
-
code = fs.readFileSync(file, 'utf8');
|
|
334
|
-
} catch {
|
|
335
|
-
return none;
|
|
336
|
-
}
|
|
337
|
-
if (file.endsWith('.svelte')) code = module_script(code);
|
|
338
|
-
if (code === null) return none;
|
|
339
|
-
const sf = ts.createSourceFile(file + '.ts', code, ts.ScriptTarget.Latest);
|
|
340
|
-
const names: string[] = [];
|
|
341
|
-
let directives: string | null = null;
|
|
342
|
-
const text = (n: TS.ModuleExportName) =>
|
|
343
|
-
ts.isIdentifier(n) ? n.text : n.text;
|
|
344
|
-
const exported = (s: TS.Statement) =>
|
|
345
|
-
ts.canHaveModifiers(s) &&
|
|
346
|
-
(ts.getModifiers(s) ?? []).some(
|
|
347
|
-
(m) => m.kind === ts.SyntaxKind.ExportKeyword
|
|
348
|
-
);
|
|
349
|
-
const is_default = (s: TS.Statement) =>
|
|
350
|
-
ts.canHaveModifiers(s) &&
|
|
351
|
-
(ts.getModifiers(s) ?? []).some(
|
|
352
|
-
(m) => m.kind === ts.SyntaxKind.DefaultKeyword
|
|
353
|
-
);
|
|
354
|
-
for (const s of sf.statements) {
|
|
355
|
-
if (ts.isExportDeclaration(s)) {
|
|
356
|
-
if (s.isTypeOnly) continue;
|
|
357
|
-
const clause = s.exportClause;
|
|
358
|
-
if (clause === undefined) continue;
|
|
359
|
-
if (ts.isNamespaceExport(clause)) {
|
|
360
|
-
const name = text(clause.name);
|
|
361
|
-
names.push(name);
|
|
362
|
-
if (
|
|
363
|
-
name === DIRECTIVES_EXPORT &&
|
|
364
|
-
s.moduleSpecifier !== undefined &&
|
|
365
|
-
ts.isStringLiteral(s.moduleSpecifier)
|
|
366
|
-
)
|
|
367
|
-
directives = s.moduleSpecifier.text;
|
|
368
|
-
} else {
|
|
369
|
-
for (const e of clause.elements)
|
|
370
|
-
if (!e.isTypeOnly) names.push(text(e.name));
|
|
371
|
-
}
|
|
372
|
-
} else if (ts.isExportAssignment(s)) {
|
|
373
|
-
names.push('default');
|
|
374
|
-
} else if (exported(s)) {
|
|
375
|
-
if (is_default(s)) names.push('default');
|
|
376
|
-
else if (ts.isVariableStatement(s)) {
|
|
377
|
-
for (const d of s.declarationList.declarations)
|
|
378
|
-
if (ts.isIdentifier(d.name)) names.push(d.name.text);
|
|
379
|
-
} else if (
|
|
380
|
-
(ts.isFunctionDeclaration(s) || ts.isClassDeclaration(s)) &&
|
|
381
|
-
s.name !== undefined
|
|
382
|
-
)
|
|
383
|
-
names.push(s.name.text);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
// a directives export the plugin rejects is not a namespace, it is no element either
|
|
387
|
-
return {
|
|
388
|
-
names: names.filter((n) => n !== DIRECTIVES_EXPORT),
|
|
389
|
-
directives,
|
|
390
|
-
};
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
const CONDITIONS = ['svelte', 'import', 'module', 'default'];
|
|
394
|
-
|
|
395
|
-
/** a conditional exports or imports target as a path, null when none applies */
|
|
396
|
-
function target_of(target: unknown): string | null {
|
|
397
|
-
if (typeof target === 'string') return target;
|
|
398
|
-
if (Array.isArray(target)) {
|
|
399
|
-
for (const t of target) {
|
|
400
|
-
const found = target_of(t);
|
|
401
|
-
if (found !== null) return found;
|
|
402
|
-
}
|
|
403
|
-
return null;
|
|
404
|
-
}
|
|
405
|
-
if (target !== null && typeof target === 'object') {
|
|
406
|
-
const record = target as Record<string, unknown>;
|
|
407
|
-
for (const c of CONDITIONS)
|
|
408
|
-
if (c in record) {
|
|
409
|
-
const found = target_of(record[c]);
|
|
410
|
-
if (found !== null) return found;
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
return null;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
/** a key of an exports or imports map that matches subpath, with its star */
|
|
417
|
-
function match_map(
|
|
418
|
-
map: Record<string, unknown>,
|
|
419
|
-
subpath: string
|
|
420
|
-
): string | null {
|
|
421
|
-
if (subpath in map) return target_of(map[subpath]);
|
|
422
|
-
for (const key of Object.keys(map)) {
|
|
423
|
-
const star = key.indexOf('*');
|
|
424
|
-
if (star < 0) continue;
|
|
425
|
-
const head = key.slice(0, star);
|
|
426
|
-
const tail = key.slice(star + 1);
|
|
427
|
-
if (
|
|
428
|
-
subpath.startsWith(head) &&
|
|
429
|
-
subpath.endsWith(tail) &&
|
|
430
|
-
subpath.length >= key.length - 1
|
|
431
|
-
) {
|
|
432
|
-
const target = target_of(map[key]);
|
|
433
|
-
if (target === null) return null;
|
|
434
|
-
return target.replace(
|
|
435
|
-
'*',
|
|
436
|
-
subpath.slice(head.length, subpath.length - tail.length)
|
|
437
|
-
);
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
return null;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
function read_json(file: string): Record<string, unknown> | null {
|
|
444
|
-
try {
|
|
445
|
-
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
446
|
-
} catch {
|
|
447
|
-
return null;
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
function nearest_package(
|
|
452
|
-
dir: string
|
|
453
|
-
): { dir: string; json: Record<string, unknown> } | null {
|
|
454
|
-
for (;;) {
|
|
455
|
-
const json = read_json(path.join(dir, 'package.json'));
|
|
456
|
-
if (json !== null) return { dir, json };
|
|
457
|
-
const up = path.dirname(dir);
|
|
458
|
-
if (up === dir) return null;
|
|
459
|
-
dir = up;
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
function existing(file: string): string | null {
|
|
464
|
-
try {
|
|
465
|
-
return fs.statSync(file).isFile() ? posix(file) : null;
|
|
466
|
-
} catch {
|
|
467
|
-
return null;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
/**
|
|
472
|
-
* resolves spec as an import from importer would, for relative paths, file
|
|
473
|
-
* urls, package.json subpath imports and package exports, never vite aliases
|
|
474
|
-
*/
|
|
475
|
-
export function resolve_specifier(
|
|
476
|
-
spec: string,
|
|
477
|
-
importer: string
|
|
478
|
-
): string | null {
|
|
479
|
-
const dir = path.dirname(importer);
|
|
480
|
-
if (spec.startsWith('file:')) return existing(fileURLToPath(spec));
|
|
481
|
-
if (spec.startsWith('.') || path.isAbsolute(spec))
|
|
482
|
-
return existing(path.resolve(dir, spec));
|
|
483
|
-
if (spec.startsWith('#')) {
|
|
484
|
-
const pkg = nearest_package(dir);
|
|
485
|
-
const imports = pkg?.json.imports;
|
|
486
|
-
if (pkg === null || typeof imports !== 'object' || imports === null)
|
|
487
|
-
return null;
|
|
488
|
-
const target = match_map(imports as Record<string, unknown>, spec);
|
|
489
|
-
if (target === null) return null;
|
|
490
|
-
return target.startsWith('.')
|
|
491
|
-
? existing(path.resolve(pkg.dir, target))
|
|
492
|
-
: resolve_specifier(target, path.join(pkg.dir, 'package.json'));
|
|
493
|
-
}
|
|
494
|
-
const parts = spec.split('/');
|
|
495
|
-
const name = spec.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
|
|
496
|
-
const subpath = '.' + spec.slice(name.length);
|
|
497
|
-
for (let at = dir; ; ) {
|
|
498
|
-
const root = path.join(at, 'node_modules', name);
|
|
499
|
-
const json = read_json(path.join(root, 'package.json'));
|
|
500
|
-
if (json !== null) {
|
|
501
|
-
const exports = json.exports;
|
|
502
|
-
if (exports !== undefined && exports !== null) {
|
|
503
|
-
const target =
|
|
504
|
-
typeof exports === 'object' &&
|
|
505
|
-
!Array.isArray(exports) &&
|
|
506
|
-
Object.keys(exports).some((k) => k.startsWith('.'))
|
|
507
|
-
? match_map(exports as Record<string, unknown>, subpath)
|
|
508
|
-
: subpath === '.'
|
|
509
|
-
? target_of(exports)
|
|
510
|
-
: null;
|
|
511
|
-
return target === null ? null : existing(path.resolve(root, target));
|
|
512
|
-
}
|
|
513
|
-
return existing(
|
|
514
|
-
path.resolve(root, subpath === '.' ? 'index.js' : subpath)
|
|
515
|
-
);
|
|
516
|
-
}
|
|
517
|
-
const up = path.dirname(at);
|
|
518
|
-
if (up === at) return null;
|
|
519
|
-
at = up;
|
|
520
|
-
}
|
|
521
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
export { create_pfm_language_plugin, is_document } from "./language_plugin";
|
|
2
|
-
export { create_svelte_language_plugin } from "./svelte_plugin";
|
|
3
|
-
export {
|
|
4
|
-
CONFIG_FILE,
|
|
5
|
-
create_config_loader,
|
|
6
|
-
document_options,
|
|
7
|
-
from_json,
|
|
8
|
-
resolve_specifier,
|
|
9
|
-
scan_exports,
|
|
10
|
-
} from "./config";
|
|
11
|
-
|
|
12
|
-
export type {
|
|
13
|
-
PfmLanguagePluginOptions,
|
|
14
|
-
PfmVirtualCode,
|
|
15
|
-
TemplateChoice,
|
|
16
|
-
} from "./language_plugin";
|
|
17
|
-
export { template_value_at } from "@mdsvex/source-map/pfm-to-svelte";
|
|
18
|
-
export {
|
|
19
|
-
completion_context,
|
|
20
|
-
probe_for,
|
|
21
|
-
props_of,
|
|
22
|
-
template_named,
|
|
23
|
-
} from "./completions";
|
|
24
|
-
export type { CompletionContext, Prop } from "./completions";
|
|
25
|
-
export type {
|
|
26
|
-
ConfigLoader,
|
|
27
|
-
ConfigLoaderOptions,
|
|
28
|
-
DocumentOptions,
|
|
29
|
-
JsonConfig,
|
|
30
|
-
LoadedConfig,
|
|
31
|
-
} from "./config";
|