
- Astro JS Tutorial
- Astro JS - Home
- Astro JS - Overview
- Astro JS vs Next JS
- Astro JS vs React JS
- Astro JS Setup
- Astro JS - Installation
- Astro JS - Project Structure
- Astro JS - Pages
- Astro JS Architecture
- Astro JS - Islands
- Astro JS - Islands Architecture
- Astro JS Routing
- Astro JS - Routing
- Astro JS - Dynamic Routing
- Astro JS - Redirecting Routes
- Astro JS - i18n Routing
- Astro JS Configuration
- Astro JS - Configuration
- Astro JS - Editor Setup
- Astro JS - TypeScript Configuration
- Astro JS - Environment Variables
- Astro JS Build UI
- Astro JS - Components
- Astro JS - Slots
- Astro JS - Layouts
- Astro JS - Fonts
- Astro JS - Scripts
- Astro JS Create Website
- Astro JS - Markdown Contents
- Astro JS - Add Images
- Astro JS - Manage Content
- Astro JS - Content Collections
- Astro JS - Data Fetching
- Astro JS Styling and CSS
- Astro JS - Styling
- Astro JS - CSS Integration
- Astro JS - CSS Cascading Order
- Astro JS Integrations
- Astro JS - React Integrations
- Astro JS - Svelte Integrations
- Astro JS - Solid Integrations
- Astro JS - Vue Integrations
- Astro JS Adapters
- Astro JS - Netlify Adapter
- Astro JS - Cloudflare Adapter
- Astro JS Testing and Deployment
- Astro JS - Testing
- Astro JS - Deployment
- Astro JS Advanced Topics
- Astro JS - State Management
- Astro JS - Prefetching
- Astro JS - Middleware
- Astro JS - Endpoints
- Astro JS - Authentication
- Astro JS - Bun Environment
- Astro JS - Docker
- Astro JS - View Transition
- Astro JS - Transition Directives
- Astro JS - Astro DB
- Astro JS - Bundling
- Astro JS Useful Resources
- Astro JS - Interview Questions
- Astro JS - Cheatsheet
Astro JS - Bundlings
What is Bundling?
Bundling is the process of combining multiple javascript files into a single file. This is done to improve the performance of your application by reducing the number of HTTP requests to server. Most of the modern web applications are using bundlers to bundle their JavaScript files. Astro provides a built-in bundler that can be used to bundle your JavaScript files.
Astro Bundlers
In Astro framework, bundling is handled automatically by the Astro bundler. It will reduce the number of HTTP requests to server and improve the performance of your application. Astro achieves bundling through number of techniques, such as −
- Automatic Code Splitting:Astro automatically splits JavaScript and CSS codes into smaller, optimized chunks. Only the necessary code for a given page will be loaded at userâs end.
- Partial Hydration: Astro uses partial hydration to only hydrate the code that is needed for a given page. Also, it introduces 'island architecture', where interactive components (like React, Vue, Svelte, or Solid) are only hydrated when needed.
- Tree Shaking: Astro uses tree shaking to remove unused JavaScript and CSS code from your bundle.
- Optimized Asset Handling: Astro automatically compresses and minifies assets like images, fonts, and CSS files whenever possible.
Analyze Astro Bundles
Astro provides a way to analyze the bundles generated by the Astro bundler. Visualizing the bundle can give ideas for where to changes and how to reduce the bundle size. Astro let you create an HTML file which contain all the information about bundles in your project. Follow the steps below to analyze bundle of your astro project.
Step 1: Install the package
The rollup-plugin-visualizer library allows you to visualize and analyze your Rollup bundle to see which modules are taking up space. To install the package, run the following command in your terminal −
>> npm install rollup-plugin-visualizer --save-dev
Step 2: Add Plugin to Astro Config
Next step is to add the rollup-plugin-visualizer to the Astro config file. Open the astro.config.mjs file and add the following code −
// @ts-check import { defineConfig } from 'astro/config'; import { visualizer } from "rollup-plugin-visualizer"; export default defineConfig({ vite: { plugins: [visualizer({ emitFile: true, filename: "stats.html", })] } });
Step 3: Run Project Build
Now you can run the project build and the stats.html file will be generated. Run the following command in your terminal −
>> npm run build
This command will create an optimized production build of your project. After successfully running this command, a stat.html file can be found /dist folder at root of your project. This file will give all the details about bundles in your project.
