[英]How do I share common typescript code from one VS2013 project into a separate VS2013 project
我將Visual Studio 2013,Typescript 1.3和模塊系統設置為AMD(通過Nuget下拉的requirejs 2.1.15,RequireJS 2.1.8的類型定義)
我想要一個Web應用程序,我們稱它為MyWebApplication1,它有一個簡單的html頁面,其中包括一個從我的打字稿生成的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
但是,我想要這樣的東西(各種Web項目可能需要一個單獨的項目)
MyWebApplication1 -Scripts -MyTypescript.ts -requireConfig.ts -MyHtml.html MyCommonWebCode1 -Scripts -MyCommonResource.ts
在MyTypescript.ts中沒有這樣的東西,我看不到實現此目的的好方法:
import MyCommonResource = require("..\..\AnotherProject\Scripts\MyCommonResource");
這對於在IIS下進行部署不起作用,因為生成的js定義如下所示:
define(["require", "exports", "..\..\AnotherProject\Scripts\MyCommonResource"], function (require, exports, MyCommonResource) {
有沒有針對這種情況的好的解決方案?
我考慮過使用構建后事件,使用本地nuget服務器,...使用打字稿和Visual Studio時,似乎並不是處理不同Web應用程序中通用代碼段的好方法。
提前致謝。
您已經提到了我認為正確的選擇。
如果您有要在多個項目中共享的代碼,則包管理器是共享代碼的理想方法。 它不僅有助於共享代碼的分發,還有助於版本控制。
有一些巧妙的擴展使NuGet軟件包的創建非常容易-我正在使用NuGet Packager 。
您可以使用Pro Get的免費版本開始使用-這是一台非常整潔的NuGet服務器。
您還可以將本地NuGet服務器用作代理,以保持其他庫版本的一致性,保留“團隊批准的”軟件包的列表,並確保已檢查許可證並遵守許可證條件(而不是必須檢查是否有人添加了一個新包)。
如果您希望使用其他軟件包管理器(即npm),則可以遵循相同的模式,因為功能非常相似。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.