From bc0daf6de52520cb9413e9f6541e9738181f5a56 Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Thu, 2 Jun 2022 22:21:08 +0200 Subject: [PATCH] feat: refactor to use modern exports this should follow the suggested format from Microbundle --- package.json | 20 ++++++++++++-------- scripts/build-copy.js | 21 +++++++++++++++++++-- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 34c4a9df..29d489d9 100644 --- a/package.json +++ b/package.json @@ -2,11 +2,15 @@ "name": "react-intersection-observer", "version": "8.26.1", "description": "Monitor if a component is inside the viewport, using IntersectionObserver API", - "source": "src/index.tsx", - "main": "dist/react-intersection-observer.js", - "module": "dist/react-intersection-observer.m.js", - "unpkg": "dist/react-intersection-observer.umd.js", - "typings": "dist/index.d.ts", + "source": "./src/index.tsx", + "main": "./dist/react-intersection-observer.js", + "module": "./dist/react-intersection-observer.mjs", + "exports": { + "require": "./dist/react-intersection-observer.js", + "default": "./dist/react-intersection-observer.modern.mjs" + }, + "unpkg": "./dist/react-intersection-observer.umd.js", + "typings": "./dist/index.d.ts", "author": "Daniel Schmidt", "sideEffects": false, "repository": { @@ -89,19 +93,19 @@ }, "size-limit": [ { - "path": "dist/react-intersection-observer.m.js", + "path": "dist/react-intersection-observer.mjs", "name": "InView", "import": "{ InView }", "limit": "1.8 kB" }, { - "path": "dist/react-intersection-observer.m.js", + "path": "dist/react-intersection-observer.mjs", "name": "useInView", "import": "{ useInView }", "limit": "1.3 kB" }, { - "path": "dist/react-intersection-observer.m.js", + "path": "dist/react-intersection-observer.mjs", "name": "observe", "import": "{ observe }", "limit": "1 kB" diff --git a/scripts/build-copy.js b/scripts/build-copy.js index 9a4bda9b..8079dec4 100644 --- a/scripts/build-copy.js +++ b/scripts/build-copy.js @@ -29,8 +29,25 @@ packageFieldsToRemove.forEach((field) => { }); // Remove 'dist' from the files inside the 'dist' dir, after we move them -const fields = ['main', 'module', 'unpkg', 'esmodule', 'exports', 'typings']; -fields.forEach((key) => (pck[key] = pck[key]?.replace('dist/', ''))); +const fields = [ + 'main', + 'module', + 'unpkg', + 'exports', + 'esmodule', + 'exports', + 'typings', +]; +fields.forEach((key) => { + if (typeof pck[key] === 'object') { + const keys = Object.keys(pck[key]); + keys.forEach((subkey) => { + pck[key][subkey] = pck[key][subkey]?.replace('./dist/', './'); + }); + } else { + pck[key] = pck[key]?.replace('./dist/', './'); + } +}); fs.writeFileSync( path.resolve(distDir, 'package.json'),