Skip to content

Commit 08efcd9

Browse files
committed
docs
1 parent 295e7cf commit 08efcd9

File tree

3 files changed

+63
-37
lines changed

3 files changed

+63
-37
lines changed

documentation/docs/20-commands/20-sv-add.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Prevents installing dependencies
7171
> [!NOTE]
7272
> Svelte maintainers have not reviewed community add-ons for malicious code!
7373
74-
You can find [community add-ons on npm](https://www.npmjs.com/search?q=keywords%3Asv-add) by searching for `keywords:sv-add`.
74+
You can find [community add-ons on npm](https://www.npmx.dev/search?q=keyword:sv-add) by searching for keyword: `sv-add`.
7575

7676
### How to install a community add-on
7777

documentation/docs/40-api/10-add-on.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ npx sv create --template addon my-addon
1919

2020
Typically, an add-on looks like this:
2121

22-
_hover keywords in the code to have some more context_
23-
2422
```js
2523
// @noErrors
2624
import { transforms } from '@sveltejs/sv-utils';
@@ -52,9 +50,12 @@ export default defineAddon({
5250
if (!isKit) return cancel('SvelteKit is required');
5351

5452
// Add "Hello [who]!" to the root page
55-
sv.file(directory.routes + '/+page.svelte', transforms.svelte(({ ast, svelte }) => {
56-
svelte.addFragment(ast, `<p>Hello ${options.who}!</p>`);
57-
}));
53+
sv.file(
54+
directory.routes + '/+page.svelte',
55+
transforms.svelte(({ ast, svelte }) => {
56+
svelte.addFragment(ast, `<p>Hello ${options.who}!</p>`);
57+
})
58+
);
5859
}
5960
});
6061
```

documentation/docs/40-api/20-sv-utils.md

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ Transform a JavaScript/TypeScript file. The callback receives `{ ast, comments,
3838
// @noErrors
3939
import { transforms } from '@sveltejs/sv-utils';
4040

41-
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
42-
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
43-
js.vite.addPlugin(ast, { code: 'foo()' });
44-
}));
41+
sv.file(
42+
files.viteConfig,
43+
transforms.script(({ ast, js }) => {
44+
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
45+
js.vite.addPlugin(ast, { code: 'foo()' });
46+
})
47+
);
4548
```
4649

4750
### `transforms.svelte`
@@ -52,9 +55,12 @@ Transform a Svelte component. The callback receives `{ ast, content, svelte, js
5255
// @noErrors
5356
import { transforms } from '@sveltejs/sv-utils';
5457

55-
sv.file(layoutPath, transforms.svelte(({ ast, svelte }) => {
56-
svelte.addFragment(ast, '<Foo />');
57-
}));
58+
sv.file(
59+
layoutPath,
60+
transforms.svelte(({ ast, svelte }) => {
61+
svelte.addFragment(ast, '<Foo />');
62+
})
63+
);
5864
```
5965

6066
### `transforms.svelteScript`
@@ -65,10 +71,13 @@ Transform a Svelte component with a `<script>` block guaranteed. Pass `{ languag
6571
// @noErrors
6672
import { transforms } from '@sveltejs/sv-utils';
6773

68-
sv.file(layoutPath, transforms.svelteScript({ language }, ({ ast, svelte, js }) => {
69-
js.imports.addDefault(ast.instance.content, { as: 'Foo', from: './Foo.svelte' });
70-
svelte.addFragment(ast, '<Foo />');
71-
}));
74+
sv.file(
75+
layoutPath,
76+
transforms.svelteScript({ language }, ({ ast, svelte, js }) => {
77+
js.imports.addDefault(ast.instance.content, { as: 'Foo', from: './Foo.svelte' });
78+
svelte.addFragment(ast, '<Foo />');
79+
})
80+
);
7281
```
7382

7483
### `transforms.css`
@@ -79,9 +88,12 @@ Transform a CSS file. The callback receives `{ ast, content, css }`.
7988
// @noErrors
8089
import { transforms } from '@sveltejs/sv-utils';
8190

82-
sv.file(files.stylesheet, transforms.css(({ ast, css }) => {
83-
css.addAtRule(ast, { name: 'import', params: "'tailwindcss'" });
84-
}));
91+
sv.file(
92+
files.stylesheet,
93+
transforms.css(({ ast, css }) => {
94+
css.addAtRule(ast, { name: 'import', params: "'tailwindcss'" });
95+
})
96+
);
8597
```
8698

8799
### `transforms.json`
@@ -92,10 +104,13 @@ Transform a JSON file. Mutate the `data` object directly. The callback receives
92104
// @noErrors
93105
import { transforms } from '@sveltejs/sv-utils';
94106

95-
sv.file(files.tsconfig, transforms.json(({ data }) => {
96-
data.compilerOptions ??= {};
97-
data.compilerOptions.strict = true;
98-
}));
107+
sv.file(
108+
files.tsconfig,
109+
transforms.json(({ data }) => {
110+
data.compilerOptions ??= {};
111+
data.compilerOptions.strict = true;
112+
})
113+
);
99114
```
100115
101116
### `transforms.yaml` / `transforms.toml`
@@ -110,9 +125,12 @@ Transform a plain text file (.env, .gitignore, etc.). No parser - string in, str
110125
// @noErrors
111126
import { transforms } from '@sveltejs/sv-utils';
112127

113-
sv.file('.env', transforms.text(({ content }) => {
114-
return content + '\nDATABASE_URL="file:local.db"';
115-
}));
128+
sv.file(
129+
'.env',
130+
transforms.text(({ content }) => {
131+
return content + '\nDATABASE_URL="file:local.db"';
132+
})
133+
);
116134
```
117135
118136
### Aborting a transform
@@ -123,14 +141,17 @@ Return `false` from any transform callback to abort - the original content is re
123141
// @noErrors
124142
import { transforms } from '@sveltejs/sv-utils';
125143

126-
sv.file(files.eslintConfig, transforms.script(({ ast, js }) => {
127-
const { value: existing } = js.exports.createDefault(ast, { fallback: myConfig });
128-
if (existing !== myConfig) {
129-
// config already exists, don't touch it
130-
return false;
131-
}
132-
// ... continue modifying ast
133-
}));
144+
sv.file(
145+
files.eslintConfig,
146+
transforms.script(({ ast, js }) => {
147+
const { value: existing } = js.exports.createDefault(ast, { fallback: myConfig });
148+
if (existing !== myConfig) {
149+
// config already exists, don't touch it
150+
return false;
151+
}
152+
// ... continue modifying ast
153+
})
154+
);
134155
```
135156
136157
### Standalone usage & testing
@@ -152,10 +173,14 @@ For cases where you need to mix transforms and raw edits, use `sv.file` with a c
152173
```js
153174
// @noErrors
154175
sv.file(path, (content) => {
176+
// curried
155177
content = transforms.script(({ ast, js }) => {
156-
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
178+
js.imports.addDefault(ast, { as: 'foo', from: 'bar' });
157179
})(content);
158-
content = content.replace('foo', 'bar');
180+
181+
// raw string manipulation
182+
content = content.replace('foo', 'baz');
183+
159184
return content;
160185
});
161186
```

0 commit comments

Comments
 (0)