Skip to content

Commit beb3f65

Browse files
authored
docs(site-2): CJS to ESM for code snippets (#8449)
1 parent 050c103 commit beb3f65

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

site/content/docs/04-compiler-and-api/01-svelte-compiler.md

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ result: {
2222
This is where the magic happens. `svelte.compile` takes your component source code, and turns it into a JavaScript module that exports a class.
2323

2424
```js
25-
const svelte = require('svelte/compiler');
25+
import svelte from 'svelte/compiler';
2626

2727
const result = svelte.compile(source, {
2828
// options
@@ -160,7 +160,7 @@ ast: object = svelte.parse(
160160
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.
161161

162162
```js
163-
const svelte = require('svelte/compiler');
163+
import svelte from 'svelte/compiler';
164164

165165
const ast = svelte.parse(source, { filename: 'App.svelte' });
166166
```
@@ -208,8 +208,8 @@ The `markup` function receives the entire component source text, along with the
208208
> Preprocessor functions should additionally return a `map` object alongside `code` and `dependencies`, where `map` is a sourcemap representing the transformation.
209209
210210
```js
211-
const svelte = require('svelte/compiler');
212-
const MagicString = require('magic-string');
211+
import svelte from 'svelte/compiler';
212+
import MagicString from 'magic-string';
213213

214214
const { code } = await svelte.preprocess(
215215
source,
@@ -235,37 +235,27 @@ const { code } = await svelte.preprocess(
235235

236236
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.
237237

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).
239239

240240
```js
241-
const svelte = require('svelte/compiler');
242-
const sass = require('node-sass');
243-
const { dirname } = require('path');
241+
import { preprocess } from 'svelte/compiler';
242+
import sass from 'sass';
243+
import { dirname } from 'path';
244244

245-
const { code, dependencies } = await svelte.preprocess(
245+
const { code, dependencies } = await preprocess(
246246
source,
247247
{
248248
style: async ({ content, attributes, filename }) => {
249249
// only process <style lang="sass">
250-
if (attributes.lang !== 'sass') return;
251-
252-
const { css, stats } = await new Promise((resolve, reject) =>
253-
sass.render(
254-
{
255-
file: filename,
256-
data: content,
257-
includePaths: [dirname(filename)]
258-
},
259-
(err, result) => {
260-
if (err) reject(err);
261-
else resolve(result);
262-
}
263-
)
264-
);
250+
if (attributes.lang !== 'sass' && attributes.lang !== 'scss') return;
251+
252+
const { css, loadedUrls } = await sass.compileStringAsync(content, {
253+
loadPaths: [dirname(filename)]
254+
});
265255

266256
return {
267-
code: css.toString(),
268-
dependencies: stats.includedFiles
257+
code: css,
258+
dependencies: loadedUrls
269259
};
270260
}
271261
},
@@ -278,7 +268,7 @@ const { code, dependencies } = await svelte.preprocess(
278268
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`.
279269

280270
```js
281-
const svelte = require('svelte/compiler');
271+
import svelte from 'svelte/compiler';
282272

283273
const { code } = await svelte.preprocess(
284274
source,
@@ -326,7 +316,8 @@ The `walk` function provides a way to walk the abstract syntax trees generated b
326316
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.
327317

328318
```js
329-
const svelte = require('svelte/compiler');
319+
import svelte from 'svelte/compiler';
320+
330321
svelte.walk(ast, {
331322
enter(node, parent, prop, index) {
332323
do_something(node);
@@ -345,6 +336,6 @@ svelte.walk(ast, {
345336
The current version, as set in package.json.
346337

347338
```js
348-
const svelte = require('svelte/compiler');
339+
import svelte from 'svelte/compiler';
349340
console.log(`running svelte version ${svelte.VERSION}`);
350341
```

0 commit comments

Comments
 (0)