feat: add print(...)
function
Over on sveltejs/esrap#68 we're working on making esrap
pluggable, so that it can be used to print any AST composed of { type: string, ... }
nodes rather than just estree
and its TypeScript extensions. That includes Svelte ASTs.
The main motivation for exposing this is so that we can make it easier to write preprocessors. Historically, Svelte exposed a preprocess
API, but it's all strings and duct tape, and it's difficult to integrate preprocessors cleanly with bundlers as we've seen with enhanced-img
.
When the preprocessor API was introduced, things looked very different. Preprocessing was necessary to support things like TypeScript and Sass. Today, TypeScript is supported natively, and CSS is sufficiently capable that Sass is little more than a historical curiosity.
In the long term, we'd therefore like to move away from the preprocessor API in favour of providing more robust lower-level utilities. For example enhanced-img
, which can only be used in a Vite context, really should just be a Vite plugin:
import { parse, print } from 'svelte/compiler';
import { walk } from 'zimmerframe';
function transform(code) {
const ast = parse(code);
const transformed = walk(ast, null, {
RegularElement(node, context) {
if (node.name !== 'enhanced:img') return;
// ...
}
});
return print(ast);
}
There are other potential uses, such as migrations or sv add
or having a 'format' button in the playground.
As a side-effect, quality of compiler output will be slightly better in certain cases, such as when encountering comments inside nodes. (Today, we attach leadingComments
and trailingComments
to each node, but this is a brittle and not-very-widely-used convention. The new esrap
API expects an array of comments
to be passed instead.)
This functionality already exists in svelte-ast-print
(thank you @xeho91!), but having it in core means we can re-use the esrap
version that's already installed alongside svelte
, and will help ensure it stays current with new features.
feat:
, fix:
, chore:
, or docs:
.packages/svelte/src
, add a changeset (npx changeset
).pnpm test
and lint the project with pnpm lint
Latest commit: 1377c40
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
Nice!
I'll be happy to sunset svelte-ast-print
in favour of built-in into the core print()
function. 👍
I'm glad the need for it finally reached this point. I'm swamped right now with my real-life stuff, but just in case, both @manuel3108 and @paoloricciuti have access to the svelte-ast-print
repository in case I don't manage to announce it on time.
pnpm add https://pkg.pr.new/svelte@16188