Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup global bootstrap via scss in Svelte?

I want to use Bootstrap (v4.5) in a Svelte (v3) project with custom theme.

The bootstrap documentation states that you can do this with scss. So I've setup Svelte with svelte-preprocess as follows:

Added to my package.json:

    "bootstrap": "^4.5.2",
    "node-sass": "^4.14.1",
    "svelte-preprocess": "^4.0.10",

In my rollup.config.js:

...
import preprocess from "svelte-preprocess";

export default {
  ...,
  plugins: [
    ...,
    svelte({
      // enable run-time checks when not in production
      dev: !production,
      // we'll extract any component CSS out into
      // a separate file - better for performance
      css: (css) => {
        css.write("public/build/bundle.css");
      },
      preprocess: preprocess(),
    }),

In my App component:

<style type="text/scss" global>
  // Variable overrides for bootstrap
  $primary: #19197c;
  $secondary: #fd6400;
  $light: #d8d8d8;

  @import "../node_modules/bootstrap/scss/bootstrap";

</style>

Unfortunately, it looks like Svelte purges all the styles since I don't get bootstrap styling in my application. I would like to use bootstrap normalization as well as bootstrap classes. Any tips? Thanks!

like image 997
Wilco Avatar asked Aug 07 '20 10:08

Wilco


People also ask

Can you use bootstrap with SCSS?

In your custom. scss , you'll import Bootstrap's source Sass files. You have two options: include all of Bootstrap, or pick the parts you need. We encourage the latter, though be aware there are some requirements and dependencies across our components.

Can I use SCSS in svelte?

You must first import the svelte preprocess plugin, and add it inside the svelte plugin configuration. If you'd like to add options to your SASS configurations you can do so. import { scss } from 'svelte-preprocess'; svelte({ preprocess: [ scss({ /** options */ }) ] }), That's it!

Can you use bootstrap with svelte?

Since Svelte app is basically an html page we can simply add links to Bootstrap CSS and JS in src/app.

Is it possible to use Bootstrap with svelte?

I want to use Bootstrap (v4.5) in a Svelte (v3) project with custom theme. The bootstrap documentation states that you can do this with scss. So I've setup Svelte with svelte-preprocess as follows:

Does svelte have SCSS support?

The new Svelte framework, though exciting, doesn’t have SCSS support built in. But it’s simple to add! Follow these steps to get SASS support in your Svelte app, plus get SASS syntax highlighting in VSCode. The first piece of this puzzle is to get your project able to build with SASS enabled.

Is it possible to write global styles in svelte?

In Svelte, styles are scoped by default to prevent collisions. This is great and all, but there are plenty of cases where it is practical to write global styles. Here is an example of making a <p> element globally white when it has the .dark-mode selector:

How do I get Started with svelte?

Here's how we start with Svelte. The easiest and best way to get Svelte up and running straight away is to install their master template and work from there. This will bring everything you need into the project, setup the basic folder structure and create your first component for you.


Video Answer


3 Answers

I actually found a much easier way, using Svelte Preprocess! All you need to do, in App.svelte

<!-- app.svelte content -->
...
<!-- ... -->

<style lang="scss" global>
  @import "path/to/your/scss/files";
</style>

Just note that if you use "./..." in the @import, that means it's referencing local files. If there is no "./..." (so just plain "name", then it will import from node_modules.

And that's it! If you wanted to use the settings, all you would need to do is

like image 127
Jose Quesada Avatar answered Oct 20 '22 23:10

Jose Quesada


I figured out how to get this working. You have to separately process the sass outside of Svelte, using rollup-plugin-scss, so that the classes are not purged.

In rollup.config.js:

...
import scss from "rollup-plugin-scss";

export default {
  ...,
  plugins: [
    ...,
    svelte({
      // enable run-time checks when not in production
      dev: !production,
      emitCss: true
    }),
    scss(),
    ...,

Create a new file called main.scss:

// Variable overrides for bootstrap
$primary: #19197c;
$secondary: #fd6400;
$light: #d8d8d8;

@import "../node_modules/bootstrap/scss/bootstrap";

In main.js:

import "./main.scss";
import App from "./App.svelte";

const app = new App({
  target: document.body,
  props: {},
});

export default app;

That's it!

like image 31
Wilco Avatar answered Oct 21 '22 01:10

Wilco


I usually use <svelte:head> to set global stuff.

Example

<svelte:head>
  <!-- Google Font -->
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">

  <style>
    /* Global CSS via SASS */
    @import '../../_assets/sass/global';

    ._another_global_stuff {
      z_index: 1001;
    }
  </style>
</svelte:head>

And rollup setup is very simple.

Example

Modules required:

  • svelte-preprocess
  • rollup-plugin-css-only
  • postcss
  • autoprefixer
  • node-sass
let plugins = [
  //...
  svelte({
    compilerOptions: {
      dev: !production
    },
    preprocess: sveltePreprocess({
      sourceMap: !production,
      defaults: {
        style: 'scss'
      },
      postcss: {
        plugins: [
          require('autoprefixer')()
        ]
      }
    }),
    emitCss: true
  }),
  css({output: 'bundle.css'}),
  //...
]
like image 3
Thiago Lagden Avatar answered Oct 21 '22 00:10

Thiago Lagden