简体   繁体   中英

How to compile a nodejs Typescript project with classes in separate files within NTVS?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM