DEV Community

Mateusz Iwaniuk
Mateusz Iwaniuk

Posted on • Updated on

Localnetwork configuring, CDN libraries, global styles and much more in Svelte

JavaScript language is still growing. Nowadays, there are many different frameworks, which are commonly used in web development. One of them is Svelte – I call it ”a half-framework” because of its ease and flexibility. Programmers, who move to other frameworks are often confused and they need answer for common issues. People, who start their journey with svelte might be a little bit uncertain about basic subjects like f.e configuring local-network and adding libraries. In this article I’m going to show you how to:

  • configure your local network with svelte
  • add external JS libraries using CDN
  • add sass prepocessor and use it
  • declaring global scss styles and variables
  • deploy your app to Netlify

Configuring your local network

During your development time, regardless of state of your app progress,you want to have a view on your website. Basic local-host comes with npm run dev but there is one problem – you can’t see your project using mobile phone or any other device. Solving this issue is quite simple.

  1. Open your package.json
  2. Look at your ”scripts” section
  3. Add --host to the end of the dev and start line
  4. Your package.json scripts should look like this:
 "scripts": {
    "build": "rollup -c",
    "dev": "rollup -c -w --host",
    "start": "sirv public --host"
Enter fullscreen mode Exit fullscreen mode

Now typing either npm run dev and npm start command, your svelte app will be opened on local network and thanks to that, you will be able to see your project state on each device connected to your network (example:

Adding External CDN libraries

There is a moment, when you need to use any JS library (particlejs for simple example) but you don’t know how to do it. Your first think is to append it to your index.html which is located in public folder, but everything which is located in <body> tag is filled with your transpiled svelte code.
Solving this issue is also quiet simple – all you have to do is to change the place, which is filled by your svelte code.It may sound a little bit weird,but I’m going to explain it in 5 easy steps:

  1. Open your index.html file (located in public folder)
  2. Insert <div id="my-app"> into your <body> tag
  3. Insert all external CDN links to the end of your </body> tag
  4. Open your main.js file
  5. Find and change the target value to document.getElementById('my-app') everything should look like that:


  <div id="my-app"></div>
  <script src="" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous">
Enter fullscreen mode Exit fullscreen mode


import App from "./App.svelte";
const app = new App({
  target: document.getElementById("my-app")
Enter fullscreen mode Exit fullscreen mode

Now you are able to add external CDN Libraries and use them.

Adding SCSS

Nowadays,using sass is very common. Most of web-developers can’t imagine doing any project without using sass – it’s absolutely understandable. You can implement sass into your svelte app in very easy way. All you have to do is to download npm package responsible for sass, configure it in rollup.config.js file and you can use it in your project.

  1. npm install svelte-preprocess
  2. In your rollup.config.js import autoProcess and implement this
import autoPreprocess from 'svelte-preprocess';

export default {
  plugins: [
      preprocess: autoPreprocess()
Enter fullscreen mode Exit fullscreen mode

Now if you want to use scss in your project, you’ll just have to add type parameter in style tag.It should look like that:

<style type="text/scss">
Enter fullscreen mode Exit fullscreen mode

You are not obligated to have your whole scss code in the one style tag. You can write it in the different files and import it.


<style type="text/scss">
  @import "./tip.scss";

<h1 class="tip">Red color comes from different scss file:)</h1>
Enter fullscreen mode Exit fullscreen mode


.tip {
    color: red;
Enter fullscreen mode Exit fullscreen mode

Declaring global styles and variables

Now that you’ve installed and implemented sass preprocessor,it’s time to show you how does the scope in svelte work and how to use global styles and variables.

If you add any simple code,you will see,that these styles don't work for components in other files, which have even the same class structure. Let me show you this, using example files in unreal project.


<h1 class="header"> This header has got pink color </h1>

<style type="text/scss">

Enter fullscreen mode Exit fullscreen mode


<h1 class="header"> 
And this header is no color-styled,
 there are literally no styles in this file

Enter fullscreen mode Exit fullscreen mode

Working with that is kind of specific in svelte,but I will show you how it works.
The way to handle global styles is to put your styles into :global class.

Everything which is included in global class will affect every element on your webpage.

    :global(.header) {
Enter fullscreen mode Exit fullscreen mode

Thanks to this line of code, each of elements on your webpage with .header class will have a specific font-size and text-transform, regardless of it's location in any file.

The best place to declare your global CSS variables and styles is in Svelte.App file.

The global variables work in very similar way. To make sure, that your variables are available in each of files in your projects, the best option is to have it in one, :global class.


 --color-grey: #AEAEAE;

Enter fullscreen mode Exit fullscreen mode


<h2 class="grey"> 
I have a color, which comes from 
--color-grey variable

<style type="text/scss">

Enter fullscreen mode Exit fullscreen mode

Deploying to netfliy

When you are ready to show your project to the world,it’s time to depoly it. The best and the easiest way to do it is netlify, but you can do it with literally every hosting that you want.

1) Connect your project with github
It's so simple - All you have to do is to create new repo and follow given instructions
Alt Text

Alt Text

Alt Text

2) Login to Netlify and click Create new site from git
Alt Text

3) Login with your GitHub account
Alt Text

4) If you don't see your repo to select, click this link on the bottom of the page
Alt Text

5) Find your repository and save it
Alt Text

6) Type npm run build in Build command input and public in Publish directory and then click deploy site
Alt Text

7) And it works! If you want, you can change your domain name in Domain settings > Custom domains > Options
Alt Text
And then change name
Alt Text

Everything should work fine.


Svelte is amazing framework and it works perfect with small projects. It may be hard to find yourself and to configure basic things, but I believe that I could help you.

Top comments (0)