@@ -2,6 +2,7 @@ import fs from 'node:fs';
2
2
import path from 'node:path' ;
3
3
import * as js from '@sveltejs/cli-core/js' ;
4
4
import { parseJson , parseScript , parseSvelte } from '@sveltejs/cli-core/parsers' ;
5
+ import { isVersionUnsupportedBelow } from '@sveltejs/cli-core' ;
5
6
6
7
export function validatePlaygroundUrl ( link : string ) : boolean {
7
8
try {
@@ -196,6 +197,16 @@ export function setupPlaygroundProject(
196
197
}
197
198
}
198
199
200
+ let experimentalAsyncNeeded = true ;
201
+ const addExperimentalAsync = ( ) => {
202
+ const svelteConfigPath = path . join ( cwd , 'svelte.config.js' ) ;
203
+ const svelteConfig = fs . readFileSync ( svelteConfigPath , 'utf-8' ) ;
204
+ const { ast, generateCode } = parseScript ( svelteConfig ) ;
205
+ const { value : config } = js . exports . createDefault ( ast , { fallback : js . object . create ( { } ) } ) ;
206
+ js . object . overrideProperties ( config , { compilerOptions : { experimental : { async : true } } } ) ;
207
+ fs . writeFileSync ( svelteConfigPath , generateCode ( ) , 'utf-8' ) ;
208
+ } ;
209
+
199
210
// we want to change the svelte version, even if the user decieded
200
211
// to not install external dependencies
201
212
if ( playground . svelteVersion ) {
@@ -204,11 +215,18 @@ export function setupPlaygroundProject(
204
215
// from https://github.com/sveltejs/svelte.dev/blob/ba7ad256f786aa5bc67eac3a58608f3f50b59e91/packages/repl/src/lib/workers/npm.ts#L14
205
216
const pkgPrNewRegex = / ^ ( p r | c o m m i t | b r a n c h ) - ( .+ ) / ;
206
217
const match = pkgPrNewRegex . exec ( playground . svelteVersion ) ;
207
- pkgJson . data . devDependencies [ 'svelte' ] = match
208
- ? `https://pkg.pr.new/svelte@${ match [ 2 ] } `
209
- : `^${ playground . svelteVersion } ` ;
218
+ const version = match ? `https://pkg.pr.new/svelte@${ match [ 2 ] } ` : `${ playground . svelteVersion } ` ;
219
+ pkgJson . data . devDependencies [ 'svelte' ] = version ;
220
+
221
+ // if the version is a "pkg.pr.new" version, we don't need to check for support, we will use the fallback
222
+ if ( ! version . includes ( 'pkg.pr.new' ) ) {
223
+ const unsupported = isVersionUnsupportedBelow ( version , '5.36' ) ;
224
+ if ( unsupported ) experimentalAsyncNeeded = false ;
225
+ }
210
226
}
211
227
228
+ if ( experimentalAsyncNeeded ) addExperimentalAsync ( ) ;
229
+
212
230
// only update the package.json if we made any changes
213
231
if ( updatePackageJson ) fs . writeFileSync ( pkgPath , pkgJson . generateCode ( ) , 'utf-8' ) ;
214
232
}
0 commit comments