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-import-util

v3.0.0

Published

Utility for manipulating imports within babel plugins

Downloads

1,664,406

Readme

babel-import-util

Makes it easier for a babel plugin to emit imported names. Key benefits:

  • the output composes correctly with subsequent babel plugins, because we update Babel's understanding of the bindings
  • redundant imports will be deduplicated automatically
  • written in TypeScript

Usage by example:

If you want to rewrite:

myTarget('hello world');

To:

import { theMethod } from 'my-implementation';
theMethod('hello world');

Your plugin would look like this:

function testTransform(babel) {
  return {
    visitor: {
      Program: {
        enter(path, state) {
          // Always instantiate the ImportUtil instance at the Program scope
          state.importUtil = new ImportUtil(babel.types, path);
        },
      },
      CallExpression(path, state) {
        let callee = path.get('callee');
        if (callee.isIdentifier() && callee.node.name === 'myTarget') {
          state.importUtil.replaceWith(callee, (i) =>
            i.import(callee, 'my-implementation', 'theMethod')
          );
        }
      },
    },
  };
}

API

import type { NodePath } from '@babel/traverse';
import type * as t from '@babel/types';

class ImportUtil {
  /*
   Replace `target` with the new node produced by your callback. Your
   callback can use `i.import` to gain access to imported identifiers.

   Example:

   util.replaceWith(path, (i) =>
     t.callExpression(i.import('my-library', 'someFunction'), [])
   );
  */
  replaceWith<T extends t.Node, R extends t.Node>(
    target: NodePath<T>,
    fn: (i: Importer) => R
  ): NodePath<R>;

  /*
    Similar to `replaceWith` above, except instead of replacing the target 
    we will insert the new Node before or after it.
  */
  insertAfter<T extends t.Node, R extends t.Node>(
    target: NodePath<T>,
    fn: (i: Importer) => R
  ): NodePath<R>;
  insertBefore<T extends t.Node, R extends t.Node>(
    target: NodePath<T>,
    fn: (i: Importer) => R
  ): NodePath<R>;

  // If needed, adds a bare import like:
  //    import "your-module";
  importForSideEffect(moduleSpecifier: string): void;

  // Remove an import specifier. If the removed specifier is
  // the last one on the whole import statement, the whole
  // statement is also removed.
  //
  // You can use "default" and "*" as exportedName to handle
  // those special cases.
  removeImport(moduleSpecifier: string, exportedName: string): void;

  // Remove all imports from the given moduleSpecifier. Unlike
  // removeImport(), this can also remove "bare" import statements
  //  that were purely for side effect.
  removeAllImports(moduleSpecifier: string): void;

  // Import the given value (if needed) and return an Identifier representing
  // it.
  // CAUTION: this is a lower-level API that leaves some of the reference
  // safety up to you. It's better to use replaceWith, insertAfter, insertBefore,
  // or mutate. But this can still be helpful in contexts where you're already
  // planning to manage babel's scopes anyawy.
  import(
    // the spot at which you will insert the Identifier we return to you
    target: NodePath<t.Node>,

    // the path to the module you're importing from
    moduleSpecifier: string,

    // the name you're importing from that module. Use "default" for the default
    // export. Use "*" for the namespace.
    exportedName: string,

    // Optional hint for helping us pick a name for the imported binding
    nameHint?: string
  ): t.Identifier;
}