GithubHelp home page GithubHelp logo

babel-import-util's Introduction

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') {
          callee.replaceWith(state.importUtil.import(callee, 'my-implementation', 'theMethod'));
        }
      },
    },
  };
}

API

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

class ImportUtil {
  // Import the given value (if needed) and return an Identifier representing
  // it.
  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;

  // 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;
}

babel-import-util's People

Contributors

ef4 avatar patricklx avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

babel-import-util's Issues

Cannot read properties of null (reading 'source')

Version 1.2.0 of this package has, I think?, introduced a bug we noticed in this CI run as an example:

/home/runner/work/design-system/design-system/packages/components/@hashicorp/ember-flight-icons/components/flight-icon.js: Cannot read properties of null (reading 'source')TypeError: /home/runner/work/design-system/design-system/packages/components/@hashicorp/ember-flight-icons/components/flight-icon.js: Cannot read properties of null (reading 'source')

Pinning to 1.1.0 seems to fix the issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.