简体   繁体   English

如何将一个VS2013项目中的通用打字稿代码共享到一个单独的VS2013项目中

[英]How do I share common typescript code from one VS2013 project into a separate VS2013 project

I have Visual Studio 2013, Typescript 1.3, and Module System set to AMD (requirejs 2.1.15 pulled down via Nuget, Type definitions for RequireJS 2.1.8) 我将Visual Studio 2013,Typescript 1.3和模块系统设置为AMD(通过Nuget下拉的requirejs 2.1.15,RequireJS 2.1.8的类型定义)

I want to have a Web Application, let's call it MyWebApplication1, that has a simple html page which includes a javascript file that is generated from my typescript, eg. 我想要一个Web应用程序,我们称它为MyWebApplication1,它有一个简单的html页面,其中包括一个从我的打字稿生成的javascript文件。 MyTypescript.ts MyTypescript.ts

MyHtml.html: 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: requireConfig.ts:

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

MyTypescript.ts: 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: MyCommonResource.ts:

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

This all works fine with the following folder structure in one project: 在一个项目中,以下文件夹结构都可以很好地工作:

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) 但是,我想要这样的东西(各种Web项目可能需要一个单独的项目)

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: 在MyTypescript.ts中没有这样的东西,我看不到实现此目的的好方法:

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

Which won't work for deployment under IIS, because the generated js define would look like this: 这对于在IIS下进行部署不起作用,因为生成的js定义如下所示:

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

Is there a good solution for this situation? 有没有针对这种情况的好的解决方案?

I have considered using post build events, using a local nuget server,...there just doesn't seem to be a nice way of handling common bits of code in different web apps when using typescript and visual studio though. 我考虑过使用构建后事件,使用本地nuget服务器,...使用打字稿和Visual Studio时,似乎并不是处理不同Web应用程序中通用代码段的好方法。

Thanks in advance. 提前致谢。

You have already mentioned what I think is the right option. 您已经提到了我认为正确的选择。

If you have code you want to share across multiple projects, a package manager is the perfect way to share it. 如果您有要在多个项目中共享的代码,则包管理器是共享代码的理想方法。 Not only does it aid the distribution of the shared code, it also helps with versioning. 它不仅有助于共享代码的分发,还有助于版本控制。

There are some neat extensions that make NuGet package creation really easy - I am using NuGet Packager . 有一些巧妙的扩展使NuGet软件包的创建非常容易-我正在使用NuGet Packager

You can use the free edition of Pro Get to get started - it is a really neat NuGet server. 您可以使用Pro Get的免费版本开始使用-这是一台非常整洁的NuGet服务器。

You can also use your local NuGet server as a proxy to keep versions of other libraries consistent, keep a list of "team-approved" packages and to ensure you have checked licenses and kept to license conditions (rather than having to check if anyone has added a new package). 您还可以将本地NuGet服务器用作代理,以保持其他库版本的一致性,保留“团队批准的”软件包的列表,并确保已检查许可证并遵守许可证条件(而不是必须检查是否有人添加了一个新包)。

If you prefer a different package manager (ie npm) you can pretty much follow the same pattern as the features are very similar. 如果您希望使用其他软件包管理器(即npm),则可以遵循相同的模式,因为功能非常相似。

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

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