npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

vite-plugin-resolve-barrels

v0.1.0

Published

Vite plugin for optimizing React barrel file imports - reduces bundle size and improves build performance

Downloads

211

Readme

🎯 @vite-plugin-resolve-barrels

A Vite plugin that optimizes imports through barrel files (index.ts files) by resolving them to direct imports from specific modules instead of going through re-exports.

📚 Table of Contents

🚀 Problem It Solves

📦 Barrel Files and Performance Issues

Barrel files (index.ts files) are a popular pattern for creating convenient APIs for module exports:

// src/widgets/index.ts (barrel file)
export { CreateShopLayout } from './authentication/createShopLayout';
export { ProductList } from './products/productList';
export { ShoppingCart } from './cart/shoppingCart';
// ... dozens of other exports

However, using barrel files creates serious performance problems:

🌳 Tree-shaking doesn't work effectively - bundlers can't determine which modules are actually used
📦 Excessive code in bundle - entire barrel file is imported even for a single component
🔥 HMR breaks or works poorly - changing one file forces reloading the entire barrel
📈 Increased bundle size - especially critical for production builds

🔄 Problem Example

❌ Instead of this (inefficient):

import { CreateShopLayout, ProductList } from 'widgets';
// ⚠️  Imports entire barrel file with all dependencies

✅ Plugin automatically transforms to (optimized):

import { CreateShopLayout } from './widgets/authentication/createShopLayout';
import { ProductList } from './widgets/products/productList';
// 🎯 Direct imports of only needed modules

📦 Installation

npm install -D vite-plugin-resolve-barrels

⚙️ Usage

🚀 Basic Configuration

// vite.config.ts
import { defineConfig } from 'vite';
import { resolveBarrelsPlugin } from 'vite-plugin-resolve-barrels';

export default defineConfig({
  plugins: [
    resolveBarrelsPlugin({
      directories: ['widgets', 'features', 'entities', 'shared'],
    }),
  ],
});

🛠️ Full Configuration with Options

// vite.config.ts
import { defineConfig } from 'vite';
import { resolveBarrelsPlugin } from 'vite-plugin-resolve-barrels';

export default defineConfig({
  plugins: [
    resolveBarrelsPlugin({
      directories: ['widgets', 'features', 'entities', 'shared'],
      enable: process.env.NODE_ENV === 'production', // Only for production
      logReplacements: true, // Console logging
      logToFile: true, // Save logs to file
      logFilePath: 'barrel-resolutions.log',
    }),
  ],
});

⚙️ Configuration Options

| Option | Type | Default | Description | | -------------------- | ---------- | ------------ | ------------------------------------------- | | 📁 directories | string[] | required | List of directories to process barrel files | | 🔧 enable | boolean | true | Enable/disable the plugin | | 🖥️ logReplacements | boolean | false | Output replacement logs to console | | 📄 logToFile | boolean | false | Save logs to file | | 📍 logFilePath | string | '' | Path to log file |

⚠️ Important Recommendations

🚀 Use Only for Production Builds

NOT recommended for development mode due to:

  • Additional computations on every code change
  • Debugging complexity
  • HMR may not work properly or at all
// Recommended configuration
resolveBarrelsPlugin({
  directories: ['widgets', 'features'],
  enable: process.env.NODE_ENV === 'production', // 👈 Only for production
});

🚫 When NOT to Use

🏠 Small projects - if your bundle is small, optimization may not provide significant benefits
🔧 Development server - may slow down module reloading
📁 Projects without barrel files - plugin won't provide any benefits

💡 Usage Examples

// vite.config.ts
resolveBarrelsPlugin({
  directories: ['app', 'pages', 'widgets', 'features', 'entities', 'shared'],
  enable: process.env.NODE_ENV === 'production',
});

📊 Optimization Results

❌ Before optimization:

// main.tsx
import { CreateShopLayout, ProductCard, useAuth } from 'widgets';

📦 Bundle Size: ~150kb
🚨 Issues: Entire widgets/index.ts + all barrel dependencies

✅ After optimization:

// Automatically transforms to:
import { CreateShopLayout } from './widgets/authentication/createShopLayout';
import { ProductCard } from './widgets/products/productCard';
import { useAuth } from './widgets/auth/useAuth';

📦 Bundle Size: ~45kb (70% reduction)
🎯 Result: Only specific files + direct dependencies

🐛 Logging and Debugging

🖥️ Console Logs

resolveBarrelsPlugin({
  directories: ['widgets'],
  logReplacements: true, // Enable console logs
});

// Output:
// [resolve-barrels] src/pages/main.tsx:
// original: import { CreateShopLayout } from 'widgets';
// transform: import { CreateShopLayout } from './widgets/authentication/createShopLayout';

📄 File Logs

resolveBarrelsPlugin({
  directories: ['widgets'],
  logToFile: true,
  logFilePath: './build-logs/barrel-resolutions.log',
});

🔧 Technical Details

⚙️ How the Plugin Works

1️⃣ Barrel files analysis - scans index.ts files in specified directories
2️⃣ Export maps building - creates a map of what is exported from where
3️⃣ AST transformation - analyzes imports through TypeScript AST
4️⃣ Import replacement - replaces barrel imports with direct imports

📥 Supported Import Forms

// ✅ Supported
import { Component } from 'widgets';
import { Component as MyComponent } from 'widgets/auth';
import { Component1, Component2 } from 'features/auth';

// ❌ Not supported (yet)
import * as widgets from 'widgets';
import widgets from 'widgets';

⚛️ Framework Support

React Only - This plugin is specifically designed and tested for React projects
Supports: .ts.tsx.js.jsx files

🚀 Development and Testing

# 📦 Install dependencies
npm install

# 🧪 Run tests
npm run test

# 📊 Tests with coverage
npm run test:coverage

# 🔨 Build package
npm run build

# 🔍 Linting
npm run lint:js

Made with ❤️ for optimizing Vite projects

🚀 Faster builds • 📦 Smaller bundles • 🎯 Better performance