webpack has been a developer favorite for years because of its effective module bundling capabilities. However, 2019 saw the introduction of Snowpack, a lighter alternative to webpack that promised increased speed and ease of use. This article will walk you through a brief comparison between these two tools and provide a step-by-step guide to getting started with Snowpack.
Since its initial release in 2012, webpack has gained widespread recognition as an essential tool for web development. Webpack is a module bundler that takes a large number of JavaScript files and transforms them into a single output file or a few smaller files that you can use to run your app.
Webpack creates a dependency graph that manages the dependency relationships between your modules. With the addition of loaders, the tool can also bundle other file types besides JavaScript, such as CSS, PNG, JPG, and more. This effectively allows you to add all of your static assets — along with your JavaScript — to the dependency graph, which saves you the extra effort it takes to manage each dependency manually.
Other benefits of webpack include:
But the powerful complexity of webpack also comes with some drawbacks:
Introduced in 2019, Snowpack is a new-generation, front-end build tool for JavaScript applications. Compared to webpack, Snowpack is faster, more lightweight, and much easier to configure for beginners.
Unlike webpack, Snowpack doesn’t need to go through an entire rebuild and re-bundle process every time you modify and save an asset file. Webpack’s time-intensive build process accounts for the delay you experience between saving your files and seeing your changes load in the browser. A few developers have even reported wait times as long as 12 minutes for larger apps.
Some people have devised clever workarounds for boosting the speed of webpack, such as upgrading the UglifyJsPlugin, jettisoning image loaders, rethinking caching strategies, and switching from node-sass to sass-loader, to name a few.
For those who can’t afford to take days out of your project schedule to analyze build performance and cobble together an optimization plan, Snowpack is a better option.
So what’s different about Snowpack? It uses JavaScript’s ES module system (ESM) to write your file changes directly and instantly to the browser with no wait time. As opposed to bundlers like webpack, Snowpack builds every one of your files once and then caches them. It only needs to rebuild a file when you make changes, and the rebuild happens for that single file only.
Snowpack lets you work in a lightning-fast, unbundled development environment and even lets you keep your final builds unbundled and runnable. You also have the option to support bundled builds when it comes time to roll your app out to production. All you have to do is add the right plugin for your favorite bundler, or even write your own using the Snowpack plugin API.
In short, Snowpack offers the following advantages over a traditional bundler tool like webpack:
To learn more about how Snowpack stacks up against webpack, check out our blog post.
You’ve done the research, you’ve compared the tools, and you’ve decided that Snowpack is the one for you. So how do you get started? Luckily, you’ve come to the right place.
In the rest of this article, we’ll guide you through the steps to migrate your development environment and get your app up and running with Snowpack.
Before you begin, check to ensure you have what you need to complete the setup. Thankfully, Snowpack’s requirements are minimal. To develop with the tool, you need:
Tip: The modern browser requirement applies only to your development environment. Once you’ve created your final build with Snowpack and pushed it to production, users can view your site in any browser, modern or legacy.
You can use these installation commands:
With npm:
id="globalinstallation">global installation npm install -g snowpack local installation per project npm install --save-dev snowpack
Snowpack’s creators recommend that you install locally rather than globally when possible.
With yarn:
local installation per project
yarn add --dev snowpack
There are three ways that you can run the Snowpack CLI locally:
root package.json
filenpx snowpack
yarn snowpack
To create a new app project, use Create Snowpack App (CSA). With CSA, you get a new initialized app configured using one of Snowpack’s app templates. Snowpack provides templates for React, Vue, Svelte, and other popular frameworks.
Here’s a sample initialization command that uses Snowpack’s template for React:
npx create-snowpack-app new-dir --template [@snowpack/app-template-react] [--use-yarn]
You can find a list of official CSA templates on the Snowpack documentation page.
Use the app you created with a CSA template as the starting point for migrating your existing app.
Snowpack supports most of the technologies used for web development nowadays, such as Babel, PostCSS, and Create React App, among others. Chances are, your app already uses one of these supported technologies, so the migration to Snowpack should be a snap.
In fact, if you have an existing Create React App project, you can run it directly through CSA without making any changes at all.
To migrate your app, begin by initializing a new app project using the CSA template of your choice (see the previous section for instructions). Next, copy over all the files from the src
and public
directories of your existing app site. Now, start your development server and load the site locally using this command: snowpack dev
Now it’s time to troubleshoot any issues you might run into. If you get stuck, check the Snowpack docs for help.
Finally, when you’re ready, build your site for production: snowpack build
While not technically as powerful as webpack, Snowpack offers a faster, more user-friendly alternative that provides all the functionality most developers need. If you’re looking to simplify or speed up your workflow, Snowpack can get the job done.
Install LogRocket via npm or script tag. LogRocket.init()
must be called client-side, not
server-side
$ npm i --save logrocket // Code: import LogRocket from 'logrocket'; LogRocket.init('app/id');
// Add to your HTML: <script src="https://cdn.lr-ingest.com/LogRocket.min.js"></script> <script>window.LogRocket && window.LogRocket.init('app/id');</script>
Would you be interested in joining LogRocket's developer community?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowDesign React Native UIs that look great on any device by using adaptive layouts, responsive scaling, and platform-specific tools.
Angular’s two-way data binding has evolved with signals, offering improved performance, simpler syntax, and better type inference.
Fix sticky positioning issues in CSS, from missing offsets to overflow conflicts in flex, grid, and container height constraints.
From basic syntax and advanced techniques to practical applications and error handling, here’s how to use node-cron.
One Reply to "Why and how to use Snowpack instead of webpack"
You should probably remove the square brackets from this “npx create-snowpack-app new-dir –template [@snowpack/app-template-react] [–use-yarn]” command