-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(site-2): CJS to ESM for code snippets #8449
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ result: { | |
This is where the magic happens. `svelte.compile` takes your component source code, and turns it into a JavaScript module that exports a class. | ||
|
||
```js | ||
const svelte = require('svelte/compiler'); | ||
import svelte from 'svelte/compiler'; | ||
|
||
const result = svelte.compile(source, { | ||
// options | ||
|
@@ -160,7 +160,7 @@ ast: object = svelte.parse( | |
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. | ||
|
||
```js | ||
const svelte = require('svelte/compiler'); | ||
import svelte from 'svelte/compiler'; | ||
|
||
const ast = svelte.parse(source, { filename: 'App.svelte' }); | ||
``` | ||
|
@@ -208,8 +208,8 @@ The `markup` function receives the entire component source text, along with the | |
> Preprocessor functions should additionally return a `map` object alongside `code` and `dependencies`, where `map` is a sourcemap representing the transformation. | ||
|
||
```js | ||
const svelte = require('svelte/compiler'); | ||
const MagicString = require('magic-string'); | ||
import svelte from 'svelte/compiler'; | ||
import MagicString from 'magic-string'; | ||
|
||
const { code } = await svelte.preprocess( | ||
source, | ||
|
@@ -238,9 +238,9 @@ The `script` and `style` functions receive the contents of `<script>` and `<styl | |
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). | ||
|
||
```js | ||
const svelte = require('svelte/compiler'); | ||
const sass = require('node-sass'); | ||
const { dirname } = require('path'); | ||
import svelte from 'svelte/compiler'; | ||
import sass from 'node-sass'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we maybe update this to use dart-sass? Recommending something as obnoxious as node-sass can't be a good impression There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. svelte preprocess uses a deprecated API of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe you should send it as a PR to |
||
import { dirname } from 'path'; | ||
|
||
const { code, dependencies } = await svelte.preprocess( | ||
source, | ||
|
@@ -278,7 +278,7 @@ const { code, dependencies } = await svelte.preprocess( | |
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`. | ||
|
||
```js | ||
const svelte = require('svelte/compiler'); | ||
import svelte from 'svelte/compiler'; | ||
|
||
const { code } = await svelte.preprocess( | ||
source, | ||
|
@@ -326,7 +326,8 @@ The `walk` function provides a way to walk the abstract syntax trees generated b | |
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. | ||
|
||
```js | ||
const svelte = require('svelte/compiler'); | ||
import svelte from 'svelte/compiler'; | ||
|
||
svelte.walk(ast, { | ||
enter(node, parent, prop, index) { | ||
do_something(node); | ||
|
@@ -345,6 +346,6 @@ svelte.walk(ast, { | |
The current version, as set in package.json. | ||
|
||
```js | ||
const svelte = require('svelte/compiler'); | ||
import svelte from 'svelte/compiler'; | ||
console.log(`running svelte version ${svelte.VERSION}`); | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about vite-plugin-svelte?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it appears to use it as well: https://github.com/sveltejs/vite-plugin-svelte/blob/5e9ec7023c465e023f227e1ef01c76b74ce9f3ee/packages/vite-plugin-svelte/src/utils/compile.ts#L94