Skip to content

Commit d359f27

Browse files
authored
--from-playground will create projects with experimental.async (#729)
1 parent b48c291 commit d359f27

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

.changeset/solid-singers-help.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv': patch
3+
---
4+
5+
fix(cli): `--from-playground` will create projects with `experimental.async` enabled _(if svelte version allows it)_

packages/create/playground.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import * as js from '@sveltejs/cli-core/js';
44
import { parseJson, parseScript, parseSvelte } from '@sveltejs/cli-core/parsers';
5+
import { isVersionUnsupportedBelow } from '@sveltejs/cli-core';
56

67
export function validatePlaygroundUrl(link: string): boolean {
78
try {
@@ -196,6 +197,16 @@ export function setupPlaygroundProject(
196197
}
197198
}
198199

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+
199210
// we want to change the svelte version, even if the user decieded
200211
// to not install external dependencies
201212
if (playground.svelteVersion) {
@@ -204,11 +215,18 @@ export function setupPlaygroundProject(
204215
// from https://github.com/sveltejs/svelte.dev/blob/ba7ad256f786aa5bc67eac3a58608f3f50b59e91/packages/repl/src/lib/workers/npm.ts#L14
205216
const pkgPrNewRegex = /^(pr|commit|branch)-(.+)/;
206217
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+
}
210226
}
211227

228+
if (experimentalAsyncNeeded) addExperimentalAsync();
229+
212230
// only update the package.json if we made any changes
213231
if (updatePackageJson) fs.writeFileSync(pkgPath, pkgJson.generateCode(), 'utf-8');
214232
}

packages/create/test/playground.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ test('detect dependencies from playground files', () => {
147147
expect(Array.from(dependencies.keys()).length).toBe(3);
148148
});
149149

150-
test('real world download and convert playground', async () => {
150+
test('real world download and convert playground async', async () => {
151151
const directory = path.join(testWorkspaceDir, 'real-world-playground');
152152
if (fs.existsSync(directory)) {
153153
fs.rmdirSync(directory, { recursive: true });
@@ -174,5 +174,38 @@ test('real world download and convert playground', async () => {
174174
const packageJsonPath = path.join(directory, 'package.json');
175175
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf-8');
176176
expect(packageJsonContent).toContain('"change-case": "latest"');
177-
expect(packageJsonContent).toContain('"svelte": "^5.38.7"');
177+
expect(packageJsonContent).toContain('"svelte": "5.38.7"');
178+
179+
const svelteConfigPath = path.join(directory, 'svelte.config.js');
180+
const svelteConfigContent = fs.readFileSync(svelteConfigPath, 'utf-8');
181+
expect(svelteConfigContent).toContain('experimental: { async: true }');
182+
});
183+
184+
test('real world download and convert playground without async', async () => {
185+
const directory = path.join(testWorkspaceDir, 'real-world-playground-old');
186+
if (fs.existsSync(directory)) {
187+
fs.rmdirSync(directory, { recursive: true });
188+
}
189+
190+
create(directory, {
191+
name: 'real-world-playground-old',
192+
template: 'minimal',
193+
types: 'typescript'
194+
});
195+
196+
const playground = await downloadPlaygroundData({
197+
playgroundId: '770bbef086034b9f8e337bab57efe8d8',
198+
hash: undefined,
199+
svelteVersion: '5.0.5'
200+
});
201+
202+
setupPlaygroundProject(playground, directory, true);
203+
204+
const packageJsonPath = path.join(directory, 'package.json');
205+
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf-8');
206+
expect(packageJsonContent).toContain('"svelte": "5.0.5"');
207+
208+
const svelteConfigPath = path.join(directory, 'svelte.config.js');
209+
const svelteConfigContent = fs.readFileSync(svelteConfigPath, 'utf-8');
210+
expect(svelteConfigContent).not.toContain('experimental: { async: true }');
178211
});

0 commit comments

Comments
 (0)