Skip to content

Commit

Permalink
Merge pull request #70 from n1ru4l/fix-remove-dev-dependencies-from-p…
Browse files Browse the repository at this point in the history
…ublished-bundle

fix: remove dev dependencies from published bundle
  • Loading branch information
n1ru4l authored May 12, 2020
2 parents c405c7b + f2018f3 commit 67c5548
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
- *js_deps_cache_key
- *js_deps_backup_cache_key
- run: yarn build
- run: node scripts/prepare-deploy-package-json.js
- run: yarn semantic-release

workflows:
Expand Down
38 changes: 22 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@ const MyComponent = ({ filter }) => {
const [data, setData] = React.useState(null);

useAsyncEffect(
function*(onCancel, c) {
function* (onCancel, c) {
const controller = new AbortController();

onCancel(() => controller.abort());

const data = yield* c(
fetch("/data?filter=" + filter, {
signal: controller.signal
}).then(res => res.json())
signal: controller.signal,
}).then((res) => res.json())
);

setData(data);
},
[filter]
);

return data ? <RenderData data={data} /> : null;
};
```

Expand Down Expand Up @@ -96,8 +98,8 @@ const MyComponent = ({ filter }) => {
const runHandler = async () => {
try {
const data = await fetch("/data?filter=" + filter, {
signal: controller.signal
}).then(res => res.json());
signal: controller.signal,
}).then((res) => res.json());
if (isCanceled) {
return;
}
Expand All @@ -111,6 +113,8 @@ const MyComponent = ({ filter }) => {
controller.abort();
};
}, [filter]);

return data ? <RenderData data={data} /> : null;
};
```

Expand All @@ -124,21 +128,23 @@ const MyComponent = ({ filter }) => {
const [data, setData] = useState(null);

useAsyncEffect(
function*(onCancel, c) {
function* (onCancel, c) {
const controller = new AbortController();

onCancel(() => controller.abort());

const data = yield* c(
fetch("/data?filter=" + filter, {
signal: controller.signal
}).then(res => res.json())
signal: controller.signal,
}).then((res) => res.json())
);

setData(data);
},
[filter]
);

return data ? <RenderData data={data} /> : null;
};
```

Expand All @@ -154,9 +160,9 @@ import useAsyncEffect from "@n1ru4l/use-async-effect";

const MyDoggoImage = () => {
const [doggoImageSrc, setDoggoImageSrc] = useState(null);
useAsyncEffect(function*(_, c) {
useAsyncEffect(function* (_, c) {
const { message } = yield* c(
fetch("https://dog.ceo/api/breeds/image/random").then(res => res.json())
fetch("https://dog.ceo/api/breeds/image/random").then((res) => res.json())
);
setDoggoImageSrc(message);
}, []);
Expand All @@ -178,13 +184,13 @@ import useAsyncEffect from "@n1ru4l/use-async-effect";

const MyDoggoImage = () => {
const [doggoImageSrc, setDoggoImageSrc] = useState(null);
useAsyncEffect(function*(onCancel, c) {
useAsyncEffect(function* (onCancel, c) {
const abortController = new AbortController();
onCancel(() => abortController.abort());
const { message } = yield c(
fetch("https://dog.ceo/api/breeds/image/random", {
signal: abortController.signal
}).then(res => res.json())
signal: abortController.signal,
}).then((res) => res.json())
);
setDoggoImageSrc(message);
}, []);
Expand All @@ -208,9 +214,9 @@ import useAsyncEffect from "@n1ru4l/use-async-effect";

const MyDoggoImage = () => {
const [doggoImageSrc, setDoggoImageSrc] = useState(null);
useAsyncEffect(function*(_, c) {
useAsyncEffect(function* (_, c) {
const { message } = yield* c(
fetch("https://dog.ceo/api/breeds/image/random").then(res => res.json())
fetch("https://dog.ceo/api/breeds/image/random").then((res) => res.json())
);
setDoggoImageSrc(message);

Expand Down Expand Up @@ -251,7 +257,7 @@ Add the following to your eslint config file:
We expose a helper function for TypeScript that allows interferring the correct Promise resolve type. It uses some type-casting magic under the hood and requires you to use the `yield*` keyword instead of the `yield` keyword.

```tsx
useAsyncEffect(function*(setErrorHandler, c) {
useAsyncEffect(function* (setErrorHandler, c) {
const numericValue = yield* c(Promise.resolve(123));
// type of numericValue is number 🎉
});
Expand Down
30 changes: 30 additions & 0 deletions scripts/prepare-deploy-package-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable @typescript-eslint/no-var-requires */
"use strict";

const fs = require("fs");
const path = require("path");

const pickFields = [
"name",
"version",
"license",
"author",
"homepage",
"repository",
"bugs",
"keywords",
"module",
"main",
"typings",
"dependencies",
"peerDependencies",
"files",
];

const filePath = path.resolve(__dirname, "..", "package.json");
const contents = fs.readFileSync(filePath);
const packageJson = JSON.parse(contents);
const newPackageJson = Object.fromEntries(
Object.entries(packageJson).filter(([key]) => pickFields.includes(key))
);
fs.writeFileSync(filePath, JSON.stringify(newPackageJson, null, 2));
2 changes: 0 additions & 2 deletions src/use-async-effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ function* cast<T>(input: Promise<T>): Generator<Promise<T>, T> {
return yield input;
}

type ThenArg<T> = T extends PromiseLike<infer U> ? U : T;

export const useAsyncEffect = (
createGenerator: (
setCancelHandler: (
Expand Down

0 comments on commit 67c5548

Please sign in to comment.