Skip to content

chore(gatsby): Create type declarations for gatsby plugin files #4928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ tmp.js
# eslint
.eslintcache
eslintcache/*

*.d.ts
5 changes: 2 additions & 3 deletions packages/gatsby/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ module.exports = {
parserOptions: {
jsx: true,
},
// ignoring the package-specific prepack script here b/c it is not
// covered by a `tsconfig` which makes eslint throw an error
ignorePatterns: ['scripts/prepack.ts'],
// ignore these because they're not covered by a `tsconfig`, which makes eslint throw an error
ignorePatterns: ['scripts/prepack.ts', 'gatsby-browser.d.ts', 'gatsby-node.d.ts'],
extends: ['../../.eslintrc.js'],
};
2 changes: 2 additions & 0 deletions packages/gatsby/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
# Gatsby specific
!gatsby-browser.js
!gatsby-node.js
!gatsby-browser.d.ts
!gatsby-node.d.ts
Comment on lines +13 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Do we want to include these declaration files in the tarball? If yes, then we need to adjust the Gatsby-specific prepack script to copy them to build, too. Currently they do not end up in the tarball. Or is there another reason why we're adding them to .npmignore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to include these declaration files in the tarball?

I was on the fence about this. I opted for including them because they're useful for debugging, but I don't feel strongly about it. What do you think?

If yes, then we need to adjust the Gatsby-specific prepack script to copy them to build, too.

Oh, good catch. Unless you think it's a terrible idea to add them, I'll fix this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can't hurt to include them. Don't know enough about Gatsby to know how many people would need them but if we're already producing them why not. We have to copy them to build/types to be found by the types entry point, i think, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing just came to my mind: If, in the tarball, they end up in build/types, we could just adjust tsconfig.plugin.json to output them to build/types, right? Then we don't have to adjust Gatsby's prepack.ts.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to copy them to build/types to be found by the types entry point, i think, right?

You know, this is an excellent question, to which I don't actually know the answer. ts-jest finds them where they are, and I'd have to experiment to see if it similarly finds them in types/. Another thought I have here, though, is that these two JS files are already outside of the entrypoint structure, so maybe it's fine that their declaration files are, too. Is the types endpoint even supposed to include them, given that main and module don't include the original source files?

Copy link
Member Author

@lobsterkatie lobsterkatie Apr 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: I added them to the prepack script (and a few other places I'd forgotten about), but left them alongside the the source files they describe. I say we try it like this, and if it's a problem, someone will tell us. (Though my gut says it's fine.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good point, I didn't think about that. I say if it works for the source file this way, then why not for the declarations as well. Sounds good.

5 changes: 3 additions & 2 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@
"react": "^18.0.0"
},
"scripts": {
"build": "run-p build:cjs build:esm build:types",
"build": "run-p build:cjs build:esm build:types build:plugin",
"build:cjs": "tsc -p tsconfig.cjs.json",
"build:dev": "run-s build",
"build:es5": "yarn build:cjs # *** backwards compatibility - remove in v7 ***",
"build:esm": "tsc -p tsconfig.esm.json",
"build:plugin": "tsc -p tsconfig.plugin.json",
"build:types": "tsc -p tsconfig.types.json",
"build:watch": "run-p build:cjs:watch build:esm:watch build:types:watch",
"build:cjs:watch": "tsc -p tsconfig.cjs.json --watch",
Expand All @@ -48,7 +49,7 @@
"build:types:watch": "tsc -p tsconfig.types.json --watch",
"build:npm": "ts-node ../../scripts/prepack.ts && npm pack ./build",
"circularDepCheck": "madge --circular src/index.ts",
"clean": "rimraf build coverage",
"clean": "rimraf build coverage *.d.ts",
"fix": "run-s fix:eslint fix:prettier",
"fix:eslint": "eslint . --format stylish --fix",
"fix:prettier": "prettier --write \"{src,test,scripts}/**/*.ts\"",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/scripts/prepack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as fs from 'fs';
import * as path from 'path';

const PACKAGE_ASSETS = ['gatsby-browser.js', 'gatsby-node.js'];
const PACKAGE_ASSETS = ['gatsby-browser.js', 'gatsby-browser.d.ts', 'gatsby-node.js', 'gatsby-node.d.ts'];

export function prepack(buildDir: string): boolean {
// copy package-specific assets to build dir
Expand Down
17 changes: 17 additions & 0 deletions packages/gatsby/tsconfig.plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "./tsconfig.json",

"include": ["gatsby-browser.js", "gatsby-node.js"],

"compilerOptions": {
// should include all types from `./tsconfig.json` plus types for all test frameworks used
// "types": ["node", "jest"]
"declaration": true,
"declarationMap": false,
"emitDeclarationOnly": true,
"allowJs": true,
"skipLibCheck": true

// other package-specific, plugin-specific options
}
}