estrella icon indicating copy to clipboard operation
estrella copied to clipboard

Ability to generate .d.ts types file

Open holtwick opened this issue 3 years ago • 7 comments

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

holtwick avatar Dec 21 '20 13:12 holtwick

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,
}) 

rsms avatar Mar 11 '21 00:03 rsms

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,

lann avatar Mar 16 '21 21:03 lann

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.

holtwick avatar Mar 21 '21 15:03 holtwick

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.

rsms avatar Mar 21 '21 17:03 rsms

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.

lann avatar Mar 22 '21 14:03 lann

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

rsms avatar May 12 '21 03:05 rsms

Would be great if this was just an option to estrella!

marcofugaro avatar Aug 02 '21 13:08 marcofugaro