From 8b0177fb35a982d0ed23d63d12579977228dfcf9 Mon Sep 17 00:00:00 2001 From: Illya Klymov Date: Sat, 8 Oct 2022 00:53:35 +0300 Subject: [PATCH] chore(stubs): remove useless check --- src/directives.ts | 73 +++++++++++++++++++ .../stubComponentsTransformer.ts | 4 - 2 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 src/directives.ts diff --git a/src/directives.ts b/src/directives.ts new file mode 100644 index 000000000..7e202e6b7 --- /dev/null +++ b/src/directives.ts @@ -0,0 +1,73 @@ +import { + transformVNodeArgs, + Directive, + ComponentInternalInstance, + VNodeTypes +} from 'vue' +import { isObject } from './utils' + +interface CreateDirectivesTransformerConfig { + directives?: Record +} +type VNodeArgsTransformer = NonNullable< + Parameters[0] +> + +export function createDirectivesTransformer({ + directives = {} +}: CreateDirectivesTransformerConfig): VNodeArgsTransformer { + const directivesTransformerCache: WeakMap< + VNodeTypes & object, + VNodeTypes & object + > = new WeakMap() + + if (Object.keys(directives).length === 0) { + return (args) => args + } + + return function directivesTransformer( + args, + instance: ComponentInternalInstance | null + ) { + // We care about + // * object components + // * functional components + // * legacy components + // * legacy functional components + // * class components (sigh) + + const [nodeType, props, children, patchFlag, dynamicProps] = args + + if (!isObject(nodeType)) { + return args + } + + if ((nodeType as any).__PATCHED) { + console.log('wtf') + } + + const cachedTransformation = directivesTransformerCache.get(nodeType) + if (cachedTransformation) { + return [cachedTransformation, props, children, patchFlag, dynamicProps] + } + + const nodeTypeWithDirectives = { + __PATCHED: true, + ...nodeType, + directives: { + ...((nodeType as any)?.directives ?? {}), + ...directives + } + } + + directivesTransformerCache.set(nodeType, nodeTypeWithDirectives as any) + + return [ + nodeTypeWithDirectives as any, + props, + children, + patchFlag, + dynamicProps + ] + } +} diff --git a/src/vnodeTransformers/stubComponentsTransformer.ts b/src/vnodeTransformers/stubComponentsTransformer.ts index 4d4cbdec9..62affe12d 100644 --- a/src/vnodeTransformers/stubComponentsTransformer.ts +++ b/src/vnodeTransformers/stubComponentsTransformer.ts @@ -231,10 +231,6 @@ export function createStubComponentsTransformer({ // Set name when using shallow without stub const stubName = name || registeredName || componentName - if (!isComponent(type)) { - throw new Error('Attempted to stub a non-component') - } - const newStub = config.plugins.createStubs?.({ name: stubName,