<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: subaiyalShk</title>
    <description>The latest articles on Forem by subaiyalShk (@subaiyalshk).</description>
    <link>https://forem.com/subaiyalshk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F605210%2F88d33fb5-242f-4bd9-82f0-66245d4720e5.png</url>
      <title>Forem: subaiyalShk</title>
      <link>https://forem.com/subaiyalshk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/subaiyalshk"/>
    <language>en</language>
    <item>
      <title>Building a Reactive Single-File Web App with Svelte</title>
      <dc:creator>subaiyalShk</dc:creator>
      <pubDate>Sun, 10 Mar 2024 01:38:09 +0000</pubDate>
      <link>https://forem.com/subaiyalshk/an-entire-reactive-application-within-a-single-html-file-with-svelte-5dcg</link>
      <guid>https://forem.com/subaiyalshk/an-entire-reactive-application-within-a-single-html-file-with-svelte-5dcg</guid>
      <description>&lt;p&gt;&lt;strong&gt;Step 1: ViteJS&lt;/strong&gt;&lt;br&gt;
Vite is a relatively new build tool and development server which provides an extremely fast and lean development experience. Here we will use ViteJS to quickly scaffold our Svelte project. To do so on you terminal run the command: &lt;code&gt;npm create vite@latest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3wk6af6c5ej8zcov3k82.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3wk6af6c5ej8zcov3k82.png" alt="creating project" width="800" height="245"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the command is executed it will ask for project name so you can enter a name of your choice, then select Svelte when prompted to select a framework and lastly choose a variant. Here I have selected project name as test-project, framework as Svelte and variant as Javascript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd test-project
npm install
npm install @sveltejs/vite-plugin-svelte unocss vite-plugin-singlefile
npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first command will navigate us into our new project then we install all the dependencies for this project. Next we will install 3 additional packages &lt;code&gt;@sveltejs/vite-plugin-svelte&lt;/code&gt;, &lt;code&gt;unocss&lt;/code&gt; and &lt;code&gt;vite-plugin-singlefile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;1.&lt;code&gt;@sveltejs/vite-plugin-svelte&lt;/code&gt;: is the official Vite plugin provided by the Svelte team. It integrates Svelte into the Vite build process, enabling us to compile Svelte components.&lt;/p&gt;

&lt;p&gt;2.&lt;code&gt;unocss&lt;/code&gt;: This library will help us significantly reduce the size and complexity of our stylesheet.&lt;/p&gt;

&lt;p&gt;3.&lt;code&gt;vite-plugin-singlefile&lt;/code&gt;: This plugin will enable the compilation of our entire project into a single HTML file.&lt;br&gt;
making it super easy to distribute and share the project.&lt;/p&gt;

&lt;p&gt;Next we need to configure our project to leverage the above plugins and library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// vite.config.js
import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import UnoCSS from 'unocss/vite';
import { viteSingleFile } from 'vite-plugin-singlefile';

export default defineConfig(({ command }) =&amp;gt; ({
    plugins: [
        UnoCSS(),
        svelte({
            /* plugin options */
        }),
        command === 'build' &amp;amp;&amp;amp;
            viteSingleFile({
                removeViteModuleLoader: true
            })
    ],
    build: {
        minify: true
    }
}));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Find the file called vite.config.js then place the above code in there. The defineConfig function is used customize our build and development process. Here, we’re adding our plugins to the Vite configuration and specifying that the vite-plugin-singlefile should only be used during the build process. This is achieved through the conditional statement command === 'build'. When we run the build command, this plugin will step in to compile our entire Svelte application into a single HTML file&lt;/p&gt;

&lt;p&gt;The removeViteModuleLoader: true option within vite-plugin-singlefile configuration is particularly significant. This option strips away the Vite module loader from the final build, further simplifying our HTML file and ensuring that all our code is self-contained.&lt;/p&gt;

&lt;p&gt;Lastly, we set minify to true. minification can reduce the file size, improve load times and make it difficult for malicious actors to read our code and find vulnerabilities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Building and Compiling Your Svelte Application&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm2pcdz03ivv4phpok9dl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm2pcdz03ivv4phpok9dl.png" alt="build processing" width="800" height="317"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you have developed your Svelte application - including all your individual .svelte components - execute &lt;code&gt;npm run build&lt;/code&gt; in the terminal. This will compile your entire application into a single self-contained HTML file. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiy2bxxb716asbm3l3h9v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiy2bxxb716asbm3l3h9v.png" alt="file location" width="800" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This single HTML file is entirely self-contained, meaning it includes all of the HTML, CSS, and JavaScript needed to run your application. This makes it incredibly easy to share your project. You can distribute this single HTML file, and anyone can open it in their browser to run your application, without any additional setup or installation.&lt;/p&gt;

&lt;p&gt;This ability to compile an entire Svelte project into a single, portable HTML file is a game-changer for sharing and deploying small-scale projects.&lt;/p&gt;

</description>
      <category>svelte</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
