Skip to content

Commit 6bf4bec

Browse files
committed
invert if
1 parent 1f5c62e commit 6bf4bec

File tree

1 file changed

+60
-60
lines changed

1 file changed

+60
-60
lines changed

packages/addons/vitest-addon/index.ts

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,66 @@ export default defineAddon({
3838
`;
3939
});
4040

41-
if (kit) {
41+
if (!kit) {
42+
sv.file(`vite.config.${ext}`, (content) => {
43+
const { ast, generateCode } = parseScript(content);
44+
45+
// find `defineConfig` import declaration for "vite"
46+
const importDecls = ast.body.filter((n) => n.type === 'ImportDeclaration');
47+
const defineConfigImportDecl = importDecls.find(
48+
(importDecl) =>
49+
(importDecl.source.value === 'vite' || importDecl.source.value === 'vitest/config') &&
50+
importDecl.importKind === 'value' &&
51+
importDecl.specifiers?.some(
52+
(specifier) =>
53+
specifier.type === 'ImportSpecifier' && specifier.imported.name === 'defineConfig'
54+
)
55+
);
56+
57+
// we'll need to replace the "vite" import for a "vitest/config" import.
58+
// if `defineConfig` is the only specifier in that "vite" import, remove the entire import declaration
59+
if (defineConfigImportDecl?.specifiers?.length === 1) {
60+
const idxToRemove = ast.body.indexOf(defineConfigImportDecl);
61+
ast.body.splice(idxToRemove, 1);
62+
} else {
63+
// otherwise, just remove the `defineConfig` specifier
64+
const idxToRemove = defineConfigImportDecl?.specifiers?.findIndex(
65+
(s) => s.type === 'ImportSpecifier' && s.imported.name === 'defineConfig'
66+
);
67+
if (idxToRemove) defineConfigImportDecl?.specifiers?.splice(idxToRemove, 1);
68+
}
69+
70+
const config = common.expressionFromString('defineConfig({})');
71+
const defaultExport = exports.defaultExport(ast, config);
72+
73+
const test = object.create({
74+
include: common.expressionFromString("['src/**/*.{test,spec}.{js,ts}']")
75+
});
76+
77+
// uses the `defineConfig` helper
78+
if (
79+
defaultExport.value.type === 'CallExpression' &&
80+
defaultExport.value.arguments[0]?.type === 'ObjectExpression'
81+
) {
82+
// if the previous `defineConfig` was aliased, reuse the alias for the "vitest/config" import
83+
const importSpecifier = defineConfigImportDecl?.specifiers?.find(
84+
(sp) => sp.type === 'ImportSpecifier' && sp.imported.name === 'defineConfig'
85+
);
86+
const defineConfigAlias = (importSpecifier?.local?.name ?? 'defineConfig') as string;
87+
imports.addNamed(ast, 'vitest/config', { defineConfig: defineConfigAlias });
88+
89+
object.properties(defaultExport.value.arguments[0], { test });
90+
} else if (defaultExport.value.type === 'ObjectExpression') {
91+
// if the config is just an object expression, just add the property
92+
object.properties(defaultExport.value, { test });
93+
} else {
94+
// unexpected config shape
95+
log.warn('Unexpected vite config for vitest add-on. Could not update.');
96+
}
97+
98+
return generateCode();
99+
});
100+
} else {
42101
sv.devDependency('@testing-library/svelte', '^5.2.4');
43102
sv.devDependency('@testing-library/jest-dom', '^6.6.3');
44103
sv.devDependency('jsdom', '^25.0.1');
@@ -114,65 +173,6 @@ export default defineAddon({
114173
array.push(workspaceArray, clientObjectExpression);
115174
array.push(workspaceArray, serverObjectExpression);
116175

117-
return generateCode();
118-
});
119-
} else {
120-
sv.file(`vite.config.${ext}`, (content) => {
121-
const { ast, generateCode } = parseScript(content);
122-
123-
// find `defineConfig` import declaration for "vite"
124-
const importDecls = ast.body.filter((n) => n.type === 'ImportDeclaration');
125-
const defineConfigImportDecl = importDecls.find(
126-
(importDecl) =>
127-
(importDecl.source.value === 'vite' || importDecl.source.value === 'vitest/config') &&
128-
importDecl.importKind === 'value' &&
129-
importDecl.specifiers?.some(
130-
(specifier) =>
131-
specifier.type === 'ImportSpecifier' && specifier.imported.name === 'defineConfig'
132-
)
133-
);
134-
135-
// we'll need to replace the "vite" import for a "vitest/config" import.
136-
// if `defineConfig` is the only specifier in that "vite" import, remove the entire import declaration
137-
if (defineConfigImportDecl?.specifiers?.length === 1) {
138-
const idxToRemove = ast.body.indexOf(defineConfigImportDecl);
139-
ast.body.splice(idxToRemove, 1);
140-
} else {
141-
// otherwise, just remove the `defineConfig` specifier
142-
const idxToRemove = defineConfigImportDecl?.specifiers?.findIndex(
143-
(s) => s.type === 'ImportSpecifier' && s.imported.name === 'defineConfig'
144-
);
145-
if (idxToRemove) defineConfigImportDecl?.specifiers?.splice(idxToRemove, 1);
146-
}
147-
148-
const config = common.expressionFromString('defineConfig({})');
149-
const defaultExport = exports.defaultExport(ast, config);
150-
151-
const test = object.create({
152-
include: common.expressionFromString("['src/**/*.{test,spec}.{js,ts}']")
153-
});
154-
155-
// uses the `defineConfig` helper
156-
if (
157-
defaultExport.value.type === 'CallExpression' &&
158-
defaultExport.value.arguments[0]?.type === 'ObjectExpression'
159-
) {
160-
// if the previous `defineConfig` was aliased, reuse the alias for the "vitest/config" import
161-
const importSpecifier = defineConfigImportDecl?.specifiers?.find(
162-
(sp) => sp.type === 'ImportSpecifier' && sp.imported.name === 'defineConfig'
163-
);
164-
const defineConfigAlias = (importSpecifier?.local?.name ?? 'defineConfig') as string;
165-
imports.addNamed(ast, 'vitest/config', { defineConfig: defineConfigAlias });
166-
167-
object.properties(defaultExport.value.arguments[0], { test });
168-
} else if (defaultExport.value.type === 'ObjectExpression') {
169-
// if the config is just an object expression, just add the property
170-
object.properties(defaultExport.value, { test });
171-
} else {
172-
// unexpected config shape
173-
log.warn('Unexpected vite config for vitest add-on. Could not update.');
174-
}
175-
176176
return generateCode();
177177
});
178178
}

0 commit comments

Comments
 (0)