Skip to content
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

Merged
merged 4 commits into from
Apr 3, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions site/content/docs/04-compiler-and-api/01-svelte-compiler.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' });
```
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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).
Copy link
Collaborator Author

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


```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';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's what svelte-preprocess does, so I guess it wouldn't hurt to change for consistency: https://github.com/sveltejs/svelte-preprocess/blob/537b97536c2a48574ca748dccf3fcc0dc6d3ffc5/src/transformers/scss.ts

Copy link
Collaborator Author

@PuruVJ PuruVJ Apr 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

svelte preprocess uses a deprecated API of sass, so I tested on staclkblitz with new compileStringAsync API, works, so gonna keep that here https://stackblitz.com/edit/node-ncrbch?file=index.js

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you should send it as a PR to svelte-preprocess as well 😄

import { dirname } from 'path';

const { code, dependencies } = await svelte.preprocess(
source,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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}`);
```