Skip to content

Commit d5cec90

Browse files
committed
Transpiling again, separate polyfilled build.
Resolves ZeeCoder#14
1 parent afec9ce commit d5cec90

9 files changed

+105
-7020
lines changed

.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": [["@babel/preset-env", { "loose": true} ]]
3+
}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
.cache
22
node_modules
33
.idea
4+
yarn.lock
45
yarn-error.log*
56
dist
7+
/polyfilled.js
68

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## 5.0.0
44

5-
- **[BREAKING]** `#14` Removed Babel code transpiling and the ResizeObserver polyfill.
5+
- **[BREAKING]** `#14` Removed the polyfill from the default builds, and shipping
6+
it instead as as separate module.
67
- **[BREAKING]** `#21` Returning an object instead of an array, so that values not
78
needed could be omitted.
89
- `#18` Added missing copyright notice in the MIT license.

README.md

+19-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ 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-
## On Transpilation / Polyfilling
9-
10-
This library is neither transpiled nor polyfilled by default.
11-
I recommend using Babel with its "env" preset to transpile your code to your
12-
target browsers, and adding a [ResizeObserver](https://github.com/que-etc/resize-observer-polyfill)
13-
poly- or [ponyfill](https://github.com/sindresorhus/ponyfill) when necessary.
14-
158
## In Action
169

1710
[CodeSandbox Demo](https://codesandbox.io/s/nrp0w2r5z0)
@@ -26,6 +19,9 @@ npm install use-resize-observer --save-dev
2619

2720
## Basic Usage
2821

22+
Note that the default builds are not polyfilled! For instructions and alternatives,
23+
see the [Transpilation / Polyfilling](##transpilation-/-polyfilling) section.
24+
2925
```js
3026
import React from "react";
3127
import useResizeObserver from "use-resize-observer";
@@ -102,6 +98,22 @@ container/element queries:
10298

10399
[CodeSandbox Demo](https://codesandbox.io/s/use-resize-observer-container-query-with-css-in-js-iitxl)
104100

101+
## Transpilation / Polyfilling
102+
103+
By default the library provides transpiled ES5 modules in CJS / ESM module formats.
104+
105+
Polyfilling is recommended to be done in the host app, and not in the library, as
106+
that gives users more control over what exact polyfills they might want to use.
107+
108+
However, there's a polyfilled CJS module (without affecting globals) that can be
109+
used for convenience:
110+
111+
```js
112+
import useResizeObserver from "use-resize-observer/polyfilled";
113+
```
114+
115+
[Bundled ResizeObserver implementation](<[ResizeObserver](https://github.com/que-etc/resize-observer-polyfill)>)
116+
105117
## Related
106118

107119
- [@zeecoder/container-query](https://github.com/ZeeCoder/container-query)

browserslist

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Using IE to declare a fixed point.
2+
# "Last n versions" and such could potentially change the build as time goes on,
3+
# while this will transpile down to ES5 that'll run in most browsers you probably
4+
# care about.
5+
IE >= 11

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "5.0.0",
44
"main": "dist/bundle.cjs.js",
55
"module": "dist/bundle.esm.js",
6+
"sideEffects": false,
67
"repository": "[email protected]:ZeeCoder/use-resize-observer.git",
78
"description": "A React hook that allows you to use a ResizeObserver to measure an element's size.",
89
"author": "Viktor Hubert <[email protected]>",
@@ -55,6 +56,11 @@
5556
"prettier": "^1.14.3",
5657
"react": "^16.9.0",
5758
"react-dom": "^16.9.0",
58-
"rollup": "^1.20.1"
59+
"rollup": "^1.20.1",
60+
"rollup-plugin-babel": "^4.3.3",
61+
"rollup-plugin-inject": "^3.0.2"
62+
},
63+
"dependencies": {
64+
"resize-observer-polyfill": "^1.5.1"
5965
}
6066
}

rollup.config.js

+40-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
1-
export default {
2-
input: "src/index.js",
3-
output: [
4-
{
5-
file: "dist/bundle.cjs.js",
6-
format: "cjs"
7-
},
8-
{
9-
file: "dist/bundle.esm.js",
10-
format: "esm"
11-
}
12-
],
13-
external: ["react"]
1+
import babel from "rollup-plugin-babel";
2+
import inject from "rollup-plugin-inject";
3+
4+
const getConfig = polyfill => {
5+
const config = {
6+
input: "src/index.js",
7+
output: [],
8+
plugins: [babel()],
9+
external: ["react"]
10+
};
11+
12+
if (polyfill) {
13+
config.output = [
14+
{
15+
file: "polyfilled.js",
16+
format: "cjs"
17+
}
18+
];
19+
config.external.push("resize-observer-polyfill");
20+
config.plugins.push(
21+
inject({
22+
ResizeObserver: "resize-observer-polyfill"
23+
})
24+
);
25+
} else {
26+
config.output = [
27+
{
28+
file: "dist/bundle.cjs.js",
29+
format: "cjs"
30+
},
31+
{
32+
file: "dist/bundle.esm.js",
33+
format: "esm"
34+
}
35+
];
36+
}
37+
38+
return config;
1439
};
40+
41+
export default [getConfig(), getConfig(true)];

tests/index.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import React, { useEffect, useState, useRef, forwardRef } from "react";
1+
import React, { useEffect, useState, useRef } from "react";
22
import ReactDOM from "react-dom";
33
import useResizeObserver from "../dist/bundle.esm";
4+
import useResizeObserverPolyfilled from "../polyfilled";
45
import delay from "delay";
56
// Using the following to support async/await in tests.
67
// I'm intentionally not using babel/polyfill, as that would introduce polyfills
@@ -363,3 +364,28 @@ it("should ignore invalid custom refs", async () => {
363364
await delay(50);
364365
assertSize({ width: 1, height: 1 });
365366
});
367+
368+
it("should work with the polyfilled version", async () => {
369+
const Test = ({ resolveHandler }) => {
370+
const { ref, width, height } = useResizeObserverPolyfilled();
371+
372+
useEffect(() => {
373+
resolveHandler({
374+
assertSize: ({ width, height }) => {
375+
expect(ref.current.textContent).toBe(`${width}x${height}`);
376+
}
377+
});
378+
}, []);
379+
380+
return (
381+
<div style={{ width: 50, height: 40 }} ref={ref}>
382+
{width}x{height}
383+
</div>
384+
);
385+
};
386+
387+
const { assertSize } = await render(Test);
388+
389+
await delay(50);
390+
assertSize({ width: 50, height: 40 });
391+
});

0 commit comments

Comments
 (0)