I recently started looking into TypeScript. I am using require.js to load the modules. Currently I am only loading jquery, but this will obviously be expanded.
My project setup is as followed:
app
> classes
> Greeter.ts
> AppConfig.ts
> AppMain.ts
lib
> jquery-1.7.2.js
> require.js
modules
> jquery.d.ts
> require.d.ts
app.ts
default.htm
AppConfig.ts
/// <reference path="../modules/require.d.ts" />
/// <reference path="AppMain.ts" />
require.config({
baseUrl: '../',
paths: {
'jquery': 'lib/jquery-1.7.2'
},
shim: {
jquery: {
exports: '$'
}
}
});
require(['jquery','app/AppMain'],
($, main) => {
// code from window.onload
var appMain = new main.AppMain();
appMain.run();
});
AppMain.ts
import gt = module("app/classes/Greeter");
export class AppMain {
public run() {
var el = document.getElementById('CONTENT_PH');
var greeter = new gt.Greeter(el);
greeter.start();
}
}
Greeter.ts
export class Greeter {
element: HTMLElement;
span: HTMLElement;
timerToken: number;
constructor (element: HTMLElement) {
this.element = element;
this.element.innerText += "The time is: ";
this.span = document.createElement('span');
this.element.appendChild(this.span);
this.span.innerText = new Date().toUTCString();
}
start() {
this.timerToken = setInterval(() => this.span.innerText = new Date().toUTCString(), 500);
}
stop() {
clearTimeout(this.timerToken);
}
}
.csproj
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<TypeScriptSourceMap> --module AMD</TypeScriptSourceMap>
</PropertyGroup>
<Target Name="BeforeBuild">
<Message Text="Compiling TypeScript files" />
<Message Text="Executing tsc$(TypeScriptSourceMap) @(TypeScriptCompile ->'"%(fullpath)"', ' ')" />
<Exec Command="tsc$(TypeScriptSourceMap) @(TypeScriptCompile ->'"%(fullpath)"', ' ')" />
</Target>
I got a few examples working on my local machine, but when I try to run it on a live server it gives the following Script Error from require.js.
Error: Script error http://requirejs.org/docs/errors.html#scripterror [Break On This Error]
var e = new Error(msg + '\nhttp://requirejs.org/docs/errors.html#' + id); require.js (line 194)
I have no idea why this works on my developing machine, but not on a live server.
Running this example locally works in chrome and internet explorer, but not in firefox. But this might be another issue.
If you need additional details please let me know.