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 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-resolve-barrel-files

v0.2.0

Published

A babel plugin that solves typescript barrel files

Downloads

17

Readme

babel-plugin-resolve-barrel-files

This plugin parses and resolves a barrel file

What is a barrel file?

Usually, index files with a lot of export statements, more info here

But really, what it does?

Given a module that export its dependencies using a barrel file like this:

// my-lib/index.js
export { map } from './dist/map';
export { chain } from './dist/chain';
export { filter } from './dist/filter';
export { groupBy } from './dist/groupBy';

And that you import it like this:

import { map, chain } from 'my-lib';

it will transform your code to:

import { map } from 'my-lib/dist/map';
import { chain } from 'my-lib/dist/chain';

Since a barrel file exports all files from a lib, babel/bundlers usually will import and parse all of those files because they can have side effects.

This plugin will drop unused imports from barrel files at lib roots, which will also remove import side effects.

This probably can help you to reduce the bundle size and help your bundler to be faster.

Why? babel isn't a bundler, why resolve in babel?

Because React Native sucks (or I suck because I don't know how to do this in metro).

Not supported

Full imports

Right now, this plugin doesn't support full imports like:

import all from 'my-lib'

We could ignore this, in the future and just warn that this kind of import make this plugin useless.

Barrel effects and local exports

Since this plugin is meant to make the barrel file 'invisible' to the bundler, it will not resolve local exports.

a barrel file like this:

export default "foo";

if (!x) {
  throw Error("foo")
}

Will do nothing

CommonJS dynamic stuff

CommonJS is supported, although a lot of corner cases are not supported because I wrote myself the code to find and track exports. (PR welcome, would love to improve the CJS support)

Usually, a babel generated CommonJS will work fine.

Installation

npm install --save babel-plugin-resolve-barrel-files
# or
yarn add -D babel-plugin-resolve-barrel-files

Usage

Declare it in your babel config file:

const path = require('path');

module.exports = {
  // ...,
  plugins: [
    [
      'resolve-barrel-files',
      {
        'my-lib': {
          moduleType: 'commonjs', // or 'esm'
          barrelFilePath: require.resolve('my-lib')
          // if you want to debug this plugin
          // logLevel: "debug" | "info" 
        },
      },
    ]
  ]
}

Note: if you make changes to the babel config be sure to restart your bundler with a clear babel cache

I have a problem, what should I do?

Feel free to open a PR or an issue and I'll try to help you :D

Mentions

Heavily inspired by babel-plugin-transform-imports