import type * as d from '../../declarations' ; import type { BundleOptions } from './bundle-interface' ; import { getModule } from '../transpile/transpiled-module' ; import { isString , normalizeFsPath } from '@utils' ; import type { LoadResult , Plugin , TransformResult } from 'rollup' ; import { tsResolveModuleName } from '../sys/typescript/typescript-resolve-module' ; import { isAbsolute , basename } from 'path' ; import ts from 'typescript' ; export const typescriptPlugin = ( compilerCtx , bundleOpts , config ) : Plugin => { return { name : `${ bundleOpts . id }TypescriptPlugin` , load ( id ) { if ( isAbsolute ( id ) ) { const fsFilePath = normalizeFsPath ( id ) ; const module = getModule ( compilerCtx , fsFilePath ) ; if ( module ) { if ( ! module . sourceMapFileText ) { return { code : module . staticSourceFileText , map : null } ; } const sourceMap = JSON . parse ( module . sourceMapFileText ) ; sourceMap . sources = sourceMap . sources . map ( ( src ) => basename ( src ) ) ; return { code : module . staticSourceFileText , map : sourceMap } ; } } return null ; } , transform ( _code , id ) { if ( isAbsolute ( id ) ) { const fsFilePath = normalizeFsPath ( id ) ; const mod = getModule ( compilerCtx , fsFilePath ) ; if ( mod && mod . cmps . length > 0 ) { const tsResult = ts . transpileModule ( mod . staticSourceFileText , { compilerOptions : config . tsCompilerOptions , fileName : mod . sourceFilePath , transformers : { before : bundleOpts . customTransformers } , } ) ; const sourceMap = tsResult . sourceMapText ? JSON . parse ( tsResult . sourceMapText ) : null ; return { code : tsResult . outputText , map : sourceMap } ; } } return null ; } , } ; } ; export const resolveIdWithTypeScript = ( config , compilerCtx ) : Plugin => { return { name : `resolveIdWithTypeScript` , async resolveId ( importee , importer ) { if ( /\0/ . test ( importee ) || ! isString ( importer ) ) { return null ; } const tsResolved = tsResolveModuleName ( config , compilerCtx , importee , importer ) ; if ( tsResolved && tsResolved . resolvedModule ) { const tsResolvedPath = tsResolved . resolvedModule . resolvedFileName ; if ( isString ( tsResolvedPath ) && ! tsResolvedPath . endsWith ( '.d.ts' ) ) { return tsResolvedPath ; } } return null ; } , } ; } ;