Skip to content

feat: add vite.addPlugin #633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Jul 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
61ea4dd
wip
jycouet Jul 15, 2025
991420a
testing things out
jycouet Jul 15, 2025
cc5eb4a
testing things out 2
jycouet Jul 15, 2025
6b30cec
append & prepend
jycouet Jul 15, 2025
40ddef6
looks cool already
jycouet Jul 15, 2025
defc00c
split
jycouet Jul 15, 2025
122ac46
some cleanup
jycouet Jul 15, 2025
2c78390
adding some tests
jycouet Jul 15, 2025
7ba3876
api could look like this
jycouet Jul 15, 2025
42b2052
Merge branch 'main' of github.com:sveltejs/cli into add-plugin-to-vite
jycouet Jul 18, 2025
746b849
refactor location
jycouet Jul 18, 2025
fe70e14
refacto to test style of repo
jycouet Jul 18, 2025
f344a3d
managing also function return (my case actually!)
jycouet Jul 18, 2025
6adc1ee
playing with api style
jycouet Jul 18, 2025
394daa2
lol! that's even simpler now!
jycouet Jul 18, 2025
4726fc2
add convenience imports.addNamed
jycouet Jul 18, 2025
69041ed
update paraglide to new style
jycouet Jul 18, 2025
13c7c1e
update tailwind to new style
jycouet Jul 18, 2025
533e562
.
jycouet Jul 18, 2025
9e9a175
Update .changeset/tough-carrots-eat.md
jycouet Jul 20, 2025
24160b0
chore: allow passing an array of import names to imports.addNamed
jycouet Jul 20, 2025
0326995
update usage
jycouet Jul 20, 2025
ef52ff9
Merge branch 'chore/imports-array' of github.com:jycouet/cli into add…
jycouet Jul 20, 2025
f53fbf0
Merge branch 'add-plugin-to-vite' of github.com:jycouet/cli into add-…
jycouet Jul 20, 2025
f3e47a1
update mode test
jycouet Jul 20, 2025
b299283
bailing out early
jycouet Jul 20, 2025
cfc1102
Merge branch 'main' of github.com:sveltejs/cli into add-plugin-to-vite
jycouet Jul 25, 2025
718f65f
already merged with the other PR. (an better naming)
jycouet Jul 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tough-carrots-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': patch
---

feat: add `vite.addPlugin` to simplify adding a plugin on various vite config styles
19 changes: 3 additions & 16 deletions packages/addons/devtools-json/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineAddon } from '@sveltejs/cli-core';
import { array, functions, imports, object, exports } from '@sveltejs/cli-core/js';
import { imports, vite } from '@sveltejs/cli-core/js';
import { parseScript } from '@sveltejs/cli-core/parsers';

export default defineAddon({
Expand All @@ -18,21 +18,8 @@ export default defineAddon({
const { ast, generateCode } = parseScript(content);

const vitePluginName = 'devtoolsJson';
imports.addDefault(ast, { from: 'vite-plugin-devtools-json', as: vitePluginName });

const { value: rootObject } = exports.createDefault(ast, {
fallback: functions.createCall({ name: 'defineConfig', args: [] })
});

const param1 = functions.getArgument(rootObject, {
index: 0,
fallback: object.create({})
});

const pluginsArray = object.property(param1, { name: 'plugins', fallback: array.create() });
const pluginFunctionCall = functions.createCall({ name: vitePluginName, args: [] });

array.append(pluginsArray, pluginFunctionCall);
imports.addDefault(ast, { as: vitePluginName, from: 'vite-plugin-devtools-json' });
vite.addPlugin(ast, { code: `${vitePluginName}()` });

return generateCode();
});
Expand Down
36 changes: 7 additions & 29 deletions packages/addons/paraglide/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import MagicString from 'magic-string';
import { colors, defineAddon, defineAddonOptions, log } from '@sveltejs/cli-core';
import {
array,
common,
functions,
imports,
object,
variables,
exports,
kit as kitJs
} from '@sveltejs/cli-core/js';
import { common, imports, variables, exports, kit as kitJs, vite } from '@sveltejs/cli-core/js';
import * as html from '@sveltejs/cli-core/html';
import { parseHtml, parseJson, parseScript, parseSvelte } from '@sveltejs/cli-core/parsers';
import { addToDemoPage } from '../common.ts';
Expand Down Expand Up @@ -94,26 +85,13 @@ export default defineAddon({
const { ast, generateCode } = parseScript(content);

const vitePluginName = 'paraglideVitePlugin';
imports.addNamed(ast, {
from: '@inlang/paraglide-js',
imports: [vitePluginName]
});
const { value: rootObject } = exports.createDefault(ast, {
fallback: functions.createCall({ name: 'defineConfig', args: [] })
});
const param1 = functions.getArgument(rootObject, {
index: 0,
fallback: object.create({})
});

const pluginsArray = object.property(param1, { name: 'plugins', fallback: array.create() });
const pluginFunctionCall = functions.createCall({ name: vitePluginName, args: [] });
const pluginConfig = object.create({
project: './project.inlang',
outdir: `./${paraglideOutDir}`
imports.addNamed(ast, { imports: [vitePluginName], from: '@inlang/paraglide-js' });
vite.addPlugin(ast, {
code: `${vitePluginName}({
project: './project.inlang',
outdir: './${paraglideOutDir}'
})`
});
functions.getArgument(pluginFunctionCall, { index: 0, fallback: pluginConfig });
array.append(pluginsArray, pluginFunctionCall);

return generateCode();
});
Expand Down
17 changes: 3 additions & 14 deletions packages/addons/tailwindcss/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineAddon, defineAddonOptions } from '@sveltejs/cli-core';
import { array, functions, imports, object, exports } from '@sveltejs/cli-core/js';
import { imports, vite } from '@sveltejs/cli-core/js';
import { parseCss, parseJson, parseScript, parseSvelte } from '@sveltejs/cli-core/parsers';
import { addSlot } from '@sveltejs/cli-core/html';

Expand Down Expand Up @@ -61,19 +61,8 @@ export default defineAddon({
const { ast, generateCode } = parseScript(content);

const vitePluginName = 'tailwindcss';
imports.addDefault(ast, { from: '@tailwindcss/vite', as: vitePluginName });

const { value: rootObject } = exports.createDefault(ast, {
fallback: functions.createCall({ name: 'defineConfig', args: [] })
});
const param1 = functions.getArgument(rootObject, {
index: 0,
fallback: object.create({})
});

const pluginsArray = object.property(param1, { name: 'plugins', fallback: array.create() });
const pluginFunctionCall = functions.createCall({ name: vitePluginName, args: [] });
array.prepend(pluginsArray, pluginFunctionCall);
imports.addDefault(ast, { as: vitePluginName, from: '@tailwindcss/vite' });
vite.addPlugin(ast, { code: `${vitePluginName}()`, mode: 'prepend' });

return generateCode();
});
Expand Down
9 changes: 9 additions & 0 deletions packages/core/tests/js/vite/add-plugin-mode/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig({
plugins: [
// a default plugin
sveltekit()
]
});
15 changes: 15 additions & 0 deletions packages/core/tests/js/vite/add-plugin-mode/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { firstPlugin } from 'first-plugin';
import lastPlugin from 'last-plugin';
import middlePlugin from 'middle-plugin';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig({
plugins: [
firstPlugin(),
// a default plugin
sveltekit(),
middlePlugin(),
lastPlugin()
]
});
15 changes: 15 additions & 0 deletions packages/core/tests/js/vite/add-plugin-mode/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { imports, vite, type AstTypes } from '@sveltejs/cli-core/js';

export function run(ast: AstTypes.Program): void {
const pMiddle = 'middlePlugin';
imports.addDefault(ast, { as: pMiddle, from: 'middle-plugin' });
vite.addPlugin(ast, { code: `${pMiddle}()` });

const pLast = 'lastPlugin';
imports.addDefault(ast, { as: pLast, from: 'last-plugin' });
vite.addPlugin(ast, { code: `${pLast}()`, mode: 'append' });

const pFirst = 'firstPlugin';
imports.addNamed(ast, { imports: { [pFirst]: pFirst }, from: 'first-plugin' });
vite.addPlugin(ast, { code: `${pFirst}()`, mode: 'prepend' });
}
6 changes: 6 additions & 0 deletions packages/core/tests/js/vite/default/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig({
plugins: [sveltekit()]
});
5 changes: 5 additions & 0 deletions packages/core/tests/js/vite/default/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import myPlugin from 'my-plugin';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig({ plugins: [sveltekit(), myPlugin()] });
7 changes: 7 additions & 0 deletions packages/core/tests/js/vite/default/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { imports, vite, type AstTypes } from '@sveltejs/cli-core/js';

export function run(ast: AstTypes.Program): void {
const vitePluginName = 'myPlugin';
imports.addDefault(ast, { as: vitePluginName, from: 'my-plugin' });
vite.addPlugin(ast, { code: `${vitePluginName}()` });
}
9 changes: 9 additions & 0 deletions packages/core/tests/js/vite/function-return/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig(() => {
// Some stuff here
return {
plugins: [sveltekit()]
};
});
8 changes: 8 additions & 0 deletions packages/core/tests/js/vite/function-return/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import myPlugin from 'my-plugin';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig(() => {
// Some stuff here
return { plugins: [sveltekit(), myPlugin()] };
});
7 changes: 7 additions & 0 deletions packages/core/tests/js/vite/function-return/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { imports, vite, type AstTypes } from '@sveltejs/cli-core/js';

export function run(ast: AstTypes.Program): void {
const vitePluginName = 'myPlugin';
imports.addDefault(ast, { as: vitePluginName, from: 'my-plugin' });
vite.addPlugin(ast, { code: `${vitePluginName}()` });
}
8 changes: 8 additions & 0 deletions packages/core/tests/js/vite/with-variable/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

const config = defineConfig({
plugins: [sveltekit()]
});

export default config;
7 changes: 7 additions & 0 deletions packages/core/tests/js/vite/with-variable/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import myPlugin from 'my-plugin';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

const config = defineConfig({ plugins: [sveltekit(), myPlugin()] });

export default config;
7 changes: 7 additions & 0 deletions packages/core/tests/js/vite/with-variable/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { imports, vite, type AstTypes } from '@sveltejs/cli-core/js';

export function run(ast: AstTypes.Program): void {
const vitePluginName = 'myPlugin';
imports.addDefault(ast, { as: vitePluginName, from: 'my-plugin' });
vite.addPlugin(ast, { code: `${vitePluginName}()` });
}
8 changes: 8 additions & 0 deletions packages/core/tests/js/vite/without-defineConfig/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { sveltekit } from '@sveltejs/kit/vite';
import type { UserConfig } from 'vite';

const config: UserConfig = {
plugins: [sveltekit()]
};

export default config;
7 changes: 7 additions & 0 deletions packages/core/tests/js/vite/without-defineConfig/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import myPlugin from 'my-plugin';
import { sveltekit } from '@sveltejs/kit/vite';
import type { UserConfig, defineConfig } from 'vite';

const config: UserConfig = { plugins: [myPlugin(), sveltekit()] };

export default config;
7 changes: 7 additions & 0 deletions packages/core/tests/js/vite/without-defineConfig/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { imports, vite, type AstTypes } from '@sveltejs/cli-core/js';

export function run(ast: AstTypes.Program): void {
const vitePluginName = 'myPlugin';
imports.addDefault(ast, { as: vitePluginName, from: 'my-plugin' });
vite.addPlugin(ast, { code: `${vitePluginName}()`, mode: 'prepend' });
}
2 changes: 1 addition & 1 deletion packages/core/tooling/js/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export function addDefault(node: AstTypes.Program, options: { from: string; as:
export function addNamed(
node: AstTypes.Program,
options: {
from: string;
imports: Record<string, string> | string[];
from: string;
isType?: boolean;
}
): void {
Expand Down
1 change: 1 addition & 0 deletions packages/core/tooling/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export * as imports from './imports.ts';
export * as variables from './variables.ts';
export * as exports from './exports.ts';
export * as kit from './kit.ts';
export * as vite from './vite.ts';
export type { AstTypes } from '../index.ts';
Loading
Loading