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

gatsby-codemods

v4.13.1

Published

A collection of codemod scripts for use with JSCodeshift that help migrate to newer versions of Gatsby.

Downloads

13,004

Readme

gatsby-codemods

A collection of codemod scripts for use with JSCodeshift that help migrate to newer versions of Gatsby.

Note: Codemods are designed to rewrite your project's files. Ensure you have a backup before going any further.

Setup & Run

There are two ways to run codemods on this package.

npx

npx gatsby-codemods <codemod-name> <filepath>

filepath is not required and will default to the directory you're currently in.

Note that you cannot pass additional flags to this command. It will automatically run the codemod against file extensions js, jsx, ts, tsx and ignore the node_modules, .cache and public directories of your project.

JSCodeshift

  1. Install JSCodeshift as a global module
npm install --global jscodeshift
  1. Install this package
npm install gatsby-codemods
  1. Run a transform from this package on your project
jscodeshift -t node_modules/gatsby-codemods/transforms/global-graphql-calls.js my-project

Note that jscodeshift tries to match the formatting of your existing code, but you may need to use a tool like prettier to ensure consistency after running these codemods.

Structure of a jscodeshift call:

  • jscodeshift -t <codemod-script> <path>
    • codemod-script - path to the transform file, see available scripts below
    • path - files or directory to transform, typically the path to your Gatsby project
    • use the -d option for a dry-run and use -p to print the output for comparison
    • use the --extensions option if your files have different extensions than .js (for example, --extensions js,jsx)
    • see all available jscodeshift options.

Included scripts

sort-and-aggr-graphql

Apply changes to the sort argument and aggregation's field argument as explained in the RFC: Change to sort and aggregation fields API.

See the Gatsby v4 to v5 migration guide for details on when to use this.

npx gatsby-codemods sort-and-aggr-graphql <filepath>

Example result:

{
- allMarkdownRemark(sort: { fields: [frontmatter___date], order: DESC }) {
+ allMarkdownRemark(sort: { frontmatter: { date: DESC } }) {
    nodes {
      ...fields
    }
  }
}

global-graphql-calls

Add a graphql import to modules that use the graphql tag function without an import. This was supported in Gatsby v1 and deprecated for Gatsby v2.

See the Gatsby v1 to v2 migration guide for details on when to use this.

npx gatsby-codemods global-graphql-calls <filepath>

Example result:

import React from "react"
+ import { graphql } from "gatsby"

export default ({ data }) => (
  <h1>{data.site.siteMetadata.title}</h1>
)

export const query = graphql`
  query {
    site {
      siteMetadata {
        title
      }
    }
  }
`

import-link

Import Link from gatsby instead of gatsby-link and remove the gatsby-link import.

See the Gatsby v1 to v2 migration guide for details on when to use this.

npx gatsby-codemods import-link <filepath>

Example result:

- import Link from "gatsby-link"
+ import { Link } from "gatsby"

export default props => (
  <Link to="/">Home</Link>
)

navigate-calls

Change the deprecated navigateTo method from gatsby-link to navigate from the gatsby module.

See the Gatsby v1 to v2 migration guide for details on when to use this.

npx gatsby-codemods navigate-calls <filepath>

Example result:

import React from "react"
- import { navigateTo } from "gatsby-link"
+ import { navigate } from "gatsby"

// Don't use navigate with an onClick btw :-)
// Generally just use the `<Link>` component.
export default props => (
-  <div onClick={() => navigateTo(`/`)}>Click to go to home</div>
+  <div onClick={() => navigate(`/`)}>Click to go to home</div>
)

rename-bound-action-creators

Rename boundActionCreators to actions. boundActionCreators has been deprecated in Gatsby v2

Note: Run this codemod only against files that use boundActionCreators instead of running it against a whole directory.

See the Gatsby v1 to v2 migration guide for details on when to use this.

npx gatsby-codemods rename-bound-action-creators <filepath>

Example result:

- exports.onCreateNode = ({ node, getNode, boundActionCreators }) => {
+ exports.onCreateNode = ({ node, getNode, actions }) => {
- const { createNodeField } = boundActionCreators
+ const { createNodeField } = actions
  if (node.internal.type === `MarkdownRemark`) {
    const slug = createFilePath({ node, getNode, basePath: `pages` })
    createNodeField({
      node,
      name: `slug`,
      value: slug,
    })
  }
}