Skip to content

Commit 4f84d68

Browse files
committed
make paraglide work
1 parent 3c6c63a commit 4f84d68

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

packages/addons/common.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { imports, exports, common } from '@sveltejs/cli-core/js';
2+
import { toSvelteFragment, type SvelteAst } from '@sveltejs/cli-core/html';
23
import { parseScript, parseSvelte } from '@sveltejs/cli-core/parsers';
34
import process from 'node:process';
45

@@ -64,17 +65,29 @@ export function addEslintConfigPrettier(content: string): string {
6465
}
6566

6667
export function addToDemoPage(content: string, path: string): string {
67-
const { template, generateCode } = parseSvelte(content);
68-
69-
for (const node of template.ast.childNodes) {
70-
if (node.type === 'tag' && node.attribs['href'] === `/demo/${path}`) {
71-
return content;
68+
const { ast, generateCode } = parseSvelte(content);
69+
70+
for (const node of ast.fragment.nodes) {
71+
if (node.type === 'RegularElement') {
72+
const hrefAttribute = node.attributes.find(
73+
(x) => x.type === 'Attribute' && x.name === 'href'
74+
) as SvelteAst.Attribute;
75+
if (!hrefAttribute || !hrefAttribute.value) continue;
76+
77+
if (!Array.isArray(hrefAttribute.value)) continue;
78+
79+
const hasDemo = hrefAttribute.value.find(
80+
(x) => x.type === 'Text' && x.data === `/demo/${path}`
81+
);
82+
if (hasDemo) {
83+
return content;
84+
}
7285
}
7386
}
7487

75-
const newLine = template.source ? '\n' : '';
76-
const src = template.source + `${newLine}<a href="/demo/${path}">${path}</a>`;
77-
return generateCode({ template: src });
88+
ast.fragment.nodes.push(...toSvelteFragment(`<a href="/demo/${path}">${path}</a>`));
89+
90+
return generateCode();
7891
}
7992

8093
/**

packages/addons/paraglide/index.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import MagicString from 'magic-string';
21
import { colors, defineAddon, defineAddonOptions, log } from '@sveltejs/cli-core';
32
import {
43
array,
@@ -187,38 +186,44 @@ export default defineAddon({
187186

188187
// add usage example
189188
sv.file(`${kit.routesDirectory}/demo/paraglide/+page.svelte`, (content) => {
190-
const { script, template, generateCode } = parseSvelte(content, { typescript });
189+
console.log(content);
190+
const { ast, generateCode } = parseSvelte(content);
191+
192+
let scriptAst = ast.instance?.content;
193+
if (!scriptAst) {
194+
scriptAst = parseScript('').ast;
195+
ast.instance = {
196+
type: 'Script',
197+
start: 0,
198+
end: 0,
199+
context: 'default',
200+
attributes: [],
201+
content: scriptAst
202+
};
203+
}
191204

192-
imports.addNamed(script.ast, '$lib/paraglide/messages.js', { m: 'm' });
193-
imports.addNamed(script.ast, '$app/navigation', { goto: 'goto' });
194-
imports.addNamed(script.ast, '$app/state', { page: 'page' });
195-
imports.addNamed(script.ast, '$lib/paraglide/runtime', {
205+
imports.addNamed(scriptAst, '$lib/paraglide/messages.js', { m: 'm' });
206+
imports.addNamed(scriptAst, '$lib/paraglide/runtime', {
196207
setLocale: 'setLocale'
197208
});
198209

199-
const scriptCode = new MagicString(script.generateCode());
200-
201-
const templateCode = new MagicString(template.source);
202-
203210
// add localized message
204-
templateCode.append("\n\n<h1>{m.hello_world({ name: 'SvelteKit User' })}</h1>\n");
211+
let templateCode = "<h1>{m.hello_world({ name: 'SvelteKit User' })}</h1>";
205212

206213
// add links to other localized pages, the first one is the default
207214
// language, thus it does not require any localized route
208215
const { validLanguageTags } = parseLanguageTagInput(options.languageTags);
209216
const links = validLanguageTags
210-
.map(
211-
(x) =>
212-
`${templateCode.getIndentString()}<button onclick={() => setLocale('${x}')}>${x}</button>`
213-
)
214-
.join('\n');
215-
templateCode.append(`<div>\n${links}\n</div>`);
216-
217-
templateCode.append(
218-
'<p>\nIf you use VSCode, install the <a href="https://marketplace.visualstudio.com/items?itemName=inlang.vs-code-extension" target="_blank">Sherlock i18n extension</a> for a better i18n experience.\n</p>'
219-
);
217+
.map((x) => `<button onclick={() => setLocale('${x}')}>${x}</button>`)
218+
.join('');
219+
templateCode += `<div>${links}</div>`;
220220

221-
return generateCode({ script: scriptCode.toString(), template: templateCode.toString() });
221+
templateCode +=
222+
'<p>If you use VSCode, install the <a href="https://marketplace.visualstudio.com/items?itemName=inlang.vs-code-extension" target="_blank">Sherlock i18n extension</a> for a better i18n experience.</p>';
223+
224+
ast.fragment.nodes.push(...html.toSvelteFragment(templateCode));
225+
226+
return generateCode();
222227
});
223228
}
224229

0 commit comments

Comments
 (0)