Skip to content

Commit aed658a

Browse files
committed
chore(gatsby): Create type declarations for gatsby plugin files (#4928)
When creating a Gatsby plugin (which is what `@sentry/gatsby` actually is), there are certain files[1] which need to live at the package root level. Because they are written in JS (not TS) and don't live in `src/`, we have thus far more or less ignored them as far as type-ful processing goes. With the recent updates to TS, jest, and ts-jest, typechecking has now gotten stricter (especially in tests) and so now declaration files are needed for those gatsby-required, root-level files if we want to test them (which we do). This adds machinery to create, manage, and publish these declaration files. [1] https://www.gatsbyjs.com/docs/files-gatsby-looks-for-in-a-plugin/
1 parent 9de41b8 commit aed658a

File tree

6 files changed

+27
-6
lines changed

6 files changed

+27
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ tmp.js
4040
# eslint
4141
.eslintcache
4242
eslintcache/*
43+
44+
*.d.ts

packages/gatsby/.eslintrc.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ module.exports = {
66
parserOptions: {
77
jsx: true,
88
},
9-
// ignoring the package-specific prepack script here b/c it is not
10-
// covered by a `tsconfig` which makes eslint throw an error
11-
ignorePatterns: ['scripts/prepack.ts'],
9+
// ignore these because they're not covered by a `tsconfig`, which makes eslint throw an error
10+
ignorePatterns: ['scripts/prepack.ts', 'gatsby-browser.d.ts', 'gatsby-node.d.ts'],
1211
extends: ['../../.eslintrc.js'],
1312
};

packages/gatsby/.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
# Gatsby specific
1111
!gatsby-browser.js
1212
!gatsby-node.js
13+
!gatsby-browser.d.ts
14+
!gatsby-node.d.ts

packages/gatsby/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
"react": "^18.0.0"
3535
},
3636
"scripts": {
37-
"build": "run-p build:cjs build:esm build:types",
37+
"build": "run-p build:cjs build:esm build:types build:plugin",
3838
"build:cjs": "tsc -p tsconfig.cjs.json",
3939
"build:dev": "run-s build",
4040
"build:es5": "yarn build:cjs # *** backwards compatibility - remove in v7 ***",
4141
"build:esm": "tsc -p tsconfig.esm.json",
42+
"build:plugin": "tsc -p tsconfig.plugin.json",
4243
"build:types": "tsc -p tsconfig.types.json",
4344
"build:watch": "run-p build:cjs:watch build:esm:watch build:types:watch",
4445
"build:cjs:watch": "tsc -p tsconfig.cjs.json --watch",
@@ -48,7 +49,7 @@
4849
"build:types:watch": "tsc -p tsconfig.types.json --watch",
4950
"build:npm": "ts-node ../../scripts/prepack.ts && npm pack ./build",
5051
"circularDepCheck": "madge --circular src/index.ts",
51-
"clean": "rimraf build coverage",
52+
"clean": "rimraf build coverage *.d.ts",
5253
"fix": "run-s fix:eslint fix:prettier",
5354
"fix:eslint": "eslint . --format stylish --fix",
5455
"fix:prettier": "prettier --write \"{src,test,scripts}/**/*.ts\"",

packages/gatsby/scripts/prepack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import * as fs from 'fs';
77
import * as path from 'path';
88

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

1111
export function prepack(buildDir: string): boolean {
1212
// copy package-specific assets to build dir

packages/gatsby/tsconfig.plugin.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
4+
"include": ["gatsby-browser.js", "gatsby-node.js"],
5+
6+
"compilerOptions": {
7+
// should include all types from `./tsconfig.json` plus types for all test frameworks used
8+
// "types": ["node", "jest"]
9+
"declaration": true,
10+
"declarationMap": false,
11+
"emitDeclarationOnly": true,
12+
"allowJs": true,
13+
"skipLibCheck": true
14+
15+
// other package-specific, plugin-specific options
16+
}
17+
}

0 commit comments

Comments
 (0)