From ebf3d4c347369875e47fa9549f339bfe5bbf5e29 Mon Sep 17 00:00:00 2001 From: Kevin Bot Date: Thu, 29 Oct 2026 12:37:03 +0900 Subject: [PATCH] fix(vite): skip Vitest fs module cache for modules importing macros --- __snapshots__/tsnapi/index.snapshot.js | 2 +- package.json | 6 +++- src/core/utils.ts | 22 ++++++++++++ src/index.ts | 11 ++++++ tests/utils.test.ts | 47 ++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 tests/utils.test.ts diff --git a/__snapshots__/tsnapi/index.snapshot.js b/__snapshots__/tsnapi/index.snapshot.js index 6ce4deb..b1036a9 100644 --- a/__snapshots__/tsnapi/index.snapshot.js +++ b/__snapshots__/tsnapi/index.snapshot.js @@ -2,5 +2,5 @@ * Generated by tsnapi — public API snapshot of `unplugin-macros` */ // #region Re-exports -export { t as Macros, r as defineMacro, a as nativeRunner, o as resolveMacroPath, i as resolveOptions, s as transformMacros, n as unrunRunner } from "./src-25akV9P6.mjs"; +export { t as Macros, r as defineMacro, a as nativeRunner, o as resolveMacroPath, i as resolveOptions, s as transformMacros, n as unrunRunner } from "./src-CHA9-pLX.mjs"; // #endregion \ No newline at end of file diff --git a/package.json b/package.json index 9231b69..8fe1216 100644 --- a/package.json +++ b/package.json @@ -60,11 +60,15 @@ "prepublishOnly": "pnpm run build" }, "peerDependencies": { - "unrun": "^0.3.1" + "unrun": "^0.3.1", + "vitest": "^5.0.0" }, "peerDependenciesMeta": { "unrun": { "optional": true + }, + "vitest": { + "optional": true } }, "dependencies": { diff --git a/src/core/utils.ts b/src/core/utils.ts index a37e5aa..a480496 100644 --- a/src/core/utils.ts +++ b/src/core/utils.ts @@ -31,3 +31,25 @@ export function resolveMemberChain( id.unshift(current.name) return { root: current, id } } + +function escapeRegExp(str: string): string { + return str.replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw`\$&`) +} + +/** + * Cheaply checks whether the source may import a macro, i.e. whether every + * expected import attribute appears as a `key: 'value'` pair. It may report + * false positives, but never misses a macro import. + */ +export function mayContainMacros( + source: string, + attrs: Record, +): boolean { + return Object.entries(attrs).every(([key, value]) => { + const k = escapeRegExp(key) + const v = escapeRegExp(value) + return new RegExp( + String.raw`(?:(? = return [...affected, ...modules] }, + + // Vitest's fs module cache is keyed by the module's own source, so it + // can't see changes in macros or in files they read. Opt modules + // that may import macros out of the cache. + configureVitest({ defineCacheKeyGenerator }: VitestPluginContext) { + defineCacheKeyGenerator?.(({ sourceCode }) => { + if (mayContainMacros(sourceCode, options.attrs)) return false + }) + }, }, } }) diff --git a/tests/utils.test.ts b/tests/utils.test.ts new file mode 100644 index 0000000..7b3692b --- /dev/null +++ b/tests/utils.test.ts @@ -0,0 +1,47 @@ +import { describe, expect, test } from 'vitest' +import { mayContainMacros } from '../src/core/utils.ts' + +describe('mayContainMacros', () => { + const attrs = { type: 'macro' } + + test('detects macro imports', () => { + expect( + mayContainMacros(`import { a } from './a' with { type: 'macro' }`, attrs), + ).toBe(true) + expect( + mayContainMacros(`import { a } from './a' with {type:"macro"}`, attrs), + ).toBe(true) + expect( + mayContainMacros( + `import { a } from './a' with { 'type': 'macro' }`, + attrs, + ), + ).toBe(true) + expect( + mayContainMacros(`export * from './a' assert { type: 'macro' }`, attrs), + ).toBe(true) + }) + + test('ignores other modules', () => { + expect(mayContainMacros(`import { a } from './a'`, attrs)).toBe(false) + expect( + mayContainMacros(`import a from './a.json' with { type: 'json' }`, attrs), + ).toBe(false) + expect(mayContainMacros(`const x = { subtype: 'macro' }`, attrs)).toBe( + false, + ) + }) + + test('custom attrs', () => { + const custom = { 'x-kind': 'macro', mode: 'build' } + expect( + mayContainMacros( + `import a from './a' with { 'x-kind': 'macro', mode: 'build' }`, + custom, + ), + ).toBe(true) + expect( + mayContainMacros(`import a from './a' with { mode: 'build' }`, custom), + ).toBe(false) + }) +})