Skip to content

Commit 3bcd789

Browse files
jycouetmanuel3108
andauthored
feat: add vite.addPlugin (#633)
* wip * testing things out * testing things out 2 * append & prepend * looks cool already * split * some cleanup * adding some tests * api could look like this * refactor location * refacto to test style of repo * managing also function return (my case actually!) * playing with api style * lol! that's even simpler now! * add convenience imports.addNamed * update paraglide to new style * update tailwind to new style * . * Update .changeset/tough-carrots-eat.md Co-authored-by: Manuel <[email protected]> * chore: allow passing an array of import names to imports.addNamed * update usage * update mode test * bailing out early * already merged with the other PR. (an better naming) --------- Co-authored-by: Manuel <[email protected]>
1 parent 519304e commit 3bcd789

File tree

22 files changed

+280
-60
lines changed

22 files changed

+280
-60
lines changed

.changeset/tough-carrots-eat.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+
feat: add `vite.addPlugin` to simplify adding a plugin on various vite config styles

packages/addons/devtools-json/index.ts

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

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

2020
const vitePluginName = 'devtoolsJson';
21-
imports.addDefault(ast, { from: 'vite-plugin-devtools-json', as: vitePluginName });
22-
23-
const { value: rootObject } = exports.createDefault(ast, {
24-
fallback: functions.createCall({ name: 'defineConfig', args: [] })
25-
});
26-
27-
const param1 = functions.getArgument(rootObject, {
28-
index: 0,
29-
fallback: object.create({})
30-
});
31-
32-
const pluginsArray = object.property(param1, { name: 'plugins', fallback: array.create() });
33-
const pluginFunctionCall = functions.createCall({ name: vitePluginName, args: [] });
34-
35-
array.append(pluginsArray, pluginFunctionCall);
21+
imports.addDefault(ast, { as: vitePluginName, from: 'vite-plugin-devtools-json' });
22+
vite.addPlugin(ast, { code: `${vitePluginName}()` });
3623

3724
return generateCode();
3825
});

packages/addons/paraglide/index.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
import MagicString from 'magic-string';
22
import { colors, defineAddon, defineAddonOptions, log } from '@sveltejs/cli-core';
3-
import {
4-
array,
5-
common,
6-
functions,
7-
imports,
8-
object,
9-
variables,
10-
exports,
11-
kit as kitJs
12-
} from '@sveltejs/cli-core/js';
3+
import { common, imports, variables, exports, kit as kitJs, vite } from '@sveltejs/cli-core/js';
134
import * as html from '@sveltejs/cli-core/html';
145
import { parseHtml, parseJson, parseScript, parseSvelte } from '@sveltejs/cli-core/parsers';
156
import { addToDemoPage } from '../common.ts';
@@ -94,26 +85,13 @@ export default defineAddon({
9485
const { ast, generateCode } = parseScript(content);
9586

9687
const vitePluginName = 'paraglideVitePlugin';
97-
imports.addNamed(ast, {
98-
from: '@inlang/paraglide-js',
99-
imports: [vitePluginName]
100-
});
101-
const { value: rootObject } = exports.createDefault(ast, {
102-
fallback: functions.createCall({ name: 'defineConfig', args: [] })
103-
});
104-
const param1 = functions.getArgument(rootObject, {
105-
index: 0,
106-
fallback: object.create({})
107-
});
108-
109-
const pluginsArray = object.property(param1, { name: 'plugins', fallback: array.create() });
110-
const pluginFunctionCall = functions.createCall({ name: vitePluginName, args: [] });
111-
const pluginConfig = object.create({
112-
project: './project.inlang',
113-
outdir: `./${paraglideOutDir}`
88+
imports.addNamed(ast, { imports: [vitePluginName], from: '@inlang/paraglide-js' });
89+
vite.addPlugin(ast, {
90+
code: `${vitePluginName}({
91+
project: './project.inlang',
92+
outdir: './${paraglideOutDir}'
93+
})`
11494
});
115-
functions.getArgument(pluginFunctionCall, { index: 0, fallback: pluginConfig });
116-
array.append(pluginsArray, pluginFunctionCall);
11795

11896
return generateCode();
11997
});

packages/addons/tailwindcss/index.ts

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

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

6363
const vitePluginName = 'tailwindcss';
64-
imports.addDefault(ast, { from: '@tailwindcss/vite', as: vitePluginName });
65-
66-
const { value: rootObject } = exports.createDefault(ast, {
67-
fallback: functions.createCall({ name: 'defineConfig', args: [] })
68-
});
69-
const param1 = functions.getArgument(rootObject, {
70-
index: 0,
71-
fallback: object.create({})
72-
});
73-
74-
const pluginsArray = object.property(param1, { name: 'plugins', fallback: array.create() });
75-
const pluginFunctionCall = functions.createCall({ name: vitePluginName, args: [] });
76-
array.prepend(pluginsArray, pluginFunctionCall);
64+
imports.addDefault(ast, { as: vitePluginName, from: '@tailwindcss/vite' });
65+
vite.addPlugin(ast, { code: `${vitePluginName}()`, mode: 'prepend' });
7766

7867
return generateCode();
7968
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { sveltekit } from '@sveltejs/kit/vite';
2+
import { defineConfig } from 'vite';
3+
4+
export default defineConfig({
5+
plugins: [
6+
// a default plugin
7+
sveltekit()
8+
]
9+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { firstPlugin } from 'first-plugin';
2+
import lastPlugin from 'last-plugin';
3+
import middlePlugin from 'middle-plugin';
4+
import { sveltekit } from '@sveltejs/kit/vite';
5+
import { defineConfig } from 'vite';
6+
7+
export default defineConfig({
8+
plugins: [
9+
firstPlugin(),
10+
// a default plugin
11+
sveltekit(),
12+
middlePlugin(),
13+
lastPlugin()
14+
]
15+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { imports, vite, type AstTypes } from '@sveltejs/cli-core/js';
2+
3+
export function run(ast: AstTypes.Program): void {
4+
const pMiddle = 'middlePlugin';
5+
imports.addDefault(ast, { as: pMiddle, from: 'middle-plugin' });
6+
vite.addPlugin(ast, { code: `${pMiddle}()` });
7+
8+
const pLast = 'lastPlugin';
9+
imports.addDefault(ast, { as: pLast, from: 'last-plugin' });
10+
vite.addPlugin(ast, { code: `${pLast}()`, mode: 'append' });
11+
12+
const pFirst = 'firstPlugin';
13+
imports.addNamed(ast, { imports: { [pFirst]: pFirst }, from: 'first-plugin' });
14+
vite.addPlugin(ast, { code: `${pFirst}()`, mode: 'prepend' });
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { sveltekit } from '@sveltejs/kit/vite';
2+
import { defineConfig } from 'vite';
3+
4+
export default defineConfig({
5+
plugins: [sveltekit()]
6+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import myPlugin from 'my-plugin';
2+
import { sveltekit } from '@sveltejs/kit/vite';
3+
import { defineConfig } from 'vite';
4+
5+
export default defineConfig({ plugins: [sveltekit(), myPlugin()] });
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { imports, vite, type AstTypes } from '@sveltejs/cli-core/js';
2+
3+
export function run(ast: AstTypes.Program): void {
4+
const vitePluginName = 'myPlugin';
5+
imports.addDefault(ast, { as: vitePluginName, from: 'my-plugin' });
6+
vite.addPlugin(ast, { code: `${vitePluginName}()` });
7+
}

0 commit comments

Comments
 (0)