Skip to content

Commit d191b81

Browse files
committed
Update: exporting types separate from code
1 parent ce4f6f6 commit d191b81

File tree

7 files changed

+59
-24
lines changed

7 files changed

+59
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dist
1313
# production
1414
/dist
1515
/module
16+
/types
1617

1718
# misc
1819
.DS_Store

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "2.2.0",
44
"description": "An HTMLElement extension that provides a simple setup for Web Components using React.",
55
"main": "module/index.js",
6+
"types": "types",
67
"exports": {
78
"import": "./module/index.js",
89
"require": "./dist/index.js",
@@ -11,13 +12,14 @@
1112
"types": "module/index.d.ts",
1213
"files": [
1314
"dist",
14-
"module"
15+
"module",
16+
"types"
1517
],
1618
"scripts": {
1719
"test": "npm run lint && npm run build && jest",
1820
"build": "tsc -p tsconfig.json && tsc -p require-tsconfig.json",
1921
"lint": "eslint --ext .js,.jsx,.ts,.tsx ./src",
20-
"prepublishOnly": "npm run build"
22+
"prepack": "npm run build"
2123
},
2224
"repository": {
2325
"type": "git",

require-tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
2-
"extends": "./tsconfig.json",
2+
"extends": "./tsconfig.base.json",
33
"compilerOptions": {
44
"outDir": "dist",
55
"module": "commonjs",
6-
"target": "es5"
6+
"target": "es5",
7+
"declaration": false
78
}
89
}

src/ReactHTMLElement.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { version as reactVersion } from 'react';
22
import ReactDOM from 'react-dom';
3-
import type { Root } from 'react-dom/client';
3+
import type { Root, createRoot as createRootOriginal } from 'react-dom/client';
44

55
type Renderable = Parameters<ReactDOM.Renderer>[0][number];
66

77
const [, major] = /^(\d+)\.\d+\.\d+$/.exec(reactVersion) || [undefined, '16'];
88
const reactMajor = Number(major);
99

1010
const isPreEighteen = reactMajor < 18;
11+
const REACT_DOM_CLIENT_IMPORT = isPreEighteen
12+
? './react-dom-client-polyfill'
13+
: 'react-dom/client';
1114

1215
class ReactHTMLElement extends HTMLElement {
1316
private _initialized?: boolean;
@@ -49,7 +52,12 @@ class ReactHTMLElement extends HTMLElement {
4952
async root(): Promise<Root> {
5053
if (this._root) return this._root;
5154

52-
const { createRoot } = await import('react-dom/client');
55+
const { createRoot } = (await import(
56+
/* webpackExports: ['createRoot'] */
57+
`${REACT_DOM_CLIENT_IMPORT}`
58+
)) as {
59+
createRoot: typeof createRootOriginal;
60+
};
5361
this._root = createRoot(this.mountPoint);
5462
return this._root;
5563
}
@@ -62,10 +70,10 @@ class ReactHTMLElement extends HTMLElement {
6270
return;
6371
}
6472

65-
void this.rootRender(app);
73+
void this.renderRoot(app);
6674
}
6775

68-
async rootRender(app: Renderable): Promise<void> {
76+
async renderRoot(app: Renderable): Promise<void> {
6977
const root = await this.root();
7078
root.render(app);
7179
}

src/react-dom-client-polyfill.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { createRoot as createRootOriginal } from 'react-dom/client';
2+
3+
type CreateRootFunc = typeof createRootOriginal;
4+
5+
//*---
6+
// this function should never be used
7+
// it is only here to satisfy webpack
8+
//*---
9+
10+
// eslint-disable-next-line import/prefer-default-export
11+
export const createRoot: CreateRootFunc = (
12+
// eslint-disable-next-line no-unused-vars,@typescript-eslint/no-unused-vars
13+
...args: Parameters<CreateRootFunc>
14+
) => ({
15+
render: () => {},
16+
unmount: () => {},
17+
});

tsconfig.base.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"allowSyntheticDefaultImports": true,
5+
"downlevelIteration": true,
6+
"esModuleInterop": true,
7+
"forceConsistentCasingInFileNames": true,
8+
"isolatedModules": true,
9+
"lib": ["dom", "dom.iterable", "esnext"],
10+
"module": "es2020",
11+
"moduleResolution": "node",
12+
"outDir": "module",
13+
"resolveJsonModule": true,
14+
"skipLibCheck": true,
15+
"strict": true,
16+
"target": "es6"
17+
},
18+
"include": ["src"],
19+
"exclude": ["src/__tests__"]
20+
}

tsconfig.json

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
{
2+
"extends": "./tsconfig.base.json",
23
"compilerOptions": {
3-
"allowJs": true,
4-
"allowSyntheticDefaultImports": true,
54
"declaration": true,
6-
"downlevelIteration": true,
7-
"esModuleInterop": true,
8-
"forceConsistentCasingInFileNames": true,
9-
"isolatedModules": true,
10-
"lib": ["dom", "dom.iterable", "esnext"],
11-
"module": "es2020",
12-
"moduleResolution": "node",
13-
"outDir": "module",
14-
"resolveJsonModule": true,
15-
"skipLibCheck": true,
16-
"strict": true,
17-
"target": "es6"
5+
"declarationDir": "types",
186
},
19-
"include": ["src"],
20-
"exclude": ["src/__tests__"]
217
}

0 commit comments

Comments
 (0)