简体   繁体   English

如何在NTVS中使用单独文件中的类编译nodejs Typescript项目?

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

NTVS = Node Tools for Visual Studio NTVS = Visual Studio的节点工具

I created a new project with the server.js as main file. 我使用server.js作为主文件创建了一个新项目。 Then created several classes, each one in their file. 然后创建几个类,每个类在其文件中。 All those files are referenced in a references.ts file. 所有这些文件都在references.ts文件中引用。

All files contain a reference to references.ts. 所有文件都包含对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). 因此,我勾选了“将Javascript输出合并到文件中”,但是server.ts中的代码未附加到生成的文件中(所有类都很严格)。

How could I use internal references ? 我如何使用内部引用?

Edit: Here are the files I use 编辑:这是我使用的文件

server.ts server.ts

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

import http = require('http');

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

Classes/HtmlElement.ts 类/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 : 如果不使用合并选项进行编译,则这是node.js窗口的输出:

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. 如果使用Combine选项,则不会发生任何事情,因为生成的文件仅包含类声明。

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”-一个TypeScript文件-而不是您提到的“ server.js”)的顶部,您应该引用“ references.ts”文件,该文件具有对您所有其他的TypeScript文件:

//
//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. 在ASP.NET项目中,项目设置中有一个“ TypeScript Build”部分,您可以在其中选择“将JavaScript输出合并到文件中”,然后在其中放置“ server.js”。

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. 不幸的是,看起来NTVS家伙没有在他们自己的项目设置中添加任何类似的功能,因此您必须实施自己的预构建过程才能使其在该环境中正常工作,直到他们提供某种正式的方式来支持该功能为止。 。

As an alternative, you could compile your server.js file using a BeforeBuild step. 或者,您可以使用BeforeBuild步骤来编译server.js文件。 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): 您需要手动编辑.proj文件(或.njsproj文件),并在最后一行(即项目文件中的倒数第二行)之前添加如下内容:

<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. 其中“ ReferencedFile1”等是您尝试包含在最终server.js文件中的文件。 You should look at the output of your normal build to see what arguments are ordinarily being passed to tsc.exe. 您应该查看常规构建的输出,以查看通常将哪些参数传递给tsc.exe。

The "Combine Javascript output into file" option does not work for files containing the import directive. “将Javascript输出合并到文件中”选项不适用于包含import指令的文件。

You can replace 您可以更换

import http = require('http');

by 通过

var http = require('http');

Then the server.ts will be combined and everything works fine. 然后将server.ts合并,一切正常。

You could now create one file per class and instanciate thoses using 您现在可以为每个班级创建一个文件,并使用实例化这些文件

var instance=new MyClass(); var instance = new MyClass();

Wich is a much more convenient syntax in my own opinion. 我个人认为Wich是一种更为方便的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用nodejs 4编译Typescript? - How do I compile Typescript to with nodejs 4? TypeScript / NodeJS:未定义变量,对单独的文件使用内部引用路径 - TypeScript / NodeJS: variable not defined, using internal ref paths for separate files TypeScript with nodejs: TypeScript 编译报错 - TypeScript with nodejs: TypeScript compile Error 如何让 webpack 包含为 Typescript Nodejs(不是浏览器)项目生成的声明文件(以及如何使用) - How to get webpack to include the generated declaration files for a Typescript Nodejs (not browser) project (and how to consume) 将现有 NodeJS 项目转换为 TypeScript 时如何强制将所有文件视为模块 - How to force all files to be treated as modules when converting an existing NodeJS project to TypeScript 在同一nodejs项目中如何混合coffescript和打字稿文件? - What to do to mix coffescript and typescript files in the same nodejs project? 如何动态导入打字稿中的编译文件(nodejs) - How to import compiled files in typescript on fly (nodejs) 如何将整个 Typescript 项目编译为可执行文件 - How to compile whole Typescript project to Executable 如何创建/配置 NodeTS(TypeScript NodeJS)项目? - How to create/config a NodeTS(TypeScript NodeJS) project? TypeScript和NodeJS项目配置 - TypeScript and NodeJS Project Configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM