diff --git a/package.json b/package.json index 876a4a3..1bf5a61 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "lint:fix": "pnpm run lint --fix", "build": "tsdown", "dev": "tsdown --watch", - "test": "node --test tests/index.test.js", + "test": "node --test tests/index.test.ts", "test:bun": "bun test tests/bun.test.ts", "typecheck": "tsgo --noEmit", "format": "prettier --cache --write .", diff --git a/src/index.ts b/src/index.ts index 3653c39..7c97254 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,8 @@ export interface Options { const RE_NODE_MODULES = /[/\\]node_modules[/\\]/ +export const loaded: Set = new Set() + let deregister: (() => void) | undefined export function init({ skipNodeModules }: Options = {}): () => void { if (process.versions.bun) { @@ -42,6 +44,7 @@ export function init({ skipNodeModules }: Options = {}): () => void { const parentUUID = getParentUUID(context.parentURL) if (!noCache && !parentUUID) return resolved + loaded.add(resolved.url) resolved.url = appendUUID(resolved.url, parentUUID || crypto.randomUUID()) return resolved }, @@ -54,6 +57,7 @@ export function init({ skipNodeModules }: Options = {}): () => void { return (deregister = () => { hooks.deregister() + loaded.clear() deregister = undefined }) } diff --git a/tests/index.test.js b/tests/index.test.ts similarity index 93% rename from tests/index.test.js rename to tests/index.test.ts index 4344319..a6da94f 100644 --- a/tests/index.test.js +++ b/tests/index.test.ts @@ -1,8 +1,6 @@ -// @ts-check - import assert from 'node:assert' import { afterEach, test } from 'node:test' -import { clearRequireCache, init, unregister } from '../dist/index.mjs' +import { clearRequireCache, init, unregister, loaded } from '../dist/index.mjs' afterEach(() => { unregister() @@ -13,6 +11,7 @@ const RE_NO_CACHE = /\?no-cache=[0-9a-f-]{36}$/ test('import attributes', async () => { const deregister = init() const { uuid, url, cjs, requireESM, importCJS, dynamicImportCJS } = + // @ts-ignore missing types await import('./fixtures/mod.js', { with: { cache: 'no' }, }) @@ -22,9 +21,11 @@ test('import attributes', async () => { requireESM: requireESM2, importCJS: importCJS2, dynamicImportCJS: dynamicImportCJS2, + // @ts-ignore missing types } = await import('./fixtures/mod.js', { with: { cache: 'no' }, }) + assert(loaded.size > 0) clearRequireCache() const { uuid: uuid3, @@ -68,6 +69,7 @@ test('import attributes', async () => { deregister() await assert.rejects( + // @ts-ignore missing types () => import('./fixtures/mod.js', { with: { cache: 'no' } }), ) }) diff --git a/tsdown.config.ts b/tsdown.config.ts index 6ead994..8352d2b 100644 --- a/tsdown.config.ts +++ b/tsdown.config.ts @@ -1,6 +1,7 @@ import { defineConfig } from 'tsdown' export default defineConfig({ + dts: true, exports: true, deps: { onlyBundle: [],