mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
55 lines
2.1 KiB
TypeScript
55 lines
2.1 KiB
TypeScript
import { normalize } from 'pathe'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { ImpoundPlugin } from 'impound'
|
|
import { createImportProtectionPatterns } from '../src/core/plugins/import-protection'
|
|
import type { NuxtOptions } from '../schema'
|
|
|
|
const testsToTriggerOn = [
|
|
['~/nuxt.config', 'app.vue', true],
|
|
['./nuxt.config', 'app.vue', true],
|
|
['./nuxt.config.ts', 'app.vue', true],
|
|
['nuxt.config.ts', 'app.vue', true],
|
|
['./.nuxt/nuxt.config', 'app.vue', false],
|
|
['.nuxt/nuxt.config', 'app.vue', false],
|
|
['nuxt', 'components/Component.vue', true],
|
|
['nuxt3', 'components/Component.vue', true],
|
|
['nuxt-nightly', 'components/Component.vue', true],
|
|
['/root/node_modules/@vue/composition-api', 'components/Component.vue', true],
|
|
['@vue/composition-api', 'components/Component.vue', true],
|
|
['@nuxt/kit', 'components/Component.vue', true],
|
|
['nuxt/config', 'components/Component.vue', true],
|
|
['nuxt/kit', 'components/Component.vue', true],
|
|
['nuxt/schema', 'components/Component.vue', true],
|
|
['/root/node_modules/@nuxt/kit', 'components/Component.vue', true],
|
|
['some-nuxt-module', 'components/Component.vue', true],
|
|
['/root/src/server/api/test.ts', 'components/Component.vue', true],
|
|
['src/server/api/test.ts', 'components/Component.vue', true],
|
|
] as const
|
|
|
|
describe('import protection', () => {
|
|
it.each(testsToTriggerOn)('should protect %s', async (id, importer, isProtected) => {
|
|
const result = await transformWithImportProtection(id, importer, 'nuxt-app')
|
|
if (!isProtected) {
|
|
expect(result).toBeNull()
|
|
} else {
|
|
expect(result).toBeDefined()
|
|
expect(normalize(result)).contains('unenv/runtime/mock/proxy')
|
|
}
|
|
})
|
|
})
|
|
|
|
const transformWithImportProtection = (id: string, importer: string, context: 'nitro-app' | 'nuxt-app' | 'shared') => {
|
|
const plugin = ImpoundPlugin.rollup({
|
|
cwd: '/root',
|
|
patterns: createImportProtectionPatterns({
|
|
options: {
|
|
modules: ['some-nuxt-module'],
|
|
srcDir: '/root/src/',
|
|
serverDir: '/root/src/server',
|
|
} satisfies Partial<NuxtOptions> as NuxtOptions,
|
|
}, { context }),
|
|
})
|
|
|
|
return (plugin as any).resolveId.call({ error: () => {} }, id, importer)
|
|
}
|