sv-utils
Le paquet
@sveltejs/sv-utilsest actuellement expérimental. Son API peut encore évoluer.
Le paquet @sveltejs/sv-utils est un utilitaire d’add-on pour parser, transformer, et générer du
code dans les add-ons.
npm install -D @sveltejs/sv-utilstransforms
transforms est une collection de fonction conscientes des parsers vous permettant de modifier les
fichiers via un AST. Elle accepte un callback. La valeur de retour est conçue pour être passée
directement à sv.file(). Le choix du parser est inclus dans le type de transformation — vous ne
pouvez pas parser accidentellement une configuration Vite en tant que fichier Svelte, car vous
n’appelez jamais un parser vous-même.
Chaque transformation injecte les utilitaires pertinents dans le callback, de sorte que vous n’avez besoin que d’un import :
import { transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
} from '@sveltejs/sv-utils';
transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
.script(method) script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => stringTransform a JavaScript/TypeScript file.
Return false from the callback to abort - the original content is returned unchanged.
(/* ... */);
transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
.svelte(method) svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => stringTransform a Svelte component file.
Return false from the callback to abort - the original content is returned unchanged.
(/* ... */);
// ...transforms.script
Transforme un fichier JavaScript/TypeScript. Le callback reçoit { ast, comments, content, js }.
import { transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
} from '@sveltejs/sv-utils';
svany.fileany(
fileany.viteConfigany,
transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
.script(method) script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => stringTransform a JavaScript/TypeScript file.
Return false from the callback to abort - the original content is returned unchanged.
(({ ast(parameter) ast: Program, js(parameter) js: typeof index_d_exports$3 }) => {
js(parameter) js: typeof index_d_exports$3.imports(alias) namespace index_d_exports$3.imports
export index_d_exports$3.imports.addDefault(alias) imports_d_exports.addDefault(node: Program, options: {
from: string;
as: string;
}): void
export imports_d_exports.addDefault(ast(parameter) ast: Program, { as(property) as: string: 'foo', from(property) from: string: 'foo' });
js(parameter) js: typeof index_d_exports$3.vite(alias) namespace index_d_exports$3.vite
export index_d_exports$3.vite.addPlugin(alias) vite_d_exports.addPlugin(ast: Program, options: {
code: string;
mode?: "append" | "prepend";
}): void
export vite_d_exports.addPlugin(ast(parameter) ast: Program, { code(property) code: string: 'foo()' });
})
);transforms.svelte
Transforme un composant Svelte. Le callback reçoit { ast, content, svelte, js }.
import { transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
} from '@sveltejs/sv-utils';
svany.fileany(
layoutPathany,
transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
.svelte(method) svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => stringTransform a Svelte component file.
Return false from the callback to abort - the original content is returned unchanged.
(({ ast(parameter) ast: AST.Root, svelte(parameter) svelte: typeof index_d_exports$4 }) => {
svelte(parameter) svelte: typeof index_d_exports$4.addFragment(alias) index_d_exports$4.addFragment(ast: AST.Root, content: string, options?: {
mode?: "append" | "prepend";
language?: "ts" | "js";
}): void
export index_d_exports$4.addFragment(ast(parameter) ast: AST.Root, '<Foo />');
})
);transforms.svelteScript
Transforme un composant Svelte avec un bloc <script> garanti. Passez { language } en tant que
premier argument. Le callback reçoit { ast, content, svelte, js } où ast.instance est toujours
non nul.
import { transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
} from '@sveltejs/sv-utils';
svany.fileany(
layoutPathany,
transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
.svelteScript(method) svelteScript(scriptOptions: {
language: "ts" | "js";
}, cb: (file: {
ast: RootWithInstance;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): TransformFnTransform a Svelte component file with a script block guaranteed.
Calls ensureScript before invoking your callback, so ast.instance is always non-null.
Pass { language } as the first argument to set the script language.
Return false from the callback to abort - the original content is returned unchanged.
({ language(property) language: "ts" | "js": 'ts' }, ({ ast(parameter) ast: RootWithInstance, svelte(parameter) svelte: typeof index_d_exports$4, js(parameter) js: typeof index_d_exports$3 }) => {
js(parameter) js: typeof index_d_exports$3.imports(alias) namespace index_d_exports$3.imports
export index_d_exports$3.imports.addDefault(alias) imports_d_exports.addDefault(node: Program, options: {
from: string;
as: string;
}): void
export imports_d_exports.addDefault(ast(parameter) ast: RootWithInstance.instance(property) instance: AST.ScriptThe parsed <script> element, if exists
.content(property) AST.Script.content: Program, { as(property) as: string: 'Foo', from(property) from: string: './Foo.svelte' });
svelte(parameter) svelte: typeof index_d_exports$4.addFragment(alias) index_d_exports$4.addFragment(ast: AST.Root, content: string, options?: {
mode?: "append" | "prepend";
language?: "ts" | "js";
}): void
export index_d_exports$4.addFragment(ast(parameter) ast: RootWithInstance, '<Foo />');
})
);transforms.css
Transforme une fichier CSS. Le callback reçoit { ast, content, css }.
import { transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
} from '@sveltejs/sv-utils';
svany.fileany(
fileany.stylesheetany,
transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
.css(method) css(cb: (file: {
ast: Omit<_CSS.StyleSheetBase, "attributes" | "content">;
content: string;
css: typeof index_d_exports$1;
}) => void | false, options?: TransformOptions): TransformFnTransform a CSS file.
Return false from the callback to abort - the original content is returned unchanged.
(({ ast(parameter) ast: Omit<_CSS.StyleSheetBase, "attributes" | "content">, css(parameter) css: typeof index_d_exports$1 }) => {
css(parameter) css: typeof index_d_exports$1.addAtRule(alias) index_d_exports$1.addAtRule(node: _CSS.StyleSheetBase, options: {
name: string;
params: string;
append: boolean;
}): _CSS.Atrule
export index_d_exports$1.addAtRule(ast(parameter) ast: Omit<_CSS.StyleSheetBase, "attributes" | "content">, { name(property) name: string: 'import', params(property) params: string: "'tailwindcss'" });
})
);transforms.json
Transforme un fichier JSON. Mute l’objet data directement. Le callback reçoit { data, content, json }.
import { transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
} from '@sveltejs/sv-utils';
svany.fileany(
fileany.typeConfigany,
transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
.json(method) json<any>(cb: (file: {
data: any;
content: string;
json: typeof json_d_exports;
}) => void | false, options?: TransformOptions): TransformFnTransform a JSON file.
Return false from the callback to abort - the original content is returned unchanged.
(({ data(parameter) data: any }) => {
data(parameter) data: any.compilerOptionsany ??= {};
data(parameter) data: any.compilerOptionsany.strictany = true;
})
);transforms.yaml / transforms.toml
Similaire à transforms.json, pour les fichiers YAML et TOML, respectivement. Le callback reçoit { data, content }.
transforms.text
Transforme un fichier texte brut (.env, .gitignore, etc.). Aucun parser — une string en entrée,
une string en sortie. Le callback reçoit { content, text }.
import { transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
} from '@sveltejs/sv-utils';
svany.fileany(
'.env',
transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
.text(method) text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFnTransform a plain text file (.env, .gitignore, etc.).
Unlike other transforms there's no AST here - just string in, string out.
Return the new content, or false to abort (original content is returned unchanged).
(({ content(parameter) content: string }) => {
return content(parameter) content: string + '\nDATABASE_URL="file:local.db"';
})
);Annuler une transformation
Renvoyez false depuis n’importe quel callback pour annuler la transformation — le contenu original
est renvoyé inchangé.
import { transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
} from '@sveltejs/sv-utils';
svany.fileany(
'eslint.config.js',
transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
.script(method) script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => stringTransform a JavaScript/TypeScript file.
Return false from the callback to abort - the original content is returned unchanged.
(({ ast(parameter) ast: Program, js(parameter) js: typeof index_d_exports$3 }) => {
const { value(property) value: any: existingconst existing: any } = js(parameter) js: typeof index_d_exports$3.exports(alias) namespace index_d_exports$3.exports
export index_d_exports$3.exports.createDefault(alias) exports_d_exports.createDefault<any>(node: Program, options: {
fallback: any;
}): ExportDefaultResult<any>
export exports_d_exports.createDefault(ast(parameter) ast: Program, { fallback(property) fallback: any: myConfigany });
if (existingconst existing: any !== myConfigany) {
// la configuration existe déjà, on n'y touche pas
return false;
}
// ... on continue de modifier l'ast
})
);Usage standalone & tests
Les transformations sont des fonctions curry — appelez-les avec un callback, puis appliquez-les sur votre contenu :
import { transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
} from '@sveltejs/sv-utils';
const transformconst transform: (content: string) => string = transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
.script(method) script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => stringTransform a JavaScript/TypeScript file.
Return false from the callback to abort - the original content is returned unchanged.
(({ ast(parameter) ast: Program, js(parameter) js: typeof index_d_exports$3 }) => {
js(parameter) js: typeof index_d_exports$3.imports(alias) namespace index_d_exports$3.imports
export index_d_exports$3.imports.addDefault(alias) imports_d_exports.addDefault(node: Program, options: {
from: string;
as: string;
}): void
export imports_d_exports.addDefault(ast(parameter) ast: Program, { as(property) as: string: 'foo', from(property) from: string: 'foo' });
});
const resultconst result: string = transformconst transform: (content: string) => string('export default {}');Composabilité
Pour les cas nécessitant d’utiliser à la fois des transformations et des éditions brutes, utilisez
sv.file avec un callback de contenu, puis invoquer la transformation curryfiée manuellement :
svany.fileany(pathany, (content(parameter) content: any) => {
// curryfication
const transformconst transform: any = transformsany.scriptany(({ ast(parameter) ast: any, js(parameter) js: any }) => {
js(parameter) js: any.importsany.addDefaultany(ast(parameter) ast: any, { as(property) as: string: 'foo', from(property) from: string: 'bar' });
});
// manipulation du parser
content(parameter) content: any = transformconst transform: any(content(parameter) content: any);
// manipulation brute de string
content(parameter) content: any = content(parameter) content: any.replaceany('foo', 'baz');
return content(parameter) content: any;
});Les add-ons peuvent également exporter des fonctions de transformations réutilisables :
import { transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
} from '@sveltejs/sv-utils';
// ré-utilisable — exportez-la depuis votre paquet
export const addFooImportconst addFooImport: (content: string) => string = transforms(alias) const transforms: {
script(cb: (file: {
ast: Program;
comments: Comments;
content: string;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => string;
... 6 more ...;
text(cb: (file: {
content: string;
text: typeof text_d_exports;
}) => string | false): TransformFn;
}
import transformsFile transform primitives that know their format.
sv-utils = what to do to content, sv = where and when to do it.
Each transform wraps: parse -> callback({ast/data, utils}) -> generateCode().
The parser choice is baked into the transform type - you can't accidentally
parse a vite config as svelte because you never call a parser yourself.
Transforms are curried: call with the callback to get a (content: string) => string
function that plugs directly into sv.file().
import { transforms } from '@sveltejs/sv-utils';
// use with sv.file() - curried form plugs in directly
sv.file(files.viteConfig, transforms.script(({ ast, js }) => {
js.vite.addPlugin(ast, { code: 'kitRoutes()' });
}));
// standalone usage / testing
const result = transforms.script(({ ast, js }) => {
js.imports.addDefault(ast, { as: 'foo', from: 'foo' });
})(fileContent);
.svelte(method) svelte(cb: (file: {
ast: AST.Root;
content: string;
svelte: typeof index_d_exports$4;
js: typeof index_d_exports$3;
}) => void | false, options?: TransformOptions): (content: string) => stringTransform a Svelte component file.
Return false from the callback to abort - the original content is returned unchanged.
(({ ast(parameter) ast: AST.Root, svelte(parameter) svelte: typeof index_d_exports$4, js(parameter) js: typeof index_d_exports$3 }) => {
svelte(parameter) svelte: typeof index_d_exports$4.ensureScript(alias) index_d_exports$4.ensureScript(ast: AST.Root, options?: {
language?: "ts" | "js";
}): asserts ast is RootWithInstance
export index_d_exports$4.ensureScript(ast(parameter) ast: AST.Root, { language(property) language?: "js" | "ts" | undefined });
js(parameter) js: typeof index_d_exports$3.imports(alias) namespace index_d_exports$3.imports
export index_d_exports$3.imports.addDefault(alias) imports_d_exports.addDefault(node: Program, options: {
from: string;
as: string;
}): void
export imports_d_exports.addDefault(ast(parameter) ast: AST.Root.instance(property) AST.Root.instance: AST.Script | nullThe parsed <script> element, if exists
.content(property) AST.Script.content: Program, { as(property) as: string: 'Foo', from(property) from: string: './Foo.svelte' });
});svany.fileany('+page.svelte', addFooImportany);
svany.fileany('index.svelte', addFooImportany);Parsers (bas-niveau)
transforms devrait convenir à la plupart des besoins (par ex. parsing conditionnel, gestion
d’erreur autour du parser). Si ce n’est pas le cas pour vous, parse est une API bas-niveau à
laquelle vous pouvez accéder :
import { parse(alias) const parse: {
css: (source: string) => {
ast: Omit<_CSS.StyleSheetBase, "attributes" | "content">;
} & ParseBase;
html: (source: string) => {
ast: AST.Fragment;
} & ParseBase;
json: (source: string) => {
data: any;
} & ParseBase;
script: (source: string) => {
ast: Program;
comments: Comments;
} & ParseBase;
svelte: (source: string) => {
ast: AST.Root;
} & ParseBase;
toml: (source: string) => {
data: TomlTable;
} & ParseBase;
yaml: (source: string) => {
data: YamlDocument;
} & ParseBase;
}
import parseLow-level parsers. Prefer transforms for add-on file edits — it picks the
right parser for you and handles generateCode() automatically.
Use parse directly when you need error handling around parsing or
conditional parser selection at runtime.
import { parse } from '@sveltejs/sv-utils';
const { ast, generateCode } = parse.script('function add(a, b) { return a + b; }');
const { ast, generateCode } = parse.svelte('<div>Hello, world!</div>');
const { ast, generateCode } = parse.css('body { color: red; }');
const { data, generateCode } = parse.json('{ "name": "John", "age": 30 }');
const { data, generateCode } = parse.yaml('name: John');
const { data, generateCode } = parse.toml('name = "John"');
const { ast, generateCode } = parse.html('<div>Hello, world!</div>');
} from '@sveltejs/sv-utils';
const { astconst ast: Program, generateCodeconst generateCode: () => stringGenerate the code after manipulating the ast.
import { svelte } from 'sv/core';
const { ast, generateCode } = parse.svelte(content);
svelte.addFragment(ast, '<p>Hello World</p>');
const code = generateCode();
} = parse(alias) const parse: {
css: (source: string) => {
ast: Omit<_CSS.StyleSheetBase, "attributes" | "content">;
} & ParseBase;
html: (source: string) => {
ast: AST.Fragment;
} & ParseBase;
json: (source: string) => {
data: any;
} & ParseBase;
script: (source: string) => {
ast: Program;
comments: Comments;
} & ParseBase;
svelte: (source: string) => {
ast: AST.Root;
} & ParseBase;
toml: (source: string) => {
data: TomlTable;
} & ParseBase;
yaml: (source: string) => {
data: YamlDocument;
} & ParseBase;
}
import parseLow-level parsers. Prefer transforms for add-on file edits — it picks the
right parser for you and handles generateCode() automatically.
Use parse directly when you need error handling around parsing or
conditional parser selection at runtime.
import { parse } from '@sveltejs/sv-utils';
const { ast, generateCode } = parse.script('function add(a, b) { return a + b; }');
const { ast, generateCode } = parse.svelte('<div>Hello, world!</div>');
const { ast, generateCode } = parse.css('body { color: red; }');
const { data, generateCode } = parse.json('{ "name": "John", "age": 30 }');
const { data, generateCode } = parse.yaml('name: John');
const { data, generateCode } = parse.toml('name = "John"');
const { ast, generateCode } = parse.html('<div>Hello, world!</div>');
.script(property) script: (source: string) => {
ast: Program;
comments: Comments;
} & ParseBase(contentany);
const { astconst ast: AST.Root, generateCodeconst generateCode: () => stringGenerate the code after manipulating the ast.
import { svelte } from 'sv/core';
const { ast, generateCode } = parse.svelte(content);
svelte.addFragment(ast, '<p>Hello World</p>');
const code = generateCode();
} = parse(alias) const parse: {
css: (source: string) => {
ast: Omit<_CSS.StyleSheetBase, "attributes" | "content">;
} & ParseBase;
html: (source: string) => {
ast: AST.Fragment;
} & ParseBase;
json: (source: string) => {
data: any;
} & ParseBase;
script: (source: string) => {
ast: Program;
comments: Comments;
} & ParseBase;
svelte: (source: string) => {
ast: AST.Root;
} & ParseBase;
toml: (source: string) => {
data: TomlTable;
} & ParseBase;
yaml: (source: string) => {
data: YamlDocument;
} & ParseBase;
}
import parseLow-level parsers. Prefer transforms for add-on file edits — it picks the
right parser for you and handles generateCode() automatically.
Use parse directly when you need error handling around parsing or
conditional parser selection at runtime.
import { parse } from '@sveltejs/sv-utils';
const { ast, generateCode } = parse.script('function add(a, b) { return a + b; }');
const { ast, generateCode } = parse.svelte('<div>Hello, world!</div>');
const { ast, generateCode } = parse.css('body { color: red; }');
const { data, generateCode } = parse.json('{ "name": "John", "age": 30 }');
const { data, generateCode } = parse.yaml('name: John');
const { data, generateCode } = parse.toml('name = "John"');
const { ast, generateCode } = parse.html('<div>Hello, world!</div>');
.svelte(property) svelte: (source: string) => {
ast: AST.Root;
} & ParseBase(contentany);
const { astconst ast: Omit<_CSS.StyleSheetBase, "attributes" | "content">, generateCodeconst generateCode: () => stringGenerate the code after manipulating the ast.
import { svelte } from 'sv/core';
const { ast, generateCode } = parse.svelte(content);
svelte.addFragment(ast, '<p>Hello World</p>');
const code = generateCode();
} = parse(alias) const parse: {
css: (source: string) => {
ast: Omit<_CSS.StyleSheetBase, "attributes" | "content">;
} & ParseBase;
html: (source: string) => {
ast: AST.Fragment;
} & ParseBase;
json: (source: string) => {
data: any;
} & ParseBase;
script: (source: string) => {
ast: Program;
comments: Comments;
} & ParseBase;
svelte: (source: string) => {
ast: AST.Root;
} & ParseBase;
toml: (source: string) => {
data: TomlTable;
} & ParseBase;
yaml: (source: string) => {
data: YamlDocument;
} & ParseBase;
}
import parseLow-level parsers. Prefer transforms for add-on file edits — it picks the
right parser for you and handles generateCode() automatically.
Use parse directly when you need error handling around parsing or
conditional parser selection at runtime.
import { parse } from '@sveltejs/sv-utils';
const { ast, generateCode } = parse.script('function add(a, b) { return a + b; }');
const { ast, generateCode } = parse.svelte('<div>Hello, world!</div>');
const { ast, generateCode } = parse.css('body { color: red; }');
const { data, generateCode } = parse.json('{ "name": "John", "age": 30 }');
const { data, generateCode } = parse.yaml('name: John');
const { data, generateCode } = parse.toml('name = "John"');
const { ast, generateCode } = parse.html('<div>Hello, world!</div>');
.css(property) css: (source: string) => {
ast: Omit<_CSS.StyleSheetBase, "attributes" | "content">;
} & ParseBase(contentany);
const { dataconst data: any, generateCodeconst generateCode: () => stringGenerate the code after manipulating the ast.
import { svelte } from 'sv/core';
const { ast, generateCode } = parse.svelte(content);
svelte.addFragment(ast, '<p>Hello World</p>');
const code = generateCode();
} = parse(alias) const parse: {
css: (source: string) => {
ast: Omit<_CSS.StyleSheetBase, "attributes" | "content">;
} & ParseBase;
html: (source: string) => {
ast: AST.Fragment;
} & ParseBase;
json: (source: string) => {
data: any;
} & ParseBase;
script: (source: string) => {
ast: Program;
comments: Comments;
} & ParseBase;
svelte: (source: string) => {
ast: AST.Root;
} & ParseBase;
toml: (source: string) => {
data: TomlTable;
} & ParseBase;
yaml: (source: string) => {
data: YamlDocument;
} & ParseBase;
}
import parseLow-level parsers. Prefer transforms for add-on file edits — it picks the
right parser for you and handles generateCode() automatically.
Use parse directly when you need error handling around parsing or
conditional parser selection at runtime.
import { parse } from '@sveltejs/sv-utils';
const { ast, generateCode } = parse.script('function add(a, b) { return a + b; }');
const { ast, generateCode } = parse.svelte('<div>Hello, world!</div>');
const { ast, generateCode } = parse.css('body { color: red; }');
const { data, generateCode } = parse.json('{ "name": "John", "age": 30 }');
const { data, generateCode } = parse.yaml('name: John');
const { data, generateCode } = parse.toml('name = "John"');
const { ast, generateCode } = parse.html('<div>Hello, world!</div>');
.json(property) json: (source: string) => {
data: any;
} & ParseBase(contentany);
const { dataconst data: YamlDocument, generateCodeconst generateCode: () => stringGenerate the code after manipulating the ast.
import { svelte } from 'sv/core';
const { ast, generateCode } = parse.svelte(content);
svelte.addFragment(ast, '<p>Hello World</p>');
const code = generateCode();
} = parse(alias) const parse: {
css: (source: string) => {
ast: Omit<_CSS.StyleSheetBase, "attributes" | "content">;
} & ParseBase;
html: (source: string) => {
ast: AST.Fragment;
} & ParseBase;
json: (source: string) => {
data: any;
} & ParseBase;
script: (source: string) => {
ast: Program;
comments: Comments;
} & ParseBase;
svelte: (source: string) => {
ast: AST.Root;
} & ParseBase;
toml: (source: string) => {
data: TomlTable;
} & ParseBase;
yaml: (source: string) => {
data: YamlDocument;
} & ParseBase;
}
import parseLow-level parsers. Prefer transforms for add-on file edits — it picks the
right parser for you and handles generateCode() automatically.
Use parse directly when you need error handling around parsing or
conditional parser selection at runtime.
import { parse } from '@sveltejs/sv-utils';
const { ast, generateCode } = parse.script('function add(a, b) { return a + b; }');
const { ast, generateCode } = parse.svelte('<div>Hello, world!</div>');
const { ast, generateCode } = parse.css('body { color: red; }');
const { data, generateCode } = parse.json('{ "name": "John", "age": 30 }');
const { data, generateCode } = parse.yaml('name: John');
const { data, generateCode } = parse.toml('name = "John"');
const { ast, generateCode } = parse.html('<div>Hello, world!</div>');
.yaml(property) yaml: (source: string) => {
data: YamlDocument;
} & ParseBase(contentany);
const { dataconst data: TomlTable, generateCodeconst generateCode: () => stringGenerate the code after manipulating the ast.
import { svelte } from 'sv/core';
const { ast, generateCode } = parse.svelte(content);
svelte.addFragment(ast, '<p>Hello World</p>');
const code = generateCode();
} = parse(alias) const parse: {
css: (source: string) => {
ast: Omit<_CSS.StyleSheetBase, "attributes" | "content">;
} & ParseBase;
html: (source: string) => {
ast: AST.Fragment;
} & ParseBase;
json: (source: string) => {
data: any;
} & ParseBase;
script: (source: string) => {
ast: Program;
comments: Comments;
} & ParseBase;
svelte: (source: string) => {
ast: AST.Root;
} & ParseBase;
toml: (source: string) => {
data: TomlTable;
} & ParseBase;
yaml: (source: string) => {
data: YamlDocument;
} & ParseBase;
}
import parseLow-level parsers. Prefer transforms for add-on file edits — it picks the
right parser for you and handles generateCode() automatically.
Use parse directly when you need error handling around parsing or
conditional parser selection at runtime.
import { parse } from '@sveltejs/sv-utils';
const { ast, generateCode } = parse.script('function add(a, b) { return a + b; }');
const { ast, generateCode } = parse.svelte('<div>Hello, world!</div>');
const { ast, generateCode } = parse.css('body { color: red; }');
const { data, generateCode } = parse.json('{ "name": "John", "age": 30 }');
const { data, generateCode } = parse.yaml('name: John');
const { data, generateCode } = parse.toml('name = "John"');
const { ast, generateCode } = parse.html('<div>Hello, world!</div>');
.toml(property) toml: (source: string) => {
data: TomlTable;
} & ParseBase(contentany);
const { astconst ast: AST.Fragment, generateCodeconst generateCode: () => stringGenerate the code after manipulating the ast.
import { svelte } from 'sv/core';
const { ast, generateCode } = parse.svelte(content);
svelte.addFragment(ast, '<p>Hello World</p>');
const code = generateCode();
} = parse(alias) const parse: {
css: (source: string) => {
ast: Omit<_CSS.StyleSheetBase, "attributes" | "content">;
} & ParseBase;
html: (source: string) => {
ast: AST.Fragment;
} & ParseBase;
json: (source: string) => {
data: any;
} & ParseBase;
script: (source: string) => {
ast: Program;
comments: Comments;
} & ParseBase;
svelte: (source: string) => {
ast: AST.Root;
} & ParseBase;
toml: (source: string) => {
data: TomlTable;
} & ParseBase;
yaml: (source: string) => {
data: YamlDocument;
} & ParseBase;
}
import parseLow-level parsers. Prefer transforms for add-on file edits — it picks the
right parser for you and handles generateCode() automatically.
Use parse directly when you need error handling around parsing or
conditional parser selection at runtime.
import { parse } from '@sveltejs/sv-utils';
const { ast, generateCode } = parse.script('function add(a, b) { return a + b; }');
const { ast, generateCode } = parse.svelte('<div>Hello, world!</div>');
const { ast, generateCode } = parse.css('body { color: red; }');
const { data, generateCode } = parse.json('{ "name": "John", "age": 30 }');
const { data, generateCode } = parse.yaml('name: John');
const { data, generateCode } = parse.toml('name = "John"');
const { ast, generateCode } = parse.html('<div>Hello, world!</div>');
.html(property) html: (source: string) => {
ast: AST.Fragment;
} & ParseBase(contentany);Outillage de langage
Les utilitaires de namespace pour la manipulation d’AST :
js.*- imports, exports, objets, tableaux, variables, fonctions, utilitaires de configuration Vite, utilitaires SvelteKitcss.*- règles, déclarations, at-rules, importssvelte.*- ensureScript, addSlot, addFragmentjson.*- arrayUpsert, packageScriptsUpserthtml.*- manipulation d’attributstext.*- insertion de lignes dans les fichiers bruts (.env, .gitignore)
Configuration de Svelte
La configuration de Svelte/kit peut se définir à deux endroits : directement fournie au plugin
sveltekit() dans le fichier vite.config.{js,ts}, ou en tant qu’export par défaut dans un fichier
séparé svelte.config.{js,ts}. Les projets créés par sv gardent leur configuration dans le
fichier vite.config.js et ne fournissent pas de svelte.config.js.
svelteConfig permet au add-ons de lire et de modifier cette configuration peu importe sa position
— l’argument sveltekit() dans vite.config.{js,ts}, ou l’export par défaut
svelte.config.{js,ts} — sans avoir besoin de la connaître.
svelteConfig.edit
Vous gérez les options par nom, et l’utilitaire les écrit chacune au bon endroit, de sorte que vous
n’avez pas besoin de gérer l’imbrication de kit vous-même. Les options se trouvant au niveau de
Svelte (compilerOptions, preprocess, extensions, vitePlugin) se trouvent dans l’objet de
configuration ; tout le reste (adapter, alias, files, typescript, …) est une option de
SvelteKit, ce qui implique qu’elles sont aplaties dans l’argument de sveltekit() dans une
configuration Vite, ou imbriqués sous kit dans un svelte.config.
import { svelteConfig(alias) const svelteConfig: {
edit: (target: {
sv: SvFileApi;
cwd: string;
}, editFn: SvelteConfEdit) => void;
find: (source: ConfigSource) => SvelteConfigLocation | null;
read: (source: ConfigSource) => SvelteConfigObjects | null;
}
import svelteConfigHelpers for the svelte/kit config, which can live either in a svelte.config.{js,ts} default
export or in the object passed to sveltekit() in a vite.config.{js,ts}.
} from '@sveltejs/sv-utils';
// dans le `run({ sv, cwd })` d'un add-on
svelteConfig(alias) const svelteConfig: {
edit: (target: {
sv: SvFileApi;
cwd: string;
}, editFn: SvelteConfEdit) => void;
find: (source: ConfigSource) => SvelteConfigLocation | null;
read: (source: ConfigSource) => SvelteConfigObjects | null;
}
import svelteConfigHelpers for the svelte/kit config, which can live either in a svelte.config.{js,ts} default
export or in the object passed to sveltekit() in a vite.config.{js,ts}.
.edit(property) edit: (target: {
sv: SvFileApi;
cwd: string;
}, editFn: SvelteConfEdit) => voidEdit the config wherever it lives (creating svelte.config.js if there is none).
({ sv(property) sv: SvFileApi, cwd(property) cwd: string }, ({ ast(parameter) ast: Program, property(parameter) property: <T extends Expression | Identifier>(name: string, opts: {
fallback: T;
}) => TGet-or-create a top-level config option's value, placed in the correct location for its name
(kit-level options end up under kit in a svelte.config, flattened in a vite.config).
, override(parameter) override: (props: ObjectMap$1, opts?: {
dropLeadingComments?: string[];
}) => voidSet/override top-level config options, each routed to the correct location by its name.
Pass dropLeadingComments with option names whose now-stale leading comments should be removed
(e.g. the adapter-auto note when switching adapters).
, js(parameter) js: typeof index_d_exports$3 }) => {
// l'option du niveau svelte — récupère ou crée sa valeur, puis la mute sur place :
js(parameter) js: typeof index_d_exports$3.array(alias) namespace index_d_exports$3.array
export index_d_exports$3.array.append(alias) array_d_exports.append(node: ArrayExpression, element: string | Expression | SpreadElement): void
export array_d_exports.append(property(parameter) property: <ArrayExpression>(name: string, opts: {
fallback: ArrayExpression;
}) => ArrayExpressionGet-or-create a top-level config option's value, placed in the correct location for its name
(kit-level options end up under kit in a svelte.config, flattened in a vite.config).
('extensions', { fallback(property) fallback: ArrayExpression: js(parameter) js: typeof index_d_exports$3.array(alias) namespace index_d_exports$3.array
export index_d_exports$3.array.create(alias) array_d_exports.create(): ArrayExpression
export array_d_exports.create() }), '.svx');
// l'option kit — positionnée automatiquement, pas de gestion de l'étage `kit` :
js(parameter) js: typeof index_d_exports$3.imports(alias) namespace index_d_exports$3.imports
export index_d_exports$3.imports.addDefault(alias) imports_d_exports.addDefault(node: Program, options: {
from: string;
as: string;
}): void
export imports_d_exports.addDefault(ast(parameter) ast: Program, { from(property) from: string: '@sveltejs/adapter-node', as(property) as: string: 'adapter' });
override(parameter) override: (props: ObjectMap$1, opts?: {
dropLeadingComments?: string[];
}) => voidSet/override top-level config options, each routed to the correct location by its name.
Pass dropLeadingComments with option names whose now-stale leading comments should be removed
(e.g. the adapter-auto note when switching adapters).
({
adapter(property) adapter: CallExpression: js(parameter) js: typeof index_d_exports$3.functions(alias) namespace index_d_exports$3.functions
export index_d_exports$3.functions.createCall(alias) function_d_exports.createCall(options: {
name: string;
args: string[];
useIdentifiers?: boolean;
}): CallExpression
export function_d_exports.createCall({ name(property) name: string: 'adapter', args(property) args: string[]: [], useIdentifiers(property) useIdentifiers?: boolean | undefined: true })
});
});property(name, { fallback })— récupère ou crée la valeur d’une option pour ensuite la muter sur place (tableaux, objets imbriqués).override(props, { dropLeadingComments })— définit ou remplace des optionsdropLeadingCommentssupprime un commentaire considéré comme devenu inutile (par ex. la note sur adapter-auto lorsque vous changez d’adaptateur).
Cette méthode écrit dans sv.file, de sorte que la modification est suivi comme n’importe quelle
autre. Si le projet n’a aucun fichier de configuration, un svelte.config.js est créé.
svelteConfig.find / svelteConfig.read
Les blocs de construction de plus bas niveau, les deux lisant des fichiers potentiels au travers
d’un read(path) injecté (renvoie le contenu du fichier ou null) de sorte que la détection reste
statique — la configuration n’est jamais exécutée :
svelteConfig.find(read)— renvoie{ path, kind }ounull(kindvaut'vite'ou'svelte';svelte.configgagne lorsque les deux sont présents).svelteConfig.read(read)— localise et parse en une passe, renvoyant{ location, config, kit }(les expressions de l’objet) ounull.
Utilitaires de gestion de paquets
pnpm.allowBuilds
Renvoie une transformation pour pnpm-workspace.yaml qui ajoute des paquets à la config “allow
builds”. À utiliser avec sv.files lorsque le projet utilise pnpm.
L’utilitaire détecte la version de pnpm actuellement installée via pnpm --version :
- pnpm
>= 11: écrit dans la map unifiéeallowBuilds({ pkg: true }), en migrant toute listeonlyBuiltDependenciesvers la map. - pnpm
< 11: écrit dans la listeonlyBuiltDependencieslegacy.
import { pnpm(alias) namespace pnpm
import pnpm } from '@sveltejs/sv-utils';
if (packageManagerany === 'pnpm') {
svany.fileany(fileany.findUpany('pnpm-workspace.yaml'), pnpm(alias) namespace pnpm
import pnpm.allowBuilds(alias) pnpm_d_exports.allowBuilds(...packages: string[]): TransformFn
export pnpm_d_exports.allowBuildsReturns a TransformFn for pnpm-workspace.yaml that adds packages to the
pnpm "allow builds" config.
The helper detects the installed pnpm version (via pnpm --version) and:
- on pnpm
>= 11 writes to the unified allowBuilds map ({ pkg: true }),
migrating any legacy onlyBuiltDependencies list into the map;
- on pnpm
< 11 writes to the legacy onlyBuiltDependencies list.
if (packageManager === 'pnpm') {
sv.file(file.findUp('pnpm-workspace.yaml'), pnpm.allowBuilds('my-native-dep'));
}
('my-native-dep'));
}Modifier cette page sur Github llms.txt