import type { TranspileOptions , TranspileResults , Config , TransformOptions , TransformCssToEsmInput , ImportData , CompilerSystem , } from '../../declarations' ; import { createSystem } from '../sys/stencil-sys' ; import { IS_NODE_ENV , requireFunc } from '../sys/environment' ; import { isString } from '@utils' ; import { parseImportPath } from '../transformers/stencil-import-path' ; import { STENCIL_INTERNAL_CLIENT_ID } from '../bundle/entry-alias-ids' ; import type { CompilerOptions } from 'typescript' ; export const getTranspileResults = ( code , input ) => { if ( ! isString ( input . file ) ) { input . file = 'module.tsx' ; } const parsedImport = parseImportPath ( input . file ) ; const results = { code : typeof code === 'string' ? code : '' , data : [ ] , diagnostics : [ ] , inputFileExtension : parsedImport . ext , inputFilePath : input . file , imports : [ ] , map : null , outputFilePath : null , } ; return { importData : parsedImport . data , results , } ; } ; const transpileCtx = { sys : null as CompilerSystem } ; export const getTranspileConfig = ( input ) => { if ( input . sys ) { transpileCtx . sys = input . sys ; } else if ( ! transpileCtx . sys ) { if ( IS_NODE_ENV ) { transpileCtx . sys = requireFunc ( '../sys/node/index.js' ) . createNodeSys ( ) ; } else { transpileCtx . sys = createSystem ( ) ; } } const compileOpts = { componentExport : getTranspileConfigOpt ( input . componentExport , VALID_EXPORT , 'customelement' ) , componentMetadata : getTranspileConfigOpt ( input . componentMetadata , VALID_METADATA , null ) , coreImportPath : isString ( input . coreImportPath ) ? input . coreImportPath : STENCIL_INTERNAL_CLIENT_ID , currentDirectory : isString ( input . currentDirectory ) ? input . currentDirectory : transpileCtx . sys . getCurrentDirectory ( ) , file : input . file , proxy : getTranspileConfigOpt ( input . proxy , VALID_PROXY , 'defineproperty' ) , module : getTranspileConfigOpt ( input . module , VALID_MODULE , 'esm' ) , sourceMap : input . sourceMap === 'inline' ? 'inline' : input . sourceMap !== false , style : getTranspileConfigOpt ( input . style , VALID_STYLE , 'static' ) , styleImportData : getTranspileConfigOpt ( input . styleImportData , VALID_STYLE_IMPORT_DATA , 'queryparams' ) , target : getTranspileConfigOpt ( input . target , VALID_TARGET , 'latest' ) , } ; const tsCompilerOptions = { allowSyntheticDefaultImports : true , esModuleInterop : true , sourceMap : compileOpts . sourceMap !== false , isolatedModules : true , suppressOutputPathCheck : true , allowNonTsExtensions : true , noLib : true , noResolve : true , } ; if ( isString ( input . baseUrl ) ) { compileOpts . baseUrl = input . baseUrl ; tsCompilerOptions . baseUrl = compileOpts . baseUrl ; } if ( input . paths ) { compileOpts . paths = { ... input . paths } ; tsCompilerOptions . paths = { ... compileOpts . paths } ; } const transformOpts = { coreImportPath : compileOpts . coreImportPath , componentExport : compileOpts . componentExport as any , componentMetadata : compileOpts . componentMetadata as any , currentDirectory : compileOpts . currentDirectory , isolatedModules : true , module : compileOpts . module as any , proxy : compileOpts . proxy as any , file : compileOpts . file , style : compileOpts . style as any , styleImportData : compileOpts . styleImportData as any , target : compileOpts . target as any , } ; const config = { rootDir : compileOpts . currentDirectory , srcDir : compileOpts . currentDirectory , devMode : true , minifyCss : true , minifyJs : false , _isTesting : true , validateTypes : false , enableCache : false , sys : transpileCtx . sys , tsCompilerOptions , } ; return { compileOpts , config , transformOpts , } ; } ; export const getTranspileCssConfig = ( compileOpts , importData , results ) => { const transformInput = { file : results . inputFilePath , input : results . code , tag : importData && importData . tag , encapsulation : importData && importData . encapsulation , mode : importData && importData . mode , sourceMap : compileOpts . sourceMap !== false , commentOriginalSelector : false , minify : false , autoprefixer : false , module : compileOpts . module , styleImportData : compileOpts . styleImportData , } ; return transformInput ; } ; const getTranspileConfigOpt = ( value , validValues , defaultValue ) => { if ( value === null || value === 'null' ) { return null ; } value = isString ( value ) ? value . toLowerCase ( ) . trim ( ) : null ; if ( validValues . has ( value ) ) { return value ; } return defaultValue ; } ; const VALID_EXPORT = new Set ( [ 'customelement' , 'module' ] ) ; const VALID_METADATA = new Set ( [ 'compilerstatic' , null ] ) ; const VALID_MODULE = new Set ( [ 'cjs' , 'esm' ] ) ; const VALID_PROXY = new Set ( [ 'defineproperty' , null ] ) ; const VALID_STYLE = new Set ( [ 'static' ] ) ; const VALID_STYLE_IMPORT_DATA = new Set ( [ 'queryparams' ] ) ; const VALID_TARGET = new Set ( [ 'latest' , 'esnext' , 'es2020' , 'es2019' , 'es2018' , 'es2017' , 'es2016' , 'es2015' , 'es5' ] ) ;