Gathering detailed insights and metrics for gatsby-plugin-ts
Gathering detailed insights and metrics for gatsby-plugin-ts
Gathering detailed insights and metrics for gatsby-plugin-ts
Gathering detailed insights and metrics for gatsby-plugin-ts
gatsby-plugin-ts-config
Configure Gatsby to use Typescript configuration files
gatsby-plugin-ts-checker
Adds typescript type checking to Gatsby
gatsby-plugin-ts-loader
Adds Typescript and tslint support to gatsby.
gatsby-typescript
Typescript support via ts-loader & fork-ts-checker-webpack-plugin + automate codegen
Alternative typescript support plugin for Gatsbyjs. Aims to make using typescript in Gatsby as painless as possible
npm install gatsby-plugin-ts
Typescript
Module System
Node Version
NPM Version
TypeScript (78.57%)
JavaScript (14.86%)
CSS (6.56%)
Total Downloads
647,345
Last Day
145
Last Week
2,326
Last Month
9,215
Last Year
99,942
MIT License
122 Stars
152 Commits
18 Forks
3 Watchers
25 Branches
14 Contributors
Updated on Apr 03, 2024
Minified
Minified + Gzipped
Latest Version
3.1.1
Package Id
gatsby-plugin-ts@3.1.1
Unpacked Size
12.69 kB
Size
5.13 kB
File Count
6
NPM Version
lerna/4.0.0/node@v16.13.1+arm64 (darwin)
Node Version
16.13.1
Cumulative downloads
Total Downloads
Last Day
-11%
145
Compared to previous day
Last Week
1.7%
2,326
Compared to previous week
Last Month
12%
9,215
Compared to previous month
Last Year
-11%
99,942
Compared to previous year
3
4
An alternative to the official typescript plugin, with ts-loader
& automatic type generation for your graphql queries (using graphql-code-generator
)
yarn add typescript gatsby-plugin-ts
Add this to your gatsby config like any other plugins:
1// gatsby-config.js 2module.exports = { 3 plugins: [ 4 `gatsby-plugin-ts`, 5 ] 6}
Unlike the official plugin, you'd have to bring your own tsconfig.json
.
1# generate a tsconfig if you have none 2tsc --init
In order for this plugin to work right, you'd need to set your compile options like the following:
1 "compilerOptions": { 2 "target": "ES2018", /* or at least ES2015 */ 3 "module": "ESNext", /* or at least ES2015 */ 4 "lib": ["dom"], /* <-- required! */ 5 "jsx": "preserve", /* <-- required! */ 6 "moduleResolution": "node", /* <-- required! */ 7 8 /* for mixed ts/js codebase */ 9 "allowJs": true, 10 "outDir": "./build" /* this won't be used by ts-loader */ 11 /* other options... */ 12 } 13
key | default | value |
---|---|---|
typecheck options | ||
options.tsLoader | {} | option to be passed into ts-loader . transpileOnly is always true , since typechecking is handled by fork-ts-checker-webpack-plugin . See ts-loader docs for more |
options.alwaysCheck | false | ⚠️deprecated By default type checking is disabled in production mode (during gatsby build ). Set this to true to enable type checking in production as well |
options.typeCheck | true | Enable / disable type checking with fork-ts-checker-webpack-plugin . |
options.forkTsCheckerPlugin | {} | Options that'll be passed to fork-ts-checker-webpack-plugin . For all options, please see their docs |
codegen options | ||
options.codegen | true | enable / disable generating definitions for graphql queries |
options.documentPaths | ['./src/**/*.{ts,tsx}', | The paths to files containing graphql queries. ⚠️ The default paths will be overwritten by the documentPaths you pass in, so please make sure to include all necessary paths ⚠️ |
options.fileName | graphql-type.ts | path to the generated file. By default, it's placed at the project root directory & it should not be placed into src , since this will create an infinite loop |
options.codegenDelay | 200 | amount of delay from file change to codegen |
options.pluckConfig | { globalGqlIdentifierName: "graphql", modules: [ { name: 'gatsby', identifier: 'graphql' } ] } | options passed to graphql-tag-pluck when extracting queries and fragments from documents |
options.failOnError (2.5.0) | process.env.NODE_ENV === 'production' | Throw error if the codegen fails. By default only apply to production builds. |
options.codegenConfig (^2.7.0) | {} | Add config directly to graphql-codegen . These key-value config will be applied to every graphql-codegen plugins. See graphql-codegen docs on the config field |
options.codegenPlugins (^2.7.0) | [] | Add additional plugins to graphql-codegen . We use the same format as Gatsby's. See example usage below. |
options.additionalSchemas (^2.6.0) | [] | array of additional schemas (other than the schema used by gatsby queries) for which types should be generated for. This is useful when you use client-side queries (e.g. with apollo-client) where you are querying another schema/endpoint |
1module.exports = { 2 plugins: [ 3 `gatsby-plugin-ts`, 4 ] 5}
1module.exports = { 2 plugins: [ 3 { 4 resolve: `gatsby-plugin-ts`, 5 options: { 6 fileName: `gen/graphql-types.ts`, 7 } 8 } 9 ] 10}
1// gatsby-config.js 2{ 3 resolve: `gatsby-plugin-ts`, 4 options: { 5 tsLoader: { 6 logLevel: 'warn', 7 }, 8 forkTsCheckerPlugin: { 9 eslint: true, 10 }, 11 fileName: `types/graphql-types.ts`, 12 codegen: true, 13 codegenDelay: 250, 14 typeCheck: false, 15 pluckConfig: { 16 // this is the default config 17 globalGqlIdentifierName: 'graphql', 18 modules: [ 19 { name: 'gatsby', identifier: 'graphql' }, 20 ], 21 }, 22 additionalSchemas: [ 23 { 24 key: 'example', 25 fileName: 'graphql-types-example.ts', 26 schema: 'https://example.com/graphql', 27 pluckConfig: { 28 // config to ensure only queries using the `gql` tag are used for this schema 29 globalGqlIdentifierName: 'gql', 30 modules: [ 31 { 32 name: 'graphql-tag', 33 identifier: 'gql', 34 }, 35 ], 36 }, 37 } 38 ], 39 }, 40}
gatsby-config
has to be a .js
filegatsby-node
is run directly by node
, so it has to be a .js file as well. It is a shame, because in a complicated Gatsby app it is where a lot of logic live & will benefit from ts. As a work around, it can be built with tsc
independently, in a script in package.json
or somehow in gatsby's pre-init hook.__PATH_PREFIX__
can be handled by declaring this code somewhere:1// src/global.d.ts 2declare const __PATH_PREFIX__: string
By default, this plugin will build typing for your queries automatically to graphql-types.d.ts
on every edit. Please note that the definition file should not be placed inside src
since this triggers a never ending loop during development.
In order to take advantage of the generated code, user needs to name their query:
1// src/pages/index.tsx 2 3 export const pageQuery = graphql` 4- query { 5+ query BlogIndex { 6 site { 7 siteMetadata { 8 title 9 } 10 } 11 ...
...and import it from the generated type file:
1// src/pages/index.tsx 2 3import { BlogIndexQuery } from '../graphqlTypes' 4 5interface IBlogIndexProps { 6 data: BlogIndexQuery; 7 location: Location; 8} 9 10const BlogIndex: React.FC<IBlogIndexProps> = ({ data, location }) => { 11 ... 12}
Previously this plugin disable type checking in production by default, which can be changed by setting alwaysCheck
to true
. Since 2.0.0 it no longer does this. If you want to preseve the previous behavior, please set the typeCheck
option like below:
1{ 2 resolve: 'gatsby-plugin-ts', 3 options: { 4 // Disable type checking in production 5 typeCheck: process.env.NODE_ENV !== 'production', 6 } 7}
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 8/26 approved changesets -- score normalized to 3
Reason
project is archived
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
106 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More