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

jscodeshift-transport

v2.0.0

Published

jscodeshift transform and API for finding and replacing module names in import/require statements

Downloads

13

Readme

jscodeshift-transport

Build Status Coverage Status semantic-release Commitizen friendly npm version

A great jscodeshift transform and API for finding and replacing module names in import/require statements. I wrote this because other transforms I found seemed not as convenient or comprehensive. This handles both relative imports/requires and imports/requires from node_modules. For instance, it's easy to replace a component from a library with your own wrapper in a local file:

jscodeshift -t ~/jscodeshift-transport ./src \
  --find=@material-ui/core/Button \
  --replace=./src/components/Button

The correct relative path will be used in each replacement, relative to the file being processed. It will work just as well if you swap the --find and --replace values, restoring the original module names.

Requirements

  • Node 8 or greater

requires it's not magic enough to handle

  • scopes where require is redeclared, e.g. requires inside a function (require) { ... } will be ignored, if you're doing that, please stahp
  • computed require paths (any argument besides a string literal)

CLI

Run index.js in this repo with jscodeshift and pass two options to it:

  • --find=<VALUE>: The module name to find, just like you would use in an import or require statement, relative to the current working directory: Paths to local files must be absolute or begin with ../ or ./; otherwise, it is assumed you meant an import from node_modules.
  • --replace=<VALUE>: The module name to replace it with, just like you would use in an import or require statement, relative to the current working directory: Paths to local files must be absolute or begin with ../ or ./; otherwise, it is assumed you meant an import from node_modules.

It's also possible to do regex replacement on module names:

  • --regex=1: Treat --find as a RegExp and find module names that match it, and regex replace --find with --replace on the module names
  • --flags=<FLAGS>: Regular expression flags to use for --regex
npm i -g jscodeshift
cd ~
git clone https://github.com/jcoreio/jscodeshift-transport
cd ~/jscodeshift-transport
npm i
cd ~/path/to/your/project
jscodeshift -t ~/jscodeshift-transport ./src \
  --find=@material-ui/core/Button \
  --replace=./src/components/Button

API

jscodeshift-transport exports a replaceModuleNames function you can use in your own jscodeshift transforms.

const { replaceModuleNames } = require('jscodeshift-transport')

It accepts the following arguments:

file: string

The path to the file being transformed

root: Collection

The root jscodeshift Collection from the source code of file. You must use the babylon parser (e.g. require('jscodeshift').withParser('babylon')(code))

find: string | RegExp | (moduleName: string) => boolean

The module name to find in import/require statements. Paths starting with ./ or ../ are treated as relative to the current working directory, not file. If a RegExp is given, it will find all module names that match the RegExp. If a function is given, it will find all module names for which find(moduleName) is truthy.

replace: string | (moduleName: string, info: Info) => ?string

The module name to replace find with in import/require statements, or a function that computes the replacement. Paths starting with ./ or ../ are treated as relative to the current working directory, not file. If you pass a function, it is called with the moduleName and an info object with the following properties, and may return a string replacement module name.

  • file: string: The file you passed to replaceModuleNames.
  • path: NodePath: The babel NodePath for the import or require statement.

If find is a RegExp, the new module name will be computed as moduleName.replace(find, replace).