Parcel Web Application Bundler
website·Indexed 42 days ago
https://parceljs.org/A zero-config web application bundler featuring a development server with hot reloading, scope hoisting, and a robust plugin system. Parcel provides out-of-the-box support for HTML, CSS, TypeScript, Vue.js, and various preprocessors like Sass, Less, and Stylus, as well as specialized transformers for GLSL and GraphQL.
What's inside Parcel
- @parcel/css is a Rust-based CSS parser, compiler, and minifier designed for high performance and high-quality minification. It supports compiling CSS modules, tree shaking, automatic vendor prefixing based on browser targets, and transpiling modern CSS features such as nesting, logical properties, and level 4 color syntax. It can be used within Parcel, as a standalone JavaScript/Rust library, or as a plugin for other tools.
Overview of Parcel 2 Plugin System
Parcel 2 is designed as a small core that orchestrates a series of plugins to handle the bundling process. Most applications can use the default configuration, but the core behavior can be extended or overridden using the following plugin types:
- Resolvers: Convert imports (e.g.,
import './foo') into full file paths. - Transformers: Compile code/assets from one language to another (e.g., TypeScript to JS) and extract dependencies.
- Bundlers: Group the asset graph into bundles (e.g., grouping JS and CSS together).
- Namers: Determine the output filenames for bundles, typically including content hashing.
- Runtimes: Insert code into bundles at build time (e.g., HMR runtime or async loading code).
- Packagers: Combine assets of a common type into a single output bundle for the browser.
- Optimizers: Minimize or compress bundles (e.g., using Terser for JS).
- Validators: Perform background code validation (e.g., ESLint or TypeScript type checking). These do not block dev builds but will block production builds.
- Reporters: Handle events, status information, and logging (e.g., the Parcel CLI and dev server).
- Resolvers: Convert imports (e.g.,
Overview of Parcel Validator Plugins
Validator plugins are used to validate assets (e.g., for linting or typing issues). They run after a build has fully completed to avoid impacting compilation performance.
Behavior differs by mode:
- Watch/Serve mode: Updated bundles are still served/saved even if a validator throws an error; the error is simply logged.
- Build mode: Parcel exits with a failure status code if a validator throws an error, preventing the deployment of invalid code.
Overview of Parcel Validator Plugins
Validator plugins are used to validate assets (e.g., for linting or typing issues) after a build has fully completed to avoid impacting compilation performance.
Behavior varies by mode:
- Watch/Serve mode: If a validator throws an error, it is logged, but Parcel continues to serve/save updated bundles.
- Build mode: If a validator throws an error, Parcel exits with a failure status code to prevent deploying invalid code.
Note: The Validator API is experimental and subject to change.
Overview of Parcel 2 core features
v2Parcel 2 is a zero-configuration web bundler designed for scalability and extensibility. Key features include:
- Plugin System: Fully extensible architecture for complex build requirements.
- Tree Shaking: Enabled by default for ES modules, CommonJS, dynamic imports, and CSS modules.
- Performance: Uses a Rust-based JavaScript compiler and a parallelized architecture.
- Differential Bundling: Automatic native ES module bundling for modern browsers with fallbacks for older ones.
- Code Splitting: Automatic deduplication of common modules into shared bundles.
- Image Processing: Built-in resizing, conversion (AVIF, WebP), and lossless optimization for JPEGs and PNGs.
- Caching: Portable, reliable cache that automatically tracks configs, plugins, and dependencies.
- Development Experience: Support for React Fast Refresh, lazy development mode (builds only requested files), and a C++ based file watcher.
- Library Support: Ability to build libraries with output for ES modules, CommonJS, and TypeScript definitions.
- Web Workers: Support for native ES module workers, worklets, and service worker manifests.
Use Stylus files in a Parcel project
Parcel automatically supports Stylus files using the@parcel/transformer-stylusplugin, which is installed automatically when a.stylfile is detected. Compiled Stylus files are processed like CSS, meaning they are compiled for browser targets and processed by any configured PostCSS plugins. To use CSS modules with Stylus, use the.module.stylfile extension.Implement Static Rendering with React Server Components
v2-14-0Parcel includes a built-in static site generator using RSC. Entry components are rendered to static HTML at build time. Parcel providespages(a list of all pages) andcurrentPage(metadata about the current page) as props to the entry component to facilitate site navigation.export default function Page({pages, currentPage}) { return ( <html> <head> <title>{currentPage.name}</title> </head> <body> <nav> {pages.map(page => ( <a href={page.url}>{page.name}</a> ))} </nav> </body> </html> ); }Understand parallel loading of scripts and styles
When a JavaScript file imports a CSS file, Parcel automatically inserts a<link>element into the HTML<head>so the stylesheet loads in parallel with the script. Similarly, shared bundles (e.g., React or Lodash used across multiple pages) are split into separate bundles and loaded via additional<script>tags to optimize loading.Automatic code splitting in Parcel v2.8.0
v2-8-0Parcel v2.8.0 introduces a new bundling algorithm that improves automatic code splitting. Instead of placing assets into bundles based on manual split points and removing duplication later, it starts with a graph where each asset is in only one bundle and combines them to meet constraints like parallel request limits and minimum bundle size. This reduces time complexity by removing nested graph traversals and pre-computing information, resulting in faster build times and smaller bundle sizes for large projects.Use the modernized React error overlay in Parcel
v2-14-0Parcel v2.14.0 introduces a modernized, built-in error overlay for React apps that replaces the deprecatedreact-error-overlaypackage. The overlay provides a UI with inline code frames for runtime errors. To improve performance, the dev server now handles source map parsing and stack trace mapping in Rust via an internal endpoint, reducing browser-side network round trips and processing. Additionally, the overlay now supports displaying errors from React Server Components and provides colorized diffs for React hydration errors.Use MDX in Parcel projects
v2-14-0Parcel provides first-class MDX support powered bymdx-rs. It processes images and links in both Markdown and JSX syntax. When used with React Server Components, Parcel can automatically extract a table of contents and metadata from static exports.Use the new Rust-based HTML transformer in Parcel v2.15.0
v2-15-0Starting with Parcel v2.15.0, a new HTML transformer and minifier implemented in Rust (using the
html5everparser) is used by default. This ensures browser-grade parsing and more conservative, correct minification.- PostHTML Support: If a
.posthtmlrcfile is present, it will still be applied. - Minification: The new minifier is compatible with many
htmlnanoconfiguration options. - Reverting to htmlnano: To continue using
htmlnanospecifically, add@parcel/optimizer-htmlnanoto your.parcelrcconfiguration.
- PostHTML Support: If a