Skip to content

Commit

Permalink
Add support for d.ts declaration generation
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmurphy committed Jul 29, 2019
1 parent 9018668 commit ad56f5b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
"types"
],
"scripts": {
"build": "npm run -s build:babel && npm run -s build:self && npm run build:types",
"build:babel": "babel-node --extensions \".ts\" src/cli.ts \"src/{cli,index}.ts\" --target=8 --output=dist --compress",
"build:self": "node dist/cli.js \"src/{cli,index}.ts\" --target=8 --output=dist --compress",
"build:types": "tsc --emitDeclarationOnly --outdir types --declaration --allowSyntheticDefaultImports src/{cli,index}.ts",
"build": "npm run -s build:babel && npm run -s build:self",
"build:babel": "babel-node --extensions \".ts\" src/cli.ts \"src/{cli,index}.ts\" --target=8 --output=dist --compress --types=types",
"build:self": "node dist/cli.js \"src/{cli,index}.ts\" --target=8 --output=dist --compress --types=types",
"prerelease": "npm run build",
"release": "np",
"test": "echo \"Error: no test specified\" && exit 0"
Expand Down
3 changes: 2 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ async function main(argv_: string[]) {
const outputDir = args.output;
const compress = args.compress;
const nodeTarget = args.target;
const typesDir = args.types;

await bundler({ compress, input, nodeTarget, outputDir });
await bundler({ compress, input, nodeTarget, outputDir, typesDir });
}

main(process.argv).catch(console.error);
33 changes: 32 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// native
import { spawn } from 'child_process';
import { basename, parse, resolve } from 'path';

// packages
Expand Down Expand Up @@ -124,29 +125,56 @@ async function createRollupConfig({
return { inputOptions, outputOptions };
}

function createTypes({ input, output }: { input: string; output: string }) {
return new Promise((fulfill, reject) => {
const child = spawn('tsc', [
'--declaration',
'--emitDeclarationOnly',
'--allowSyntheticDefaultImports',
'--declarationDir',
output,
input,
]);

child.on('error', reject);
child.on('exit', fulfill);
});
}

interface BundlerOptions {
compress: boolean;
input: string;
nodeTarget: string;
outputDir: string;
typesDir?: string;
}

export async function bundler({
compress,
input,
nodeTarget,
outputDir,
typesDir,
}: BundlerOptions) {
// the current working directory
const cwd = process.cwd();

// if a custom typesDir was not passed, use outputDir
typesDir = typesDir || outputDir;

// get the contents of package.json
const pkg = await getConfig(cwd);

// pull out all of the dependencies to flag externals
const pkgDependencies = Object.keys(pkg.dependencies || {});

// find all the input TypeScript files
const inputs = await glob(input, { absolute: true });

// if we have more than one input, flag this as a multi-input run
const withMultipleInputs = inputs.length > 1;

// loop thorugh the inputs, creating a rollup configuraion for each one
for (let idx = 0; idx < inputs.length; idx++) {
const input = inputs[idx];

Expand All @@ -167,7 +195,10 @@ export async function bundler({
const bundle = await rollup(inputOptions);

for (let idx = 0; idx < outputOptions.length; idx++) {
await bundle.write(outputOptions[idx]);
const output = outputOptions[idx];

await bundle.write(output);
await createTypes({ input, output: typesDir });
}
}
}

0 comments on commit ad56f5b

Please sign in to comment.