From ac9ef8786fad90e34aeb1aef566ff3863e2cd1ef Mon Sep 17 00:00:00 2001 From: Kevin Deng Date: Mon, 1 Dec 2025 02:39:58 +0800 Subject: [PATCH] feat!: rename `clearCJSCache` to `clearRequireCache` --- README.md | 9 +++++---- package.json | 2 ++ pnpm-lock.yaml | 17 +++++++++++++++++ src/index.ts | 2 +- tests/bun.test.ts | 22 ++++++++++++++++++++++ tests/fixtures/mod.js | 2 +- tests/index.test.js | 10 +++++----- tsconfig.json | 2 +- 8 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 tests/bun.test.ts diff --git a/README.md b/README.md index 381d6a0..cbf0a96 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ npm i import-without-cache ## Usage ```ts -import { clearCJSCache, init, isSupported } from 'import-without-cache' +import { clearRequireCache, init, isSupported } from 'import-without-cache' if (!isSupported) { throw new Error('import-without-cache is not supported in this environment.') @@ -30,7 +30,7 @@ const deregister = init() const mod = await import('some-module', { with: { cache: 'no' } }) -clearCJSCache() // Optional: clear CommonJS cache if needed +clearRequireCache() // Optional: clear CommonJS cache if needed // or const mod2 = await import(`no-cache://some-module`) @@ -42,8 +42,9 @@ deregister() // Optional: deregister the hooks when no longer needed ## Known Limitations -- Support Node.js since v22.15.0, and doesn't support Deno or Bun. -- Only supports ESM modules by default. CommonJS cache can be cleared by `clearCJSCache`. +- Support Node.js since v22.15.0, and doesn't support Deno. + - For Bun, `clearRequireCache` works, but `init` is not needed since ESM imports is the same as CJS requires. +- Only supports ESM modules by default. CommonJS cache can be cleared by `clearRequireCache`. - `require(esm)` is not supported. ## Sponsors diff --git a/package.json b/package.json index 28d9e0f..5ec5d79 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "build": "tsdown", "dev": "tsdown --watch", "test": "node --test tests/index.test.js", + "test:bun": "bun test tests/bun.test.ts", "typecheck": "tsc --noEmit", "format": "prettier --cache --write .", "release": "bumpp", @@ -42,6 +43,7 @@ "devDependencies": { "@sxzz/eslint-config": "^7.4.0", "@sxzz/prettier-config": "^2.2.6", + "@types/bun": "^1.3.3", "@types/node": "^24.10.1", "bumpp": "^10.3.2", "eslint": "^9.39.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e50f0c1..6c5d4c0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@sxzz/prettier-config': specifier: ^2.2.6 version: 2.2.6 + '@types/bun': + specifier: ^1.3.3 + version: 1.3.3 '@types/node': specifier: ^24.10.1 version: 24.10.1 @@ -400,6 +403,9 @@ packages: '@tybys/wasm-util@0.10.1': resolution: {integrity: sha512-9tTaPJLSiejZKx+Bmog4uSubteqTvFrVrURwkmHixBo0G4seD0zUxp98E1DzUBJxLQ3NPwXrGKDiVjwx/DpPsg==} + '@types/bun@1.3.3': + resolution: {integrity: sha512-ogrKbJ2X5N0kWLLFKeytG0eHDleBYtngtlbu9cyBKFtNL3cnpDZkNdQj8flVf6WTZUX5ulI9AY1oa7ljhSrp+g==} + '@types/debug@4.1.12': resolution: {integrity: sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ==} @@ -651,6 +657,9 @@ packages: engines: {node: '>=18'} hasBin: true + bun-types@1.3.3: + resolution: {integrity: sha512-z3Xwlg7j2l9JY27x5Qn3Wlyos8YAp0kKRlrePAOjgjMGS5IG6E7Jnlx736vH9UVI4wUICwwhC9anYL++XeOgTQ==} + c12@3.3.2: resolution: {integrity: sha512-QkikB2X5voO1okL3QsES0N690Sn/K9WokXqUsDQsWy5SnYb+psYQFGA10iy1bZHj3fjISKsI67Q90gruvWWM3A==} peerDependencies: @@ -2097,6 +2106,10 @@ snapshots: tslib: 2.8.1 optional: true + '@types/bun@1.3.3': + dependencies: + bun-types: 1.3.3 + '@types/debug@4.1.12': dependencies: '@types/ms': 2.1.0 @@ -2341,6 +2354,10 @@ snapshots: transitivePeerDependencies: - magicast + bun-types@1.3.3: + dependencies: + '@types/node': 24.10.1 + c12@3.3.2: dependencies: chokidar: 4.0.3 diff --git a/src/index.ts b/src/index.ts index adc3877..f8b0854 100644 --- a/src/index.ts +++ b/src/index.ts @@ -53,7 +53,7 @@ export function init(): () => void { }) } -export function clearCJSCache(): void { +export function clearRequireCache(): void { for (const key of Object.keys(require.cache)) { delete require.cache[key] } diff --git a/tests/bun.test.ts b/tests/bun.test.ts new file mode 100644 index 0000000..43222c1 --- /dev/null +++ b/tests/bun.test.ts @@ -0,0 +1,22 @@ +import { expect, test } from 'bun:test' +import { clearRequireCache } from '../src/index.ts' + +test('clear cache', async () => { + // @ts-expect-error missing type + const mod = await import('./fixtures/mod.js') + // @ts-expect-error missing type + const mod2 = await import('./fixtures/mod.js') + + expect(mod).toBe(mod2) + + clearRequireCache() + // @ts-expect-error missing type + const mod3 = await import('./fixtures/mod.js') + expect(mod).not.toBe(mod3) + expect(mod.uuid).not.toBe(mod3.uuid) + expect(mod.cjs).not.toBe(mod3.cjs) + expect(mod.requireESM).not.toBe(mod3.requireESM) + expect(mod.importCJS).not.toBe(mod3.importCJS) + expect(mod.dynamicImportCJS).not.toBe(mod3.dynamicImportCJS) + expect(mod.url).toMatch(/\.js$/) +}) diff --git a/tests/fixtures/mod.js b/tests/fixtures/mod.js index 1342c0f..5aa3060 100644 --- a/tests/fixtures/mod.js +++ b/tests/fixtures/mod.js @@ -10,7 +10,7 @@ export * from './rand.js' export const cjs = require('./cjs.cjs') // require + ESM -export const requireESM = require('./require-esm.js') +export const requireESM = require('./require-esm.js').requireESM // import + CJS export { default as importCJS } from './cjs-import.cjs' // dynamic import + CJS diff --git a/tests/index.test.js b/tests/index.test.js index 53f61d4..d610ace 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -2,7 +2,7 @@ import assert from 'node:assert' import { test } from 'node:test' -import { clearCJSCache, init } from '../dist/index.mjs' +import { clearRequireCache, init } from '../dist/index.mjs' test('import attributes', async () => { const deregister = init() @@ -19,7 +19,7 @@ test('import attributes', async () => { } = await import('./fixtures/mod.js', { with: { cache: 'no' }, }) - clearCJSCache() + clearRequireCache() const { uuid: uuid3, cjs: cjs3, @@ -42,17 +42,17 @@ test('import attributes', async () => { // require + CJS assert.equal(cjs, cjs2) - // require + CJS + clearCJSCache + // require + CJS + clearRequireCache assert.notEqual(cjs2, cjs3) // import + CJS assert.equal(importCJS, importCJS2) - // import + CJS + clearCJSCache + // import + CJS + clearRequireCache assert.notEqual(importCJS2, importCJS3) // dynamic import + CJS assert.equal(dynamicImportCJS, dynamicImportCJS2) - // dynamic import + CJS + clearCJSCache + // dynamic import + CJS + clearRequireCache assert.notEqual(dynamicImportCJS2, dynamicImportCJS3) // known limitation: require cache can't be fully cleared diff --git a/tsconfig.json b/tsconfig.json index 1086ea3..78edd83 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,7 @@ "module": "nodenext", "moduleResolution": "node16", "resolveJsonModule": true, - "types": ["node"], + "types": ["node", "bun"], "allowImportingTsExtensions": true, "strict": true, "noUnusedLocals": true, -- 2.51.2