You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: site/content/docs/04-compiler-and-api/01-svelte-compiler.md
+20-29Lines changed: 20 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ result: {
22
22
This is where the magic happens. `svelte.compile` takes your component source code, and turns it into a JavaScript module that exports a class.
23
23
24
24
```js
25
-
constsvelte=require('svelte/compiler');
25
+
importsveltefrom'svelte/compiler';
26
26
27
27
constresult=svelte.compile(source, {
28
28
// options
@@ -160,7 +160,7 @@ ast: object = svelte.parse(
160
160
The `parse` function parses a component, returning only its abstract syntax tree. Unlike compiling with the `generate: false` option, this will not perform any validation or other analysis of the component beyond parsing it. Note that the returned AST is not considered public API, so breaking changes could occur at any point in time.
@@ -208,8 +208,8 @@ The `markup` function receives the entire component source text, along with the
208
208
> Preprocessor functions should additionally return a `map` object alongside `code` and `dependencies`, where `map` is a sourcemap representing the transformation.
The `script` and `style` functions receive the contents of `<script>` and `<style>` elements respectively (`content`) as well as the entire component source text (`markup`). In addition to `filename`, they get an object of the element's attributes.
237
237
238
-
If a `dependencies` array is returned, it will be included in the result object. This is used by packages like [rollup-plugin-svelte](https://github.com/sveltejs/rollup-plugin-svelte) to watch additional files for changes, in the case where your `<style>` tag has an `@import` (for example).
238
+
If a `dependencies` array is returned, it will be included in the result object. This is used by packages like [vite-plugin-svelte](https://github.com/sveltejs/vite-plugin-svelte) and [rollup-plugin-svelte](https://github.com/sveltejs/rollup-plugin-svelte) to watch additional files for changes, in the case where your `<style>` tag has an `@import` (for example).
Multiple preprocessors can be used together. The output of the first becomes the input to the second. `markup` functions run first, then `script` and `style`.
279
269
280
270
```js
281
-
constsvelte=require('svelte/compiler');
271
+
importsveltefrom'svelte/compiler';
282
272
283
273
const { code } =awaitsvelte.preprocess(
284
274
source,
@@ -326,7 +316,8 @@ The `walk` function provides a way to walk the abstract syntax trees generated b
326
316
The walker takes an abstract syntax tree to walk and an object with two optional methods: `enter` and `leave`. For each node, `enter` is called (if present). Then, unless `this.skip()` is called during `enter`, each of the children are traversed, and then `leave` is called on the node.
327
317
328
318
```js
329
-
constsvelte=require('svelte/compiler');
319
+
importsveltefrom'svelte/compiler';
320
+
330
321
svelte.walk(ast, {
331
322
enter(node, parent, prop, index) {
332
323
do_something(node);
@@ -345,6 +336,6 @@ svelte.walk(ast, {
345
336
The current version, as set in package.json.
346
337
347
338
```js
348
-
constsvelte=require('svelte/compiler');
339
+
importsveltefrom'svelte/compiler';
349
340
console.log(`running svelte version ${svelte.VERSION}`);
0 commit comments