javascript - 如何将一个 VS2013 项目中的通用 typescript 代码共享到一个单独的 VS2013 项目中

标签 javascript visual-studio requirejs typescript amd

我将 Visual Studio 2013、Typescript 1.3 和模块系统设置为 AMD(通过 Nuget 下拉 requirejs 2.1.15,RequireJS 2.1.8 的类型定义)

我想要一个 Web 应用程序,我们称之为 MyWebApplication1,它有一个简单的 html 页面,其中包含一个从我的 typescript 生成的 javascript 文件,例如MyTypescript.ts

MyHtml.html:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript" src="Scripts/require.js" data-main="Scripts/requireConfig.js">   </script>
</head>
<body>
    <div id="result">DEFAULT</div>
</body>
</html>

requireConfig.ts:

require.config({
    baseUrl: 'Scripts',  
});
require([ '../Scripts/MyTypescript'],
    function (MyTypescript) {
        MyTypescript.MyFunction("Mr Anderson");
    }
);

MyTypescript.ts:

import MyCommonResource = require("MyCommonResource");
export = MyApp;
module MyApp {
    export function MyFunction(myParameter: string) {
        var aString: string = (new Date()).toTimeString() ;
        var myCommonThing: MyCommonResource.CommonClass = new MyCommonResource.CommonClass();
        aString = myCommonThing.CommonMethod();
        alert("HELLO " + myParameter + " @  " + aString);
    }
}

MyCommonResource.ts:

export = MyCommon;
module MyCommon {
    export class CommonClass {
        constructor() { }
        public CommonMethod(): string {
            return "COMMON";
        }
    }
}

这一切都适用于一个项目中的以下文件夹结构:

MyWebApplication1
  -Scripts
    -MyTypescript.ts
    -MyCommonResource.ts
    -requireConfig.ts
  -MyHtml.html

However, I want something like this (a separate project that may be needed by various web projects)

MyWebApplication1
  -Scripts
    -MyTypescript.ts
    -requireConfig.ts
  -MyHtml.html
MyCommonWebCode1
  -Scripts
    -MyCommonResource.ts

I don't see a good way to accomplish this, without having something like this in MyTypescript.ts:

import MyCommonResource = require("..\..\AnotherProject\Scripts\MyCommonResource");

这不适用于 IIS 下的部署,因为生成的 js 定义如下所示:

define(["require", "exports", "..\..\AnotherProject\Scripts\MyCommonResource"], function (require, exports, MyCommonResource) {

对于这种情况有好的解决办法吗?

我考虑过使用构建后事件,使用本地 nuget 服务器,...不过,在使用 Typescript 和 Visual Studio 时,似乎没有一种很好的方法来处理不同 Web 应用程序中的常见代码位。

提前致谢。

最佳答案

您已经提到了我认为正确的选择。

如果您想要在多个项目之间共享代码,包管理器是共享它的完美方式。它不仅有助于共享代码的分发,还有助于版本控制。

有一些简洁的扩展可以使 NuGet 包的创建变得非常简单 - 我正在使用 NuGet Packager .

您可以使用Pro Get的免费版本首先 - 这是一个非常简洁的 NuGet 服务器。

您还可以使用本地 NuGet 服务器作为代理来保持其他库的版本一致,保留“团队批准的”软件包列表,并确保您已检查许可证并遵守许可证条件(而不必检查如果有人添加了新包)。

如果您更喜欢不同的包管理器(即 npm),您几乎可以遵循相同的模式,因为功能非常相似。

关于javascript - 如何将一个 VS2013 项目中的通用 typescript 代码共享到一个单独的 VS2013 项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27235977/

相关文章:

c# - Visual Studio 如何确定它是否必须启动 MSBuild?

javascript - 通过 dojo 加载自定义 Typescript 类不起作用

javascript - 如何将 VueJS 与 RequireJS 一起使用?

javascript - 让游戏角色走路,Javascript

c# - 如何在 VS 中进行单元测试?

c# - resx 资源文件显示警告 "The resource name is not a valid identifier"

javascript - Requirejs 未加载 jquery 插件

javascript - js.erb 文件中 Ruby 代码的范围是什么以及它如何访问 Controller 变量

javascript - 每次加载单页应用程序时检索 cookie

Javascript 商店营业时间