Kokodoko
Kokodoko

Reputation: 28128

How to start a Typescript app with SystemJS modules?

I am using the typescript compiler to bundle my modules into one main.js file, using these settings in tsconfig.json:

"module": "system",
"out": "docs/js/main.js"

This works, so according to the SystemJS documentation, I only have to include the SystemJS production file and kickstart the app with these tags in my HTML:

<script src="js/system.js"></script>
<script>
  SystemJS.import('js/main.js');
</script> 

My app:

import { Message } from "./message";

export class App {
    constructor() {
        let demomessage = new Message("hello");
    }
}

export class Message {      
    constructor(str:string) {
        console.log(str);
    }
}

This results in this javascript code in main.js:

System.register("message", ...) {
    // message code here
});
System.register("app", ...) {
    // app code here
});

The part that I'm missing (and that's also not explained in Microsoft's always-lacking-Typescript-documentation) is how to actually start the app... How does SystemJS know which class is the starting point? Even if I just put console.log in my app it doesn't execute....

EDIT

I discovered that using system.js instead of system-production.js at least starts the process. After a lot of fiddling I got my app to start with the following code, but it looks weird and ugly. Is this how it's supposed to work???

<script src="js/system.js"></script>
<script>
  // get the anonymous scope
  System.import('js/main.js')
    .then(function() {
      // now we can get to the app and make a new instance
      System.import('app').then(function(m){
         let app = new m.App();
      })
    });
</script>

Upvotes: 5

Views: 2483

Answers (1)

Kokodoko
Kokodoko

Reputation: 28128

After much head-scratching I found out the answer is more simple than expected: just load the bundle first as a regular .js file, and then you can import the app directly:

<script src="js/system.js"></script>
<script src="js/main.js"></script>
<script>
  System.import('app').then(function(module) {
    let a = new module.App();
  });
</script>

Upvotes: 6

Related Questions