Esbuild Svelte screenshot

Esbuild Svelte

Author Avatar Theme by Emh333
Updated: 14 Mar 2025
254 Stars

An esbuild plugin to compile Svelte components

Categories

Overview

The esbuild-svelte plugin is a tool that allows developers to compile Svelte components for bundling with esbuild. It provides a simple build script for getting started quickly and supports features such as CSS output, Typescript and other Svelte preprocessing, importing Svelte component libraries, and advanced caching for incremental or watch builds. The plugin also has a comprehensive API that includes support for Svelte’s compiler options and preprocessing API.

Features

  • Compile Svelte components: The plugin allows developers to compile Svelte components for bundling with esbuild.
  • CSS output: By default, the plugin emits external CSS files from Svelte for esbuild to process. However, developers can configure it to include CSS in the JavaScript output instead.
  • Typescript and other Svelte preprocessing: The plugin supports Typescript and other languages that require preprocessing before being fed into the Svelte compiler. Developers can easily add preprocessors to the plugin’s configuration.
  • Svelte component library support: When importing a Svelte component library, developers need to add “svelte” to the conditions in esbuild build options. This allows esbuild to use the svelte property in the library’s exports conditions in package.json.
  • Advanced caching: For incremental or watch build modes, esbuild-svelte automatically caches files that haven’t changed. This allows esbuild to skip the Svelte compiler for those files, saving time. Developers can disable this caching by setting the cache option to false.

Installation

To install the esbuild-svelte plugin and its dependencies, follow these steps:

  1. Install esbuild, Svelte, and the esbuild-svelte plugin using npm:
npm install --save-dev esbuild svelte esbuild-svelte
  1. Create a simple build script to use the plugin:
const esbuild = require('esbuild');
const sveltePlugin = require('esbuild-svelte');

esbuild.build({
  entryPoints: ['src/main.js'],
  bundle: true,
  outdir: 'dist',
  plugins: [sveltePlugin()],
}).catch(() => process.exit(1));
  1. Start using the plugin in your Svelte project.

Summary

The esbuild-svelte plugin is a powerful tool that allows developers to compile Svelte components for bundling with esbuild. It offers features such as CSS output, Typescript and other Svelte preprocessing, support for importing Svelte component libraries, and advanced caching for incremental or watch builds. With its easy installation process and comprehensive API, the esbuild-svelte plugin is a valuable asset for developers working with Svelte and esbuild.