@@ -26,15 +26,19 @@ import { createRequire } from 'module';
2626
2727const { readFile, access } = fsPromises ;
2828
29+ export type CodeFileLoaderConfig = {
30+ pluckConfig ?: GraphQLTagPluckOptions ;
31+ noPluck ?: boolean ;
32+ noRequire ?: boolean ;
33+ } ;
34+
2935/**
3036 * Additional options for loading from a code file
3137 */
3238export type CodeFileLoaderOptions = {
3339 require ?: string | string [ ] ;
34- pluckConfig ?: GraphQLTagPluckOptions ;
35- noPluck ?: boolean ;
36- noRequire ?: boolean ;
37- } & BaseLoaderOptions ;
40+ } & CodeFileLoaderConfig &
41+ BaseLoaderOptions ;
3842
3943const FILE_EXTENSIONS = [ '.ts' , '.tsx' , '.js' , '.jsx' , '.vue' ] ;
4044
@@ -57,11 +61,21 @@ function createGlobbyOptions(options: CodeFileLoaderOptions): GlobbyOptions {
5761 * Supported extensions include: `.ts`, `.tsx`, `.js`, `.jsx`, `.vue`
5862 */
5963export class CodeFileLoader implements Loader < CodeFileLoaderOptions > {
64+ private config : CodeFileLoaderConfig ;
65+ constructor ( config ?: CodeFileLoaderConfig ) {
66+ this . config = config ?? { } ;
67+ }
68+
69+ private getMergedOptions ( options : CodeFileLoaderOptions ) : CodeFileLoaderOptions {
70+ return { ...this . config , ...options } ;
71+ }
72+
6073 loaderId ( ) : string {
6174 return 'code-file' ;
6275 }
6376
6477 async canLoad ( pointer : string , options : CodeFileLoaderOptions ) : Promise < boolean > {
78+ options = this . getMergedOptions ( options ) ;
6579 if ( isGlob ( pointer ) ) {
6680 // FIXME: parse to find and check the file extensions?
6781 return true ;
@@ -83,6 +97,7 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
8397 }
8498
8599 canLoadSync ( pointer : string , options : CodeFileLoaderOptions ) : boolean {
100+ options = this . getMergedOptions ( options ) ;
86101 if ( isGlob ( pointer ) ) {
87102 // FIXME: parse to find and check the file extensions?
88103 return true ;
@@ -99,16 +114,19 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
99114 }
100115
101116 async resolveGlobs ( glob : string , options : CodeFileLoaderOptions ) {
117+ options = this . getMergedOptions ( options ) ;
102118 const ignores = asArray ( options . ignore || [ ] ) ;
103119 return globby ( [ glob , ...ignores . map ( v => `!(${ v } )` ) . map ( v => unixify ( v ) ) ] , createGlobbyOptions ( options ) ) ;
104120 }
105121
106122 resolveGlobsSync ( glob : string , options : CodeFileLoaderOptions ) {
123+ options = this . getMergedOptions ( options ) ;
107124 const ignores = asArray ( options . ignore || [ ] ) ;
108125 return globby . sync ( [ glob , ...ignores . map ( v => `!(${ v } )` ) . map ( v => unixify ( v ) ) ] , createGlobbyOptions ( options ) ) ;
109126 }
110127
111128 async load ( pointer : string , options : CodeFileLoaderOptions ) : Promise < Source [ ] | null > {
129+ options = this . getMergedOptions ( options ) ;
112130 if ( isGlob ( pointer ) ) {
113131 const resolvedPaths = await this . resolveGlobs ( pointer , options ) ;
114132 const finalResult : Source [ ] = [ ] ;
@@ -127,6 +145,7 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
127145 }
128146
129147 loadSync ( pointer : string , options : CodeFileLoaderOptions ) : Source [ ] | null {
148+ options = this . getMergedOptions ( options ) ;
130149 if ( isGlob ( pointer ) ) {
131150 const resolvedPaths = this . resolveGlobsSync ( pointer , options ) ;
132151 const finalResult : Source [ ] = [ ] ;
@@ -143,6 +162,7 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
143162 }
144163
145164 async handleSinglePath ( location : string , options : CodeFileLoaderOptions ) : Promise < Source [ ] | null > {
165+ options = this . getMergedOptions ( options ) ;
146166 const normalizedFilePath = ensureAbsolutePath ( location , options ) ;
147167
148168 const errors : Error [ ] = [ ] ;
@@ -192,6 +212,7 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
192212 }
193213
194214 handleSinglePathSync ( location : string , options : CodeFileLoaderOptions ) : Source [ ] | null {
215+ options = this . getMergedOptions ( options ) ;
195216 const normalizedFilePath = ensureAbsolutePath ( location , options ) ;
196217
197218 const errors : Error [ ] = [ ] ;
0 commit comments