diff --git a/src/helpers/config.ts b/src/helpers/config.ts index 71be36a..1e72497 100644 --- a/src/helpers/config.ts +++ b/src/helpers/config.ts @@ -46,9 +46,13 @@ export function getConfigFromVariableDeclaration( declaration.id.name === configDecalarationId && declaration.init ) { - const init = declaration.init; + const init = + declaration.init.type === "TSSatisfiesExpression" + ? declaration.init.expression + : declaration.init; - const configExpression = parseExpression(generateCode(init).code); + const code = generateCode(init).code; + const configExpression = parseExpression(code); return { declaration, diff --git a/src/helpers/vite.ts b/src/helpers/vite.ts index 1b51eb8..297bd2a 100644 --- a/src/helpers/vite.ts +++ b/src/helpers/vite.ts @@ -135,6 +135,23 @@ function insertPluginIntoVariableDeclarationConfig( declaration.init = generateCode( builders.functionCall(declaration.init.callee.name, configObject), ).code; + } else if (declaration.init.type === "TSSatisfiesExpression") { + if (declaration.init.expression.type === "ObjectExpression") { + // @ts-ignore this works despite the type error because of recast + declaration.init.expression = generateCode(configObject).code; + } + if ( + declaration.init.expression.type === "CallExpression" && + declaration.init.expression.callee.type === "Identifier" + ) { + // @ts-ignore this works despite the type error because of recast + declaration.init.expression = generateCode( + builders.functionCall( + declaration.init.expression.callee.name, + configObject, + ), + ).code; + } } } } diff --git a/test/helpers/vite.test.ts b/test/helpers/vite.test.ts index 38d2b6a..1e51d63 100644 --- a/test/helpers/vite.test.ts +++ b/test/helpers/vite.test.ts @@ -181,4 +181,76 @@ export default defineConfig({ export default myConfig;" `); }); + + it("handles default export from identifier (object with satisfies)", async () => { + const code = ` + import { somePlugin1, somePlugin2 } from 'some-module' + + import type { UserConfig } from 'vite'; + + const myConfig = { + plugins: [somePlugin1(), somePlugin2()] + } satisfies UserConfig; + + export default myConfig; + `; + + const mod = parseModule(code); + + addVitePlugin(mod, { + index: 1, + from: "vite-plugin-pwa", + imported: "VitePWA", + constructor: "VitePWA", + }); + + expect(await generate(mod)).toMatchInlineSnapshot(` + "import { VitePWA } from \\"vite-plugin-pwa\\"; + import { somePlugin1, somePlugin2 } from \\"some-module\\"; + + import type { UserConfig } from \\"vite\\"; + + const myConfig = { + plugins: [somePlugin1(), VitePWA(), somePlugin2()], + } satisfies UserConfig; + + export default myConfig;" + `); + }); + + it("handles default export from identifier (function with satisfies)", async () => { + const code = ` + import { somePlugin1, somePlugin2 } from 'some-module' + + import type { UserConfig } from 'vite'; + + const myConfig = defineConfig({ + plugins: [somePlugin1(), somePlugin2()] + }) satisfies UserConfig; + + export default myConfig; + `; + + const mod = parseModule(code); + + addVitePlugin(mod, { + index: 1, + from: "vite-plugin-pwa", + imported: "VitePWA", + constructor: "VitePWA", + }); + + expect(await generate(mod)).toMatchInlineSnapshot(` + "import { VitePWA } from \\"vite-plugin-pwa\\"; + import { somePlugin1, somePlugin2 } from \\"some-module\\"; + + import type { UserConfig } from \\"vite\\"; + + const myConfig = defineConfig({ + plugins: [somePlugin1(), VitePWA(), somePlugin2()], + }) satisfies UserConfig; + + export default myConfig;" + `); + }); });