Polyfill Node.js built-in modules with Vite
Vite is a new tool that helps to jumpstart and streamline frontend Javascript development. The main advantage of Vite against other solutions like Parcel is a better speed and limiting transpilation only to a one time operation to dependencies, keeping your source untouched (if you don’t need to, because of JSX or typescript).
It consists of two major parts:
- A dev server that serves your source files over native ES modules, with rich built-in features and astonishingly fast Hot Module Replacement (HMR).
- A build command that bundles your code with Rollup, pre-configured to output highly optimized static assets for production.
It’s very similar to Snowpack and the two projects share the same benefits.
While Snowpack uses Rollup for the developing phase and production bundling, Vite uses Esbuild during development and Rollup only to build for production. This with the final objective to totally replace Rollup with the more performant Esbuild when the latest will reach the same features level of Rollup.
A problem I have found using Vite in my project is how to poly-fill Node.js built-in modules for the browser.
Using Node.js built-in and globals (es util or process) in a browser project is discouraged but sometimes you…