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

Please provide an option for combining only declaration files #192

Closed
NoelAbrahams opened this issue Jul 22, 2014 · 10 comments
Closed

Please provide an option for combining only declaration files #192

NoelAbrahams opened this issue Jul 22, 2014 · 10 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@NoelAbrahams
Copy link

Hi,

We have this particular use-case.

Say we have two projects: MyLib.csproj and MyClient.csproj.

For each project we'd like to

  • Generate a single declarations file and have it created in the bin directory, e.g. MyLib.d.ts
  • Redirect all JavaScript output to the bin directory.
  • Not have the JavaScript files combined, instead we have a matching .js file for each .ts file.

This would enable the following

  • The ability to reference types in another project using a single include,
    e.g.
<reference path="MyLib.d.ts" />
  • The ability to debug JavaScript files individually in the browser.

At present the "Combine JavaScript output into file" option combines both the JavaScript and the declarations file.

I suggest another check box for "Combine declarations output into file".

Thanks.

@RyanCavanaugh
Copy link
Member

This would be better handled a post-build step, or as a feature in a larger build system. The set of compiler switches is intentionally small since we'd rather have specific build tools handle more complex scenarios (see https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).

@NoelAbrahams
Copy link
Author

@RyanCavanaugh,

This would be better handled [by] a post-build step, or as a feature in a larger build system.

When there are sub-folders in the Visual Studio project, setting "Redirect JavaScript output to directory" recreates the folder structure under the output directory. In this situation it's not trivial to combine the declarations (because copy *.d.ts myDeclartions.d.ts is not recursive), and it's necessary to resort to a powershell script or similar. Admittedly, this is not a big deal but when dealing with a large number of projects this is yet another maintenance issue.

Here is a suggestion that circumvents having to add another compiler flag, and is also largely backward compatible. The --out flag can be extended slightly as follows, in order to permit combining only JavaScript, or declarations or both:

-- out foo.js  // Only combine JavaScript output into a single file
-- out foo.d.ts // Only combine declarations into a single file
-- out foo.js|bar.d.ts // Combine both JavaScript and declarations

Please consider reopening this issue.

@RyanCavanaugh
Copy link
Member

It's not that we want a small number of compiler flags, it's that we want a low complexity of the commadline interface to tsc.

We can come back to this if a large proportion of people need this or there's no way to do it in another tool; I don't think either of those are the case right now.

@NoelAbrahams
Copy link
Author

@RyanCavanaugh, can you please elaborate why the following is not true:

there's no way to do it in another tool;

In other words, what tool should I use in Visual Studio to make this work? As I mentioned above, it's necessary to write a PowerShell script, which requires granting execution rights to PS1 scripts on every single workstation on which the code is compiled.

@RyanCavanaugh
Copy link
Member

You could write an MSBuild task, .targets file, use a more complex build system like grunt, or have a post-build script that used something other than Powershell.

@NoelAbrahams
Copy link
Author

So, for the lack of any sound alternatives (integrating grunt for just this requirement seemed like overkill) we went ahead with the MSBuild option. The implementation turned out to be non-trivial. It was necessary to deal with the following issues:

  • Local references (added to ensure script ordering) in _references.d.ts had to be removed.
  • The external references in _references.d.ts had to be inserted first.
  • When a project contains both internal and external modules then it was necessary to exclude the external modules.
  • Since the clean command in Visual Studio does not remove custom generated files. it was necessary to add another task to remove the generated declarations file.

After solving all these problems unfortunately we happened upon a problem that is insolvable by the MSBuidl task: When compile-on-save is enabled, changes to code files does not result in the regeneration of the combined declarations file.

So my point is: Considering that every user who has this requirement has to carry out this non-trivial task, and even then not achieve the required result 100%, it makes sense for TypeScript to enable this (potentially trivial) flag.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 23, 2014

@NoelAbrahams what is the limitation on using --out for js output as well?
We have always thought of the .d.ts and .js as group that are consumed together.

@NoelAbrahams
Copy link
Author

@mhegazy, the reason we want the .js files to be listed individually is to make debugging easier in the browser. Chrome, for example, lists them like this:

untitled

The reason we want the declarations combined into a single file is to be able to reference all the types with a single <reference> tag.

@danquirk
Copy link
Member

Couldn't you create a single file that is your master set of type references (ie references.d.ts or myTypes.d.ts) and reference that?

@NoelAbrahams
Copy link
Author

@danquirk, there are two reasons why we didn't want to maintain a master reference file:

  • All types in the master.d.ts shouldn't necessarily be visible to all projects. We would like to restrict certain subsets to specific projects.
  • We wanted a simple workflow for adding new types: adding and building a class file should result in the type becoming visible to the code-base; A master reference file creates an additional maintenance step, both when adding a new type and also when renaming or deleting an existing one.

However, your suggestion got me thinking and there is in fact a solution to the problem I outlined above, namely

When compile-on-save is enabled, changes to code files does not result in the regeneration of the combined declarations file

Basically, instead of creating the combined declarations file as consisting of the _content_ of all the declarations files in the output directory, we instead create it by inserting <reference> tags for each file. This way, compile-on-save updates the link target and the changes get picked up by the VS plugin.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Declined The issue was declined as something which matches the TypeScript vision Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants