From 4f6ba91e524461073aa81291d2fcfa6910ba7aa5 Mon Sep 17 00:00:00 2001 From: Eugene Date: Sun, 22 Jan 2023 13:09:32 -0500 Subject: [PATCH] add an ESM version PR-URL: https://github.com/isaacs/node-lru-cache/pull/266 Credit: @eugene1g Close: #266 Reviewed-by: @isaacs EDIT(@isaacs): edited to move the test into a test file, and run the build before running tests, and specify TS types explicitly for both export types, and document the hybridness. --- .gitignore | 1 + README.md | 7 +++++-- package.json | 22 ++++++++++++++++++++-- scripts/transpile-to-esm.mjs | 9 +++++++++ test/esm-load.mjs | 10 ++++++++++ 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 scripts/transpile-to-esm.mjs create mode 100644 test/esm-load.mjs diff --git a/.gitignore b/.gitignore index 314db3d6..966e2e0e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /nyc_output /coverage /bundle +/index.mjs \ No newline at end of file diff --git a/README.md b/README.md index d0e2a9c9..0612ac7e 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,10 @@ npm install lru-cache --save ## Usage ```js -const LRU = require('lru-cache') +// hybrid module, either works +import LRUCache from 'lru-cache' +// or: +const LRUCache = require('lru-cache') // At least one of 'max', 'ttl', or 'maxSize' is required, to prevent // unsafe unbounded storage. @@ -70,7 +73,7 @@ const options = { fetchMethod: async (key, staleValue, { options, signal }) => {} } -const cache = new LRU(options) +const cache = new LRUCache(options) cache.set("key", "value") cache.get("key") // "value" diff --git a/package.json b/package.json index beeaf542..d2e37fef 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ ], "sideEffects": false, "scripts": { - "build": "", + "pretest": "node ./scripts/transpile-to-esm.mjs", + "presnap": "node ./scripts/transpile-to-esm.mjs", "size": "size-limit", "test": "tap", "snap": "tap", @@ -20,7 +21,23 @@ "format": "prettier --write .", "typedoc": "typedoc ./index.d.ts" }, - "main": "index.js", + "type": "commonjs", + "main": "./index.js", + "module": "./index.mjs", + "types": "./index.d.ts", + "exports": { + ".": { + "import": { + "types": "./index.d.ts", + "default": "./index.mjs" + }, + "require": { + "types": "./index.d.ts", + "default": "./index.js" + } + }, + "./package.json": "./package.json" + }, "repository": "git://github.com/isaacs/node-lru-cache.git", "devDependencies": { "@size-limit/preset-small-lib": "^7.0.8", @@ -41,6 +58,7 @@ "license": "ISC", "files": [ "index.js", + "index.mjs", "index.d.ts" ], "engines": { diff --git a/scripts/transpile-to-esm.mjs b/scripts/transpile-to-esm.mjs new file mode 100644 index 00000000..9a694969 --- /dev/null +++ b/scripts/transpile-to-esm.mjs @@ -0,0 +1,9 @@ +#!/usr/bin/env node + +import { readFile, writeFile } from 'node:fs/promises' +const cjs = await readFile( + new URL('../index.js', import.meta.url), + 'utf8' +) +const esm = cjs.replace(/module.exports\s*=\s*/, 'export default ') +await writeFile(new URL('../index.mjs', import.meta.url), esm, 'utf8') diff --git a/test/esm-load.mjs b/test/esm-load.mjs new file mode 100644 index 00000000..40739fdc --- /dev/null +++ b/test/esm-load.mjs @@ -0,0 +1,10 @@ +import t from 'tap' +import LRUCache from '../index.mjs' +const c = new LRUCache({ max: 2 }) +t.type(c, LRUCache) +c.set(1, 1) +c.set(2, 2) +c.set(3, 3) +t.equal(c.get(1), undefined) +t.equal(c.get(2), 2) +t.equal(c.get(3), 3)