diff --git a/test/addon-match-imports.test.ts b/test/addon-match-imports.test.ts new file mode 100644 index 00000000..293bb8ad --- /dev/null +++ b/test/addon-match-imports.test.ts @@ -0,0 +1,35 @@ +import { describe, expect, it } from 'vitest' +import { createUnimport } from '../src' +import { resolverAddon } from './share' + +describe('addon match imports', () => { + const ctx = createUnimport({ + presets: ['vue'], + addons: [ + resolverAddon(), + ], + }) + + it('inject', async () => { + const code = ` +import otherModule from 'otherModule' + +const count = ref(0) +const bar = foo +const test = notDefined +console.log(otherModule) +`.trim() + + expect((await ctx.injectImports(code)).code) + .toMatchInlineSnapshot(` + "import { ref } from 'vue'; + import { foo } from 'bar'; + import otherModule from 'otherModule' + + const count = ref(0) + const bar = foo + const test = notDefined + console.log(otherModule)" + `) + }) +}) diff --git a/test/detect-acorn.test.ts b/test/detect-acorn.test.ts index 7b63cee4..c36f54f2 100644 --- a/test/detect-acorn.test.ts +++ b/test/detect-acorn.test.ts @@ -1,7 +1,8 @@ import { parse } from 'acorn' import { describe, expect, it } from 'vitest' -import { createUnimport, type Import } from '../src' +import { createUnimport } from '../src' import { traveseScopes } from '../src/detect-acorn' +import { resolverAddon } from './share' describe('detect-acorn', () => { it('scopes', async () => { @@ -135,22 +136,7 @@ describe('detect-acorn', () => { parser: 'acorn', presets: ['vue'], addons: [ - { - name: 'resolver', - async matchImports(names, matched) { - const dynamic: Import[] = [] - for (const name of names) { - if (name === 'foo') { - dynamic.push({ - from: 'bar', - name: 'foo', - as: 'foo', - }) - } - } - return [...matched, ...dynamic] - }, - }, + resolverAddon(), ], }) diff --git a/test/share.ts b/test/share.ts index c9d2c454..4d0bd892 100644 --- a/test/share.ts +++ b/test/share.ts @@ -1,4 +1,4 @@ -import type { Addon } from '../src' +import type { Addon, Import } from '../src' export function functionWrapAddon(): Addon { return { @@ -30,3 +30,22 @@ export function functionWrapAddon(): Addon { }, } } + +export function resolverAddon(): Addon { + return { + name: 'resolver', + async matchImports(names, matched) { + const dynamic: Import[] = [] + for (const name of names) { + if (name === 'foo') { + dynamic.push({ + from: 'bar', + name: 'foo', + as: 'foo', + }) + } + } + return [...matched, ...dynamic] + }, + } +}