Skip to content

Commit 4be80c0

Browse files
committed
TS refactor
1 parent 1bcc17e commit 4be80c0

16 files changed

+769
-281
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": [["@babel/preset-env", { "loose": true} ]]
2+
"presets": ["@babel/typescript", ["@babel/preset-env", { "loose": true }]]
33
}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
.cache
22
node_modules
33
.idea
4+
.vscode
45
yarn.lock
56
yarn-error.log*
67
dist
78
/polyfilled.js
8-
9+
/polyfilled.d.ts

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ browserslist
1111
.cache
1212
.travis.yml
1313
tests
14+
tsconfig.json

.size-limit.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
{
3+
"path": "dist/bundle.esm.js",
4+
"limit": "323 B",
5+
"gzip": true
6+
},
7+
{
8+
"path": "dist/bundle.esm.js",
9+
"limit": "243 B",
10+
"brotli": true
11+
},
12+
{
13+
"path": "dist/bundle.cjs.js",
14+
"limit": "313 B",
15+
"gzip": true
16+
},
17+
{
18+
"path": "dist/bundle.cjs.js",
19+
"limit": "233 B",
20+
"brotli": true
21+
},
22+
{
23+
"path": "polyfilled.js",
24+
"limit": "2644 B",
25+
"gzip": true
26+
},
27+
{
28+
"path": "polyfilled.js",
29+
"limit": "2353 B",
30+
"brotli": true
31+
}
32+
]

.travis.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
language: node_js
22
node_js:
3-
- 10
3+
- 11.7.0
44
env:
55
global:
66
- MOZ_HEADLESS=1
77
addons:
88
chrome: stable
99
firefox: latest
1010
script:
11-
- yarn build
12-
- yarn test:chrome:headless
13-
- yarn test:firefox:headless
11+
- yarn test
1412
deploy:
1513
provider: npm
16-
email: rpgmorpheus@gmail.com
14+
email: contact@hubertviktor.com
1715
api_key:
1816
secure: AVhIhFozrf45OjXuFjpL734kSr3Ar/VGqaBzmggF0VWFqOa3stMt6t4gYLRJKrXfr9alMPhB4IdQ7fTjO0EIZh51pT7uRkjAJ8ZUXyDB6fohAFw2Q2mowufOMt2Pu0peC0SDWROosXk0asRAgWhjsMNcNiwgctjr0Pw2DHswq13QBcdL0/sTtTMRnawrjZJBbXtdDb0Q6nfygQjDav6NzpK1uT3elsNKlyf+MiOqNguwc1lgoIqfp2Twvj9a7TmbMf8TFiQjsXZgFZxPYwXZyi62gbcvySEZvEQnYGzUCGsgF+aM3GYsluFmOjMUaUnm9++48gDmKF9+aigowhu5vvVl8XdoI5RIjg/5ibwh04SlHhUab/0JqQy9HmfCkFXDHnzwDyRZMjNlXMfbX+XXSrY3R+WXIfeepdressTHJr3DoPdpkJmic5LN+i8zPjJH/+TU/MMzSot7lTQLD5q5hmxpZi1fCPxAzdVwn7s3uKDtJiUU0i0s864wu7VzRLNBxTLi5uR4mToFNtfQAzuOqOvhK7mGNqSYTIAeCnf67VLHWRLZcqR3bbRCJVt3PT+LKg51nDor/jBnuZTrph1DNo7vGfxIydyQ4EbY3QqDt1gcZoB5imGUc2+4pSzXA0xX9ijwCrNPuh7Vtyg+rV2/R6e9ecdWE5CSz6FzvG5k5TQ=
1917
on:

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 6.1.0-alpha1
4+
5+
- Rewrote the source in TypeScript. (Feedback is welcome.)
6+
- Rewrote tests in TypeScript as well. (Ensures the types make sense.)
7+
- Added checks to ensure reasonable bundle size.
8+
39
## 6.0.0
410

511
- **[BREAKING]** Default width and height can now be set by declaring defaults

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ A React hook that allows you to use a ResizeObserver to measure an element's siz
55
[![npm version](https://badge.fury.io/js/use-resize-observer.svg)](https://npmjs.com/package/use-resize-observer)
66
[![build](https://travis-ci.org/ZeeCoder/use-resize-observer.svg?branch=master)](https://travis-ci.org/ZeeCoder/use-resize-observer)
77

8+
## Highlights
9+
10+
- Written in **TypeScript**.
11+
- **Tiny**: 323 B (minified, gzipped) Monitored by [size-limit](https://github.com/ai/size-limit).
12+
- Exposes an **onResize callback** if you need more control.
13+
- [Throttle / Debounce](#throttle--debounce)
14+
- Works with **SSR**.
15+
- Works with **CSS-in-JS**.
16+
- **Supports custom refs** in case you [had one already](#passing-in-your-own-ref).
17+
- **Tested** in real browsers. (Headless Chrome and Firefox).
18+
819
## In Action
920

1021
[CodeSandbox Demo](https://codesandbox.io/s/nrp0w2r5z0)
@@ -69,7 +80,7 @@ const App = () => {
6980
const { ref } = useResizeObserver({
7081
onResize: ({ width, height }) => {
7182
// do something here.
72-
}
83+
},
7384
});
7485

7586
return <div ref={ref} />;

package.json

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
{
22
"name": "use-resize-observer",
3-
"version": "6.0.0",
3+
"version": "6.1.0-alpha.1",
44
"main": "dist/bundle.cjs.js",
55
"module": "dist/bundle.esm.js",
6+
"types": "dist/index.d.ts",
67
"sideEffects": false,
78
"repository": "[email protected]:ZeeCoder/use-resize-observer.git",
89
"description": "A React hook that allows you to use a ResizeObserver to measure an element's size.",
9-
"author": "Viktor Hubert <rpgmorpheus@gmail.com>",
10+
"author": "Viktor Hubert <contact@hubertviktor.com>",
1011
"license": "MIT",
1112
"scripts": {
1213
"build": "run-s src:build test:build",
1314
"watch": "KARMA_BROWSERS=Chrome run-p src:watch test:watch karma:watch",
14-
"src:build": "rollup -c",
15+
"src:build": "rollup -c && tsc && cp dist/index.d.ts polyfilled.d.ts",
1516
"src:watch": "rollup -c -w",
16-
"test": "run-s build karma:run",
17-
"test:build": "parcel build tests/index.js --out-dir tests/dist",
18-
"test:watch": "parcel watch tests/index.js --out-dir tests/dist",
17+
"tsc": "tsc",
18+
"size-limit": "size-limit",
19+
"test": "run-s 'build' 'size-limit' 'tsc -p tests' 'test:chrome:headless' 'test:firefox:headless'",
20+
"test:build": "parcel build tests/index.tsx --out-dir tests/dist",
21+
"test:watch": "parcel watch tests/index.ts --out-dir tests/dist",
1922
"test:chrome": "KARMA_BROWSERS=Chrome yarn karma:run",
2023
"test:chrome:headless": "KARMA_BROWSERS=ChromeHeadless yarn karma:run",
2124
"test:firefox": "KARMA_BROWSERS=Firefox yarn karma:run",
@@ -30,9 +33,8 @@
3033
}
3134
},
3235
"lint-staged": {
33-
"*.{js,md}": [
34-
"prettier --write",
35-
"git add"
36+
"*.{js,ts,md}": [
37+
"prettier --write"
3638
]
3739
},
3840
"peerDependencies": {
@@ -42,23 +44,31 @@
4244
"devDependencies": {
4345
"@babel/core": "^7.7.7",
4446
"@babel/preset-env": "^7.7.7",
47+
"@babel/preset-typescript": "^7.9.0",
48+
"@rollup/plugin-inject": "^4.0.1",
49+
"@size-limit/preset-small-lib": "^4.4.5",
50+
"@types/karma": "^5.0.0",
51+
"@types/karma-jasmine": "^3.1.0",
52+
"@types/react": "^16.9.34",
53+
"@types/react-dom": "^16.9.6",
4554
"babel-regenerator-runtime": "^6.5.0",
4655
"delay": "^4.1.0",
47-
"husky": "^3.0.4",
48-
"karma": "^4.2.0",
56+
"husky": "^4.2.5",
57+
"karma": "^5.0.1",
4958
"karma-chrome-launcher": "^3.0.0",
50-
"karma-firefox-launcher": "^1.2.0",
51-
"karma-jasmine": "^2.0.1",
59+
"karma-firefox-launcher": "^1.3.0",
60+
"karma-jasmine": "^3.1.1",
5261
"karma-spec-reporter": "^0.0.32",
53-
"lint-staged": "^9.5.0",
62+
"lint-staged": "^10.1.3",
5463
"npm-run-all": "^4.1.5",
5564
"parcel-bundler": "^1.10.3",
56-
"prettier": "^1.14.3",
65+
"prettier": "^2.0.4",
5766
"react": "^16.9.0",
5867
"react-dom": "^16.9.0",
59-
"rollup": "^1.27.14",
60-
"rollup-plugin-babel": "^4.3.3",
61-
"rollup-plugin-inject": "^3.0.2"
68+
"rollup": "^2.6.1",
69+
"rollup-plugin-babel": "^4.4.0",
70+
"size-limit": "^4.4.5",
71+
"typescript": "^3.8.3"
6272
},
6373
"dependencies": {
6474
"resize-observer-polyfill": "^1.5.1"

rollup.config.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
import babel from "rollup-plugin-babel";
2-
import inject from "rollup-plugin-inject";
2+
import inject from "@rollup/plugin-inject";
33

4-
const getConfig = polyfill => {
4+
const getConfig = ({ polyfill = false } = {}) => {
55
const config = {
6-
input: "src/index.js",
6+
input: "src/index.ts",
77
output: [],
8-
plugins: [babel()],
9-
external: ["react"]
8+
plugins: [babel({ extensions: ["ts"] })],
9+
external: ["react"],
1010
};
1111

1212
if (polyfill) {
1313
config.output = [
1414
{
1515
file: "polyfilled.js",
16-
format: "cjs"
17-
}
16+
format: "cjs",
17+
},
1818
];
1919
config.external.push("resize-observer-polyfill");
2020
config.plugins.push(
2121
inject({
22-
ResizeObserver: "resize-observer-polyfill"
22+
ResizeObserver: "resize-observer-polyfill",
2323
})
2424
);
2525
} else {
2626
config.output = [
2727
{
2828
file: "dist/bundle.cjs.js",
29-
format: "cjs"
29+
format: "cjs",
3030
},
3131
{
3232
file: "dist/bundle.esm.js",
33-
format: "esm"
34-
}
33+
format: "esm",
34+
},
3535
];
3636
}
3737

3838
return config;
3939
};
4040

41-
export default [getConfig(), getConfig(true)];
41+
export default [getConfig(), getConfig({ polyfill: true })];

0 commit comments

Comments
 (0)