Join our webinar on Wednesday, June 26, at 1pm EDTHow Chia Mitigates Risk in the Crypto Industry.Register
Socket
Socket
Sign inDemoInstall

svelte-preprocess

Package Overview
Dependencies
250
Maintainers
3
Versions
173
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    svelte-preprocess

A Svelte preprocessor wrapper with baked-in support for commonly used preprocessors


Version published
Weekly downloads
327K
decreased by-18.59%
Maintainers
3
Install size
109 kB
Created
Weekly downloads
 

Package description

What is svelte-preprocess?

svelte-preprocess is a versatile preprocessor for Svelte that allows you to use various languages and tools in your Svelte components. It supports languages like TypeScript, SCSS, Less, and PostCSS, among others, and can be configured to handle multiple preprocessors simultaneously.

What are svelte-preprocess's main functionalities?

TypeScript

Allows you to write your Svelte component scripts in TypeScript, providing type safety and other TypeScript features.

<script lang="ts">
  let message: string = 'Hello, TypeScript!';
</script>

SCSS

Enables you to use SCSS for styling your Svelte components, allowing for variables, nesting, and other SCSS features.

<style lang="scss">
  $primary-color: #ff3e00;
  h1 {
    color: $primary-color;
  }
</style>

PostCSS

Allows you to use PostCSS with your Svelte components, enabling features like Tailwind CSS and other PostCSS plugins.

<style lang="postcss">
  :global(body) {
    @apply bg-gray-100;
  }
</style>

Pug

Enables you to use Pug (formerly Jade) for your Svelte component templates, providing a more concise syntax for writing HTML.

<template lang="pug">
  h1 Hello, Pug!
</template>

Less

Allows you to use Less for styling your Svelte components, providing features like variables and mixins.

<style lang="less">
  @primary-color: #ff3e00;
  h1 {
    color: @primary-color;
  }
</style>

Other packages similar to svelte-preprocess

Changelog

Source

6.0.1 (2024-06-14)

Bug Fixes

  • deprecate default export in favor of named export (#641) (a43de10), closes #591

BREAKING CHANGES

  • remove TS mixed imports support, require TS 5.0 or higher
  • remove preserve option as it's unnecessary
  • require Svelte 4+, Node 18+
  • add exports map

Bug Fixes

  • adjust globalifySelector to not split selectors with parentheses. (#632) (c435ebd), closes #501
  • fix: allow TS filename to be undefined, fixes #488
  • fix: adjust Svelte compiler type import
  • fix: remove pug types and magic-string from dependencies
  • chore: bump peer deps, fixes #553

Readme

Source

Svelte Preprocess

A Svelte preprocessor with sensible defaults and support for: PostCSS, SCSS, Less, Stylus, CoffeeScript, TypeScript, Pug and much more.

npm version license action-CI

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;

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. 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.

<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

MIT

Keywords

FAQs

Last updated on 14 Jun 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc