简体   繁体   English

是否可以使用tsc命令将单个TypeScript文件编译到输出路径?

[英]Is it possible to compile a single TypeScript file to an output path using the tsc command?

In an attempt to separate my .ts source files from the .js output, I'm trying to implement a formal file-watcher in TypeScript and it seems as though the ability to specify an output path for a single file does not exist. 为了将我的.ts源文件与.js输出分开,我试图在TypeScript中实现一个正式的文件监视程序,并且似乎不存在为单个文件指定输出路径的功能。

The flow, just to be clear: I begin watching my entire src directory, make a change to, say, src/views/HomeView.ts , and i want node to pick up that the file has been changed, and move the compiled version to public/js/views/HomeView.js . 流程要明确一点:我开始查看我的整个src目录,将其更改为src/views/HomeView.ts ,并且我希望节点选择该文件已更改,然后移动编译的版本到public/js/views/HomeView.js

Using tsc myfile.ts --out myfile.js it travels through all of the modules and compiles each in the same path that the .ts file exists, without placing the final file(s) in the properly specified path. 使用tsc myfile.ts --out myfile.js它可以遍历所有模块,并以.ts文件所在的相同路径编译每个模块,而无需将最终文件放在正确指定的路径中。 It does however create an empty file where I would like it to end up. 但是,它的确创建了一个空文件,我希望将其结束。

I'm wondering: 我在想:

1) Is it possible to use the --out parameter and only compile that one file? 1)是否可以使用--out参数编译一个文件? I do not want it to traverse imports and compile every single file, but merely do syntax / error checking at compile-time (and that's only a secondary requirement, not necessary); 我不希望它遍历导入并编译每个文件,而只是在编译时进行语法/错误检查(这只是次要条件,没有必要); and

2) Is there a bug in the compiler that prevents it from properly combining / creating files? 2)编译器中是否有错误阻止其正确组合/创建文件? Again, the final output in the --out path directive is empty, but a file is indeed created. 同样,-- --out path指令中的最终输出为空,但确实创建了一个文件。

Any help would be appreciated. 任何帮助,将不胜感激。

* Update * *更新*

While I don't want to close this question as I do believe it's still an issue, I went ahead and implemented the core TypeScript compiler to achieve what I needed to do, bypassing tsc altogether. 虽然我不想结束这个问题,因为我确实相信这仍然是一个问题,但是我继续实现了TypeScript核心编译器来实现我需要做的事情,完全绕过了tsc Please see https://github.com/damassi/TypeScript-Watcher for more information and usage. 请参阅https://github.com/damassi/TypeScript-Watcher了解更多信息和用法。

When you use the --out parameter, you get a single file with the compiler walking the dependencies and working out the correct order for the final file. 使用--out参数时,编译器将遍历依赖项并为最终文件计算出正确的顺序,从而得到一个文件。

For example... 例如...

tsc --out final.js app.ts

Will find any dependencies in app.ts and compile them all too. 将在app.ts找到任何依赖项,并将其全部编译。 After it works out the correct order it will save all of the JavaScript in final.js . 确定正确的顺序后,它将所有JavaScript保存在final.js If this JavaScript file is empty, it is normally indicative of a compiler error. 如果此JavaScript文件为空,则通常表示编译器错误。

It is not possible to use the --out parameter to generate a JavaScript file for just the TypeScript file you specify, unless that file has no dependencies. 不能使用--out参数仅为您指定的TypeScript文件生成JavaScript文件,除非该文件没有依赖项。

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

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