Skip to content

Commit 4319028

Browse files
liana-pwilkmaia
andauthored
feat: TypeScript conversion of the codebase (#799)
* trying to setup typescript transpiling in graphql-hooks * GraphQLClient file converted to typescript * progress on typescript working, need to fix rollup bundling * rollup build working with esbuild + type checks passing * my comment in the ci.yml file seems to break the ci check... * add proper generation of `.d.ts` types * type generation improvements, trying to fix the typescript example * fixed a type issue with the typescript example + removed obsolete tsd from the graphql-hooks package * fixed last tests * type fix + conversion of local graphql client to typescript * chore: improve naming around graphql-ws client type * chore: set client context's default value to null Co-authored-by: Wilk Maia <[email protected]>
1 parent 8d5d230 commit 4319028

23 files changed

+40627
-265
lines changed

.github/workflows/ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
- run: npm run lint
2626
- run: npm run test:coverage
2727
- run: npm run tsd
28+
- run: npm run check-types
2829
- uses: coverallsapp/[email protected]
2930
with:
3031
github-token: ${{ secrets.GITHUB_TOKEN }}

config/rollup.config.js

+107-99
Original file line numberDiff line numberDiff line change
@@ -5,117 +5,125 @@ import replace from 'rollup-plugin-replace'
55
import commonjs from 'rollup-plugin-commonjs'
66
import { terser } from 'rollup-plugin-terser'
77
import { sizeSnapshot } from 'rollup-plugin-size-snapshot'
8+
import esbuild from 'rollup-plugin-esbuild' // Used for TS transpiling
89

910
// get the package.json for the current package
1011
const packageDir = path.join(__filename, '..')
1112
const pkg = require(`${packageDir}/package.json`)
1213
const external = [...Object.keys(pkg.peerDependencies || {})]
1314

1415
// name will be used as the global name exposed in the UMD bundles
15-
const generateRollupConfig = (name, overrides = {}) => [
16+
const generateRollupConfig = ({ name, overrides, entryPoint }) => {
17+
overrides = overrides || {}
18+
entryPoint = entryPoint || 'src/index.js'
1619
// CommonJS
17-
{
18-
input: 'src/index.js',
19-
output: { file: `lib/${pkg.name}.js`, format: 'cjs', indent: false },
20-
external,
21-
plugins: [babel(), sizeSnapshot()],
22-
...overrides
23-
},
24-
25-
// ES
26-
{
27-
input: 'src/index.js',
28-
output: { file: `es/${pkg.name}.js`, format: 'es', indent: false },
29-
external,
30-
plugins: [babel(), sizeSnapshot()],
31-
...overrides
32-
},
20+
return [
21+
{
22+
input: entryPoint,
23+
output: { file: `lib/${pkg.name}.js`, format: 'cjs', indent: false },
24+
external,
25+
plugins: [commonjs(), esbuild(), babel(), sizeSnapshot()],
26+
...overrides
27+
},
3328

34-
// ES for Browsers
35-
{
36-
input: 'src/index.js',
37-
output: { file: `es/${pkg.name}.mjs`, format: 'es', indent: false },
38-
external,
39-
plugins: [
40-
commonjs(),
41-
nodeResolve({
42-
jsnext: true
43-
}),
44-
replace({
45-
'process.env.NODE_ENV': JSON.stringify('production')
46-
}),
47-
terser({
48-
compress: {
49-
pure_getters: true,
50-
unsafe: true,
51-
unsafe_comps: true,
52-
warnings: false
53-
}
54-
}),
55-
sizeSnapshot()
56-
],
57-
...overrides
58-
},
29+
// ES
30+
{
31+
input: entryPoint,
32+
output: { file: `es/${pkg.name}.js`, format: 'es', indent: false },
33+
external,
34+
plugins: [commonjs(), esbuild(), babel(), sizeSnapshot()],
35+
...overrides
36+
},
5937

60-
// UMD Development
61-
{
62-
input: 'src/index.js',
63-
output: {
64-
file: `dist/${pkg.name}.js`,
65-
format: 'umd',
66-
name,
67-
indent: false
38+
// ES for Browsers
39+
{
40+
input: entryPoint,
41+
output: { file: `es/${pkg.name}.mjs`, format: 'es', indent: false },
42+
external,
43+
plugins: [
44+
commonjs(),
45+
nodeResolve({
46+
jsnext: true
47+
}),
48+
esbuild(),
49+
replace({
50+
'process.env.NODE_ENV': JSON.stringify('production')
51+
}),
52+
terser({
53+
compress: {
54+
pure_getters: true,
55+
unsafe: true,
56+
unsafe_comps: true,
57+
warnings: false
58+
}
59+
}),
60+
sizeSnapshot()
61+
],
62+
...overrides
6863
},
69-
external,
70-
plugins: [
71-
commonjs(),
72-
nodeResolve({
73-
jsnext: true
74-
}),
75-
babel({
76-
exclude: 'node_modules/**'
77-
}),
78-
replace({
79-
'process.env.NODE_ENV': JSON.stringify('development')
80-
}),
81-
sizeSnapshot()
82-
],
83-
...overrides
84-
},
8564

86-
// UMD Production
87-
{
88-
input: 'src/index.js',
89-
output: {
90-
file: `dist/${pkg.name}.min.js`,
91-
format: 'umd',
92-
name,
93-
indent: false
65+
// UMD Development
66+
{
67+
input: entryPoint,
68+
output: {
69+
file: `dist/${pkg.name}.js`,
70+
format: 'umd',
71+
name,
72+
indent: false
73+
},
74+
external,
75+
plugins: [
76+
commonjs(),
77+
nodeResolve({
78+
jsnext: true
79+
}),
80+
esbuild(),
81+
babel({
82+
exclude: 'node_modules/**'
83+
}),
84+
replace({
85+
'process.env.NODE_ENV': JSON.stringify('development')
86+
}),
87+
sizeSnapshot()
88+
],
89+
...overrides
9490
},
95-
external,
96-
plugins: [
97-
commonjs(),
98-
nodeResolve({
99-
jsnext: true
100-
}),
101-
babel({
102-
exclude: 'node_modules/**'
103-
}),
104-
replace({
105-
'process.env.NODE_ENV': JSON.stringify('production')
106-
}),
107-
terser({
108-
compress: {
109-
pure_getters: true,
110-
unsafe: true,
111-
unsafe_comps: true,
112-
warnings: false
113-
}
114-
}),
115-
sizeSnapshot()
116-
],
117-
...overrides
118-
}
119-
]
91+
92+
// UMD Production
93+
{
94+
input: entryPoint,
95+
output: {
96+
file: `dist/${pkg.name}.min.js`,
97+
format: 'umd',
98+
name,
99+
indent: false
100+
},
101+
external,
102+
plugins: [
103+
commonjs(),
104+
nodeResolve({
105+
jsnext: true
106+
}),
107+
esbuild(),
108+
babel({
109+
exclude: 'node_modules/**'
110+
}),
111+
replace({
112+
'process.env.NODE_ENV': JSON.stringify('production')
113+
}),
114+
terser({
115+
compress: {
116+
pure_getters: true,
117+
unsafe: true,
118+
unsafe_comps: true,
119+
warnings: false
120+
}
121+
}),
122+
sizeSnapshot()
123+
],
124+
...overrides
125+
}
126+
]
127+
}
120128

121129
export default generateRollupConfig

config/tsconfig.base.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2021", "dom"],
4+
"module": "esnext",
5+
"target": "es2021",
6+
"moduleResolution": "node",
7+
"strict": true,
8+
"esModuleInterop": true,
9+
"skipLibCheck": true,
10+
"forceConsistentCasingInFileNames": true,
11+
"allowJs": true,
12+
"jsx": "react-jsx",
13+
"noFallthroughCasesInSwitch": true,
14+
// Eventually those should be switched to more strict options, but we can ease into it while we're converting to typescript
15+
"noImplicitAny": false
16+
}
17+
}

0 commit comments

Comments
 (0)