-
SummaryThis is an extension of the already existing RFC: gatsby-config/node typescript support We want to introduce an Ahead of Time (AOT) compilation of Gatsby's config files (gatsby-config and gatsby-node) to support use cases like TypeScript. In this first phase TypeScript will also be the only use case, however this will enable writing ESModules Syntax / You can try out a Proof of Concept with: npm install gatsby@latest Then you'll be able to write and use How can I help?
Detailed designYou can find the code of the current Pull Request here: #34776 It uses https://parceljs.org/ to compile Then the internal pieces of Gatsby don't look for Parcel is super fast and also supports using That's pretty much the technical details to make this work. Some of the current limitations that should be supported will warrant some more changes, but fundamentally the technical solution is: Use Parcel to compile the files to a directory and point Gatsby to that new directory. Drawbacks / AlternativesThe RFC gatsby-config/node typescript support already explained AOT and JIT in some detail (and favored JIT). Drawbacks we saw with JIT:
Drawbacks we saw with AOT:
Why we chose AOT:
Rollout
What's next?This is not the end of Gatsby's TypeScript support initiative! Ideas that people voiced:
These will follow as additional RFCs and/or features. I'm also aware that the feature in its current form is lacking any form of customization aside from |
Beta Was this translation helpful? Give feedback.
Replies: 29 comments 93 replies
-
Thanks for all the work on this @LekoArts. We'll definitely be trying out the PoC and giving feedback. |
Beta Was this translation helpful? Give feedback.
-
What about |
Beta Was this translation helpful? Give feedback.
-
local plugins would be great, is that coming soon ? |
Beta Was this translation helpful? Give feedback.
-
Saw this pop up a few days ago, and thought that it would be a boon in this project: https://kdy1.dev/posts/2022/1/tsc-go A bundler with Typescript support + Typechecking, so you’re not sacrificing type safety, running natively 🚀. Unfortunately, it will probably be in development for a while yet. Besides that, I did have a question: has built in Typechecking been considered? If parcel allows certain hooks, I imagine it would be possible to fork a type checker during transpilation/bundling. Obviously this would be a convenience feature, since we can always run our own type checker in parallel with Gatsby, but it would be nice if the work for it was already handled, and integrated directly into the build. Additionally, will it be possible to configure Parcel from userland? There’s various features supported in the existing Typescript universe that I would like to keep, but that may need to be reimplemented in a way that will integrate with Parcel… but to plug them in, we would need to be able to add our own configurations. |
Beta Was this translation helpful? Give feedback.
-
Just for clarification: This link seems to point to a PR fixing a type hint for a CLI option. I'm assuming it was supposed to be parcel-bundler/parcel#6925? |
Beta Was this translation helpful? Give feedback.
-
I am getting an issue with gatsby@npm:4.7.0-alpha-ts-aot.6 when running gatsby build
|
Beta Was this translation helpful? Give feedback.
-
And [email protected] doesn't detect ts :(
|
Beta Was this translation helpful? Give feedback.
-
Hey, Firstly, this version seems to have problems writing files
on the first run of gatsby build, and
on consecutive ones if i don't manually run gatsby clean before that. This happens for a barebones test project, only consisting of
and with just gatsby installed. This is testing under windows, but i can assure you that i own the directory and all files within it and have full access. Additionally,if i first run clean the project builds, but not before spamming the console with error messages formed like
this starts to happen after Thirdly, placingexport default {
plugins: [
{
resolve: "gatsby-plugin-page-creator",
options: {
path: "./src/pages",
ignore: [
"**/__generated__/*",
],
}
},
]
} in the gatsby-config to override the default behavior of the page creator results in
which does not happen in dockerI'm sure at this stage you have better things to do than tracking down some non descript error, but building a more complicated project inside of docker |
Beta Was this translation helpful? Give feedback.
-
I haven't seen this package mentioned before so I just thought you might like to discover it: https://github.com/egoist/bundle-require |
Beta Was this translation helpful? Give feedback.
-
Hi @LekoArts , I'm reading through the proposal, but I'm unsure of the complexity that comes with AOT... and the introduction of another build tool (parcel, along with its dependencies), but I'll keep reading to get a better grasp. BUT, I have been using Gatsby with full typescript support with this wrapper CLI I wrote around https://github.com/newrade/newrade-core/blob/master/packages/core-cli/src/commands/gatsby.ts I can use all config files in Local plugins work 100% in TS: The TL;DR is
It works like a charm... any thoughts on this approach? Thanks, Phil |
Beta Was this translation helpful? Give feedback.
-
Thank you @LekoArts for the work on this, and I'm 100% for the AOT approach. One frustration I've had with trying to use TypeScript with Gatsby and a JIT approach with Stack TracesI notice that using ERROR Failed to compile gatsby files. Missing initializer in const declaration
Error: Missing initializer in const declaration
not finished compile gatsby files - 1.122s Intellisense for Gatsby Config OptionsI wanted Intellisense for the Gatsby plugins I'm providing options for in One possibility would be extracting types from plugins that provide Joi validation schemas using Gatsby's built-in API. That would have the advantage of avoiding duplication and keeping types and validation in sync. Many plugins don't provide validation, so a manual approach to providing typings would still be required. There are also some Joi methods that plugins are using and that don't translate (either automatically or well) to TS types like One issue I haven't resolved with providing manual typings is that Gatsby configuration allows either a plugin name if the plugin option defaults are used (or there are no options), and an object with const gatsby_plugin_theme_ui: ThemeUIConfig = {
resolve: `gatsby-plugin-theme-ui`,
options: {
preset: `./src/styles/theme`,
prismPreset: `vs-dark`,
},
}
const GatsbyConfig = {
siteMetadata,
plugins: [
...
gatsby_plugin_theme_ui,
...
],
}
export default GatsbyConfig One really nice aside is that I get type checking and Intellisense for my Theme UI theme CSS:sparkles:. I'm building an MDX site, and it uses all of the plugins set up in @LekoArts Minimal Blog theme. Here's a gist of the typings I worked out. There's no tests, and I'm sure there's a mistake or three. It seems to me the best approach for typing Gatsby plugin configuration options is to have a standard named export for their types, and encourage plugin authors to include them in the plugin documentation. It could then be imported into import { GatsbyConfig as ThemeUiConfig } from `gatsby_plugin_theme_ui` And then mention a best practice to use DefinitelyTyped for community-provided config option typings with a standard naming convention, like Gatsby Node API TypingsThe Gatsby project manual typings for Local PluginsI'm looking forward to local plugin support! My particular use case atm involves Docs on Parcel Compiler LimitationsI saw the note with a link to Parcel about "Parcel Limitations" in the OP. The eventual documentation should note that Parcel requires and is implicitly setting the TSConfig LintingCode editors (at least VS Code and Jetbrains) will automatically pull in JSON schema for
Gratitude:sparkling_heart: :fire: :zap: :fireworks:Thank you @LekoArts for the work on this. |
Beta Was this translation helpful? Give feedback.
-
Edit: As requested - I'm putting this in a new comment 😁 Hi - I'm using version ERROR #10124 CONFIG
It looks like you were trying to add the config file? Please rename "gatsby-config.ts" to "gatsby-config.js" However when I use ts-node wrapper that we use in our project - build and develop succeeds, but I cannot serve the project since we've always used Could that be something in our tsconfig?
Here is also fragment of
|
Beta Was this translation helpful? Give feedback.
-
Thank you for linking to this RFC in the 4.8 release notes; hopefully future release notes can mention when workspace support is ready so we can give it a try. Being able to write these files in TypeScript is exciting; being able to import TypeScript and ESM into them is even more exciting. This will de-cruft a lot of our codebase. I saw an earlier request for built-in type checking. I think that makes sense for smaller projects, but larger projects probably already disable Gatsby's built-in linting in favour of standalone ESLint and TSC configs, as we do. I would suggest that linting could be moved to a removable plugin, and type checking could be implemented there as well. This would go a long way toward cutting down the number of dependencies in today's monolithic The same thing goes for GraphQL type generation. I would suggest you first expose your schema builder for integration with GraphQL Codegen, then implement it in a removable plugin. This would still improve the out-of-the-box experience, but it would help larger-scale projects, too (we currently need to run Other projects have moved in this direction, and our dev experience has benefited greatly from enhanced control of transient dependencies. Please don't grow the monolith! 🪨 |
Beta Was this translation helpful? Give feedback.
-
This feature is now available in |
Beta Was this translation helpful? Give feedback.
-
Some comments after GA release 👇 (Edited message) |
Beta Was this translation helpful? Give feedback.
-
This is now generally available in 4.9: https://www.gatsbyjs.com/docs/reference/release-notes/v4.9/ |
Beta Was this translation helpful? Give feedback.
-
@cameronbraid If you're hitting segfaults, can you check which versions of npm list lmdb Or yarn why lmdb |
Beta Was this translation helpful? Give feedback.
-
So how does GraphQL codegen support going? I'm ready to release gatsby-plugin-typegen v3 and it works quite smoothly. I decided to continue the project again because I have some ideas to improve the core DX of my Gatsby projects, I'm curious what the Gatsby core team's plan is. |
Beta Was this translation helpful? Give feedback.
-
Really glad that TypeScript is coming to I'm trying to convert this entry, and it doesn't appear to be working. The original:
Into this:
There are no complaints, but when I run Have I converted it wrong? Thanks |
Beta Was this translation helpful? Give feedback.
-
Ran into another cryptic build issue (works fine in develop) with gatsby@next and ts : (looks related to the one above, but not missing module but this time a accessing of .path on undefined
|
Beta Was this translation helpful? Give feedback.
-
Thanks for helping me solve the
The error is
Any idea how I should replace Thanks in advance |
Beta Was this translation helpful? Give feedback.
-
Probably not the right place for this, but Gatsby Cloud CMS Preview fails on TypeScript validation errors, whereas it succeeds in production. |
Beta Was this translation helpful? Give feedback.
-
One issue I ran into which was causing hours of pain, was parcel would compile successfully but none of the gatsby-node apis would run. The underlying cause was because I use a yarn link: dependency as follows :
Then in code I would What this did was make parcel think that "src" was a real module leaving it untranspiled. However when gatsby ran no error was thrown or logged and all my gatsby-node apis would be ignored completely. The solution was to configure parcel via
It might be worth adding this to the docs somewhere. |
Beta Was this translation helpful? Give feedback.
-
My Parcel PR to mitigate the Also, please read the new RFC: #35420
|
Beta Was this translation helpful? Give feedback.
-
Once #35446 is merged and released the |
Beta Was this translation helpful? Give feedback.
-
@LekoArts You mention “Future ESM support possible” – I also see questions around the current state in this issue (maybe you could share some more info?): #31599 (comment) |
Beta Was this translation helpful? Give feedback.
-
Hi! A couple questions for everyone we'd like your feedback on:
|
Beta Was this translation helpful? Give feedback.
-
Hey folks,
This specifically happens when I import a TypeScript package from within a PNPM monorepo. These packages works well out of the box when imported within pages (it seems that as a default, external packages are transpiled, contrary to Next.js that will use a "transpilePackages" option?), but not in the config. The code is open source though not minimal, this is the codebase that powers the State of JS survey, it's visible in this pull request, in the "results" app. I'll try to craft a more minimal repro and open a bug ticket but I am not sure this is a bug in the first place or I just missed some configuration? The architecture of the monorepo is like this:
|
Beta Was this translation helpful? Give feedback.
-
So I assume that using |
Beta Was this translation helpful? Give feedback.
This is now generally available in 4.9: https://www.gatsbyjs.com/docs/reference/release-notes/v4.9/