import type { TranspileOptions , Diagnostic } from '@stencil/core/internal' ; import { loadTypeScriptDiagnostic , normalizePath } from '@utils' ; import { transpile } from '../test-transpile' ; import { ts } from '@stencil/core/compiler' ; type Jest26CacheKeyOptions = { instrument : boolean ; rootDir : string ; } ; type Jest26Config = { instrument : boolean ; rootDir : string ; } ; type Jest27TransformOptions = { config : Jest26Config ; } ; param obj returns const isJest27TransformOptions = ( obj ) : obj is Jest27TransformOptions => { return typeof obj === 'object' && obj . hasOwnProperty ( 'config' ) ; } ; /**
 * Constant used for cache busting when the contents of this file have changed. When modifying this file, it's advised
 * this value be monotonically incremented.
 */ const CACHE_BUSTER = 7 ; /**
 * Fields containing the consuming library's `tsconfig.json#options` entry. The first is the original representation,
 * where the second is the stringified version. These fields are cached to prevent unnecessary I/O & repetitive
 * stringification of the read options. Note that this caching does not persist across multiple Jest workers (I.E.
 * every Jest worker will read the `tsconfig.json` file and stringify it's `options` entry.
 */ let _tsCompilerOptions = null ; let _tsCompilerOptionsKey = null ; export const jestPreprocessor = { param sourceText param sourcePath param jestConfig param transformOptions returns process ( sourceText , sourcePath , jestConfig , transformOptions ? ) { /**
         * As of Jest 27, `jestConfig` changes it's shape (as it's been moved into `transformOptions`). To preserve
         * backwards compatability, we allow Jest to pass 4 arguments and check the shape of the third and fourth arguments
         * to run Jest properly (in lieu of a global Jest version available to us). Support for this functionality will be
         * removed in a future major version of Stencil.
         */ if ( isJest27TransformOptions ( jestConfig ) ) { transformOptions = jestConfig . config ; } if ( shouldTransform ( sourcePath , sourceText ) ) { const opts = { file : sourcePath , currentDirectory : transformOptions . rootDir , } ; const tsCompilerOptions = getCompilerOptions ( transformOptions . rootDir ) ; if ( tsCompilerOptions ) { if ( tsCompilerOptions . baseUrl ) { opts . baseUrl = tsCompilerOptions . baseUrl ; } if ( tsCompilerOptions . paths ) { opts . paths = tsCompilerOptions . paths ; } } const results = transpile ( sourceText , opts ) ; const hasErrors = results . diagnostics . some ( ( diagnostic ) => diagnostic . level === 'error' ) ; if ( results . diagnostics && hasErrors ) { const msg = results . diagnostics . map ( formatDiagnostic ) . join ( '\n\n' ) ; throw new Error ( msg ) ; } return results . code ; } return sourceText ; } , param sourceText param sourcePath param jestConfigStr param transformOptions returns getCacheKey ( sourceText , sourcePath , jestConfigStr , transformOptions ? ) { /**
         * As of Jest 27, jestConfigStr is no longer an accepted argument (as it's been moved into `transformOptions`). To
         * preserve backwards compatability, we allow Jest to pass 4 arguments and check the shape of the third and fourth
         * arguments to run Jest properly (in lieu of a global Jest version available to us). Support for this
         * functionality will be removed in a future major version of Stencil.
         */ if ( isJest27TransformOptions ( jestConfigStr ) ) { transformOptions = jestConfigStr . config ; } if ( ! _tsCompilerOptionsKey ) { const opts = getCompilerOptions ( transformOptions . rootDir ) ; _tsCompilerOptionsKey = JSON . stringify ( opts ) ; } const key = [ process . version , _tsCompilerOptionsKey , sourceText , sourcePath , jestConfigStr , ! ! transformOptions . instrument , CACHE_BUSTER , ] ; return key . join ( ':' ) ; } , } ; function formatDiagnostic ( diagnostic ) { let m = '' ; if ( diagnostic . relFilePath ) { m += diagnostic . relFilePath ; if ( typeof diagnostic . lineNumber === 'number' ) { m += ':' + diagnostic . lineNumber + 1 ; if ( typeof diagnostic . columnNumber === 'number' ) { m += ':' + diagnostic . columnNumber ; } } m += '\n' ; } m += diagnostic . messageText ; return m ; } param rootDir returns function getCompilerOptions ( rootDir ) { if ( _tsCompilerOptions ) { return _tsCompilerOptions ; } if ( typeof rootDir !== 'string' ) { return null ; } rootDir = normalizePath ( rootDir ) ; const tsconfigFilePath = ts . findConfigFile ( rootDir , ts . sys . fileExists ) ; if ( ! tsconfigFilePath ) { return null ; } const tsconfigResults = ts . readConfigFile ( tsconfigFilePath , ts . sys . readFile ) ; if ( tsconfigResults . error ) { throw new Error ( formatDiagnostic ( loadTypeScriptDiagnostic ( tsconfigResults . error ) ) ) ; } const parseResult = ts . parseJsonConfigFileContent ( tsconfigResults . config , ts . sys , rootDir , undefined , tsconfigFilePath ) ; _tsCompilerOptions = parseResult . options ; return _tsCompilerOptions ; } param filePath param sourceText returns export function shouldTransform ( filePath , sourceText ) { const ext = filePath . split ( '.' ) . pop ( ) . toLowerCase ( ) . split ( '?' ) [ 0 ] ; if ( ext === 'ts' || ext === 'tsx' || ext === 'jsx' ) { return true ; } if ( ext === 'mjs' ) { return true ; } if ( ext === 'js' ) { if ( sourceText . includes ( 'import ' ) || sourceText . includes ( 'import.' ) || sourceText . includes ( 'import(' ) ) { return true ; } if ( sourceText . includes ( 'export ' ) ) { return true ; } } if ( ext === 'css' ) { return true ; } return false ; }