mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-15 02:14:44 +00:00
68 lines
2.2 KiB
TypeScript
68 lines
2.2 KiB
TypeScript
|
import { describe, expect, it } from 'vitest'
|
||
|
|
||
|
import { UnctxTransformPlugin } from '../src/core/plugins/unctx'
|
||
|
|
||
|
describe('unctx transform in nuxt', () => {
|
||
|
it('should transform nuxt plugins', async () => {
|
||
|
const code = `
|
||
|
export default defineNuxtPlugin({
|
||
|
async setup () {
|
||
|
await Promise.resolve()
|
||
|
}
|
||
|
})
|
||
|
`
|
||
|
expect(await transform(code)).toMatchInlineSnapshot(`
|
||
|
"/* _processed_nuxt_unctx_transform */
|
||
|
import { executeAsync as __executeAsync } from "unctx";
|
||
|
export default defineNuxtPlugin({
|
||
|
async setup () {let __temp, __restore;
|
||
|
;(([__temp,__restore]=__executeAsync(()=>Promise.resolve())),await __temp,__restore());
|
||
|
}
|
||
|
},1)"
|
||
|
`)
|
||
|
})
|
||
|
|
||
|
it('should transform vue components using defineNuxtComponent', async () => {
|
||
|
const code = `
|
||
|
definePageMeta({
|
||
|
async middleware() {
|
||
|
await Promise.resolve()
|
||
|
}
|
||
|
})
|
||
|
export default defineNuxtComponent({
|
||
|
async setup () {
|
||
|
await Promise.resolve()
|
||
|
}
|
||
|
})
|
||
|
`
|
||
|
expect(await transform(code, 'app.ts')).toMatchInlineSnapshot(`
|
||
|
"/* _processed_nuxt_unctx_transform */
|
||
|
import { executeAsync as __executeAsync } from "unctx";
|
||
|
definePageMeta({
|
||
|
async middleware() {let __temp, __restore;
|
||
|
;(([__temp,__restore]=__executeAsync(()=>Promise.resolve())),await __temp,__restore());
|
||
|
}
|
||
|
})
|
||
|
export default defineNuxtComponent({
|
||
|
async setup () {let __temp, __restore;
|
||
|
;(([__temp,__restore]=__executeAsync(()=>Promise.resolve())),await __temp,__restore());
|
||
|
}
|
||
|
})"
|
||
|
`)
|
||
|
})
|
||
|
})
|
||
|
|
||
|
function transform (code: string, id = 'app.vue') {
|
||
|
const transformerOptions = {
|
||
|
helperModule: 'unctx',
|
||
|
asyncFunctions: ['defineNuxtPlugin', 'defineNuxtRouteMiddleware'],
|
||
|
objectDefinitions: {
|
||
|
defineNuxtComponent: ['asyncData', 'setup'],
|
||
|
defineNuxtPlugin: ['setup'],
|
||
|
definePageMeta: ['middleware', 'validate'],
|
||
|
},
|
||
|
}
|
||
|
const plugin = UnctxTransformPlugin.raw({ sourcemap: false, transformerOptions }, {} as any) as any
|
||
|
return plugin.transformInclude(id) ? Promise.resolve(plugin.transform(code)).then((r: any) => r?.code.replace(/^ {6}/gm, '').trim()) : null
|
||
|
}
|