import type * as d from '../../../declarations' ; import { addImports } from '../add-imports' ; import { addLegacyApis } from '../core-runtime-apis' ; import { addModuleMetadataProxies } from '../add-component-meta-proxy' ; import { getComponentMeta , getModuleFromSourceFile , getScriptTarget } from '../transform-utils' ; import { catchError , loadTypeScriptDiagnostics } from '@utils' ; import { defineCustomElement } from '../define-custom-element' ; import { STENCIL_CORE_ID } from '../../bundle/entry-alias-ids' ; import { updateNativeComponentClass } from './native-component' ; import { updateStyleImports } from '../style-imports' ; import ts from 'typescript' ; export const transformToNativeComponentText = ( config , compilerCtx , buildCtx , cmp , inputJsText ) => { let outputText = null ; try { const tsCompilerOptions = { module : ts . ModuleKind . ESNext , target : getScriptTarget ( ) , } ; const transformOpts = { coreImportPath : STENCIL_CORE_ID , componentExport : null , componentMetadata : null , currentDirectory : config . sys . getCurrentDirectory ( ) , proxy : null , style : 'static' , styleImportData : 'queryparams' , } ; const transpileOpts = { compilerOptions : tsCompilerOptions , fileName : cmp . jsFilePath , transformers : { after : [ nativeComponentTransform ( compilerCtx , transformOpts ) ] , } , } ; const transpileOutput = ts . transpileModule ( inputJsText , transpileOpts ) ; buildCtx . diagnostics . push ( ... loadTypeScriptDiagnostics ( transpileOutput . diagnostics ) ) ; if ( ! buildCtx . hasError && typeof transpileOutput . outputText === 'string' ) { outputText = transpileOutput . outputText ; } } catch ( e ) { catchError ( buildCtx . diagnostics , e ) ; } return outputText ; } ; export const nativeComponentTransform = ( compilerCtx , transformOpts ) : ts . TransformerFactory < ts . SourceFile > => { return ( transformCtx ) => { return ( tsSourceFile ) => { const moduleFile = getModuleFromSourceFile ( compilerCtx , tsSourceFile ) ; const visitNode = ( node ) : any => { if ( ts . isClassDeclaration ( node ) ) { const cmp = getComponentMeta ( compilerCtx , tsSourceFile , node ) ; if ( cmp != null ) { return updateNativeComponentClass ( transformOpts , node , moduleFile , cmp ) ; } } return ts . visitEachChild ( node , visitNode , transformCtx ) ; } ; tsSourceFile = ts . visitEachChild ( tsSourceFile , visitNode , transformCtx ) ; if ( moduleFile . cmps . length > 0 ) { if ( transformOpts . componentExport === 'customelement' ) { tsSourceFile = defineCustomElement ( tsSourceFile , moduleFile , transformOpts ) ; } else if ( transformOpts . proxy === 'defineproperty' ) { tsSourceFile = addModuleMetadataProxies ( tsSourceFile , moduleFile ) ; } tsSourceFile = updateStyleImports ( transformOpts , tsSourceFile , moduleFile ) ; } if ( moduleFile . isLegacy ) { addLegacyApis ( moduleFile ) ; } tsSourceFile = addImports ( transformOpts , tsSourceFile , moduleFile . coreRuntimeApis , transformOpts . coreImportPath ) ; return tsSourceFile ; } ; } ; } ;