sveltejs/svelte-preprocess
소개
합리적인 기본 설정을 제공하고 PostCSS, SCSS, Less, Stylus, CoffeeScript, TypeScript, Pug 등을 지원하는 ✨마법 같은✨ Svelte 전처리기입니다.
웹 및 앱 개발측정된 스타 증가
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- -1
- in 1 month
- -2
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
OriginalSvelte Preprocess
A Svelte preprocessor with sensible defaults and support for: PostCSS, SCSS, Less, Stylus, CoffeeScript, TypeScript, Pug and much more.
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, 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.
import { sveltePreprocess } from 'svelte-preprocess';
const config = {
preprocess: sveltePreprocess({ ... })
}
export default config;
When to use it?
Historically, svelte-preprocess was the go-to option whenever you had a language other than JS/HTML/CSS used inside your Svelte files. Today, depending on your setup, you may no longer need it:
- If you're using Svelte 5 and are only using type features of TypeScript (i.e. things that just "go away" after transpiling to JS), you don't need any preprocessor, Svelte handles this natively
- If you're using Vite and are using TypeScript or common CSS preprocessors (PostCSS, SCSS, Less, ...), use the preprocessor that comes with vite-plugin-svelte instead
If these cases don't apply to you, use svelte-preprocess.
Features
Template tag
Vue-like support for defining your markup between a specific tag. The default tag is template but it can be customized.
<template>
<div>Hey</div>
</template>
<style></style>
<script></script>
External files
<template src="./template.html"></template>
<script src="./script.js"></script>
<style src="./style.css"></style>
Note: using a relative path starting with
.is important. Otherwisesvelte-preprocesswill ignore thesrcattribute.
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.
<style global>
div {
color: red;
}
</style>
:global rule
Use a :global rule to only expose parts of the stylesheet:
<style lang="scss">
.scoped-style {
}
:global {
@import 'global-stylesheet.scss';
.global-style {
.global-child-style {
}
}
}
</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:
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:
autoPreprocess({
replace: [[/process\.env\.NODE_ENV/g, JSON.stringify(process.env.NODE_ENV)]],
});
Which, in a production environment, would turn
{#if process.env.NODE_ENV !== 'development'}
<h1>Production environment!</h1>
{/if}
into
{#if 'production' !== 'development'}
<h1>Production environment!</h1>
{/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.
<template lang="pug">
div Posts +each('posts as post') a(href="{post.url}") {post.title}
</template>
<script lang="ts">
export const hello: string = 'world';
</script>
<style src="./style.scss"></style>
Getting started
Preprocessing documentation
Usage documentation
Migration Guide
License
관련 프로젝트 살펴보기
불러오는 중…