From 3319db87cde4ef2fed9012d3113ca57874f41a0e Mon Sep 17 00:00:00 2001 From: n1ru4l Date: Fri, 12 Feb 2021 01:06:57 +0100 Subject: [PATCH 1/5] feat: remove @restart/hooks dependency The logic for this is super simple to inline and does not require an external dependency to achieve. --- package.json | 1 - rollup.config.js | 1 - src/useMutation.ts | 16 ++++++++++------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index a9fd645a..dd6f58f3 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,6 @@ "build:js": "babel lib --out-dir lib --extensions \".js,.jsx\"" }, "dependencies": { - "@restart/hooks": "^0.3.1", "fbjs": "^3.0.0" }, "peerDependencies": { diff --git a/rollup.config.js b/rollup.config.js index 76794e50..12a2a236 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -32,7 +32,6 @@ function createConfigInternal({ format, production }) { 'fbjs/lib/areEqual': 'areEqual', 'fbjs/lib/invariant': 'invariant', 'fbjs/lib/warning': 'warning', - '@restart/hooks/useMounted': 'useMounted', react: 'React', 'relay-runtime': 'relayRuntime', }, diff --git a/src/useMutation.ts b/src/useMutation.ts index 54e40f50..d523c748 100644 --- a/src/useMutation.ts +++ b/src/useMutation.ts @@ -1,4 +1,3 @@ -import useMounted from '@restart/hooks/useMounted'; import * as invariant from 'fbjs/lib/invariant'; import * as React from 'react'; import { Environment, MutationParameters, commitMutation } from 'relay-runtime'; @@ -38,7 +37,13 @@ export function useMutation( error: null, }); - const isMounted = useMounted(); + const isMountedRef = React.useRef(true); + React.useEffect( + () => () => { + isMountedRef.current = false; + }, + [], + ); const relayEnvironment = useRelayEnvironment(); const resolvedEnvironment = environment || relayEnvironment; @@ -69,7 +74,7 @@ export function useMutation( invariant(mergedConfig.variables, 'you must specify variables'); - if (isMounted()) { + if (isMountedRef.current) { setState({ loading: true, data: mergedConfig.optimisticResponse, @@ -79,7 +84,7 @@ export function useMutation( return new Promise((resolve, reject) => { function handleError(error: any): void { - if (isMounted()) { + if (isMountedRef.current) { setState({ loading: false, data: null, @@ -106,7 +111,7 @@ export function useMutation( return; } - if (isMounted()) { + if (isMountedRef.current) { setState({ loading: false, data: response, @@ -134,7 +139,6 @@ export function useMutation( optimisticUpdater, optimisticResponse, updater, - isMounted, ], ); From 90d28837a55385e0e158f9e286c537b9914e17d2 Mon Sep 17 00:00:00 2001 From: n1ru4l Date: Fri, 12 Feb 2021 01:38:26 +0100 Subject: [PATCH 2/5] fix: add es module export --- package.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index dd6f58f3..036296a3 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,12 @@ "react", "ssr" ], - "main": "lib/index.js", + "main": "./lib/index.js", + "module": "./lib/es-relay-hooks.js", + "exports": { + "import": "./lib/es-relay-hooks.js", + "require": "./lib/index.js" + }, "license": "MIT", "description": "Relay Hooks", "author": { From 5a9295c2bd2f2bb970b625a1c6f04dc94c6dd073 Mon Sep 17 00:00:00 2001 From: n1ru4l Date: Fri, 12 Feb 2021 12:20:39 +0100 Subject: [PATCH 3/5] chore: use .mjs import --- package.json | 4 ++-- rollup.config.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 036296a3..c6916324 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,9 @@ "ssr" ], "main": "./lib/index.js", - "module": "./lib/es-relay-hooks.js", + "module": "./lib/es-relay-hooks.mjs", "exports": { - "import": "./lib/es-relay-hooks.js", + "import": "./lib/es-relay-hooks.mjs", "require": "./lib/index.js" }, "license": "MIT", diff --git a/rollup.config.js b/rollup.config.js index 12a2a236..4b6c2981 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -24,7 +24,7 @@ function createConfigInternal({ format, production }) { return { input: 'src/index.ts', output: { - file: 'lib/' + format + '-relay-hooks' + (production ? '.min' : '') + '.js', + file: 'lib/' + format + '-relay-hooks' + (production ? '.min' : '') + (format === "es" ? ".mjs" : '.js'), format, name: 'relay-hooks', indent: false, From 512af97db2151142b0c2dc9b01c6d6b6970ad3ca Mon Sep 17 00:00:00 2001 From: n1ru4l Date: Mon, 15 Feb 2021 12:26:33 +0100 Subject: [PATCH 4/5] chore: please the linter --- src/useMutation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/useMutation.ts b/src/useMutation.ts index d523c748..c97e50fe 100644 --- a/src/useMutation.ts +++ b/src/useMutation.ts @@ -39,7 +39,7 @@ export function useMutation( const isMountedRef = React.useRef(true); React.useEffect( - () => () => { + (): (() => void) => (): void => { isMountedRef.current = false; }, [], From 90e6d1ffc0be2da091dd255d50328e8a399d9f04 Mon Sep 17 00:00:00 2001 From: n1ru4l Date: Wed, 19 May 2021 07:53:12 +0200 Subject: [PATCH 5/5] feat: move compiled cjs files to lib/cjs (with commonjs package.json) --- package.json | 13 ++++++++----- rollup.config.js | 25 ++++++++++++++++++++++++- tsconfig.esm.json | 16 ++++++++++++++++ tsconfig.json | 4 ++-- 4 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 tsconfig.esm.json diff --git a/package.json b/package.json index c6916324..a402a4e7 100644 --- a/package.json +++ b/package.json @@ -8,11 +8,14 @@ "react", "ssr" ], - "main": "./lib/index.js", + "main": "./lib/cjs/cjs-relay-hooks.js", "module": "./lib/es-relay-hooks.mjs", "exports": { - "import": "./lib/es-relay-hooks.mjs", - "require": "./lib/index.js" + ".": { + "import": "./lib/es-relay-hooks.mjs", + "require": "./lib/cjs/cjs-relay-hooks.js" + }, + "./package.json": "./package.json" }, "license": "MIT", "description": "Relay Hooks", @@ -26,7 +29,7 @@ }, "scripts": { "clean": "rimraf lib", - "compile": "npm run clean && tsc && npm run build:js && npm run rollup", + "compile": "npm run clean && tsc && tsc --project tsconfig.esm.json && npm run build:js && npm run rollup", "rollup": "rollup -c", "build": "npm run compile && npm run test", "test": "cross-env NODE_ENV=test jest --coverage", @@ -34,7 +37,7 @@ "format:ci": "prettier --list-different \"src/**/*.{j,t}s*\"", "eslint": "eslint ./src --ext .js,.jsx,.ts,.tsx", "prepublishOnly": "npm run build", - "build:js": "babel lib --out-dir lib --extensions \".js,.jsx\"" + "build:js": "babel lib/cjs --out-dir lib/cjs --extensions \".js,.jsx\"" }, "dependencies": { "fbjs": "^3.0.0" diff --git a/rollup.config.js b/rollup.config.js index 4b6c2981..597be85e 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -8,6 +8,7 @@ import replace from '@rollup/plugin-replace'; import sourceMaps from 'rollup-plugin-sourcemaps'; import { terser } from 'rollup-plugin-terser'; import typescript from 'rollup-plugin-typescript2'; +import { promises as fs } from "fs"; import pkg from './package.json'; const makeExternalPredicate = (externalArr) => { @@ -24,7 +25,7 @@ function createConfigInternal({ format, production }) { return { input: 'src/index.ts', output: { - file: 'lib/' + format + '-relay-hooks' + (production ? '.min' : '') + (format === "es" ? ".mjs" : '.js'), + file: 'lib/' + (format === "cjs" ? "cjs/" : "") + format + '-relay-hooks' + (production ? '.min' : '') + '.js', format, name: 'relay-hooks', indent: false, @@ -82,6 +83,28 @@ function createConfigInternal({ format, production }) { toplevel: format === 'cjs', warnings: true, }), + format === "cjs" && { + name: "writePkgJSON", + writeBundle: async () => { + await fs.writeFile( + "lib/cjs/package.json", + JSON.stringify({ + type: "commonjs", + }) + ); + } + }, + format === "es" && { + name: "writePkgJSON", + writeBundle: async () => { + await fs.writeFile( + "lib/package.json", + JSON.stringify({ + type: "module", + }) + ); + } + }, ], }; } diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 00000000..fb278b9d --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "outDir": "lib", + "rootDir": "src", + "module": "ESNext", + "target": "ES2020", + "moduleResolution": "node", + "noEmitOnError": true, + "declaration": true, + "lib": ["dom", "es6", "esnext.asynciterable", "es2017.object"], + "jsx": "react", + "skipLibCheck": true + }, + "exclude": ["lib", "__tests__", "examples", "__mocks__", "coverage", "scripts"], + "compileOnSave": true +} diff --git a/tsconfig.json b/tsconfig.json index 628af437..0718b8e3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,12 +1,12 @@ { "compilerOptions": { - "outDir": "lib", + "outDir": "lib/cjs", "rootDir": "src", "module": "commonjs", "target": "es5", "moduleResolution": "node", "noEmitOnError": true, - "declaration": true, + "declaration": false, "lib": ["dom", "es6", "esnext.asynciterable", "es2017.object"], "jsx": "react", "skipLibCheck": true