ITZone

Lightning fast with Snowpack

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

Build once, cache forever

HMR feat. Fast Refresh

Out-of-the-box support

Snowpack is provided with built-in support for the following file types, no configuration required:

Optimize for production

Plugins? Plugins!

How does Snowpack work?

Here is an image of how snowpack and webpack work:

Unbundled development

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

It should work like this:

  1. Snowpack will scan your application for all the npm packages that are used
  2. Snowpack will read the installed dependencies from the node_modules directory
  3. Snowpack packs all of our despendencies separately into individual JavaScipt files. react and react-dom will be converted to react.js and react-dom.js corresponding
  4. Each resulting file can be run in a browser and imported via ESM import
  5. 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:

Then cd into the project directory to install snowpack:

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:

then go to file package.json add the following line:

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:

ở index.html we add:

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

Share the news now