Visual Studio 2012 TypeScript项目在编译过程中忽略了两个文件。

3
我有一个Visual Studio 2012 TypeScript项目。当我第一次创建该项目时,我添加了2个新的.ts文件,分别为Framework.ts & Graphics.ts。最近我又添加了两个TypeScript文件用于声明,并将它们命名为.d.ts,例如Framework.d.ts和Graphics.d.ts。有趣的是,这4个文件的所有构建操作均设置为TypeScriptCompile,但只有其中2个(Framework.ts和Graphics.ts)可以被编译,而其他的.d.ts文件仍然具有原始的.js构建代码,涉及到一个shapes模块。我认为可能是.d.ts扩展名存在问题,于是将这些文件重命名为Framework_d.ts和Graphics_d.ts,但也无法进行编译!
到现在为止,我感到很困惑。我查看了csproj文件:
<TypeScriptCompile Include="Framework\Framework.d.ts" />
<Content Include="Framework\Framework.d.js">
  <DependentUpon>Framework.d.ts</DependentUpon>
</Content>
<Content Include="Graphics\Graphics.d.js">
  <DependentUpon>Graphics.d.ts</DependentUpon>
</Content>
<Content Include="TestPages\SpriteBatch.html" />
<TypeScriptCompile Include="Framework\Framework.ts" />
<Content Include="Framework\Framework.js">
  <DependentUpon>Framework.ts</DependentUpon>
</Content>
<Content Include="Graphics\Graphics.js">
  <DependentUpon>Graphics.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="Graphics\Graphics.d.ts" />
<TypeScriptCompile Include="Graphics\Graphics.ts" />

然后是 TypeScript 编译器的执行命令:

<Target Name="BeforeBuild">
  <Exec Command="&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript\0.8.0.0\tsc&quot; @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
</Target>

我真的希望它也能构建这些文件。只是我不知道为什么它拒绝了。有人知道如何解决吗?

2个回答

2

如果它们是你所说的声明文件,那么它们将没有JavaScript输出。我认为你不需要“添加”它们到项目中,或者至少不要费心在它们上面应用TypeScriptCompile构建操作。你只需要在其他源文件中包含它们即可。

///<reference path="Framework\Framework.d.ts" 
///<reference path="Graphics.d.ts" 

因此,他们看到了声明。


1
如果我使用import Framework = module("Framework");那么我会得到一个错误,因为 require() 函数不存在。因为生成的 js 中的行是 var Framework = require("Framework"); 我是否遗漏了一些关于 require 的东西? - IanelGreenleaf
所以你需要在网页中包含require.js(在编译的TypeScript之前)。如果设置编译标志--target AMD,则TypeScript会生成与AMD兼容的代码(请参见此线程,了解如何编辑VisualStudio项目文件以添加必要的编译器标志;https://dev59.com/KGcs5IYBdhLWcg3wfEBP在底部的评论中;这也包括源映射的标志)。但是,即使TypeScript将生成对require的调用,它也不包括require功能。 - Ezward
此外,您需要具有扩展名的模块的完整路径,因此它应该是:Framework = module("Framework\Framework.ts") 如果 Framework.ts 是实际的 TypeScript 代码,会产生编译后的 JavaScript。如果您只包含声明,请使用 ///reference 这个东西。 - Ezward

0

我不确定这是否符合您的情况,但是与.ts文件编码相关的问题存在一些问题。

不幸的是,TypeScript无法编译带有签名的UTF-8。 您可以选择另一个保存选项 “文件” ->“高级保存选项” -> 选择“无签名的UTF-8”。

检查当前问题。 http://typescript.codeplex.com/workitem/85

谢谢


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接