Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
gatsby-codemods
Advanced tools
A collection of codemod scripts for use with JSCodeshift that help migrate to newer versions of Gatsby.
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.
There are two ways to run codemods on this package.
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.
npm install --global jscodeshift
npm install gatsby-codemods
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 belowpath
- files or directory to transform, typically the path to your Gatsby project-d
option for a dry-run and use -p
to print the output for comparison--extensions
option if your files have different extensions than .js
(for example, --extensions js,jsx
)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 v2 migration guide for details on when to use this.
jscodeshift -t node_modules/gatsby-codemods/dist/transforms/global-graphql-calls.js <path>
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 v2 migration guide for details on when to use this.
jscodeshift -t node_modules/gatsby-codemods/dist/transforms/import-link.js <path>
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 v2 migration guide for details on when to use this.
jscodeshift -t node_modules/gatsby-codemods/dist/transforms/navigate-calls.js <path>
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 v2 migration guide for details on when to use this.
jscodeshift -t node_modules/gatsby-codemods/dist/transforms/rename-bound-action-creators.js <path-to-file>
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,
})
}
}
Check out issue 5038 in the Gatsby repo for additional codemod ideas.
We'd love your help with writing these!
FAQs
A collection of codemod scripts for use with JSCodeshift that help migrate to newer versions of Gatsby.
The npm package gatsby-codemods receives a total of 2,666 weekly downloads. As such, gatsby-codemods popularity was classified as popular.
We found that gatsby-codemods demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.