Svelte Tailwind Integration screenshot

Svelte Tailwind Integration

Author Avatar Theme by Chrisdhanaraj
Updated: 28 Nov 2020
43 Stars

Categories

Overview:

The content is a brief repository demonstration that explains how to integrate Tailwind CSS into the Svelte pipeline without the need for additional scripts. It highlights the idea of leveraging the postcss plugins used by Tailwind, instead of relying on the Tailwind CLI. The demonstration includes steps to add the necessary dependencies, configure the rollup file, and import Tailwind using a global style block.

Features:

  • Easy Integration: The integration of Tailwind into the Svelte pipeline is demonstrated to be quick and painless.
  • Direct Use of Postcss Plugins: Instead of relying on the Tailwind CLI, the postcss plugins used by Tailwind are leveraged directly for integration.
  • Dependency Management: The content provides instructions on adding the necessary dependencies, including tailwindcss, autoprefixer, and svelte-preprocess.
  • Rollup Configuration: The rollup.config.js file is updated with additional blocks to enable the integration.
  • Preprocessing Setup: The svelte rollup plugin is configured to include preprocess, allowing for the integration of Tailwind.
  • Global Style Block: A <style global> block is added to a specific Svelte component to reference and import Tailwind.
  • Optimization Trick: It is suggested to place the global style block in a file that will not change to optimize build times during development.

Installation:

To install the Tailwind/Svelte integration, follow these steps:

  1. Add the necessary dependencies using the following command:

    yarn add -D tailwindcss autoprefixer svelte-preprocess
    
  2. Set up the rollup.config.js file by adding the following blocks:

    // ...existing code
    import sveltePreprocess from 'svelte-preprocess';
    import postcss from 'rollup-plugin-postcss';
    
    // ...existing code
    
    plugins: [
        // ...existing plugins
        postcss({
            plugins: [
                require('tailwindcss'),
                require('autoprefixer'),
            ],
        }),
    ],
    
    // ...existing code
    
  3. Configure the svelte rollup plugin to include preprocess. Add the following line to the svelte() section of the rollup.config.js file:

    preprocess: sveltePreprocess(),
    
  4. Add a <style global> block to the desired Svelte component. For example, add the following block to the src/App.svelte component:

    <style global>
        @import 'tailwindcss/base';
        @import 'tailwindcss/components';
        @import 'tailwindcss/utilities';
    </style>
    
  5. Important: In an actual app, it is recommended to place the global style block in a file that will not change. This is because the initial build process for Tailwind can be slow, going through the entire 3mb+ worth of Tailwind CSS. Placing the block in an unchanged file allows for faster incremental builds.

  6. You’re done! The Tailwind CSS integration should now be in place.

Summary:

The content provides a demonstration of integrating Tailwind CSS into the Svelte pipeline without additional scripts. Instead of relying on the Tailwind CLI, the integration leverages the postcss plugins used by Tailwind directly. The steps include adding dependencies, configuring the rollup.config.js file, and adding a global style block to import Tailwind. The article emphasizes the ease and efficiency of the integration process while also suggesting optimization tricks for faster builds during development.