diff --git a/examples/typedef-generation/README.md b/examples/typedef-generation/README.md new file mode 100644 index 0000000..af7b5ac --- /dev/null +++ b/examples/typedef-generation/README.md @@ -0,0 +1,7 @@ +This example shows how to generate TypeScript ".d.ts" files from .ts source files + +``` +$ ./build.js +``` + +Compiled .js bundle can now be found in `./out/` together with `main.d.ts` diff --git a/examples/typedef-generation/build.js b/examples/typedef-generation/build.js new file mode 100755 index 0000000..2fa8ee0 --- /dev/null +++ b/examples/typedef-generation/build.js @@ -0,0 +1,32 @@ +#!/usr/bin/env node +const { build, ts, tsconfig, dirname, glob, log } = require("estrella") + +build({ + entry: "main.ts", + outfile: "out/foo.js", + onEnd(config) { + const dtsFilesOutdir = dirname(config.outfile) + generateTypeDefs(tsconfig(config), config.entry, dtsFilesOutdir) + }, +}) + +function generateTypeDefs(tsconfig, entryfiles, outdir) { + const filenames = Array.from( + new Set( + (Array.isArray(entryfiles) ? entryfiles : [entryfiles]) + .concat(tsconfig.include || []))).filter(v => v) + log.info("Generating type declaration files for", filenames.join(", ")) + const compilerOptions = { + ...tsconfig.compilerOptions, + moduleResolution: undefined, + declaration: true, + outDir: outdir, + } + const program = ts.ts.createProgram(filenames, compilerOptions) + const targetSourceFile = undefined + const writeFile = undefined + const cancellationToken = undefined + const emitOnlyDtsFiles = true + program.emit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles) + log.info("Wrote", glob(outdir + "/*.d.ts").join(", ")) +} diff --git a/examples/typedef-generation/main.ts b/examples/typedef-generation/main.ts new file mode 100755 index 0000000..7726085 --- /dev/null +++ b/examples/typedef-generation/main.ts @@ -0,0 +1,14 @@ +export interface Named { + readonly name :string +} + +export class Foo implements Named { + readonly name :string +} + +export class Bar extends Foo { + readonly id :number + getName() :string { + return this.name + } +} diff --git a/examples/typedef-generation/test.sh b/examples/typedef-generation/test.sh new file mode 100755 index 0000000..b4e9a43 --- /dev/null +++ b/examples/typedef-generation/test.sh @@ -0,0 +1,5 @@ +#!/bin/sh +set -e +rm -rf out +./build.js -no-diag +head -n3 out/main.d.ts diff --git a/examples/typedef-generation/tsconfig.json b/examples/typedef-generation/tsconfig.json new file mode 100644 index 0000000..c050d40 --- /dev/null +++ b/examples/typedef-generation/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "noImplicitAny": true, + "strictFunctionTypes": true, + "strictBindCallApply": true, + "noImplicitThis": true, + "alwaysStrict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true, + "noImplicitReturns": true, + // "strictNullChecks": true, "strictPropertyInitialization": true, + "moduleResolution": "node", + "target": "es2017", + "baseUrl": "src" + }, + "include": [ + "main.ts" + ] +}