|
1 |
| -import MagicString from 'magic-string'; |
2 | 1 | import { colors, defineAddon, defineAddonOptions, log } from '@sveltejs/cli-core';
|
3 | 2 | import {
|
4 | 3 | array,
|
@@ -187,38 +186,44 @@ export default defineAddon({
|
187 | 186 |
|
188 | 187 | // add usage example
|
189 | 188 | 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 | + } |
191 | 204 |
|
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', { |
196 | 207 | setLocale: 'setLocale'
|
197 | 208 | });
|
198 | 209 |
|
199 |
| - const scriptCode = new MagicString(script.generateCode()); |
200 |
| - |
201 |
| - const templateCode = new MagicString(template.source); |
202 |
| - |
203 | 210 | // 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>"; |
205 | 212 |
|
206 | 213 | // add links to other localized pages, the first one is the default
|
207 | 214 | // language, thus it does not require any localized route
|
208 | 215 | const { validLanguageTags } = parseLanguageTagInput(options.languageTags);
|
209 | 216 | 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>`; |
220 | 220 |
|
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(); |
222 | 227 | });
|
223 | 228 | }
|
224 | 229 |
|
|
0 commit comments