@@ -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
getTypesPackageName ,
157
159
HasChangedAutomaticTypeDirectiveNames ,
158
160
hasChangesInResolutions ,
@@ -219,6 +221,7 @@ import {
219
221
mapDefinedIterator ,
220
222
maybeBind ,
221
223
memoize ,
224
+ mergeCustomTransformers ,
222
225
MethodDeclaration ,
223
226
ModeAwareCache ,
224
227
ModeAwareCacheKey ,
@@ -500,7 +503,8 @@ export function createCompilerHostWorker(options: CompilerOptions, setParentNode
500
503
realpath,
501
504
readDirectory : ( path , extensions , include , exclude , depth ) => system . readDirectory ( path , extensions , include , exclude , depth ) ,
502
505
createDirectory : d => system . createDirectory ( d ) ,
503
- createHash : maybeBind ( system , system . createHash )
506
+ createHash : maybeBind ( system , system . createHash ) ,
507
+ require : maybeBind ( system , system . require ) ,
504
508
} ;
505
509
return compilerHost ;
506
510
}
@@ -2702,6 +2706,30 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2702
2706
return hasEmitBlockingDiagnostics . has ( toPath ( emitFileName ) ) ;
2703
2707
}
2704
2708
2709
+ function getCustomTransformers ( ) {
2710
+ if ( ! host . require ) {
2711
+ return emptyArray ;
2712
+ }
2713
+
2714
+ const compilerOptions = program . getCompilerOptions ( ) ;
2715
+ const customTransformers = mapDefined ( compilerOptions . plugins , config => {
2716
+ if ( config . type !== "transformer" ) return undefined ;
2717
+
2718
+ // TODO(jakebailey): The LS plugin loader is more complicated than this; copy.
2719
+ const result = host . require ! ( program . getCurrentDirectory ( ) , config . path ) ;
2720
+ // TODO(jakebailey): error handling, only do this once per, etc
2721
+ Debug . assertIsDefined ( result . module ) ;
2722
+
2723
+ const factory = result . module as CustomTransformersModuleFactory ;
2724
+ Debug . assert ( typeof factory === "function" ) ;
2725
+
2726
+ const plugin = factory ( { typescript : getTypeScriptNamespace ( ) } ) ;
2727
+ return plugin . create ( { program, config } ) ;
2728
+ } ) ;
2729
+
2730
+ return customTransformers ?? emptyArray ;
2731
+ }
2732
+
2705
2733
function emitWorker ( program : Program , sourceFile : SourceFile | undefined , writeFileCallback : WriteFileCallback | undefined , cancellationToken : CancellationToken | undefined , emitOnly ?: boolean | EmitOnly , customTransformers ?: CustomTransformers , forceDtsEmit ?: boolean ) : EmitResult {
2706
2734
if ( ! forceDtsEmit ) {
2707
2735
const result = handleNoEmitOptions ( program , sourceFile , writeFileCallback , cancellationToken ) ;
@@ -2720,11 +2748,13 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2720
2748
2721
2749
performance . mark ( "beforeEmit" ) ;
2722
2750
2751
+ const mergedCustomTransformers = mergeCustomTransformers ( ...getCustomTransformers ( ) , customTransformers ) ;
2752
+
2723
2753
const emitResult = emitFiles (
2724
2754
emitResolver ,
2725
2755
getEmitHost ( writeFileCallback ) ,
2726
2756
sourceFile ,
2727
- getTransformers ( options , customTransformers , emitOnly ) ,
2757
+ getTransformers ( options , mergedCustomTransformers , emitOnly ) ,
2728
2758
emitOnly ,
2729
2759
/*onlyBuildInfo*/ false ,
2730
2760
forceDtsEmit
0 commit comments