Skip to content
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

Rollup works #3

Merged
merged 7 commits into from
Jan 10, 2024
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
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
node_modules

# build
lib/
types/
cjs/
dist/
esm/
.rollup.cache/

# typescript
*.tsbuildinfo
*.tsbuildinfo

# cache
.eslintcache
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
"packages/*"
],
"scripts": {
"ci": "yarn prettier && yarn compile && yarn lint",
"compile": "tsc --noEmit",
"ci": "yarn prettier && yarn compile && yarn lint && yarn build",
"compile": "yarn workspace @dolthub/react-hooks compile",
"build": "yarn workspace @dolthub/react-hooks build",
"lint": "yarn workspace @dolthub/react-hooks lint",
"prettier": "yarn workspace @dolthub/react-hooks prettier"
},
"dependencies": {
"react": "^18.2.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.5",
"@types/eslint": "^8",
"@types/react": "^18.2.40",
"@types/react": "^18.2.33",
"@types/react-dom": "^18",
"@types/rollup-plugin-peer-deps-external": "^2",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2",
Expand All @@ -38,5 +36,9 @@
"rollup-plugin-postcss": "^4.0.2",
"rollup-plugin-terser": "^7.0.2",
"typescript": "^5.3.2"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}
6 changes: 0 additions & 6 deletions packages/babel-config/index.js

This file was deleted.

15 changes: 0 additions & 15 deletions packages/babel-config/package.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/hooks/.eslintcache

This file was deleted.

50 changes: 49 additions & 1 deletion packages/hooks/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
# @react-library/hooks
# @dolthub/react-hooks

A library of useful React hooks.

## Installation

```
% yarn add @dolthub/react-hooks
```

or

```
% npm install @dolthub/react-hooks
```

## Usage

```tsx
import { useEffectOnMount } from "@dolthub/react-hooks";

function MyComponent() {
useEffectOnMount(() => {
console.log("do something on component mount");
});

return <div>Home</div>;
}
```

## Testing in a local package

If you have a local copy of this package and want to test a change in another local
package, you can either add `@dolthub/react-hooks` and point it at the file path, or if
`@dolthub/react-hooks` is already installed add the file path to `resolutions` in your
`package.json`, like so:

```json
// ../other-package/package.json
{
...,
"resolutions": {
"@dolthub/react-hooks": "file:../../react-library/packages/hooks",
"@types/react": "18.2.33"
}
}
```

Note that you may also need the same `@types/react` version.
1 change: 0 additions & 1 deletion packages/hooks/babel.config.js

This file was deleted.

20 changes: 14 additions & 6 deletions packages/hooks/package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
{
"name": "@dolthub/react-hooks",
"author": "DoltHub",
"version": "0.1.0",
"version": "0.1.3",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"keywords": [
"react",
"hooks",
"react-hooks",
"state",
"frontend"
],
"packageManager": "yarn@4.0.2",
"type": "module",
"scripts": {
"compile": "tsc -b",
"build": "rollup -c --bundleConfigAsCjs",
"build:watch": "rollup -c --bundleConfigAsCjs --watch",
"lint": "eslint --cache --ext .ts,.js,.tsx,.jsx src",
"prettier": "prettier --check 'src/**/*.{js,ts}'",
"prettier-fix": "prettier --write 'src/**/*.{js,ts}'"
},
"peerDependencies": {
"react": "^18.2.0"
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@dolthub/react-babel-config": "^0.1.0",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.5",
"@types/eslint": "^8",
"@types/react": "^18.2.40",
"@types/rollup-plugin-peer-deps-external": "^2",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2",
Expand Down
11 changes: 2 additions & 9 deletions packages/hooks/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import typescript from "@rollup/plugin-typescript";
import { terser } from "rollup-plugin-terser";
import external from "rollup-plugin-peer-deps-external";
import { dts } from "rollup-plugin-dts";
import babel from "@rollup/plugin-babel";

const packageJson = require("./package.json");

export default [
{
input: "src/index.ts",
external: ["react", "react-dom"],
output: [
{
file: packageJson.main,
format: "cjs",
sourcemap: true,
name: "react-ts-lib",
},
{
file: packageJson.module,
Expand All @@ -27,19 +26,13 @@ export default [
plugins: [
external(),
resolve(),
babel({
babelHelpers: "runtime",
presets: ["@babel/preset-react"],
extensions: [".js", ".jsx"],
exclude: "**/node_modules/**",
}),
commonjs(),
typescript({ tsconfig: "./tsconfig.json" }),
terser(),
],
},
{
input: "dist/esm/lib/index.d.ts",
input: "types/index.d.ts",
output: [{ file: "dist/index.d.ts", format: "esm" }],
external: [/\.css$/],
plugins: [dts()],
Expand Down
4 changes: 3 additions & 1 deletion packages/hooks/src/useDelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function useDelay(

useEffect(() => {
if (active) {
const h = setTimeout(stop, length);
const h = setTimeout(() => stop(), length);
handle.current = h;
return () => clearTimeout(h);
}
Expand All @@ -27,12 +27,14 @@ export default function useDelay(
}
return () => {};
}, [active, length, stop]);

useEffect(() => {
const thereAreErrors = errors.some(e => !!e);
if (thereAreErrors) {
stop();
}
}, [errors, stop]);

return useMemo(
() =>
Object.freeze({
Expand Down
10 changes: 4 additions & 6 deletions packages/hooks/src/useReactiveSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import useEffectOnMount from "./useEffectOnMount";
export function useReactiveHeight<E extends HTMLElement>(
elem?: E | null,
): E["clientHeight"] {
const [clientHeight, setClientHeight] = useState(
elem?.clientHeight ?? window.innerHeight,
);
const [clientHeight, setClientHeight] = useState(elem?.clientHeight ?? 0);

useEffectOnMount(() => {
setClientHeight(elem?.clientHeight ?? window.innerHeight);
const handleResize = () =>
setClientHeight(elem?.clientHeight ?? window.innerHeight);
window.addEventListener("resize", handleResize);
Expand All @@ -30,11 +29,10 @@ export function useReactiveWidth<E extends HTMLElement>(
elem?: E | null,
mobileBreakpoint = 768,
): { clientWidth: E["clientWidth"]; isMobile: boolean } {
const [clientWidth, setClientWidth] = useState(
elem?.clientWidth ?? window.innerWidth,
);
const [clientWidth, setClientWidth] = useState(elem?.clientWidth ?? 0);

useEffectOnMount(() => {
setClientWidth(elem?.clientWidth ?? window.innerWidth);
const handleResize = () =>
setClientWidth(elem?.clientWidth ?? window.innerWidth);
window.addEventListener("resize", handleResize);
Expand Down
5 changes: 3 additions & 2 deletions packages/hooks/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"include": ["./src"],
"exclude": ["./lib", "./esm", "./cjs"],
"exclude": ["./types", "./esm", "./cjs", "node_modules", "dist"],
"compilerOptions": {
"rootDir": "src",
"baseUrl": ".",
"declaration": true,
"declarationDir": "lib",
"declarationDir": "types",
"outDir": "dist",
"target": "ES2015",
"lib": ["DOM", "ESNext"],
"module": "ESNext",
Expand Down
Loading
Loading