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'
|
2024-11-02 21:13:41 +00:00
|
|
|
import { createImportProtectionPatterns } 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],
|
2024-11-19 18:56:33 +00:00
|
|
|
['node_modules/nitropack/node_modules/crossws/dist/adapters/bun.mjs', 'node_modules/nitropack/dist/presets/bun/runtime/bun.mjs', false],
|
2022-09-12 09:23:37 +00:00
|
|
|
] as const
|
|
|
|
|
|
|
|
describe('import protection', () => {
|
|
|
|
it.each(testsToTriggerOn)('should protect %s', async (id, importer, isProtected) => {
|
2024-11-02 21:13:41 +00:00
|
|
|
const result = await transformWithImportProtection(id, importer, 'nuxt-app')
|
2022-09-12 09:23:37 +00:00
|
|
|
if (!isProtected) {
|
|
|
|
expect(result).toBeNull()
|
|
|
|
} else {
|
|
|
|
expect(result).toBeDefined()
|
|
|
|
expect(normalize(result)).contains('unenv/runtime/mock/proxy')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-11-02 21:13:41 +00:00
|
|
|
const transformWithImportProtection = (id: string, importer: string, context: 'nitro-app' | 'nuxt-app' | 'shared') => {
|
2024-08-29 19:50:01 +00:00
|
|
|
const plugin = ImpoundPlugin.rollup({
|
|
|
|
cwd: '/root',
|
2024-11-02 21:13:41 +00:00
|
|
|
patterns: createImportProtectionPatterns({
|
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,
|
2024-11-02 21:13:41 +00:00
|
|
|
}, { context }),
|
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
|
|
|
}
|