estrella
estrella copied to clipboard
Ability to generate .d.ts types file
I wonder how to use estrella correctly to generate a types file. I've set up a very simple example to show the issue:
https://github.com/holtwick/estrella-ts-module
I would like to add the working result to https://github.com/holtwick/create-estrella
Interesting idea! It would be nice if this was an Estrella build option, like build({ typedefOutput: "dist/foo.d.ts" })
In the meantime I think you could accomplish this by using the typescript API
Something like this:
#!/usr/bin/env node
const { build } = require("estrella")
const pkg = require("./package.json")
const ts = require("typescript")
function generateTypeDefs() {
const filenames = [ ... ]
const compilerOptions = { optionsFromTSConfigFile..., declaration: true }
const program = ts.createProgram(filenames, compilerOptions)
const emitResult = program.emit()
// check emitResult.diagnostics and .emittedFiles
}
build({
bundle: true,
sourcemap: true,
format: "esm",
entry: "src/index.ts",
outfile: pkg.module,
onStart: generateTypeDefs,
})
The best-performing way (probably) to do this would be for tsc to do it while type-checking. I hacked this together by modifying estrella locally; it was a little messy with the current design but it did work.
Edit: I came up with this minimally-invasive change that let me do what I needed with tslint: { args: ['--emitDeclarationOnly', ...] }
:
--- a/src/tslint.js
+++ b/src/tslint.js
@@ -107,8 +107,9 @@ export function tslint(options /*:TSLintOptions*/) {
}
// CLI arguments
+ const noEmit = !(options.args || []).includes('--emitDeclarationOnly');
let args = [
- "--noEmit",
+ noEmit && "--noEmit",
options.colors && "--pretty",
options.watch && "--watch",
tsconfigFile && "--project", tsconfigFile,
Thanks a lot for the example @rsms ❤️
I guess I will wait for an implementation in Estrella then. For now, it seems to be more straight forward to use tsc
for dist building directly for such use cases.
Nice @lann! If you'd like to take on adding this "for real" in a PR (preferably with test; I can help you in the PR with this) that would be ace.
Sure, I'd be happy to not carry the patch myself. Is the approach alright? Another possibility would be to add a noEmit
option that defaults true.
23dcb8734836f877e6186869a780e6b18c311c12 adds an example of how to generate type declaration files. Try it from source with:
git clone https://github.com/rsms/estrella.git
cd estrella
./test/test.sh examples/typedef-generation
Would be great if this was just an option to estrella!