@mdsvex/language-core 0.1.0-next.1 → 0.1.0-next.2
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 +1253 -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/svelte_plugin.ts
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Minimal Svelte LanguagePlugin.
|
|
3
|
-
*
|
|
4
|
-
* Runs svelte2tsx on .svelte files to produce TypeScript VirtualCode,
|
|
5
|
-
* enabling component prop types to resolve when imported from .pfm files.
|
|
6
|
-
*
|
|
7
|
-
* This is NOT a full Svelte language server, it only provides enough
|
|
8
|
-
* for cross-file type resolution.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type {
|
|
12
|
-
LanguagePlugin,
|
|
13
|
-
VirtualCode,
|
|
14
|
-
IScriptSnapshot,
|
|
15
|
-
CodeMapping,
|
|
16
|
-
CodegenContext,
|
|
17
|
-
} from "@volar/language-core";
|
|
18
|
-
import { forEachEmbeddedCode } from "@volar/language-core";
|
|
19
|
-
import type { TypeScriptExtraServiceScript } from "@volar/typescript";
|
|
20
|
-
|
|
21
|
-
import { svelte2tsx } from "svelte2tsx";
|
|
22
|
-
import { v3ToVolarMappings } from "@mdsvex/source-map/v3-to-volar";
|
|
23
|
-
|
|
24
|
-
const SVELTE_EXTENSION = ".svelte";
|
|
25
|
-
|
|
26
|
-
import { SVELTE_SHIMS } from "./_svelte_shims";
|
|
27
|
-
|
|
28
|
-
/** Create a simple IScriptSnapshot from a string. */
|
|
29
|
-
function createSnapshot(text: string): IScriptSnapshot {
|
|
30
|
-
return {
|
|
31
|
-
getText: (start, end) => text.slice(start, end),
|
|
32
|
-
getLength: () => text.length,
|
|
33
|
-
getChangeRange: () => undefined,
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function createSvelteVirtualCode(
|
|
38
|
-
source: string,
|
|
39
|
-
scriptId: string,
|
|
40
|
-
): VirtualCode | undefined {
|
|
41
|
-
let tsx;
|
|
42
|
-
try {
|
|
43
|
-
tsx = svelte2tsx(source, {
|
|
44
|
-
filename: scriptId,
|
|
45
|
-
isTsFile: false,
|
|
46
|
-
mode: "ts",
|
|
47
|
-
});
|
|
48
|
-
} catch {
|
|
49
|
-
return undefined;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const v3Map = tsx.map.toJSON ? tsx.map.toJSON() : tsx.map;
|
|
53
|
-
|
|
54
|
-
// Prepend svelte2tsx shims so TypeScript can resolve helper types
|
|
55
|
-
const prefix = SVELTE_SHIMS ? SVELTE_SHIMS + "\n" : "";
|
|
56
|
-
const tsCode = prefix + tsx.code;
|
|
57
|
-
const prefixLen = prefix.length;
|
|
58
|
-
|
|
59
|
-
// Build mappings from original tsx output, then shift generated offsets
|
|
60
|
-
const rawMappings = v3ToVolarMappings(v3Map as any, tsx.code, source);
|
|
61
|
-
const mappings = rawMappings.map((m) => ({
|
|
62
|
-
...m,
|
|
63
|
-
generatedOffsets: m.generatedOffsets.map((o) => o + prefixLen),
|
|
64
|
-
}));
|
|
65
|
-
|
|
66
|
-
const tsVirtualCode: VirtualCode = {
|
|
67
|
-
id: "ts",
|
|
68
|
-
languageId: "typescript",
|
|
69
|
-
snapshot: createSnapshot(tsCode),
|
|
70
|
-
mappings: mappings as CodeMapping[],
|
|
71
|
-
embeddedCodes: [],
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
return {
|
|
75
|
-
id: "root",
|
|
76
|
-
languageId: "svelte",
|
|
77
|
-
snapshot: createSnapshot(source),
|
|
78
|
-
mappings: [{
|
|
79
|
-
sourceOffsets: [0],
|
|
80
|
-
generatedOffsets: [0],
|
|
81
|
-
lengths: [source.length],
|
|
82
|
-
data: { structure: true },
|
|
83
|
-
}],
|
|
84
|
-
embeddedCodes: [tsVirtualCode],
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Create a minimal Svelte LanguagePlugin for cross-file type resolution.
|
|
90
|
-
*/
|
|
91
|
-
export function create_svelte_language_plugin(): LanguagePlugin<string> {
|
|
92
|
-
const cache = new Map<string, { hash: number; result: VirtualCode }>();
|
|
93
|
-
|
|
94
|
-
function hashString(s: string): number {
|
|
95
|
-
let h = 0;
|
|
96
|
-
for (let i = 0; i < s.length; i++) {
|
|
97
|
-
h = ((h << 5) - h + s.charCodeAt(i)) | 0;
|
|
98
|
-
}
|
|
99
|
-
return h;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return {
|
|
103
|
-
getLanguageId(scriptId: string): string | undefined {
|
|
104
|
-
if (scriptId.endsWith(SVELTE_EXTENSION)) {
|
|
105
|
-
return "svelte";
|
|
106
|
-
}
|
|
107
|
-
return undefined;
|
|
108
|
-
},
|
|
109
|
-
|
|
110
|
-
createVirtualCode(
|
|
111
|
-
scriptId: string,
|
|
112
|
-
languageId: string,
|
|
113
|
-
snapshot: IScriptSnapshot,
|
|
114
|
-
): VirtualCode | undefined {
|
|
115
|
-
if (languageId !== "svelte") return undefined;
|
|
116
|
-
|
|
117
|
-
const source = snapshot.getText(0, snapshot.getLength());
|
|
118
|
-
const hash = hashString(source);
|
|
119
|
-
const cached = cache.get(scriptId);
|
|
120
|
-
if (cached && cached.hash === hash) return cached.result;
|
|
121
|
-
|
|
122
|
-
const result = createSvelteVirtualCode(source, scriptId);
|
|
123
|
-
if (result) cache.set(scriptId, { hash, result });
|
|
124
|
-
return result;
|
|
125
|
-
},
|
|
126
|
-
|
|
127
|
-
updateVirtualCode(
|
|
128
|
-
scriptId: string,
|
|
129
|
-
_virtualCode: VirtualCode,
|
|
130
|
-
newSnapshot: IScriptSnapshot,
|
|
131
|
-
): VirtualCode | undefined {
|
|
132
|
-
const source = newSnapshot.getText(0, newSnapshot.getLength());
|
|
133
|
-
const hash = hashString(source);
|
|
134
|
-
const cached = cache.get(scriptId);
|
|
135
|
-
if (cached && cached.hash === hash) return cached.result;
|
|
136
|
-
|
|
137
|
-
const result = createSvelteVirtualCode(source, scriptId);
|
|
138
|
-
if (result) cache.set(scriptId, { hash, result });
|
|
139
|
-
return result;
|
|
140
|
-
},
|
|
141
|
-
|
|
142
|
-
disposeVirtualCode(scriptId: string) {
|
|
143
|
-
cache.delete(scriptId);
|
|
144
|
-
},
|
|
145
|
-
|
|
146
|
-
typescript: {
|
|
147
|
-
extraFileExtensions: [
|
|
148
|
-
{
|
|
149
|
-
extension: "svelte",
|
|
150
|
-
isMixedContent: true,
|
|
151
|
-
scriptKind: 7 as any, // ts.ScriptKind.Deferred
|
|
152
|
-
},
|
|
153
|
-
],
|
|
154
|
-
getServiceScript(root: VirtualCode) {
|
|
155
|
-
const tsCode = root.embeddedCodes?.find(
|
|
156
|
-
(c) => c.languageId === "typescript",
|
|
157
|
-
);
|
|
158
|
-
if (tsCode) {
|
|
159
|
-
return {
|
|
160
|
-
code: tsCode,
|
|
161
|
-
extension: ".ts" as any,
|
|
162
|
-
scriptKind: 3 as any, // ts.ScriptKind.TS
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
return undefined;
|
|
166
|
-
},
|
|
167
|
-
getExtraServiceScripts(fileName: string, root: VirtualCode) {
|
|
168
|
-
const scripts: TypeScriptExtraServiceScript[] = [];
|
|
169
|
-
for (const code of forEachEmbeddedCode(root)) {
|
|
170
|
-
if (code.languageId === "typescript") {
|
|
171
|
-
scripts.push({
|
|
172
|
-
fileName: fileName + "." + code.id + ".ts",
|
|
173
|
-
code,
|
|
174
|
-
extension: ".ts",
|
|
175
|
-
scriptKind: 3 as any,
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
return scripts;
|
|
180
|
-
},
|
|
181
|
-
},
|
|
182
|
-
};
|
|
183
|
-
}
|
package/test/completions.spec.ts
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import ts from 'typescript';
|
|
2
|
-
import { describe, expect, it } from 'vitest';
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
completion_context,
|
|
6
|
-
probe_for,
|
|
7
|
-
props_of,
|
|
8
|
-
template_named,
|
|
9
|
-
} from '../src/completions';
|
|
10
|
-
import { project } from './ts_harness';
|
|
11
|
-
|
|
12
|
-
/** the context at | in marked, with its range as text */
|
|
13
|
-
function at(marked: string) {
|
|
14
|
-
const offset = marked.indexOf('|');
|
|
15
|
-
const source = marked.replace('|', '');
|
|
16
|
-
const fm = source.startsWith('---\n')
|
|
17
|
-
? { start: 4, end: source.indexOf('\n---', 4) + 1 }
|
|
18
|
-
: null;
|
|
19
|
-
const ctx = completion_context(source, offset, fm);
|
|
20
|
-
if (ctx === null) return null;
|
|
21
|
-
const { range, ...rest } = ctx;
|
|
22
|
-
return { ...rest, text: source.slice(range.start, range.end) };
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
describe('completion_context', () => {
|
|
26
|
-
it('finds the template value', () => {
|
|
27
|
-
expect(at('---\ntemplate: do|cs\n---\n')).toEqual({
|
|
28
|
-
kind: 'template',
|
|
29
|
-
text: 'docs',
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('finds a frontmatter key being typed, leaving out keys already written', () => {
|
|
34
|
-
expect(at('---\ntitle: x\ndr|\nlevel: 1\n---\n')).toEqual({
|
|
35
|
-
kind: 'frontmatter_key',
|
|
36
|
-
text: 'dr',
|
|
37
|
-
existing: ['title', 'level'],
|
|
38
|
-
});
|
|
39
|
-
expect(at('---\n|\n---\n')).toMatchObject({
|
|
40
|
-
kind: 'frontmatter_key',
|
|
41
|
-
text: '',
|
|
42
|
-
});
|
|
43
|
-
// a key with its colon takes a value, a nested line takes nothing
|
|
44
|
-
expect(at('---\ndr|aft: x\n---\n')).toBeNull();
|
|
45
|
-
expect(at('---\nnested:\n de|\n---\n')).toBeNull();
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('finds a frontmatter value, up to a comment', () => {
|
|
49
|
-
expect(at('---\nlevel: beg|inner # x\n---\n')).toEqual({
|
|
50
|
-
kind: 'frontmatter_value',
|
|
51
|
-
key: 'level',
|
|
52
|
-
text: 'beginner',
|
|
53
|
-
});
|
|
54
|
-
expect(at('---\nlevel: |\n---\n')).toEqual({
|
|
55
|
-
kind: 'frontmatter_value',
|
|
56
|
-
key: 'level',
|
|
57
|
-
text: '',
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it('finds a directive arg name or value, leaving out args already given', () => {
|
|
62
|
-
expect(at(':::Note[x](tone=info, si|)')).toEqual({
|
|
63
|
-
kind: 'arg_key',
|
|
64
|
-
directive: 'Note',
|
|
65
|
-
inline: false,
|
|
66
|
-
text: 'si',
|
|
67
|
-
existing: ['tone'],
|
|
68
|
-
});
|
|
69
|
-
expect(at('see :kbd[x](|) here')).toMatchObject({
|
|
70
|
-
kind: 'arg_key',
|
|
71
|
-
directive: 'kbd',
|
|
72
|
-
inline: true,
|
|
73
|
-
});
|
|
74
|
-
expect(at(':::Note[a [b] c](tone="in|fo", x=y)')).toEqual({
|
|
75
|
-
kind: 'arg_value',
|
|
76
|
-
directive: 'Note',
|
|
77
|
-
key: 'tone',
|
|
78
|
-
text: '"info"',
|
|
79
|
-
});
|
|
80
|
-
// before the args or in a link there is nothing to complete
|
|
81
|
-
expect(at(':::No|te[x](tone=info)')).toBeNull();
|
|
82
|
-
expect(at('[link](/ur|l)')).toBeNull();
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
describe('template_named', () => {
|
|
87
|
-
const named = (fm: string) =>
|
|
88
|
-
template_named(fm, { start: 0, end: fm.length });
|
|
89
|
-
it('reads the template the frontmatter names as text', () => {
|
|
90
|
-
expect(named('title: x\ntemplate: docs # main\n')).toBe('docs');
|
|
91
|
-
expect(named("template: 'docs'\n")).toBe('docs');
|
|
92
|
-
expect(named('template: false\n')).toBe(false);
|
|
93
|
-
expect(named('template:\n')).toBeUndefined();
|
|
94
|
-
expect(named('title: x\n')).toBeUndefined();
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
describe('props_of', () => {
|
|
99
|
-
it('reads the props of each probe, with their literal values and docs', () => {
|
|
100
|
-
const p = project({
|
|
101
|
-
'src/T.svelte': [
|
|
102
|
-
'<script lang="ts">',
|
|
103
|
-
" import type { Snippet } from 'svelte';",
|
|
104
|
-
' interface Props {',
|
|
105
|
-
' /** shown as the page heading */',
|
|
106
|
-
' title: string;',
|
|
107
|
-
" level?: 'beginner' | 'advanced';",
|
|
108
|
-
' draft?: boolean;',
|
|
109
|
-
' children: Snippet;',
|
|
110
|
-
' }',
|
|
111
|
-
' let p: Props = $props();',
|
|
112
|
-
'</script>',
|
|
113
|
-
'',
|
|
114
|
-
].join('\n'),
|
|
115
|
-
'src/doc.svx': '# x\n',
|
|
116
|
-
});
|
|
117
|
-
try {
|
|
118
|
-
p.write(
|
|
119
|
-
'node_modules/.mdsvex/manifest.json',
|
|
120
|
-
JSON.stringify({
|
|
121
|
-
version: 1,
|
|
122
|
-
root: p.root,
|
|
123
|
-
extensions: ['.svx'],
|
|
124
|
-
component_mode: 'markdown',
|
|
125
|
-
frontmatter_parse: false,
|
|
126
|
-
directive_plugins: false,
|
|
127
|
-
select_template: false,
|
|
128
|
-
templates: {
|
|
129
|
-
t: {
|
|
130
|
-
id: 'mdsvex:template/t',
|
|
131
|
-
file: p.path('src/T.svelte'),
|
|
132
|
-
components: [],
|
|
133
|
-
extra: null,
|
|
134
|
-
directives: [],
|
|
135
|
-
},
|
|
136
|
-
},
|
|
137
|
-
components: [],
|
|
138
|
-
directives: [],
|
|
139
|
-
documents: {},
|
|
140
|
-
})
|
|
141
|
-
);
|
|
142
|
-
const doc = p.path('src/doc.svx');
|
|
143
|
-
// the document has no template, its probe still holds the props
|
|
144
|
-
expect(p.diagnostics('src/doc.svx')).toEqual([]);
|
|
145
|
-
const probe = probe_for(
|
|
146
|
-
[{ kind: 'template', name: 't', alias: '__mdsvex_props_0' }],
|
|
147
|
-
'template',
|
|
148
|
-
't',
|
|
149
|
-
undefined
|
|
150
|
-
)!;
|
|
151
|
-
const props = props_of(ts, p.program(), doc, probe.alias)!;
|
|
152
|
-
expect(props.map((x) => [x.name, x.optional, x.values])).toEqual([
|
|
153
|
-
['title', false, []],
|
|
154
|
-
['level', true, ['beginner', 'advanced']],
|
|
155
|
-
['draft', true, ['false', 'true']],
|
|
156
|
-
['children', false, []],
|
|
157
|
-
]);
|
|
158
|
-
expect(props[0]).toMatchObject({
|
|
159
|
-
type: 'string',
|
|
160
|
-
documentation: 'shown as the page heading',
|
|
161
|
-
});
|
|
162
|
-
expect(props_of(ts, p.program(), doc, '__mdsvex_props_9')).toBeNull();
|
|
163
|
-
} finally {
|
|
164
|
-
p.dispose();
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
describe('probe_for', () => {
|
|
170
|
-
const probes = [
|
|
171
|
-
{ kind: 'directive' as const, name: 'Note', alias: 'a' },
|
|
172
|
-
{ kind: 'directive' as const, name: 'Note', template: 'docs', alias: 'b' },
|
|
173
|
-
];
|
|
174
|
-
it('prefers a directive in the scope of the template to the root', () => {
|
|
175
|
-
expect(probe_for(probes, 'directive', 'Note', 'docs')?.alias).toBe('b');
|
|
176
|
-
expect(probe_for(probes, 'directive', 'Note', 'blog')?.alias).toBe('a');
|
|
177
|
-
expect(probe_for(probes, 'directive', 'Note', undefined)?.alias).toBe('a');
|
|
178
|
-
});
|
|
179
|
-
});
|
package/test/config.spec.ts
DELETED
|
@@ -1,304 +0,0 @@
|
|
|
1
|
-
import { mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { dirname, join } from "node:path";
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
4
|
-
|
|
5
|
-
import ts from "typescript";
|
|
6
|
-
import { afterEach, describe, expect, it } from "vitest";
|
|
7
|
-
import type { MdsvexManifest } from "mdsvex";
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
create_config_loader,
|
|
11
|
-
document_options,
|
|
12
|
-
from_json,
|
|
13
|
-
scan_exports,
|
|
14
|
-
} from "../src/config";
|
|
15
|
-
import { is_document } from "../src/language_plugin";
|
|
16
|
-
|
|
17
|
-
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
18
|
-
const posix = (p: string) => p.replace(/\\/g, "/");
|
|
19
|
-
|
|
20
|
-
let roots: string[] = [];
|
|
21
|
-
afterEach(() => {
|
|
22
|
-
for (const root of roots) rmSync(root, { recursive: true, force: true });
|
|
23
|
-
roots = [];
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
function tree(files: Record<string, string>): string {
|
|
27
|
-
const root = posix(mkdtempSync(join(HERE, ".tmp-config-")));
|
|
28
|
-
roots.push(root);
|
|
29
|
-
for (const [file, content] of Object.entries(files)) {
|
|
30
|
-
mkdirSync(dirname(join(root, file)), { recursive: true });
|
|
31
|
-
writeFileSync(join(root, file), content);
|
|
32
|
-
}
|
|
33
|
-
return root;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function manifest(root: string, extra: Partial<MdsvexManifest> = {}): string {
|
|
37
|
-
return JSON.stringify({
|
|
38
|
-
version: 1,
|
|
39
|
-
root,
|
|
40
|
-
extensions: [".svx"],
|
|
41
|
-
component_mode: "markdown",
|
|
42
|
-
frontmatter_parse: false,
|
|
43
|
-
directive_plugins: false,
|
|
44
|
-
select_template: false,
|
|
45
|
-
templates: {},
|
|
46
|
-
components: [],
|
|
47
|
-
directives: [],
|
|
48
|
-
documents: {},
|
|
49
|
-
...extra,
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const MANIFEST = "node_modules/.mdsvex/manifest.json";
|
|
54
|
-
|
|
55
|
-
describe("finding the config of a document", () => {
|
|
56
|
-
it("walks up to the nearest manifest or json config", () => {
|
|
57
|
-
const root = tree({
|
|
58
|
-
"app/src/routes/deep/doc.svx": "# x",
|
|
59
|
-
"app/other/doc.svx": "# x",
|
|
60
|
-
"doc.svx": "# x",
|
|
61
|
-
});
|
|
62
|
-
mkdirSync(join(root, "app/node_modules/.mdsvex"), { recursive: true });
|
|
63
|
-
writeFileSync(join(root, "app", MANIFEST), manifest(root + "/app"));
|
|
64
|
-
writeFileSync(join(root, "mdsvex.config.json"), "{}");
|
|
65
|
-
const loader = create_config_loader({ typescript: ts });
|
|
66
|
-
expect(loader.load(join(root, "app/src/routes/deep/doc.svx"))?.file).toBe(
|
|
67
|
-
join(root, "app", MANIFEST),
|
|
68
|
-
);
|
|
69
|
-
expect(loader.load(join(root, "doc.svx"))?.file).toBe(
|
|
70
|
-
join(root, "mdsvex.config.json"),
|
|
71
|
-
);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it("prefers a manifest to a json config beside it, and skips an unknown version", () => {
|
|
75
|
-
const root = tree({
|
|
76
|
-
[MANIFEST]: manifest("/r"),
|
|
77
|
-
"mdsvex.config.json": '{ "component_mode": "all" }',
|
|
78
|
-
});
|
|
79
|
-
const loader = create_config_loader({ typescript: ts });
|
|
80
|
-
const doc = join(root, "doc.svx");
|
|
81
|
-
expect(loader.load(doc)?.manifest.root).toBe("/r");
|
|
82
|
-
writeFileSync(join(root, MANIFEST), JSON.stringify({ version: 2 }));
|
|
83
|
-
utimesSync(join(root, MANIFEST), new Date(), new Date(Date.now() + 5000));
|
|
84
|
-
expect(loader.load(doc)?.manifest.component_mode).toBe("all");
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it("reads a changed file again and stamps it anew", () => {
|
|
88
|
-
const root = tree({ [MANIFEST]: manifest("/r") });
|
|
89
|
-
const seen: string[] = [];
|
|
90
|
-
const loader = create_config_loader({ on_read: (f) => seen.push(f) });
|
|
91
|
-
const doc = join(root, "doc.svx");
|
|
92
|
-
const first = loader.options_for(doc).stamp;
|
|
93
|
-
expect(loader.options_for(doc).stamp).toBe(first);
|
|
94
|
-
writeFileSync(join(root, MANIFEST), manifest("/r", { component_mode: "all" }));
|
|
95
|
-
utimesSync(join(root, MANIFEST), new Date(), new Date(Date.now() + 5000));
|
|
96
|
-
const second = loader.options_for(doc);
|
|
97
|
-
expect(second.stamp).not.toBe(first);
|
|
98
|
-
expect(second.options.compile?.component_mode).toBe("all");
|
|
99
|
-
expect(seen.filter((f) => f.endsWith("manifest.json"))).toHaveLength(2);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it("gives no options without a config", () => {
|
|
103
|
-
const root = tree({ "doc.svx": "# x" });
|
|
104
|
-
const loader = create_config_loader();
|
|
105
|
-
expect(loader.options_for(join(root, "doc.svx"))).toEqual({
|
|
106
|
-
stamp: "",
|
|
107
|
-
options: {},
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it("takes the extensions of the config as documents", () => {
|
|
112
|
-
const root = tree({
|
|
113
|
-
[MANIFEST]: manifest("/r", { extensions: [".svx", ".md"] }),
|
|
114
|
-
});
|
|
115
|
-
const loader = create_config_loader();
|
|
116
|
-
expect(is_document(join(root, "a.md"), loader)).toBe(true);
|
|
117
|
-
expect(is_document(join(root, "a.svx"), loader)).toBe(true);
|
|
118
|
-
expect(is_document(join(root, "a.pfm"), loader)).toBe(true);
|
|
119
|
-
expect(is_document(join(root, "a.ts"), loader)).toBe(false);
|
|
120
|
-
expect(is_document(join(root, "a.markdown"), loader)).toBe(false);
|
|
121
|
-
// no config, only the extensions every document has
|
|
122
|
-
expect(is_document("/nowhere/a.md", create_config_loader())).toBe(false);
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
describe("document options from a manifest", () => {
|
|
127
|
-
const m: MdsvexManifest = {
|
|
128
|
-
version: 1,
|
|
129
|
-
root: "/app",
|
|
130
|
-
extensions: [".svx"],
|
|
131
|
-
component_mode: "all",
|
|
132
|
-
frontmatter_parse: true,
|
|
133
|
-
directive_plugins: false,
|
|
134
|
-
select_template: true,
|
|
135
|
-
templates: {
|
|
136
|
-
theme: {
|
|
137
|
-
id: "mdsvex:template/theme",
|
|
138
|
-
file: "/app/node_modules/@acme/theme/Layout.svelte",
|
|
139
|
-
components: ["h1", "p"],
|
|
140
|
-
extra: {
|
|
141
|
-
id: "mdsvex:template/theme",
|
|
142
|
-
file: "/app/src/lib/theme.ts",
|
|
143
|
-
names: ["h1", "img"],
|
|
144
|
-
},
|
|
145
|
-
directives: [
|
|
146
|
-
{
|
|
147
|
-
id: "mdsvex:template-directives/theme",
|
|
148
|
-
file: "/app/src/lib/theme-directives.ts",
|
|
149
|
-
names: ["note"],
|
|
150
|
-
},
|
|
151
|
-
],
|
|
152
|
-
},
|
|
153
|
-
},
|
|
154
|
-
components: [
|
|
155
|
-
{ id: "mdsvex:components", file: "/app/src/lib/md.ts", names: ["a"] },
|
|
156
|
-
],
|
|
157
|
-
directives: [
|
|
158
|
-
{ id: "mdsvex:directives", file: "/app/src/lib/d.mts", names: ["box"] },
|
|
159
|
-
],
|
|
160
|
-
documents: {
|
|
161
|
-
"/app/src/blog/a.svx": "theme",
|
|
162
|
-
"/app/src/changelog/b.svx": null,
|
|
163
|
-
},
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
it("gives the compile options the plugin would", () => {
|
|
167
|
-
const { compile } = document_options(m, "/app/src/blog/a.svx");
|
|
168
|
-
expect(compile).toMatchObject({
|
|
169
|
-
component_mode: "all",
|
|
170
|
-
templates: {
|
|
171
|
-
theme: {
|
|
172
|
-
specifier: "mdsvex:template/theme",
|
|
173
|
-
components: ["h1", "p", "img"],
|
|
174
|
-
directives: [
|
|
175
|
-
{ specifier: "mdsvex:template-directives/theme", names: ["note"] },
|
|
176
|
-
],
|
|
177
|
-
},
|
|
178
|
-
},
|
|
179
|
-
components: [{ specifier: "mdsvex:components", names: ["a"] }],
|
|
180
|
-
directives: [{ specifier: "mdsvex:directives", names: ["box"] }],
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
it("resolves each virtual id from the document, the extra module winning", () => {
|
|
185
|
-
const { resolve } = document_options(m, "/app/src/blog/a.svx");
|
|
186
|
-
expect(resolve!("mdsvex:template/theme", "default")).toBe(
|
|
187
|
-
"../../node_modules/@acme/theme/Layout.svelte",
|
|
188
|
-
);
|
|
189
|
-
expect(resolve!("mdsvex:template/theme", "p")).toBe(
|
|
190
|
-
"../../node_modules/@acme/theme/Layout.svelte",
|
|
191
|
-
);
|
|
192
|
-
expect(resolve!("mdsvex:template/theme", "h1")).toBe("../lib/theme.js");
|
|
193
|
-
expect(resolve!("mdsvex:template-directives/theme", "note")).toBe(
|
|
194
|
-
"../lib/theme-directives.js",
|
|
195
|
-
);
|
|
196
|
-
expect(resolve!("mdsvex:components", "a")).toBe("../lib/md.js");
|
|
197
|
-
expect(resolve!("mdsvex:directives", "box")).toBe("../lib/d.mjs");
|
|
198
|
-
expect(resolve!("mdsvex:components/3", "a")).toBeUndefined();
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it("selects what select_template picked when the plugin compiled the file", () => {
|
|
202
|
-
const pick = (doc: string) =>
|
|
203
|
-
document_options(m, doc).compile!.select_template!({});
|
|
204
|
-
expect(pick("/app/src/blog/a.svx")).toBe("theme");
|
|
205
|
-
expect(pick("/app/src/changelog/b.svx")).toBe(false);
|
|
206
|
-
expect(pick("/app/src/new.svx")).toBeUndefined();
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
it("reports directives and frontmatter only when nothing at build could handle them", () => {
|
|
210
|
-
const opts = document_options(m, "/app/a.svx");
|
|
211
|
-
expect(opts.report_directives).toBe(true);
|
|
212
|
-
expect(opts.lenient_frontmatter).toBe(true);
|
|
213
|
-
expect(
|
|
214
|
-
document_options({ ...m, directive_plugins: true }, "/app/a.svx").report_directives,
|
|
215
|
-
).toBe(false);
|
|
216
|
-
});
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
describe("mdsvex.config.json", () => {
|
|
220
|
-
const app = () =>
|
|
221
|
-
tree({
|
|
222
|
-
"package.json": JSON.stringify({ imports: { "#lib/*": "./src/lib/*" } }),
|
|
223
|
-
"node_modules/@acme/theme/package.json": JSON.stringify({
|
|
224
|
-
name: "@acme/theme",
|
|
225
|
-
exports: { "./Layout.svelte": { svelte: "./src/Layout.svelte" } },
|
|
226
|
-
}),
|
|
227
|
-
"node_modules/@acme/theme/src/Layout.svelte": [
|
|
228
|
-
'<script module lang="ts">',
|
|
229
|
-
" export { default as h1 } from './H1.svelte';",
|
|
230
|
-
" export type Props = { a: string };",
|
|
231
|
-
"</script>",
|
|
232
|
-
].join("\n"),
|
|
233
|
-
"src/lib/Docs.svelte": [
|
|
234
|
-
"<!-- <script module>export const nope = 1</script> -->",
|
|
235
|
-
"<script module>",
|
|
236
|
-
" export { default as h2, default as p } from './Heading.svelte';",
|
|
237
|
-
" export * as directives from './directives.ts';",
|
|
238
|
-
" export const helper = 1;",
|
|
239
|
-
"</script>",
|
|
240
|
-
"<script>let { children } = $props();</script>",
|
|
241
|
-
].join("\n"),
|
|
242
|
-
"src/lib/directives.ts": "export { default as Callout } from './Callout.svelte';\nexport function note() {}\n",
|
|
243
|
-
"src/lib/theme.ts": "export { default as img } from './Img.svelte';\n",
|
|
244
|
-
"src/lib/md.ts": "export { default as a } from './A.svelte';\nexport * as directives from './root-directives.ts';\n",
|
|
245
|
-
"src/lib/root-directives.ts": "export { default as box } from './Box.svelte';\n",
|
|
246
|
-
"src/lib/more.ts": "export { default as em } from './Em.svelte';\n",
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
it("resolves and scans what a config names, as the plugin does", () => {
|
|
250
|
-
const root = app();
|
|
251
|
-
const m = from_json(
|
|
252
|
-
{
|
|
253
|
-
extensions: ["md", ".svx"],
|
|
254
|
-
templates: {
|
|
255
|
-
docs: "#lib/Docs.svelte",
|
|
256
|
-
theme: { component: "@acme/theme/Layout.svelte", components: "./src/lib/theme.ts" },
|
|
257
|
-
missing: "#lib/Missing.svelte",
|
|
258
|
-
},
|
|
259
|
-
components: ["#lib/md.ts", "./src/lib/more.ts"],
|
|
260
|
-
},
|
|
261
|
-
root,
|
|
262
|
-
ts,
|
|
263
|
-
);
|
|
264
|
-
expect(m.extensions).toEqual([".md", ".svx"]);
|
|
265
|
-
expect(Object.keys(m.templates)).toEqual(["docs", "theme"]);
|
|
266
|
-
expect(m.templates.docs).toEqual({
|
|
267
|
-
id: "mdsvex:template/docs",
|
|
268
|
-
file: root + "/src/lib/Docs.svelte",
|
|
269
|
-
components: ["h2", "p", "helper"],
|
|
270
|
-
extra: null,
|
|
271
|
-
directives: [
|
|
272
|
-
{
|
|
273
|
-
id: "mdsvex:template-directives/docs",
|
|
274
|
-
file: root + "/src/lib/directives.ts",
|
|
275
|
-
names: ["Callout", "note"],
|
|
276
|
-
},
|
|
277
|
-
],
|
|
278
|
-
});
|
|
279
|
-
expect(m.templates.theme).toMatchObject({
|
|
280
|
-
file: root + "/node_modules/@acme/theme/src/Layout.svelte",
|
|
281
|
-
components: ["h1"],
|
|
282
|
-
extra: { file: root + "/src/lib/theme.ts", names: ["img"] },
|
|
283
|
-
});
|
|
284
|
-
expect(m.components).toEqual([
|
|
285
|
-
{ id: "mdsvex:components/0", file: root + "/src/lib/md.ts", names: ["a"] },
|
|
286
|
-
{ id: "mdsvex:components/1", file: root + "/src/lib/more.ts", names: ["em"] },
|
|
287
|
-
]);
|
|
288
|
-
expect(m.directives).toEqual([
|
|
289
|
-
{
|
|
290
|
-
id: "mdsvex:directives/0",
|
|
291
|
-
file: root + "/src/lib/root-directives.ts",
|
|
292
|
-
names: ["box"],
|
|
293
|
-
},
|
|
294
|
-
]);
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
it("scans nothing without typescript, so nothing is replaced", () => {
|
|
298
|
-
const root = app();
|
|
299
|
-
expect(scan_exports(root + "/src/lib/md.ts", undefined)).toEqual({
|
|
300
|
-
names: [],
|
|
301
|
-
directives: null,
|
|
302
|
-
});
|
|
303
|
-
});
|
|
304
|
-
});
|