1- import * as path from "path" ;
2- import * as resolve from "resolve" ;
31import * as ts from "typescript" ;
42// TODO: Don't depend on CLI?
53import * as cliDiagnostics from "../cli/diagnostics" ;
64import { CompilerOptions , TransformerImport } from "../CompilerOptions" ;
75import * as diagnosticFactories from "./diagnostics" ;
6+ import { getConfigDirectory , resolvePlugin } from "./utils" ;
87
98export const noImplicitSelfTransformer : ts . TransformerFactory < ts . SourceFile | ts . Bundle > = ( ) => node => {
109 const transformSourceFile : ts . Transformer < ts . SourceFile > = node => {
@@ -18,7 +17,7 @@ export const noImplicitSelfTransformer: ts.TransformerFactory<ts.SourceFile | ts
1817 : transformSourceFile ( node ) ;
1918} ;
2019
21- export function getCustomTransformers (
20+ export function getTransformers (
2221 program : ts . Program ,
2322 diagnostics : ts . Diagnostic [ ] ,
2423 customTransformers : ts . CustomTransformers ,
@@ -54,7 +53,7 @@ export function getCustomTransformers(
5453 } ;
5554}
5655
57- function loadTransformersFromOptions ( program : ts . Program , allDiagnostics : ts . Diagnostic [ ] ) : ts . CustomTransformers {
56+ function loadTransformersFromOptions ( program : ts . Program , diagnostics : ts . Diagnostic [ ] ) : ts . CustomTransformers {
5857 const customTransformers : Required < ts . CustomTransformers > = {
5958 before : [ ] ,
6059 after : [ ] ,
@@ -64,19 +63,23 @@ function loadTransformersFromOptions(program: ts.Program, allDiagnostics: ts.Dia
6463 const options = program . getCompilerOptions ( ) as CompilerOptions ;
6564 if ( ! options . plugins ) return customTransformers ;
6665
67- const configFileName = options . configFilePath as string | undefined ;
68- const basedir = configFileName ? path . dirname ( configFileName ) : process . cwd ( ) ;
69-
7066 for ( const [ index , transformerImport ] of options . plugins . entries ( ) ) {
7167 if ( ! ( "transform" in transformerImport ) ) continue ;
7268 const optionName = `compilerOptions.plugins[${ index } ]` ;
7369
74- const { error : resolveError , factory } = resolveTransformerFactory ( basedir , optionName , transformerImport ) ;
75- if ( resolveError ) allDiagnostics . push ( resolveError ) ;
70+ const { error : resolveError , result : factory } = resolvePlugin (
71+ "transformer" ,
72+ `${ optionName } .transform` ,
73+ getConfigDirectory ( options ) ,
74+ transformerImport . transform ,
75+ transformerImport . import
76+ ) ;
77+
78+ if ( resolveError ) diagnostics . push ( resolveError ) ;
7679 if ( factory === undefined ) continue ;
7780
7881 const { error : loadError , transformer } = loadTransformer ( optionName , program , factory , transformerImport ) ;
79- if ( loadError ) allDiagnostics . push ( loadError ) ;
82+ if ( loadError ) diagnostics . push ( loadError ) ;
8083 if ( transformer === undefined ) continue ;
8184
8285 if ( transformer . before ) {
@@ -103,12 +106,6 @@ type CompilerOptionsTransformerFactory = (
103106) => Transformer ;
104107type TypeCheckerTransformerFactory = ( typeChecker : ts . TypeChecker , options : Record < string , any > ) => Transformer ;
105108type RawTransformerFactory = Transformer ;
106- type TransformerFactory =
107- | ProgramTransformerFactory
108- | ConfigTransformerFactory
109- | CompilerOptionsTransformerFactory
110- | TypeCheckerTransformerFactory
111- | RawTransformerFactory ;
112109
113110type Transformer = GroupTransformer | ts . TransformerFactory < ts . SourceFile > ;
114111interface GroupTransformer {
@@ -117,48 +114,10 @@ interface GroupTransformer {
117114 afterDeclarations ?: ts . TransformerFactory < ts . SourceFile | ts . Bundle > ;
118115}
119116
120- function resolveTransformerFactory (
121- basedir : string ,
122- transformerOptionPath : string ,
123- { transform, import : importName = "default" } : TransformerImport
124- ) : { error ?: ts . Diagnostic ; factory ?: TransformerFactory } {
125- if ( typeof transform !== "string" ) {
126- const optionName = `${ transformerOptionPath } .transform` ;
127- return { error : cliDiagnostics . compilerOptionRequiresAValueOfType ( optionName , "string" ) } ;
128- }
129-
130- let resolved : string ;
131- try {
132- resolved = resolve . sync ( transform , { basedir, extensions : [ ".js" , ".ts" , ".tsx" ] } ) ;
133- } catch ( err ) {
134- if ( err . code !== "MODULE_NOT_FOUND" ) throw err ;
135- return { error : diagnosticFactories . couldNotResolveTransformerFrom ( transform , basedir ) } ;
136- }
137-
138- // tslint:disable-next-line: deprecation
139- const hasNoRequireHook = require . extensions [ ".ts" ] === undefined ;
140- if ( hasNoRequireHook && ( resolved . endsWith ( ".ts" ) || resolved . endsWith ( ".tsx" ) ) ) {
141- try {
142- const tsNode : typeof import ( "ts-node" ) = require ( "ts-node" ) ;
143- tsNode . register ( { transpileOnly : true } ) ;
144- } catch ( err ) {
145- if ( err . code !== "MODULE_NOT_FOUND" ) throw err ;
146- return { error : diagnosticFactories . toLoadTransformerItShouldBeTranspiled ( transform ) } ;
147- }
148- }
149-
150- const factory : TransformerFactory = require ( resolved ) [ importName ] ;
151- if ( factory === undefined ) {
152- return { error : diagnosticFactories . transformerShouldHaveAExport ( transform , importName ) } ;
153- }
154-
155- return { factory } ;
156- }
157-
158117function loadTransformer (
159- transformerOptionPath : string ,
118+ optionPath : string ,
160119 program : ts . Program ,
161- factory : TransformerFactory ,
120+ factory : unknown ,
162121 { transform, after = false , afterDeclarations = false , type = "program" , ...extraOptions } : TransformerImport
163122) : { error ?: ts . Diagnostic ; transformer ?: GroupTransformer } {
164123 let transformer : Transformer ;
@@ -179,18 +138,18 @@ function loadTransformer(
179138 transformer = ( factory as CompilerOptionsTransformerFactory ) ( program . getCompilerOptions ( ) , extraOptions ) ;
180139 break ;
181140 default : {
182- const optionName = `--${ transformerOptionPath } .type` ;
141+ const optionName = `--${ optionPath } .type` ;
183142 return { error : cliDiagnostics . argumentForOptionMustBe ( optionName , "program" ) } ;
184143 }
185144 }
186145
187146 if ( typeof after !== "boolean" ) {
188- const optionName = `${ transformerOptionPath } .after` ;
147+ const optionName = `${ optionPath } .after` ;
189148 return { error : cliDiagnostics . compilerOptionRequiresAValueOfType ( optionName , "boolean" ) } ;
190149 }
191150
192151 if ( typeof afterDeclarations !== "boolean" ) {
193- const optionName = `${ transformerOptionPath } .afterDeclarations` ;
152+ const optionName = `${ optionPath } .afterDeclarations` ;
194153 return { error : cliDiagnostics . compilerOptionRequiresAValueOfType ( optionName , "boolean" ) } ;
195154 }
196155
0 commit comments