@@ -53,6 +53,7 @@ import {
53
53
createTypeChecker ,
54
54
createTypeReferenceDirectiveResolutionCache ,
55
55
CustomTransformers ,
56
+ CustomTransformersModuleFactory ,
56
57
Debug ,
57
58
DeclarationWithTypeParameterChildren ,
58
59
Diagnostic ,
@@ -153,6 +154,7 @@ import {
153
154
getTsBuildInfoEmitOutputFilePath ,
154
155
getTsConfigObjectLiteralExpression ,
155
156
getTsConfigPropArrayElementValue ,
157
+ getTypeScriptNamespace ,
156
158
HasChangedAutomaticTypeDirectiveNames ,
157
159
hasChangesInResolutions ,
158
160
hasExtension ,
@@ -218,6 +220,7 @@ import {
218
220
mapDefinedIterator ,
219
221
maybeBind ,
220
222
memoize ,
223
+ mergeCustomTransformers ,
221
224
MethodDeclaration ,
222
225
ModeAwareCache ,
223
226
ModeAwareCacheKey ,
@@ -499,7 +502,8 @@ export function createCompilerHostWorker(options: CompilerOptions, setParentNode
499
502
realpath,
500
503
readDirectory : ( path , extensions , include , exclude , depth ) => system . readDirectory ( path , extensions , include , exclude , depth ) ,
501
504
createDirectory : d => system . createDirectory ( d ) ,
502
- createHash : maybeBind ( system , system . createHash )
505
+ createHash : maybeBind ( system , system . createHash ) ,
506
+ require : maybeBind ( system , system . require ) ,
503
507
} ;
504
508
return compilerHost ;
505
509
}
@@ -2670,6 +2674,30 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2670
2674
return hasEmitBlockingDiagnostics . has ( toPath ( emitFileName ) ) ;
2671
2675
}
2672
2676
2677
+ function getCustomTransformers ( ) {
2678
+ if ( ! host . require ) {
2679
+ return emptyArray ;
2680
+ }
2681
+
2682
+ const compilerOptions = program . getCompilerOptions ( ) ;
2683
+ const customTransformers = mapDefined ( compilerOptions . plugins , config => {
2684
+ if ( config . type !== "transformer" ) return undefined ;
2685
+
2686
+ // TODO(jakebailey): The LS plugin loader is more complicated than this; copy.
2687
+ const result = host . require ! ( program . getCurrentDirectory ( ) , config . path ) ;
2688
+ // TODO(jakebailey): error handling, only do this once per, etc
2689
+ Debug . assertIsDefined ( result . module ) ;
2690
+
2691
+ const factory = result . module as CustomTransformersModuleFactory ;
2692
+ Debug . assert ( typeof factory === "function" ) ;
2693
+
2694
+ const plugin = factory ( { typescript : getTypeScriptNamespace ( ) } ) ;
2695
+ return plugin . create ( { program, config } ) ;
2696
+ } ) ;
2697
+
2698
+ return customTransformers ?? emptyArray ;
2699
+ }
2700
+
2673
2701
function emitWorker ( program : Program , sourceFile : SourceFile | undefined , writeFileCallback : WriteFileCallback | undefined , cancellationToken : CancellationToken | undefined , emitOnly ?: boolean | EmitOnly , customTransformers ?: CustomTransformers , forceDtsEmit ?: boolean ) : EmitResult {
2674
2702
if ( ! forceDtsEmit ) {
2675
2703
const result = handleNoEmitOptions ( program , sourceFile , writeFileCallback , cancellationToken ) ;
@@ -2688,11 +2716,13 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2688
2716
2689
2717
performance . mark ( "beforeEmit" ) ;
2690
2718
2719
+ const mergedCustomTransformers = mergeCustomTransformers ( ...getCustomTransformers ( ) , customTransformers ) ;
2720
+
2691
2721
const emitResult = emitFiles (
2692
2722
emitResolver ,
2693
2723
getEmitHost ( writeFileCallback ) ,
2694
2724
sourceFile ,
2695
- getTransformers ( options , customTransformers , emitOnly ) ,
2725
+ getTransformers ( options , mergedCustomTransformers , emitOnly ) ,
2696
2726
emitOnly ,
2697
2727
/*onlyBuildInfo*/ false ,
2698
2728
forceDtsEmit
0 commit comments