Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to generate .d.ts types file #24

Open
holtwick opened this issue Dec 21, 2020 · 7 comments
Open

Ability to generate .d.ts types file #24

holtwick opened this issue Dec 21, 2020 · 7 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@holtwick
Copy link

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

@rsms rsms changed the title Create .d.ts types file Ability to generate .d.ts types file Mar 11, 2021
@rsms
Copy link
Owner

rsms commented Mar 11, 2021

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 rsms added the enhancement New feature or request label Mar 11, 2021
@lann
Copy link

lann commented Mar 16, 2021

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,

@holtwick
Copy link
Author

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.

@rsms
Copy link
Owner

rsms commented Mar 21, 2021

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 rsms added the help wanted Extra attention is needed label Mar 21, 2021
@lann
Copy link

lann commented Mar 22, 2021

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.

@rsms
Copy link
Owner

rsms commented May 12, 2021

23dcb87 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

@marcofugaro
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants