Lightning fast with Snowpack
- Tram Ho
What is Snowpack?
Snowpack is a quick UI building tool, designed for the modern web. It is an alternative solution for these bundlers
heavier, more complicated like webpack
or Parcel
in your development process. Snowpack leverages JavaScript’s native modules system to avoid unnecessary work and maintain speed no matter how big or scaling your project is.
According to the snowpack homepage, if you have tried it once then chances are you will be addicted to it, when using it you may not want to go back to using the bundler
that you used again =))
So what features does snowpack offer?
Feature
Instant startup
- Unbundled web development server
khởi động trong 50ms hoặc nhỏ hơn
and still fast in large projects
Build once, cache forever
- Snowpack never builds the same file twice. Supported by
JavaScript’s native module system (ESM)
in the browser
HMR feat. Fast Refresh
- No need to refresh. See changes reflected instantly in the browser with Hot Module Replacement (HMR) + Fast Refresh for React, Preact & Svelte.
- HMR: is the ability to update the content of files when there is a change to the browser without we needing to refresh the page. For a simple example, in a css file we fix the text color from red to blue, the program immediately updates the text color to blue, but we don’t need to resfresh the page.
- With the ability of
snowpack
when building single files almost immediately, it only takes 10-25ms to download and update in the browser - In addition to the HMR
snowpack
supportFast Refresh
for popular frameworks or libraries like react, Preact and Svelte.
Out-of-the-box support
Snowpack is provided with built-in support for the following file types, no configuration required:
- JavaScript (.js, .mjs)
- TypeScript (.ts, .tsx)
- JSON (.json)
- JSX (.jsx, .tsx)
- CSS (.css)
- CSS Modules (.module.css)
- Images & Assets (.svg, .jpg, .png, etc.)
- WASM (.wasm)
Optimize for production
- Built for production environments with built-in optimization features and plugin support for your favorite packages.
Plugins? Plugins!
- You can connect to your favorite plugins like
babel
,SASS
, …. Or you can create your own plugin
How does Snowpack work?
- With
unbundled development
Snowpack’s are still supported asbundled builds
that we used for production - When we build applications for production, we can install plugins
bundler
I like Webpack, … via the official snowpack plugin - With snowpack we don’t need to configure complicated bundler but it handles the build for us
- Snowpack helps you get the most out of: agile development, unbundled development with optimized performance in our production releases.
Here is an image of how snowpack and webpack work:
Unbundled development
- Unbundled development is the idea of moving individual files to the browser during our application development. Files can still be built using tools like
ts
,babel
,… and then loaded individually in the browser with syntax dependenciesimport
andexport
. Whenever we change the contents of the file, Snowpack just needs to rebuild the file - Advantage of
Unbundled development
compare tobundled development
traditional:- Single-file builds fast
- Single-file builds are deterministic
- Single-file builds easy to debug
- Project size does not affect development speed
- Individual files are better cached.
In general, Unbundled development: Each file is individually created and cached indefinitely. In a development environment, build the file only once and the browser will not reload the file a second time, unless we change the contents of the file.
Use NPM dependencies
- NPM packages are mainly published using module syntax, so they cannot be run on the web without some build. We want to use a package we have to come back with
bundled development
- Snowpack takes a different approach: instead of packing our entire application for one request, Snowpack handles our dependencies separately.
1 2 3 | node_modules/react/**/* -> http://localhost:8080/web_modules/react.js node_modules/react-dom/**/* -> http://localhost:8080/web_modules/react-dom.js |
It should work like this:
- Snowpack will scan your application for all the npm packages that are used
- Snowpack will read the installed dependencies from the node_modules directory
- Snowpack packs all of our despendencies separately into individual JavaScipt files.
react
andreact-dom
will be converted toreact.js
andreact-dom.js
corresponding - Each resulting file can be run in a browser and imported via ESM
import
- Because our dependencies rarely change, Snowpack rarely needs to rebuild them.
After Snowpack builds our dependencies, any packages can be imported and run directly in the browser without any additional packages or tools.
Demo
Through such theoretical items, how to practice using it.
Install snowpack
You create a folder and use the following command to init project with npm:
1 2 | npm init |
Then cd into the project directory to install snowpack:
1 2 3 4 5 6 7 | # npm: npm install --save-dev snowpack # yarn: yarn add --dev snowpack # pnpm: pnpm add --save-dev snowpac |
Here you probably do not know about pnpm
You can refer to my post about pnpm
Practice
Then you go to the root of the project to create a file index.html
with simple content as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <span class="token doctype"><!DOCTYPE html></span> <span class="token tag"><span class="token punctuation"><</span>html <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>en<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>head<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>meta <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>utf-8<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span> <span class="token tag"><span class="token punctuation"><</span>meta <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>viewport<span class="token punctuation">"</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>width=device-width, initial-scale=1<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span> <span class="token tag"><span class="token punctuation"><</span>meta <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>description<span class="token punctuation">"</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Demo Snowpack App<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span> <span class="token tag"><span class="token punctuation"><</span>title<span class="token punctuation">></span></span>Demo Snowpack App<span class="token tag"><span class="token punctuation"></</span>title<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>head<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>body<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>h1<span class="token punctuation">></span></span>Demo Snowpack App<span class="token tag"><span class="token punctuation"></</span>h1<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>body<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>html<span class="token punctuation">></span></span> |
then go to file package.json
add the following line:
1 2 3 4 5 | <span class="token property">"scripts"</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"dev"</span><span class="token operator">:</span> <span class="token string">"snowpack dev"</span><span class="token punctuation">,</span> ... <span class="token punctuation">}</span><span class="token punctuation">,</span> |
Now let’s try start
it’s up to see how npm run dev
!
Our terminal will look like this:
We can see that the time it starts is 32ms
Using JavaScript: We create a file index.js
and app.js
as follows:
1 2 3 4 | <span class="token keyword">import</span> <span class="token punctuation">{</span>app<span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'./app'</span><span class="token punctuation">;</span> <span class="token function">app</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
1 2 3 4 | <span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">app</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'App snowpack'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> |
ở index.html
we add:
1 2 3 4 | ... <span class="token tag"><span class="token punctuation"><</span>script <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>module<span class="token punctuation">"</span></span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>/index.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token punctuation"></</span>script<span class="token punctuation">></span></span> ... |
Now try to change the contents of the file, then look at the terminal to see which files have changed and the number of times that file changed
Conclude
In this article, I introduced snowpack and practiced creating a project to learn how it works. In the next article we will learn how to use the npm package, adding css, and building for the production and development environment. Thank you for watching the article
Other articles with snowpack:
References:
In my article use the content and refer to the official document page of snowpack
- snowpack.dev