1

NTVS = Node Tools for Visual Studio

I created a new project with the server.js as main file. Then created several classes, each one in their file. All those files are referenced in a references.ts file.

All files contain a reference to references.ts.

However, my project does not run. It says that some classes does not exists.

So I tick the "Combine Javascript output into file" but the code from server.ts is not appended to the resulting file (all classes are there tough).

How could I use internal references ?

Edit: Here are the files I use

server.ts

/// <reference path="references.ts"/>

import http = require('http');

var html = new HtmlElement('html');
...

Classes/HtmlElement.ts

class HtmlElement {
    tag: string;
    attributes: Array<HtmlAttribute>;
    childrens: Array<HtmlElement>;
    parent: HtmlElement;
    text: string;
...

references.ts

/// <reference path="Scripts/typings/node/node.d.ts" />
/// <reference path="Classes/HtmlElement.ts" />

If compiling without combine option this is the output of node.js window :

debugger listening on port 5858

C:\Zdisk\Projets\14 08 - QCM\NodeJsTypeScript1\ServerApp\server.js:5
var html = new HtmlElement('html');
               ^
ReferenceError: HtmlElement is not defined
    at Object.<anonymous     (C:\Projects\NodeJsTypeScript1\Server
App\server.js:5:16)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain [as _onTimeout] (module.js:497:10)
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
Press any key to continue...

If I use the combine option, nothing happends because the resulting file contain only classes declaration.

aurelius
  • 3,946
  • 7
  • 40
  • 73
bokan
  • 3,601
  • 2
  • 23
  • 38

2 Answers2

0

At the top of your "main file" (which should be "server.ts" - a TypeScript file - not "server.js" as you mentioned), you should reference your "references.ts" file, which has your references to all your other TypeScript files:

//
//server.ts

///<reference path='./references.ts' />

//Program Code...

In a ASP.NET project, there's a "TypeScript Build" section of the project's settings, where you can select "Combine JavaScript output into file" and, and you'd put "server.js" in there.

Unfortunately it looks like the NTVS guys haven't put any similar functionality in their own project settings, so you'd have to implement your own pre-build process to get this to work in that environment until they provide some formal way of supporting this.

As an alternative, you could compile your server.js file using a BeforeBuild step. You need to manually edit your .proj file (or .njsproj file) and put something like the following right BEFORE the last line (ie, the next-to-last line in the project file):

<Target Name="BeforeBuild">
    <Exec Command="tsc.exe --module CommonJS --sourcemap --target ES5 --out server.js <ReferencedFile1> <...>" />
</Target>

Where "ReferencedFile1" etc are the files you are trying to include in the final server.js file. You should look at the output of your normal build to see what arguments are ordinarily being passed to tsc.exe.

Phil Powers
  • 61
  • 1
  • 3
  • The "Set as Nodejs Startup Files" option is only available on .js files. I have done what you say... it does not work. I add details in my question. (thx for your answer) – bokan Aug 15 '14 at 16:03
  • The TypeScript files need to be combined into your final server.js file (using the --out command line parameter to the tsc compiler). Try manually running "tsc --out server.js server.ts Classes/HtmlElement.ts" and then running your server.js file. You should look at server.js to validate that your HtmlElement code has been included. – Phil Powers Aug 15 '14 at 16:24
  • How do I add the --out so it use it when I hit F5 ? – bokan Aug 15 '14 at 16:27
  • It looks like that's a limitation of Node Tools for Visual Studio. I don't see any way to automatically do that, so you'd have to do it manually. I'd probably raise this as an issue with the NTVS guys. – Phil Powers Aug 15 '14 at 16:44
  • Edited my answer to reflect the fact that there is no "TypeScript Build" section in NTVS Project Settings. – Phil Powers Aug 15 '14 at 16:51
  • Added one more potential solution - running tsc.exe as a BeforeBuild target within your project settings. This is probably the best you're going to get until the NTVS guys add formal support for combining TypeScript files. – Phil Powers Aug 15 '14 at 17:05
  • The problem was the presence of a import directive in the server.ts file. Thank you phil for your time. – bokan Aug 18 '14 at 08:28
0

The "Combine Javascript output into file" option does not work for files containing the import directive.

You can replace

import http = require('http');

by

var http = require('http');

Then the server.ts will be combined and everything works fine.

You could now create one file per class and instanciate thoses using

var instance=new MyClass();

Wich is a much more convenient syntax in my own opinion.

bokan
  • 3,601
  • 2
  • 23
  • 38