Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sveltejs/svelte-preprocess
A ✨ magical ✨ Svelte preprocessor with sensible defaults and support for: PostCSS, SCSS, Less, Stylus, Coffeescript, TypeScript, Pug and much more.
https://github.com/sveltejs/svelte-preprocess
preprocess svelte sveltejs sveltekit
Last synced: 4 days ago
JSON representation
A ✨ magical ✨ Svelte preprocessor with sensible defaults and support for: PostCSS, SCSS, Less, Stylus, Coffeescript, TypeScript, Pug and much more.
- Host: GitHub
- URL: https://github.com/sveltejs/svelte-preprocess
- Owner: sveltejs
- License: mit
- Created: 2018-05-09T17:43:29.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-09-26T12:06:44.000Z (3 months ago)
- Last Synced: 2024-10-29T14:31:51.543Z (about 1 month ago)
- Topics: preprocess, svelte, sveltejs, sveltekit
- Language: TypeScript
- Homepage:
- Size: 3.25 MB
- Stars: 1,755
- Watchers: 18
- Forks: 151
- Open Issues: 74
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - svelte-preprocess
- awesome-made-by-brazilians - svelte-preprocess
- awesome-svelte - svelte-preprocess - A preprocessor for PostCSS, SCSS, Less, Stylus, Coffeescript, TypeScript, Pug and much more. (Integrations / Preprocessing)
README
# Svelte Preprocess
> A [Svelte](https://svelte.dev) preprocessor with sensible defaults and support for: PostCSS, SCSS, Less, Stylus, CoffeeScript, TypeScript, Pug and much more.
- [What is it?](#what-is-it)
- [Getting Started](docs/getting-started.md)
- [Usage](docs/usage.md)
- [Migration Guide](docs/migration-guide.md)
- [Preprocessing](docs/preprocessing.md)
- [Preprocessors](docs/preprocessing.md#preprocessors)
- [Features](#features)
- [Template tag](#template-tag)
- [External files](#external-files)
- [Global style](#global-style)
- [Modern JavaScript syntax](#modern-javascript-syntax)
- [Replace values](#replace-values)
- [Built-in support for commonly used languages](#built-in-support-for-commonly-used-languages)## What is it?
`Svelte`'s own parser understands only JavaScript, CSS and its HTML-like syntax. To make it possible to write components in other languages, such as TypeScript or SCSS, `Svelte` provides the [preprocess API](https://svelte.dev/docs#compile-time-svelte-preprocess), which allows to easily transform the content of your `markup` and your `style`/`script` tags.
Writing your own preprocessor for, i.e SCSS is easy enough, but it can be cumbersome to have to always configure multiple preprocessors for the languages you'll be using.
`svelte-preprocess` is a custom svelte preprocessor that acts as a facilitator to use other languages with Svelte, providing multiple features, sensible defaults and a less noisy development experience.
It is recommended to use with `svelte.config.js` file, located at the project root. For other usage, please refer to [usage documentation](#usage-documentation).
```js
import { sveltePreprocess } from 'svelte-preprocess';const config = {
preprocess: sveltePreprocess({ ... })
}export default config;
```## Features
### Template tag
_Vue-like_ support for defining your markup between a specific tag. The default tag is `template` but it can be [customized](/docs/preprocessing.md#auto-preprocessing-options).
```html
Hey```
### External files
```html
```
> Note: using a relative path starting with `.` is important. Otherwise `svelte-preprocess` will ignore the `src` attribute.
### Global style
#### `global` attribute
Add a `global` attribute to your `style` tag and instead of scoping the CSS, all of its content will be interpreted as global style.
```html
div {
color: red;
}```
#### `:global` rule
Use a `:global` rule to only expose parts of the stylesheet:
```html
.scoped-style {
}:global {
@import 'global-stylesheet.scss';.global-style {
.global-child-style {
}
}
}```
Works best with nesting-enabled CSS preprocessors, but regular CSS selectors like `div :global .global1 .global2` are also supported.
_**Note**: needs PostCSS to be installed._
### Modern JavaScript syntax
`svelte-preprocess` allows you to run your component code through Babel before sending it to the compiler, allowing you to use new language features such as optional operators and nullish coalescing. However, note that Babel should transpile your component code to the javascript version supported by the Svelte compiler, so ES6+.
For example, with `@babel/preset-env` your config could be:
```js
import { sveltePreprocess } from 'svelte-preprocess'
...
preprocess: sveltePreprocess({
babel: {
presets: [
[
'@babel/preset-env',
{
loose: true,
// No need for babel to resolve modules
modules: false,
targets: {
// ! Very important. Target es6+
esmodules: true,
},
},
],
],
},
});
...
```_**Note:** If you want to transpile your app to be supported in older browsers, you must run babel from the context of your bundler._
### Replace values
Replace a set of string patterns in your components markup by passing an array of `[RegExp, ReplaceFn | string]`, the same arguments received by the `String.prototype.replace` method.
In example, to replace inject the value of `process.env.NODE_ENV`:
```js
autoPreprocess({
replace: [[/process\.env\.NODE_ENV/g, JSON.stringify(process.env.NODE_ENV)]],
});
```Which, in a production environment, would turn
```svelte
{#if process.env.NODE_ENV !== 'development'}
Production environment!
{/if}
```into
```svelte
{#if 'production' !== 'development'}
Production environment!
{/if}
```### Built-in support for commonly used languages
The current supported languages out-of-the-box are Sass, Stylus, Less, CoffeeScript, TypeScript, Pug, PostCSS, Babel.
```html
div Posts +each('posts as post') a(href="{post.url}") {post.title}
export const hello: string = 'world';
```
---
### [Getting started](/docs/getting-started.md)
### [Preprocessing documentation](/docs/preprocessing.md)
### [Usage documentation](/docs/usage.md)
### [Migration Guide](/docs/migration-guide.md)
---
## License
[MIT](LICENSE)