Snowpack: An Alternative Build Tool to Webpack

Webpack is the most popular JavaScript build tool for the last few years because of its flexible bundling configuration and the large amount of custom plugins it officially supports for different file types.The main purpose of using Webpack is to take …

Webpack is the most popular JavaScript build tool for the last few years because of its flexible bundling configuration and the large amount of custom plugins it officially supports for different file types.

The main purpose of using Webpack is to take all of your JavaScript files, along with modules imported from NPM, images, CSS, and other web assets, and bundle them all together into one build file that can be run by the browser.

Webpack bundling in a nutshell. Source

But Webpack is also a complicated tool with a steep learning curve because its flexibility means it has so many features and fits so many use cases. Furthermore, Webpack needs to rebundle and rebuild your entire JavaScript application even when you make just a small change on a single file. Without a good understanding of how Webpack works, it might take more than 30 minutes in order to build an application.

But then again, Webpack was released in 2014. At that time, the browser support for the EcmaScript Module (ESM) import and export syntax is virtually non-existent, so the only way to run modern JavaScript in the browser was to grab all modules in your project and bundle them as one.

There are other processes involved along the way, such as transpiling JavaScript from a newer version into an older version with Babel so that the browser can run the application. But the main idea for using Webpack was to help create the best developer experience while enabling JavaScript developers to use modern features (ES6 and up).

ESM syntax is widely supported by all major browsers today, so bundling your JavaScript files together is no longer mandatory for running the application on the browser.

Unbundled development with Snowpack

Snowpack is a JavaScript build tool that takes advantage of the browser support for ESM so that you can build and ship individual files to the browser. Each file built will be cached, and when you change a single file, only that file will be rebuild by Snowpack.

Snowpack serve your files unbundled. Source

Snowpack development server is also optimized to only build a file once it’s requested by the browser, which allows Snowpack to start instantly (< 50ms) and scale up to large projects without slowing down. I’ve tried it myself and my server started in 35ms:

Snowpack dev server startup

Snowpack build process

Snowpack will deploy your unbundled application to production by default, but you probably want to implement build optimization techniques like minification, code-splitting, tree-shaking, lazy loading, and more.

Snowpack also has support for bundling your application for production build by connecting to Webpack using a plugin. Now since Snowpack already handles the process of transpiling your code, your bundler (Webpack) will only build common HTML, CSS, and JavaScript files. This is why you don’t need complicated Webpack configuration for the bundling process.

Finally, you can also set the list of browser versions you’d like to support by setting the browserslist property of your package.json file:

/* package.json */
"browserslist": ">0.75%, not ie 11, not UCAndroid >0, not OperaMini all",

The property will be picked up automatically when you run the snowpack build command to build the project for production environment. Snowpack doesn’t perform any transpilation when building for development, but this shouldn’t be a problem because most of the time you will develop using the latest browser version.

Getting started with Snowpack

To start using Snowpack, you can immediately create a Snowpack application using Create Snowpack App (CSA)and NPX. For example, you can create a starter React application with CSA with the following command:

npx create-snowpack-app react-snowpack --template @snowpack/app-template-react

A new react-snowpack folder will be created and bootstrapped with minimum dependencies:

{
"scripts": {
"start": "snowpack dev",
"build": "snowpack build",
"test": "web-test-runner \"src/**/*.test.jsx\"",
"format": "prettier --write \"src/**/*.{js,jsx}\"",
"lint": "prettier --check \"src/**/*.{js,jsx}\""
},
"dependencies": {
"react": "^17.0.0",
"react-dom": "^17.0.0"
},
"devDependencies": {
"@snowpack/plugin-dotenv": "^2.0.5",
"@snowpack/plugin-react-refresh": "^2.4.0",
"@snowpack/web-test-runner-plugin": "^0.2.0",
"@testing-library/react": "^11.0.0",
"@web/test-runner": "^0.12.0",
"chai": "^4.2.0",
"prettier": "^2.0.5",
"snowpack": "^3.0.1"
}
}

You can immediately run the application with npm start command. The local development server will be opened at port 8080. The CSA template for React is very similar to Create React App’s default template:

React default page for CSA

Snowpack supports many official templates for popular libraries like React, Vue, and Svelte. You only need to add the –template option to the command.

Conclusion

You should be able to use a bundler because you want to, and not because you need to.
Snowpack documentation

Webpack and Snowpack was created years apart, and although Webpack has been the most popular choice for bundling JavaScript modules, the browser support for ESM modules has opened a new way to develop web applications.

With the power to enable unbundled development and quickly rebuild the application in development, Snowpack is an exciting alternative to Webpack that’s easier to use for building JavaScript applications. It also allows you to use Webpack for bundling your production build, enabling build optimization techniques to be implemented for your project.

Be sure to checkout Snowpack documentation for more information.

Share components between projects using Bit

Bit makes it simple to share and reuse independent components across projects.

Bit component can be independently maintained and developed so that you don’t need to worry about structuring a full dev environment just to make a few changes.

Use Bit to collaborate more effectively and keep a consistent design.

Bit (Github) supports Node, TypeScript, React, Vue, Angular, and more.

Exploring shared components on Bit.dev


Snowpack: An Alternative Build Tool to Webpack was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


Print Share Comment Cite Upload Translate
APA
Nathan Sebhastian | Sciencx (2024-03-28T21:13:32+00:00) » Snowpack: An Alternative Build Tool to Webpack. Retrieved from https://www.scien.cx/2021/03/11/snowpack-an-alternative-build-tool-to-webpack/.
MLA
" » Snowpack: An Alternative Build Tool to Webpack." Nathan Sebhastian | Sciencx - Thursday March 11, 2021, https://www.scien.cx/2021/03/11/snowpack-an-alternative-build-tool-to-webpack/
HARVARD
Nathan Sebhastian | Sciencx Thursday March 11, 2021 » Snowpack: An Alternative Build Tool to Webpack., viewed 2024-03-28T21:13:32+00:00,<https://www.scien.cx/2021/03/11/snowpack-an-alternative-build-tool-to-webpack/>
VANCOUVER
Nathan Sebhastian | Sciencx - » Snowpack: An Alternative Build Tool to Webpack. [Internet]. [Accessed 2024-03-28T21:13:32+00:00]. Available from: https://www.scien.cx/2021/03/11/snowpack-an-alternative-build-tool-to-webpack/
CHICAGO
" » Snowpack: An Alternative Build Tool to Webpack." Nathan Sebhastian | Sciencx - Accessed 2024-03-28T21:13:32+00:00. https://www.scien.cx/2021/03/11/snowpack-an-alternative-build-tool-to-webpack/
IEEE
" » Snowpack: An Alternative Build Tool to Webpack." Nathan Sebhastian | Sciencx [Online]. Available: https://www.scien.cx/2021/03/11/snowpack-an-alternative-build-tool-to-webpack/. [Accessed: 2024-03-28T21:13:32+00:00]
rf:citation
» Snowpack: An Alternative Build Tool to Webpack | Nathan Sebhastian | Sciencx | https://www.scien.cx/2021/03/11/snowpack-an-alternative-build-tool-to-webpack/ | 2024-03-28T21:13:32+00:00
https://github.com/addpipe/simple-recorderjs-demo