1
1
import fs from 'node:fs' ;
2
2
import path from 'node:path' ;
3
- import { defineAdder , defineAdderOptions , log , utils } from '@sveltejs/cli-core' ;
3
+ import MagicString from 'magic-string' ;
4
+ import { dedent , defineAdder , defineAdderOptions , log , utils } from '@sveltejs/cli-core' ;
4
5
import {
5
6
array ,
6
7
common ,
@@ -183,8 +184,8 @@ export default defineAdder({
183
184
{
184
185
// add the <ParaglideJS> component to the layout
185
186
name : ( { kit } ) => `${ kit ?. routesDirectory } /+layout.svelte` ,
186
- content : ( { content, dependencyVersion } ) => {
187
- const { script, template, generateCode } = parseSvelte ( content ) ;
187
+ content : ( { content, dependencyVersion, typescript } ) => {
188
+ const { script, template, generateCode } = parseSvelte ( content , { typescript } ) ;
188
189
189
190
const paraglideComponentName = 'ParaglideJS' ;
190
191
imports . addNamed ( script . ast , '@inlang/paraglide-sveltekit' , {
@@ -194,28 +195,21 @@ export default defineAdder({
194
195
i18n : 'i18n'
195
196
} ) ;
196
197
197
- // wrap the HTML in a ParaglideJS instance
198
- const rootChildren = template . ast . children ;
199
- if ( rootChildren . length === 0 ) {
198
+ if ( template . source . length === 0 ) {
200
199
const svelteVersion = dependencyVersion ( 'svelte' ) ;
201
200
if ( ! svelteVersion ) throw new Error ( 'Failed to determine svelte version' ) ;
202
201
203
202
html . addSlot ( script . ast , template . ast , svelteVersion ) ;
204
203
}
205
204
206
- const hasParaglideJsNode = rootChildren . find (
207
- ( x ) => x . type == 'tag' && x . name == paraglideComponentName
208
- ) ;
209
- if ( ! hasParaglideJsNode ) {
210
- const root = html . element ( paraglideComponentName , { } ) ;
211
- root . attribs = {
212
- '{i18n}' : ''
213
- } ;
214
- root . children = rootChildren ;
215
- template . ast . children = [ root ] ;
205
+ const templateCode = new MagicString ( template . generateCode ( ) ) ;
206
+ if ( ! templateCode . original . includes ( '<ParaglideJS' ) ) {
207
+ templateCode . indent ( ) ;
208
+ templateCode . prepend ( '<ParaglideJS {i18n}>\n' ) ;
209
+ templateCode . append ( '\n</ParaglideJS>' ) ;
216
210
}
217
211
218
- return generateCode ( { script : script . generateCode ( ) , template : template . generateCode ( ) } ) ;
212
+ return generateCode ( { script : script . generateCode ( ) , template : templateCode . toString ( ) } ) ;
219
213
}
220
214
} ,
221
215
{
@@ -263,40 +257,41 @@ export default defineAdder({
263
257
) ;
264
258
}
265
259
266
- const { ts } = utils . createPrinter ( { ts : typescript } ) ;
260
+ const [ ts ] = utils . createPrinter ( typescript ) ;
267
261
268
- const methodStatement = common . statementFromString ( `
262
+ const scriptCode = new MagicString ( script . generateCode ( ) ) ;
263
+ if ( ! scriptCode . original . includes ( 'function switchToLanguage' ) ) {
264
+ scriptCode . trim ( ) ;
265
+ scriptCode . append ( '\n\n' ) ;
266
+ scriptCode . append ( dedent `
267
+ ${ ts ( '' , '/**' ) }
268
+ ${ ts ( '' , '* @param import("$lib/paraglide/runtime").AvailableLanguageTag newLanguage' ) }
269
+ ${ ts ( '' , '*/' ) }
269
270
function switchToLanguage(newLanguage${ ts ( ': AvailableLanguageTag' ) } ) {
270
271
const canonicalPath = i18n.route($page.url.pathname);
271
272
const localisedPath = i18n.resolveRoute(canonicalPath, newLanguage);
272
273
goto(localisedPath);
273
274
}
274
275
` ) ;
275
- if ( ! typescript ) {
276
- common . addJsDocComment ( methodStatement , {
277
- 'import("$lib/paraglide/runtime").AvailableLanguageTag' : 'newLanguage'
278
- } ) ;
279
276
}
280
277
281
- script . ast . body . push ( methodStatement ) ;
278
+ const templateCode = new MagicString ( template . source ) ;
282
279
283
280
// add localized message
284
- html . addFromRawHtml (
285
- template . ast . childNodes ,
286
- `\n\n<h1>{m.hello_world({ name: 'SvelteKit User' })}</h1>\n`
287
- ) ;
281
+ templateCode . append ( "\n\n<h1>{m.hello_world({ name: 'SvelteKit User' })}</h1>\n" ) ;
288
282
289
283
// add links to other localized pages, the first one is the default
290
284
// language, thus it does not require any localized route
291
285
const { validLanguageTags } = parseLanguageTagInput ( options . availableLanguageTags ) ;
292
286
const links = validLanguageTags
293
- . map ( ( x ) => `\n\t<button onclick="{() => switchToLanguage('${ x } ')}">${ x } </button>` )
294
- . join ( '' ) ;
295
- const div = html . element ( 'div' ) ;
296
- html . addFromRawHtml ( div . childNodes , `${ links } \n` ) ;
297
- html . appendElement ( template . ast . childNodes , div ) ;
298
-
299
- return generateCode ( { script : script . generateCode ( ) , template : template . generateCode ( ) } ) ;
287
+ . map (
288
+ ( x ) =>
289
+ `${ templateCode . getIndentString ( ) } <button onclick={() => switchToLanguage('${ x } ')}>${ x } </button>`
290
+ )
291
+ . join ( '\n' ) ;
292
+ templateCode . append ( `<div>\n${ links } \n</div>` ) ;
293
+
294
+ return generateCode ( { script : scriptCode . toString ( ) , template : templateCode . toString ( ) } ) ;
300
295
}
301
296
}
302
297
] ,
0 commit comments