1

I have been studying angular2 for a while following the official site. Since SystemJS seems to be the recommended module loader, I'm trying to use it. From the official quick start (https://angular.io/guide/quickstart) the following index.html example loading an angular2 project can be found:

<html>
  <head>
    <title>Angular 2 QuickStart</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">
    <!-- 1. Load libraries -->
     <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
  </head>
  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>
</html>

I would like to load everything with systemJS, reaching this situation:

<html>
  <head>
    <title>Angular 2 QuickStart</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">
    <!-- 2. Loading SystemJS -->
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
  </head>
  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>
</html> 

Is that possible?

1 Answer 1

0

I would recommend you would use angular-cli (https://github.com/angular/angular-cli) to help you generate a new project and do all the configs and hard work for you. It will help you a lot and you will save time believe me. Things are changing a lot right now so it will be easier for you to have a good friend like angular-cli in your journey.

If you don't want to use it, the you can create a file like system-config.js that will contain something like this, you can add in barrels object specific libraries that you want:

var map = {};
/** User packages configuration. */
var packages = {};

var barrels = [
    // Angular specific barrels.
    '@angular/core',
    '@angular/common',
    '@angular/compiler',
    '@angular/http',
    '@angular/router',
    '@angular/platform-browser',
    '@angular/platform-browser-dynamic',
    // Thirdparty barrels.
    'rxjs',
    // App specific barrels.
    'app',
    'app/shared',
];
var cliSystemConfigPackages = {};
barrels.forEach(function (barrelName) {
    cliSystemConfigPackages[barrelName] = { main: 'index' };
});
// Apply the CLI SystemJS configuration.
System.config({
    map: {
        '@angular': 'vendor/@angular',
        'rxjs': 'vendor/rxjs',
        'main': 'main.js'
    },
    packages: cliSystemConfigPackages
});
// Apply the user's configuration.
System.config({ map: map, packages: packages });

and then in your index.html file you can have:

<script>
  System.import('system-config.js').then(function () {
    System.import('app');
  }).catch(console.error.bind(console));
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply, I wanted to try anglular-cli but I have read it's still in development and not that mature.
@GiovanniDiSanto you can use it. I'm using it also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.