Skip to content

Commit 53950c0

Browse files
committed
feat: move to tsup for transpiling to support ESM
1 parent 0c05a3b commit 53950c0

File tree

13 files changed

+7100
-4354
lines changed

13 files changed

+7100
-4354
lines changed

.eslintrc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
{
2-
"extends": ["@djthoms/eslint-config", "@djthoms/eslint-config/typescript"],
2+
"extends": [
3+
"@djthoms/eslint-config",
4+
"@djthoms/eslint-config/typescript",
5+
"@djthoms/eslint-config/react",
6+
"@djthoms/eslint-config/react-typescript",
7+
],
38
"parserOptions": {
4-
"project": "./tsconfig.json"
5-
}
9+
"project": "./tsconfig.json",
10+
},
11+
"rules": {
12+
"@typescript-eslint/no-explicit-any": "off",
13+
"@typescript-eslint/no-unsafe-argument": "warn",
14+
},
615
}

.github/workflows/on_push.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ jobs:
99
runs-on: ubuntu-latest
1010
steps:
1111
- name: Checkout
12-
uses: actions/checkout@v3
13-
- uses: pnpm/action-setup@v2
12+
uses: actions/checkout@v4
13+
- uses: pnpm/action-setup@v4
1414
with:
15-
version: 8
15+
version: 9
1616
- name: Setup Node.js
17-
uses: actions/setup-node@v3
17+
uses: actions/setup-node@v4
1818
with:
1919
node-version: 20
2020
cache: pnpm

.lintstagedrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"*.{ts,tsx,js,json}": [
2+
"*.{ts,tsx,js,jsx}": [
33
"prettier --write",
44
"eslint --fix"
55
],
6-
"*.{md,yml,yaml}": "prettier --write"
6+
"*.{md,yml,yaml,json}": "prettier --write"
77
}

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lts

.vscode/settings.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"prettier.enable": true,
3+
"eslint.validate": [
4+
"javascript",
5+
"javascriptreact",
6+
"typescript",
7+
"typescriptreact",
8+
"json",
9+
"jsonc"
10+
],
11+
"[typescript]": {
12+
"editor.defaultFormatter": "esbenp.prettier-vscode"
13+
},
14+
"[typescriptreact]": {
15+
"editor.defaultFormatter": "esbenp.prettier-vscode"
16+
},
17+
"[jsonc]": {
18+
"editor.defaultFormatter": "esbenp.prettier-vscode"
19+
}
20+
}

README.md

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ npm i --save-dev docusaurus-plugin-react-docgen-typescript react-docgen-typescri
1313

1414
## Usage
1515

16-
Inside your `docusaurus.config.js` add to the `plugins` field and configure with the `src` option
17-
with full glob support :+1:.
16+
Inside your `docusaurus.config.js` add to the `plugins` field and configure with
17+
the `src` option with full glob support :+1:.
1818

1919
```js
2020
module.exports = {
2121
// ...
2222
plugins: [
2323
[
24-
'docusaurus-plugin-react-docgen-typescript',
24+
"docusaurus-plugin-react-docgen-typescript",
2525
/** @type {import('docusaurus-plugin-react-docgen-typescript').Options} */
2626
{
2727
// pass in a single string or an array of strings
28-
src: ['path/to/**/*.tsx', '!path/to/**/*test.*'],
28+
src: ["path/to/**/*.tsx", "!path/to/**/*test.*"],
2929
parserOptions: {
3030
// pass parserOptions to react-docgen-typescript
3131
// here is a good starting point which filters out all
3232
// types from react
3333
propFilter: (prop, component) => {
3434
if (prop.parent) {
35-
return !prop.parent.fileName.includes('@types/react');
35+
return !prop.parent.fileName.includes("@types/react");
3636
}
3737

3838
return true;
@@ -44,11 +44,11 @@ module.exports = {
4444
};
4545
```
4646

47-
Any pattern supported by [`fast-glob`](https://github.com/mrmlnc/fast-glob) is allowed here
48-
(including negations).
47+
Any pattern supported by [`fast-glob`](https://github.com/mrmlnc/fast-glob) is
48+
allowed here (including negations).
4949

50-
`src` paths are relative to the location of your `docusaurus.config.js`. For example, if you
51-
had a directory structure like:
50+
`src` paths are relative to the location of your `docusaurus.config.js`. For
51+
example, if you had a directory structure like:
5252

5353
```
5454
.
@@ -69,24 +69,29 @@ had a directory structure like:
6969
└── yarn.lock
7070
```
7171

72-
Then to document all of your JSX components in your `src/` directory, you would use this path:
73-
`../src/**/*.jsx`.
72+
Then to document all of your JSX components in your `src/` directory, you would
73+
use this path: `../src/**/*.jsx`.
7474

7575
## Reading Annotations
7676

77-
Using the default settings, annotations are stored inside of the `.docusaurus` directory. The
78-
`@docgen` alias is set to ensure stable access to these files.
77+
Using the default settings, annotations are stored inside of the `.docusaurus`
78+
directory. The `@docgen` alias is set to ensure stable access to these files.
7979

8080
### Build a Prop Table
8181

82-
Most of the time props will want to be shown as API information to a particular component. For
83-
convenience, we can use a simple hook from this package to dynamically import `.json` files:
82+
Most of the time props will want to be shown as API information to a particular
83+
component. For convenience, we can use a simple hook from this package to
84+
dynamically import `.json` files:
8485

8586
```tsx
86-
import { useDynamicImport } from 'docusaurus-plugin-react-docgen-typescript/dist/esm/hooks';
87+
import { useDynamicImport } from "docusaurus-plugin-react-docgen-typescript/useDynamicImport";
88+
89+
type MyProps = {
90+
/* ... */
91+
};
8792

8893
export const PropTable = ({ name }) => {
89-
const props = useDynamicImport(name);
94+
const props = useDynamicImport<MyProps>(name);
9095

9196
if (!props) {
9297
return null;
@@ -104,7 +109,7 @@ export const PropTable = ({ name }) => {
104109
</tr>
105110
</thead>
106111
<tbody>
107-
{Object.keys(props).map(key => {
112+
{Object.keys(props).map((key) => {
108113
return (
109114
<tr key={key}>
110115
<td>
@@ -113,8 +118,12 @@ export const PropTable = ({ name }) => {
113118
<td>
114119
<code>{props[key].type?.name}</code>
115120
</td>
116-
<td>{props[key].defaultValue && <code>{props[key].defaultValue.value}</code>}</td>
117-
<td>{props[key].required ? 'Yes' : 'No'}</td>
121+
<td>
122+
{props[key].defaultValue && (
123+
<code>{props[key].defaultValue.value}</code>
124+
)}
125+
</td>
126+
<td>{props[key].required ? "Yes" : "No"}</td>
118127
<td>{props[key].description}</td>
119128
</tr>
120129
);
@@ -125,7 +134,8 @@ export const PropTable = ({ name }) => {
125134
};
126135
```
127136

128-
**N.b.** If you use `global: true`, then you must use the [`useGlobalData` hook](https://docusaurus.io/docs/docusaurus-core#useGlobalData)
137+
**N.b.** If you use `global: true`, then you must use the
138+
[`useGlobalData` hook](https://docusaurus.io/docs/docusaurus-core#useGlobalData)
129139
to access the docgen data. You cannot use `useDynamicImport`.
130140

131141
## Options

package.json

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,29 @@
1616
},
1717
"license": "MIT",
1818
"author": "Dennis Thompson",
19-
"main": "dist/cjs/index.js",
20-
"module": "dist/esm/index.js",
21-
"types": "dist/types/index.d.ts",
19+
"main": "./dist/cjs/index.js",
20+
"module": "./dist/esm/index.mjs",
21+
"types": "./dist/types/index.d.ts",
2222
"exports": {
2323
".": {
24-
"types": "./dist/types/index.d.ts",
25-
"require": "./dist/cjs/index.js",
26-
"import": "./dist/esm/index.js"
24+
"require": {
25+
"types": "./dist/cjs/index.d.ts",
26+
"default": "./dist/cjs/index.js"
27+
},
28+
"import": {
29+
"types": "./dist/esm/index.d.mts",
30+
"default": "./dist/esm/index.mjs"
31+
}
2732
},
2833
"./useDynamicImport": {
29-
"types": "./dist/types/useDynamicImport.d.ts",
30-
"require": "./dist/cjs/useDynamicImport.js",
31-
"import": "./dist/esm/useDynamicImport.js"
34+
"require": {
35+
"types": "./dist/cjs/hooks/useDynamicImport.d.ts",
36+
"default": "./dist/cjs/hooks/useDynamicImport.js"
37+
},
38+
"import": {
39+
"types": "./dist/esm/hooks/useDynamicImport.d.mts",
40+
"default": "./dist/esm/hooks/useDynamicImport.mjs"
41+
}
3242
}
3343
},
3444
"files": [
@@ -39,34 +49,34 @@
3949
"lint": "eslint src --ext=ts,tsx,js,jsx,json",
4050
"prebuild": "rm -rf dist",
4151
"build": "npm-run-all -p build:*",
42-
"build:esm": "esbuild --sourcemap --format=esm --target=es2019 --outdir=dist/esm `./scripts/files.sh`",
43-
"build:cjs": "esbuild --sourcemap --format=cjs --target=es2017 --outdir=dist/cjs `./scripts/files.sh`",
52+
"build:esm": "tsup --sourcemap --format esm --target es2022 --dts -d dist/esm `./scripts/files.sh`",
53+
"build:cjs": "tsup --sourcemap --format cjs --target es2020 --dts -d dist/cjs `./scripts/files.sh`",
4454
"build:types": "tsc -p .",
4555
"prepare": "is-ci || husky install"
4656
},
4757
"dependencies": {
48-
"glob": "^8.1.0"
58+
"glob": "^10.3.15"
4959
},
5060
"devDependencies": {
51-
"@commitlint/cli": "^18.4.1",
52-
"@commitlint/config-conventional": "^18.4.0",
53-
"@djthoms/eslint-config": "^6.1.1",
54-
"@djthoms/prettier-config": "^4.3.0",
55-
"@docusaurus/types": "^3.0.0",
61+
"@arethetypeswrong/cli": "^0.15.3",
62+
"@commitlint/cli": "^19.3.0",
63+
"@commitlint/config-conventional": "^19.2.2",
64+
"@djthoms/eslint-config": "^6.4.2",
65+
"@djthoms/prettier-config": "^4.4.1",
66+
"@docusaurus/types": "^3.3.2",
5667
"@semantic-release/changelog": "^6.0.3",
5768
"@semantic-release/git": "^10.0.1",
58-
"@types/glob": "^8.1.0",
59-
"@types/node": "^20.9.0",
60-
"@types/react": "^18.2.37",
61-
"esbuild": "^0.19.5",
62-
"husky": "^8.0.3",
69+
"@types/node": "^20.12.12",
70+
"@types/react": "^18.3.2",
71+
"husky": "^9.0.11",
6372
"is-ci": "^3.0.1",
64-
"lint-staged": "^15.1.0",
73+
"lint-staged": "^15.2.2",
6574
"npm-run-all": "^4.1.5",
66-
"react": "^18.2.0",
75+
"react": "^18.3.1",
6776
"react-docgen-typescript": "^2.2.2",
68-
"semantic-release": "^22.0.7",
69-
"typescript": "^5.2.2"
77+
"semantic-release": "^23.1.1",
78+
"tsup": "^8.0.2",
79+
"typescript": "^5.4.5"
7080
},
7181
"peerDependencies": {
7282
"react": ">=16.8.0",

0 commit comments

Comments
 (0)