2022-09-12 09:23:37 +00:00
|
|
|
import { normalize } from 'pathe'
|
|
|
|
import { describe, expect, it } from 'vitest'
|
2024-08-29 19:50:01 +00:00
|
|
|
import { ImpoundPlugin } from 'impound'
|
|
|
|
import { nuxtImportProtections } from '../src/core/plugins/import-protection'
|
2024-01-12 11:22:01 +00:00
|
|
|
import type { NuxtOptions } from '../schema'
|
2022-09-12 09:23:37 +00:00
|
|
|
|
|
|
|
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],
|
2023-10-12 14:17:38 +00:00
|
|
|
['nuxt-nightly', 'components/Component.vue', true],
|
2022-09-12 09:23:37 +00:00
|
|
|
['/root/node_modules/@vue/composition-api', 'components/Component.vue', true],
|
|
|
|
['@vue/composition-api', 'components/Component.vue', true],
|
|
|
|
['@nuxt/kit', 'components/Component.vue', true],
|
2023-09-22 10:32:13 +00:00
|
|
|
['nuxt/config', 'components/Component.vue', true],
|
|
|
|
['nuxt/kit', 'components/Component.vue', true],
|
|
|
|
['nuxt/schema', 'components/Component.vue', true],
|
2022-09-12 09:23:37 +00:00
|
|
|
['/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],
|
2024-04-05 18:08:32 +00:00
|
|
|
['src/server/api/test.ts', 'components/Component.vue', true],
|
2022-09-12 09:23:37 +00:00
|
|
|
] as const
|
|
|
|
|
|
|
|
describe('import protection', () => {
|
|
|
|
it.each(testsToTriggerOn)('should protect %s', async (id, importer, isProtected) => {
|
|
|
|
const result = await transformWithImportProtection(id, importer)
|
|
|
|
if (!isProtected) {
|
|
|
|
expect(result).toBeNull()
|
|
|
|
} else {
|
|
|
|
expect(result).toBeDefined()
|
|
|
|
expect(normalize(result)).contains('unenv/runtime/mock/proxy')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
const transformWithImportProtection = (id: string, importer: string) => {
|
2024-08-29 19:50:01 +00:00
|
|
|
const plugin = ImpoundPlugin.rollup({
|
|
|
|
cwd: '/root',
|
2024-01-12 11:22:01 +00:00
|
|
|
patterns: nuxtImportProtections({
|
2022-09-12 09:23:37 +00:00
|
|
|
options: {
|
|
|
|
modules: ['some-nuxt-module'],
|
2024-01-12 11:22:01 +00:00
|
|
|
srcDir: '/root/src/',
|
2024-04-05 18:08:32 +00:00
|
|
|
serverDir: '/root/src/server',
|
|
|
|
} satisfies Partial<NuxtOptions> as NuxtOptions,
|
|
|
|
}),
|
2022-09-12 09:23:37 +00:00
|
|
|
})
|
|
|
|
|
2024-08-29 19:50:01 +00:00
|
|
|
return (plugin as any).resolveId.call({ error: () => {} }, id, importer)
|
2022-09-12 09:23:37 +00:00
|
|
|
}
|