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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@monstermann/barrels-treeshake

v0.5.0

Published

Replaces the members of imported namespaces with direct imports.

Readme

Replaces the members of imported namespaces with direct imports.

Before:

import { User } from "#utils";

const email = User.email(user);

After:

import { email as _email } from "#utils/User/email";

const email = _email(user);

This makes namespaces, such as the ones created by @monstermann/barrels, free in bundle size without relying on what a bundler is able to tree-shake.

Installation

bun add -D @monstermann/barrels-treeshake

Usage

You decide which namespaces are replaced and what they are replaced with: resolve is called for each member of an imported namespace and returns the import that should be used instead, or nothing to leave the namespace alone.

import { treeshake } from "@monstermann/barrels-treeshake";

treeshake({
    resolve({ importAlias, importName, importPath, propertyName }) {
        if (importPath !== "#utils") return;
        return `import { ${propertyName} as ${importAlias} } from "#utils/${importName}/${propertyName}";`;
    },
});

| Property | import { Foo as Bar } from "foo"; Bar.baz | Description | | -------------- | ------------------------------------------- | ----------------------------------------------------------- | | importName | "Foo" | "*" for import * as Foo, undefined for import Foo. | | localName | "Bar" | | | importPath | "foo" | | | propertyName | "baz" | | | importAlias | "_baz" | A unique identifier to use as the local name of the import. | | filePath | | The file that is being transformed. | | scope | | All identifiers used in the file. |

resolve can be a list of resolvers, the first result is used.

Vite, Rolldown, tsdown

import { treeshake } from "@monstermann/barrels-treeshake";

export default defineConfig({
    plugins: [treeshake({ enforce: "pre", resolve })],
});

| Option | Default | Description | | --------- | -------------- | ---------------------------------------------------------------------- | | resolve | | See above. | | include | /\.[jt]sx?$/ | RegExp(s), only files whose path matches are transformed. | | exclude | | RegExp(s), files whose path matches are skipped. | | enforce | | "pre" or "post". | | debug | false | true or a RegExp matching file paths, prints what is being replaced. |

Bun

Bun.build only uses the first onLoad that returns something, so call transform from your own:

import { transform } from "@monstermann/barrels-treeshake";

await Bun.build({
    entrypoints: ["./src/index.ts"],
    plugins: [
        {
            name: "transforms",
            setup(build) {
                build.onLoad(
                    { filter: /\.tsx?$/ },
                    async ({ loader, path }) => {
                        const code = await Bun.file(path).text();
                        return {
                            contents:
                                transform(code, path, { resolve })?.code ??
                                code,
                            loader,
                        };
                    },
                );
            },
        },
    ],
});

Standalone

import { transform } from "@monstermann/barrels-treeshake";

const result = transform(code, "source.ts", { resolve });
result?.code;
result?.map;

Details

  • Named, default and wildcard imports are supported, as well as JSX (<Foo.Bar />).
  • A namespace is left untouched when one of its members is not resolved, or when the namespace itself is needed: const x = Foo, export { Foo }, Foo["bar"], Foo.bar = 1.
  • Declarations shadowing an import are respected.
  • When a namespace is also used as a type (let rect: Rect = Rect.create()), an import type is kept for it.
  • Only the first level is replaced: Foo.bar.baz becomes _bar.baz.