Skip to content

Commit e2d57a6

Browse files
authored
Merge branch 'main' into perf/tests-v7
2 parents e1aa339 + 6a9092b commit e2d57a6

File tree

20 files changed

+239
-49
lines changed

20 files changed

+239
-49
lines changed

.changeset/three-trains-allow.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` option now works correctly from node 20

packages/addons/_tests/vitest/test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { execSync } from 'node:child_process';
22
import { setupTest } from '../_setup/suite.ts';
33
import vitest from '../../vitest-addon/index.ts';
4+
import path from 'node:path';
5+
import fs from 'node:fs';
46

57
const { test, flavors } = setupTest(
68
{ vitest },
@@ -15,4 +17,10 @@ test.concurrent.for(flavors)('vitest $variant', (flavor, { expect, ...ctx }) =>
1517
expect(() => execSync('pnpm exec playwright install chromium', { cwd })).not.toThrow();
1618

1719
expect(() => execSync('pnpm test', { cwd, stdio: 'pipe' })).not.toThrow();
20+
21+
const ext = variant.includes('ts') ? 'ts' : 'js';
22+
const viteFile = path.resolve(cwd, `vite.config.${ext}`);
23+
const viteContent = fs.readFileSync(viteFile, 'utf8');
24+
25+
expect(viteContent).toContain(`vitest/config`);
1826
});

packages/addons/vitest-addon/index.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { dedent, defineAddon, defineAddonOptions, log } from '@sveltejs/cli-core';
2-
import { array, exports, functions, object } from '@sveltejs/cli-core/js';
1+
import { dedent, defineAddon, defineAddonOptions } from '@sveltejs/cli-core';
2+
import { array, imports, object, vite } from '@sveltejs/cli-core/js';
33
import { parseJson, parseScript } from '@sveltejs/cli-core/parsers';
44

55
const options = defineAddonOptions()
@@ -96,6 +96,7 @@ export default defineAddon({
9696
`;
9797
});
9898
}
99+
99100
sv.file(viteConfigFile, (content) => {
100101
const { ast, generateCode } = parseScript(content);
101102

@@ -125,19 +126,9 @@ export default defineAddon({
125126
}
126127
});
127128

128-
const defineConfigFallback = functions.createCall({ name: 'defineConfig', args: [] });
129-
const { value: defineWorkspaceCall } = exports.createDefault(ast, {
130-
fallback: defineConfigFallback
131-
});
132-
if (defineWorkspaceCall.type !== 'CallExpression') {
133-
log.warn('Unexpected vite config. Could not update.');
134-
}
129+
const viteConfig = vite.getConfig(ast);
135130

136-
const vitestConfig = functions.getArgument(defineWorkspaceCall, {
137-
index: 0,
138-
fallback: object.create({})
139-
});
140-
const testObject = object.property(vitestConfig, {
131+
const testObject = object.property(viteConfig, {
141132
name: 'test',
142133
fallback: object.create({
143134
expect: {
@@ -154,6 +145,17 @@ export default defineAddon({
154145
if (componentTesting) array.append(workspaceArray, clientObjectExpression);
155146
if (unitTesting) array.append(workspaceArray, serverObjectExpression);
156147

148+
// Manage imports
149+
const importName = 'defineConfig';
150+
const { statement, alias } = imports.find(ast, { name: importName, from: 'vite' });
151+
if (statement) {
152+
// Switch the import from 'vite' to 'vitest/config' (keeping the alias)
153+
imports.addNamed(ast, { imports: { defineConfig: alias }, from: 'vitest/config' });
154+
155+
// Remove the old import
156+
imports.remove(ast, { name: importName, from: 'vite', statement });
157+
}
158+
157159
return generateCode();
158160
});
159161
}

packages/cli/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# sv
22

3+
## 0.9.6
4+
### Patch Changes
5+
6+
7+
- fix(vitest): now import `defineConfig` from `vitest/config` ([#703](https://github.com/sveltejs/cli/pull/703))
8+
39
## 0.9.5
410
### Patch Changes
511

packages/cli/commands/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ async function createProjectFromPlayground(url: string, cwd: string): Promise<vo
242242

243243
// Detect external dependencies and ask for confirmation
244244
const dependencies = detectPlaygroundDependencies(playground.files);
245-
const installDependencies = await confirmExternalDependencies(dependencies.keys().toArray());
245+
const installDependencies = await confirmExternalDependencies(Array.from(dependencies.keys()));
246246

247247
setupPlaygroundProject(playground, cwd, installDependencies);
248248
}

packages/cli/lib/testing.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import process from 'node:process';
44
import degit from 'degit';
55
import { x, exec } from 'tinyexec';
66
import { create } from '@sveltejs/create';
7+
import pstree, { type PS } from 'ps-tree';
78

89
export { addPnpmBuildDependencies } from '../utils/package-manager.ts';
910
export type ProjectVariant = 'kit-js' | 'kit-ts' | 'vite-js' | 'vite-ts';
@@ -121,18 +122,36 @@ export async function startPreview({
121122
});
122123
}
123124

125+
async function getProcessTree(pid: number) {
126+
return new Promise<readonly PS[]>((res, rej) => {
127+
pstree(pid, (err, children) => {
128+
if (err) rej(err);
129+
res(children);
130+
});
131+
});
132+
}
133+
124134
async function terminate(pid: number) {
135+
if (process.platform === 'win32') {
136+
// on windows, use taskkill to terminate the process tree
137+
await x('taskkill', ['/PID', `${pid}`, '/T', '/F']);
138+
return;
139+
}
140+
const children = await getProcessTree(pid);
141+
// the process tree is ordered from parents -> children,
142+
// so we'll iterate in the reverse order to terminate the children first
143+
for (let i = children.length - 1; i >= 0; i--) {
144+
const child = children[i];
145+
const pid = Number(child.PID);
146+
kill(pid);
147+
}
148+
kill(pid);
149+
}
150+
151+
function kill(pid: number) {
125152
try {
126-
if (process.platform === 'win32') {
127-
await x('taskkill', ['/PID', `${pid}`, '/T', '/F']); // on windows, use taskkill to terminate the process tree
128-
} else {
129-
process.kill(-pid, 'SIGTERM'); // Kill the process group
130-
}
153+
process.kill(pid);
131154
} catch {
132-
try {
133-
process.kill(pid, 'SIGTERM'); // Kill just the process
134-
} catch {
135-
// Process might already be terminated
136-
}
155+
// this can happen if a process has been automatically terminated.
137156
}
138157
}

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sv",
3-
"version": "0.9.5",
3+
"version": "0.9.6",
44
"type": "module",
55
"description": "A CLI for creating and updating SvelteKit projects",
66
"license": "MIT",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { namedOne as namedOneAlias, namedOneAliasFound } from 'package';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { imports, type AstTypes } from '@sveltejs/cli-core/js';
2+
3+
export function run(ast: AstTypes.Program): void {
4+
imports.addNamed(ast, { from: 'package', imports: { namedOne: 'namedOneAlias' }, isType: false });
5+
6+
const result = imports.find(ast, { name: 'namedOne', from: 'package' });
7+
8+
if (result) {
9+
imports.addNamed(ast, {
10+
imports: [result.alias + 'Found'],
11+
from: 'package'
12+
});
13+
}
14+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { n1, n2 } from 'p1';
2+
import { n3 } from 'p3';

0 commit comments

Comments
 (0)