2021-12-20 16:27:36 +00:00
import { readFileSync } from 'fs'
2022-01-13 17:54:33 +00:00
import { expect , describe , it } from 'vitest'
2021-12-20 16:27:36 +00:00
import { join } from 'pathe'
import { createCommonJS , findExports } from 'mlly'
2021-11-05 14:39:14 +00:00
import * as VueFunctions from 'vue'
2022-03-11 08:09:11 +00:00
import { createUnimport , Import } from 'unimport'
2021-10-11 08:07:27 +00:00
import { TransformPlugin } from '../src/auto-imports/transform'
2022-03-11 08:09:11 +00:00
import { defaultPresets } from '../src/auto-imports/presets'
2021-08-10 00:27:23 +00:00
2021-10-18 13:39:53 +00:00
describe ( 'auto-imports:transform' , ( ) = > {
2022-03-11 08:09:11 +00:00
const imports : Import [ ] = [
2021-10-18 13:39:53 +00:00
{ name : 'ref' , as : 'ref' , from : 'vue' } ,
2021-12-21 14:28:45 +00:00
{ name : 'computed' , as : 'computed' , from : 'bar' } ,
{ name : 'foo' , as : 'foo' , from : 'excluded' }
2021-10-18 13:39:53 +00:00
]
2022-03-11 08:09:11 +00:00
const ctx = createUnimport ( {
imports
} )
2021-10-20 09:47:18 +00:00
2022-03-11 08:09:11 +00:00
const transformPlugin = TransformPlugin . raw ( { ctx , options : { transform : { exclude : [ /node_modules/ ] } } } , { framework : 'rollup' } )
const transform = async ( source : string ) = > {
const { code } = await transformPlugin . transform . call ( { error : null , warn : null } , source , '' ) || { code : null }
return code
}
2021-08-10 00:27:23 +00:00
2021-08-30 11:55:57 +00:00
it ( 'should correct inject' , async ( ) = > {
2022-03-11 08:09:11 +00:00
expect ( await transform ( 'const a = ref(0)' ) ) . toMatchInlineSnapshot ( '"import { ref } from \'vue\';const a = ref(0)"' )
expect ( await transform ( 'import { computed as ref } from "foo"; const a = ref(0)' ) ) . to . toMatchInlineSnapshot ( '"import { computed } from \'bar\';import { computed as ref } from \\"foo\\"; const a = ref(0)"' )
2021-08-10 00:27:23 +00:00
} )
2021-10-12 12:24:43 +00:00
it ( 'should ignore existing imported' , async ( ) = > {
expect ( await transform ( 'import { ref } from "foo"; const a = ref(0)' ) ) . to . equal ( null )
expect ( await transform ( 'import ref from "foo"; const a = ref(0)' ) ) . to . equal ( null )
expect ( await transform ( 'import { z as ref } from "foo"; const a = ref(0)' ) ) . to . equal ( null )
expect ( await transform ( 'let ref = () => {}; const a = ref(0)' ) ) . to . equal ( null )
expect ( await transform ( 'let { ref } = Vue; const a = ref(0)' ) ) . to . equal ( null )
expect ( await transform ( 'let [\ncomputed,\nref\n] = Vue; const a = ref(0); const b = ref(0)' ) ) . to . equal ( null )
2021-08-30 11:55:57 +00:00
} )
it ( 'should ignore comments' , async ( ) = > {
const result = await transform ( '// import { computed } from "foo"\n;const a = computed(0)' )
2022-03-11 08:09:11 +00:00
expect ( result ) . toMatchInlineSnapshot ( `
"import { computed } from 'bar';// import { computed } from \\" foo \ \ "
; const a = computed ( 0 ) "
` )
2021-08-10 00:27:23 +00:00
} )
2021-12-21 14:28:45 +00:00
2022-03-11 08:09:11 +00:00
it ( 'should exclude files from transform' , async ( ) = > {
expect ( await transform ( 'excluded' ) ) . toEqual ( null )
2021-12-21 14:28:45 +00:00
} )
2021-08-10 00:27:23 +00:00
} )
2021-11-05 14:39:14 +00:00
2021-12-20 16:27:36 +00:00
const excludedNuxtHelpers = [ 'useHydration' ]
describe ( 'auto-imports:nuxt3' , ( ) = > {
try {
const { __dirname } = createCommonJS ( import . meta . url )
const entrypointContents = readFileSync ( join ( __dirname , '../src/app/composables/index.ts' ) , 'utf8' )
const names = findExports ( entrypointContents ) . flatMap ( i = > i . names || i . name )
for ( const name of names ) {
2022-01-21 11:42:41 +00:00
if ( excludedNuxtHelpers . includes ( name ) ) {
2021-12-20 16:27:36 +00:00
continue
}
it ( ` should register ${ name } globally ` , ( ) = > {
2022-03-11 08:09:11 +00:00
expect ( defaultPresets . find ( a = > a . from === '#app' ) . imports ) . to . include ( name )
2021-12-20 16:27:36 +00:00
} )
}
} catch ( e ) {
console . log ( e )
}
} )
2021-11-05 14:39:14 +00:00
const excludedVueHelpers = [
2022-02-03 10:21:35 +00:00
// Already globally registered
'defineEmits' ,
'defineExpose' ,
'defineProps' ,
'withDefaults' ,
'stop' ,
//
2022-01-13 17:54:33 +00:00
'__esModule' ,
'devtools' ,
2021-11-05 14:39:14 +00:00
'EffectScope' ,
'ReactiveEffect' ,
'stop' ,
'camelize' ,
'capitalize' ,
'normalizeClass' ,
'normalizeProps' ,
'normalizeStyle' ,
'toDisplayString' ,
'toHandlerKey' ,
'BaseTransition' ,
'Comment' ,
'Fragment' ,
'KeepAlive' ,
'Static' ,
'Suspense' ,
'Teleport' ,
'Text' ,
'callWithAsyncErrorHandling' ,
'callWithErrorHandling' ,
'cloneVNode' ,
'compatUtils' ,
'createBlock' ,
'createCommentVNode' ,
'createElementBlock' ,
'createElementVNode' ,
'createHydrationRenderer' ,
'createPropsRestProxy' ,
'createRenderer' ,
'createSlots' ,
'createStaticVNode' ,
'createTextVNode' ,
'createVNode' ,
'getTransitionRawChildren' ,
'guardReactiveProps' ,
'handleError' ,
'initCustomFormatter' ,
'isMemoSame' ,
'isRuntimeOnly' ,
'isVNode' ,
'mergeDefaults' ,
'mergeProps' ,
'openBlock' ,
'popScopeId' ,
'pushScopeId' ,
'queuePostFlushCb' ,
'registerRuntimeCompiler' ,
'renderList' ,
'renderSlot' ,
'resolveComponent' ,
'resolveDirective' ,
'resolveDynamicComponent' ,
'resolveFilter' ,
'resolveTransitionHooks' ,
'setBlockTracking' ,
'setDevtoolsHook' ,
'setTransitionHooks' ,
'ssrContextKey' ,
'ssrUtils' ,
'toHandlers' ,
'transformVNodeArgs' ,
'useSSRContext' ,
'version' ,
'warn' ,
'watchPostEffect' ,
'watchSyncEffect' ,
'withAsyncContext' ,
'Transition' ,
'TransitionGroup' ,
'VueElement' ,
'createApp' ,
'createSSRApp' ,
'defineCustomElement' ,
'defineSSRCustomElement' ,
'hydrate' ,
'initDirectivesForSSR' ,
'render' ,
'useCssVars' ,
'vModelCheckbox' ,
'vModelDynamic' ,
'vModelRadio' ,
'vModelSelect' ,
'vModelText' ,
'vShow' ,
'compile'
]
describe ( 'auto-imports:vue' , ( ) = > {
for ( const name of Object . keys ( VueFunctions ) ) {
if ( excludedVueHelpers . includes ( name ) ) {
continue
}
it ( ` should register ${ name } globally ` , ( ) = > {
2022-03-11 08:09:11 +00:00
expect ( defaultPresets . find ( a = > a . from === 'vue' ) . imports ) . toContain ( name )
2021-11-05 14:39:14 +00:00
} )
}
} )