-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds example of how to generate TypeScript type declaration files. Is…
…sue #24
- Loading branch information
Showing
5 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(", ")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/sh | ||
set -e | ||
rm -rf out | ||
./build.js -no-diag | ||
head -n3 out/main.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
] | ||
} |