Skip to content

Commit ad56f5b

Browse files
committed
Add support for d.ts declaration generation
1 parent 9018668 commit ad56f5b

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
"types"
1414
],
1515
"scripts": {
16-
"build": "npm run -s build:babel && npm run -s build:self && npm run build:types",
17-
"build:babel": "babel-node --extensions \".ts\" src/cli.ts \"src/{cli,index}.ts\" --target=8 --output=dist --compress",
18-
"build:self": "node dist/cli.js \"src/{cli,index}.ts\" --target=8 --output=dist --compress",
19-
"build:types": "tsc --emitDeclarationOnly --outdir types --declaration --allowSyntheticDefaultImports src/{cli,index}.ts",
16+
"build": "npm run -s build:babel && npm run -s build:self",
17+
"build:babel": "babel-node --extensions \".ts\" src/cli.ts \"src/{cli,index}.ts\" --target=8 --output=dist --compress --types=types",
18+
"build:self": "node dist/cli.js \"src/{cli,index}.ts\" --target=8 --output=dist --compress --types=types",
2019
"prerelease": "npm run build",
2120
"release": "np",
2221
"test": "echo \"Error: no test specified\" && exit 0"

src/cli.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ async function main(argv_: string[]) {
2121
const outputDir = args.output;
2222
const compress = args.compress;
2323
const nodeTarget = args.target;
24+
const typesDir = args.types;
2425

25-
await bundler({ compress, input, nodeTarget, outputDir });
26+
await bundler({ compress, input, nodeTarget, outputDir, typesDir });
2627
}
2728

2829
main(process.argv).catch(console.error);

src/index.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// native
2+
import { spawn } from 'child_process';
23
import { 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+
127144
interface BundlerOptions {
128145
compress: boolean;
129146
input: string;
130147
nodeTarget: string;
131148
outputDir: string;
149+
typesDir?: string;
132150
}
133151

134152
export 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

Comments
 (0)