11// native
2+ import { spawn } from 'child_process' ;
23import { basename , parse , resolve } from 'path' ;
34
45// packages
@@ -124,29 +125,56 @@ async function createRollupConfig({
124125 return { inputOptions, outputOptions } ;
125126}
126127
128+ function createTypes ( { input, output } : { input : string ; output : string } ) {
129+ return new Promise ( ( fulfill , reject ) => {
130+ const child = spawn ( 'tsc' , [
131+ '--declaration' ,
132+ '--emitDeclarationOnly' ,
133+ '--allowSyntheticDefaultImports' ,
134+ '--declarationDir' ,
135+ output ,
136+ input ,
137+ ] ) ;
138+
139+ child . on ( 'error' , reject ) ;
140+ child . on ( 'exit' , fulfill ) ;
141+ } ) ;
142+ }
143+
127144interface BundlerOptions {
128145 compress : boolean ;
129146 input : string ;
130147 nodeTarget : string ;
131148 outputDir : string ;
149+ typesDir ?: string ;
132150}
133151
134152export async function bundler ( {
135153 compress,
136154 input,
137155 nodeTarget,
138156 outputDir,
157+ typesDir,
139158} : BundlerOptions ) {
159+ // the current working directory
140160 const cwd = process . cwd ( ) ;
141161
162+ // if a custom typesDir was not passed, use outputDir
163+ typesDir = typesDir || outputDir ;
164+
165+ // get the contents of package.json
142166 const pkg = await getConfig ( cwd ) ;
143167
168+ // pull out all of the dependencies to flag externals
144169 const pkgDependencies = Object . keys ( pkg . dependencies || { } ) ;
145170
171+ // find all the input TypeScript files
146172 const inputs = await glob ( input , { absolute : true } ) ;
147173
174+ // if we have more than one input, flag this as a multi-input run
148175 const withMultipleInputs = inputs . length > 1 ;
149176
177+ // loop thorugh the inputs, creating a rollup configuraion for each one
150178 for ( let idx = 0 ; idx < inputs . length ; idx ++ ) {
151179 const input = inputs [ idx ] ;
152180
@@ -167,7 +195,10 @@ export async function bundler({
167195 const bundle = await rollup ( inputOptions ) ;
168196
169197 for ( let idx = 0 ; idx < outputOptions . length ; idx ++ ) {
170- await bundle . write ( outputOptions [ idx ] ) ;
198+ const output = outputOptions [ idx ] ;
199+
200+ await bundle . write ( output ) ;
201+ await createTypes ( { input, output : typesDir } ) ;
171202 }
172203 }
173204}
0 commit comments