This plugin will allow you to write your
gatsby-*
configuration files in Typescript.
Table of Contents:
-
Basic Information
-
Additional Information
-
Install using your project manager
npm install -D gatsby-plugin-ts-config
Before reading below, please note that it is recommended for you to define a
configDir
in the plugin options.Because of the process this plugin has to follow so that it can interpret your typescript configuration files, some conflicts may occur regarding node ownership if you keep your
gatsby-node.ts
in the project root. In order to place that file in a sub-directory, you will need to define aconfigDir
, and you will also need to put the rest of your configurations in the same place.
You will still need to define, at minimum, one .js
Gatsby configuration: gatsby-config.js
.
As usual, it needs to be in the root of your project. You will then use it to call this plugin, and the rest of your configuration will be in Typescript files.
-
The easy way
-
You can use the
generateConfig
utility that is exported bygatsby-plugin-ts-config
to set up the plugin array the way it needs to be. -
Doing it this way allows your IDE to read the type interface for the options, so you can use intellisense while defining the plugin options.
// gatsby-config.js const { generateConfig } = require('gatsby-plugin-ts-config'); module.exports = generateConfig();
-
-
The common way
- This can also be done the normal way. The utility above just makes it easy
// gatsby-config.js module.exports = { plugins: [ `gatsby-plugin-ts-config`, ] }
-
Configuration complete
-
After that, all of your configuration files can be written in Typescript:
- gatsby-browser.ts
- gatsby-config.ts
- gatsby-node.ts
- gatsby-ssr.ts
-
For extended usage examples, see the Extended Usage chapter
-
projectRoot
:{string}
- Default:
process.cwd()
- This defines your project's root directory for the plugin. All folder/file resolutions will be performed relative to this directory.
- Default:
-
configDir
:{string}
- Default:
projectRoot
- You can define a folder, relative to
projectRoot
, that will store your Typescript configuration files. If you do not define this option, then it will automatically useprojectRoot
.
- Default:
-
babel
:{boolean|TransformOptions}
- Default:
true
- Setting this to
true
, or an object, will cause the plugin to usebabel
for transforming typescript configurations. - If an object is defined, it must contain options valid for
babel
. See the babel options documentation for a description of the available options. Anything you can put in.babelrc
can be put here. - See Determining the interpreter below for details on how the interpeter is chosen
- Default:
-
tsNode
:{boolean|RegisterOptions}
- Default:
false
- Setting this to
true
or an object will causets-node
to be used, so long asbabel
is a falsy value. - If an object, you may use any options that are valid for
ts-node
'sregister()
function. See the ts-node options documentation here - See Determining the interpreter below for details on how the interpeter is chosen
- Default:
-
props
:{object}
-
Default:
{}
-
Reference: docs
-
This object will be passed to the default functions that are exported from
gatsby-config
andgatsby-node
, as well as anyincludePlugins()
calls. -
This is meant to contain a dynamic context; anything you may want to pass around to the various functions that this plugin supports.
- This object is mutable, so any changes you make to it after it has been passed to a function will be persistent.
-
If you are using the
generateConfig()
plugin declaration, this will be represented by that function's second parameter:// gatsby-config.js module.exports = generateConfig({}, { test: 1234 });
// .gatsby/gatsby-config.ts export default ({ projectRoot }, { test }) => { console.log(test) // 1234 }
-
Babel takes priority, so will be the default interpreter.
-
If
babel
is a truthy value, ortsNode
is a falsy value, thenbabel
will be chosen. -
If
babel
is a truthy value, andtsNode
is a truthy value, thenbabel
will be chosen. -
If
babel
is a falsy value, andtsNode
is a truthy value, thents-node
will be chosen.
For the moment, there is no way to layer ts-node
-> babel
, but the feature may be included
in a later release
The primary purpose of this plugin is to allow you to write your gatsby-config
and gatsby-node
in Typescript. To that end, your gatsby-config
and gatsby-node
may follow the standard
Gatsby API pattern.
However, this plugin also supports some more advanced features that you may find useful:
-
gatsby-*
default export functions- You are no longer restricted to exporting a simple object from your root project's
gatsby-config
. Your Typescriptgatsby-config
andgatsby-node
may export a function as the default export. - The default export functions receive various parameters that may be useful to you.
- You are no longer restricted to exporting a simple object from your root project's
-
Property Bag
- A mutable object that is passed around to all of the functions that this plugin supports
- Allows you to share information between
gatsby-config
functions andgatsby-node
.
-
includePlugins()
- A utility function that allows more advanced declaration of Gatsby plugins.
- Adds support for transpiling local plugins
For more information, see the Gatsby API chapter
If you feel a feature is missing, or you find a bug, please feel free to file an issue at https://github.com/Js-Brecht/gatsby-plugin-ts-config/issues.
I would also welcome any additions anybody would like to make.
If you enjoyed using this plugin, and you'd like to help support its development, you're welcome to donate!