@@ -33,6 +33,10 @@ const schema = {
33
33
type : 'array' ,
34
34
default : [ ] ,
35
35
} ,
36
+ output : {
37
+ anyOf : [ { type : 'null' } , { instanceof : 'Function' } ] ,
38
+ default : null ,
39
+ } ,
36
40
} ,
37
41
additionalProperties : false ,
38
42
} ,
@@ -49,7 +53,7 @@ const schema = {
49
53
* properties (i.e. the user-provided object merged with default
50
54
* values).
51
55
*
52
- * @typedef {{ args: string[] } } PbtsOptions
56
+ * @typedef {{ args: string[], output: ((resourcePath: string) => string | Promise<string>) | null } } PbtsOptions
53
57
* @typedef {{
54
58
* paths: string[], pbjsArgs: string[],
55
59
* pbts: boolean | PbtsOptions,
@@ -67,24 +71,26 @@ const schema = {
67
71
68
72
/** @type { (resourcePath: string, pbtsOptions: true | PbtsOptions, compiledContent: string, callback: NonNullable<ReturnType<LoaderContext['async']>>) => any } */
69
73
const execPbts = ( resourcePath , pbtsOptions , compiledContent , callback ) => {
70
- /** @type PbtsOptions */
71
- const normalizedOptions = {
72
- args : [ ] ,
73
- ...( pbtsOptions === true ? { } : pbtsOptions ) ,
74
- } ;
75
-
76
- // pbts CLI only supports streaming from stdin without a lot of
77
- // duplicated logic, so we need to use a tmp file. :(
78
- new Promise ( ( resolve , reject ) => {
79
- tmp . file ( { postfix : '.js' } , ( err , compiledFilename ) => {
80
- if ( err ) {
81
- reject ( err ) ;
82
- } else {
83
- resolve ( compiledFilename ) ;
84
- }
85
- } ) ;
86
- } )
87
- . then (
74
+ try {
75
+ /** @type PbtsOptions */
76
+ const normalizedOptions = {
77
+ args : [ ] ,
78
+ output : null ,
79
+ ...( pbtsOptions === true ? { } : pbtsOptions ) ,
80
+ } ;
81
+
82
+ // pbts CLI only supports streaming from stdin without a lot of
83
+ // duplicated logic, so we need to use a tmp file. :(
84
+ /** @type Promise<string> */
85
+ const compiledFilenamePromise = new Promise ( ( resolve , reject ) => {
86
+ tmp . file ( { postfix : '.js' } , ( err , compiledFilename ) => {
87
+ if ( err ) {
88
+ reject ( err ) ;
89
+ } else {
90
+ resolve ( compiledFilename ) ;
91
+ }
92
+ } ) ;
93
+ } ) . then (
88
94
( compiledFilename ) =>
89
95
new Promise ( ( resolve , reject ) => {
90
96
fs . writeFile ( compiledFilename , compiledContent , ( err ) => {
@@ -95,16 +101,29 @@ const execPbts = (resourcePath, pbtsOptions, compiledContent, callback) => {
95
101
}
96
102
} ) ;
97
103
} )
98
- )
99
- . then ( ( compiledFilename ) => {
100
- const declarationFilename = `${ resourcePath } .d.ts` ;
101
- const pbtsArgs = [ '-o' , declarationFilename ]
102
- . concat ( normalizedOptions . args )
103
- . concat ( [ compiledFilename ] ) ;
104
- pbts . main ( pbtsArgs , ( err ) => {
105
- callback ( err , compiledContent ) ;
104
+ ) ;
105
+ /** @type { (resourcePath: string) => string | Promise<string> } */
106
+ const output =
107
+ normalizedOptions . output === null
108
+ ? ( r ) => `${ r } .d.ts`
109
+ : normalizedOptions . output ;
110
+ const declarationFilenamePromise = Promise . resolve ( output ( resourcePath ) ) ;
111
+
112
+ Promise . all ( [ compiledFilenamePromise , declarationFilenamePromise ] )
113
+ . then ( ( [ compiledFilename , declarationFilename ] ) => {
114
+ const pbtsArgs = [ '-o' , declarationFilename ]
115
+ . concat ( normalizedOptions . args )
116
+ . concat ( [ compiledFilename ] ) ;
117
+ pbts . main ( pbtsArgs , ( err ) => {
118
+ callback ( err , compiledContent ) ;
119
+ } ) ;
120
+ } )
121
+ . catch ( ( err ) => {
122
+ callback ( err , undefined ) ;
106
123
} ) ;
107
- } ) ;
124
+ } catch ( err ) {
125
+ callback ( err instanceof Error ? err : new Error ( `${ err } ` ) , undefined ) ;
126
+ }
108
127
} ;
109
128
110
129
/** @type { (this: LoaderContext, source: string) => any } */
0 commit comments