import type * as d from '../../declarations' ; import { convertValueToLiteral } from './transform-utils' ; import { DEFINE_CUSTOM_ELEMENT , RUNTIME_APIS , addCoreRuntimeApi } from './core-runtime-apis' ; import { formatComponentRuntimeMeta } from '@utils' ; import ts from 'typescript' ; export const defineCustomElement = ( tsSourceFile , moduleFile , transformOpts ) => { let statements = tsSourceFile . statements . slice ( ) ; statements . push ( ... moduleFile . cmps . map ( ( cmp ) => { return addDefineCustomElement ( moduleFile , cmp ) ; } ) ) ; if ( transformOpts . module === 'cjs' ) { statements = removeComponentCjsExport ( statements , moduleFile ) ; } return ts . updateSourceFileNode ( tsSourceFile , statements ) ; } ; const addDefineCustomElement = ( moduleFile , compilerMeta ) => { if ( compilerMeta . isPlain ) { return ts . createStatement ( ts . createCall ( ts . createPropertyAccess ( ts . createIdentifier ( 'customElements' ) , ts . createIdentifier ( 'define' ) ) , [ ] , [ ts . createLiteral ( compilerMeta . tagName ) , ts . createIdentifier ( compilerMeta . componentClassName ) ] ) ) ; } addCoreRuntimeApi ( moduleFile , RUNTIME_APIS . defineCustomElement ) ; const compactMeta = formatComponentRuntimeMeta ( compilerMeta , true ) ; const liternalCmpClassName = ts . createIdentifier ( compilerMeta . componentClassName ) ; const liternalMeta = convertValueToLiteral ( compactMeta ) ; return ts . createStatement ( ts . createCall ( ts . createIdentifier ( DEFINE_CUSTOM_ELEMENT ) , [ ] , [ liternalCmpClassName , liternalMeta ] ) ) ; } ; const removeComponentCjsExport = ( statements , moduleFile ) => { const cmpClassNames = new Set < string > ( moduleFile . cmps . map ( ( cmp ) => cmp . componentClassName ) ) ; return statements . filter ( ( s ) => { if ( s . kind === ts . SyntaxKind . ExpressionStatement ) { const exp = ( s as ts . ExpressionStatement ) . expression as ts . BinaryExpression ; if ( exp && exp . kind === ts . SyntaxKind . BinaryExpression ) { const left = exp . left as ts . PropertyAccessExpression ; if ( left && left . kind === ts . SyntaxKind . PropertyAccessExpression ) { if ( left . expression && left . expression . kind === ts . SyntaxKind . Identifier ) { const leftText = left . expression as ts . Identifier ; if ( leftText . text === 'exports' ) { const right = exp . right as ts . Identifier ; if ( right && cmpClassNames . has ( right . text ) ) { return false ; } } } } } } return true ; } ) ; } ;