Skip to content

Commit

Permalink
feat(helpers): handle Vite config declarations with satisfies keywo…
Browse files Browse the repository at this point in the history
…rd (#82)
  • Loading branch information
Lms24 authored Sep 14, 2023
1 parent a8752c8 commit 6b2ef88
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions src/helpers/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down
72 changes: 72 additions & 0 deletions test/helpers/vite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;"
`);
});
});

0 comments on commit 6b2ef88

Please sign in to comment.