Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __snapshots__/tsnapi/index.snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
22 changes: 22 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>,
): boolean {
return Object.entries(attrs).every(([key, value]) => {
const k = escapeRegExp(key)
const v = escapeRegExp(value)
return new RegExp(
String.raw`(?:(?<![\w$])${k}(?![\w$])|(['"])${k}\1)\s*:\s*(['"])${v}\2`,
).test(source)
})
}
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import {
VIRTUAL_ID_PREFIX,
} from './core/index.ts'
import { resolveOptions, type Options } from './core/options.ts'
import { mayContainMacros } from './core/utils.ts'
import type { MacroRunner } from './runner/index.ts'
import type { ModuleNode } from 'vite'
import type { VitestPluginContext } from 'vitest/node'

export { defineMacro } from './core/define.ts'
export {
Expand Down Expand Up @@ -113,6 +115,15 @@ export const Macros: UnpluginInstance<Options | undefined, false> =

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
})
},
},
}
})
47 changes: 47 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
Loading