Open RicoSuter opened 7 years ago
Hi, thank you for your kind feedback!
Unfortunately, there is currently no support for bundled d.ts files in typescript-bundle, but this is something i absolutely would like to look at adding. But perhaps only under the provision it didn't require too many elaborate transformations of .d.ts files to make happen.
note: I still would prefer the functionality of this project moved into the typescript compiler itself, and expect one day it eventually will make its way there. But because of this, i wouldn't want to introduce transformations or functionality that might conflict a supported compiler implementation in future. For the bundling tech, it preforms no transformation of source code, and only seeks to wrap the compilers output to make the modules loadable, it would be good to have the same for declarations also if possible.
So, just so you know, the typescript compiler can generate a single declaration bundle when compiling projects with the --module amd --outfile
options, its just that the modules in the declaration bundle are all ambient (in fitting with AMD), and not reflective of the bundle produced by tsc-bundle
.
I have attached a small project for you to have a look at too, it contains 1 top level module (index.ts) and 3 inner modules (a.ts, b.ts, c.ts) which are exported through index.ts. Have a look at the build.bat file which contains the command line args to compile.
tsc-bundle ./src/index.ts ./bin/index.js --declaration
The following is the output declaration generated from the compiler options above.
declare module "a" {
export class A {
}
}
declare module "b" {
export class B {
}
}
declare module "c" {
export class C {
}
}
declare module "index" {
import { A } from "a";
import { B } from "b";
import { C } from "c";
export { A, B, C };
}
The following would be the ideal output.
declare module [gns] { // this would be the --globalNamespace option.
export class A {
}
export class B {
}
export class C {
}
}
Because only modules exported on the top level module (index.ts in this case) should be visible to the caller (the page), the declaration "should" only contain member types which are exported through index.ts. (available on the globalNamespace name).
In addition, compiling bundles without passing the globalNamespace option should result in no declaration at all, as all types are locked away within the bundles wrapping closure.
I would be open to talking with you further on this functionality. If you have any ideas, lets talk about them here. I would be more than happy to update the project to allow for this functionality, and would value your inputs into how you would imagine things working from your side.
Many Thanks sinclairzx81
Just checked my output and simply replacing the module name of the first declaration
declare module "xxx" {
with the chosen "globalNamespace" and removing the other module declarations would be enough... I even think that only the public/required classes are in the generated .d.ts.
The ability to produce a declaration file for the bundle would be great for easily consuming TypeScript libraries in Deno (where TypeScript is a first-class citizen and, sadly, Deno's built-in bundler only emits JavaScript).
Note: declaration merging might be tricky!
Hey thanks for this project. It works without problems!
Is there also an option to generate a .d.ts file for the bundle?