我一直在使用“--out”编译器标志来创建 1 个 javascript 输出文件。 我有很多 .ts 文件,并通过将其放在每个文件的顶部来管理它:
/// <reference path="../references.ts" />
现在我真的很想在这个项目中使用 React.js。在 Typescript 中“正确”使用 react 的唯一方法(这样我仍然能够以 typescript 的方式创建类)似乎是使用这个库 react-typescript
但是这个库是通过使用 commonjs 模块设置的,然后使用 browserify 将它们组合成 1 个文件:
import React = require('react');
import ReactTypescript = require('react-typescript');
并且在项目的示例中,所有.ts文件首先通过在顶部使用大量“require”调用来列出其本地.ts依赖项。
问题是:当我将 --module "commonjs"标志添加到编译器命令时,--out 的通常行为停止工作,因为引用文件不再包含在输出中。似乎编译器不能将 --out 与 --module 一起使用,对吧?
到目前为止,我不确定如何将此“react-typescript”javascript 模块转换为内部 typescript 模块。所以在我看来,继续下去的唯一方法是将我所有的引用标签更改为需要调用。 我的问题是这是正确的方法还是有其他方法?
因为我有这么多文件和这么多依赖项,我担心所有这些 require 调用会使最终代码不必要地复杂化,甚至可能影响性能?
最佳答案
the compiler cannot use --out together with --module, right
是的。外部模块和 --out
目前是互斥的。但是,您可以跟踪一个功能请求:https://github.com/Microsoft/TypeScript/issues/17
My question is if this is the right approach or if there is another way
对于您计划扩展的任何项目,建议使用外部模块。库是 --out
的有效目标,但不是应用程序。
Since I have so many files and so many dependencies, I'm worried that all these require calls would unnecessarily complicate the final code
不需要痛苦。查看:https://github.com/grunt-ts/grunt-ts#transforms
possible even effect the performance?
与网络下载速度相比,任何性能影响(速度方面)都是最小的。在我的系统上,3megs 的 js 文件大约需要 5 毫秒才能让 requirejs 完成它的工作。
关于compiler-construction - 将许多 typescript 文件转换为 1 个 js 文件,同时还使用 commonJS 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25488924/