Search code examples
angulartypescriptsystemjs

Import folder from TypeScript components using SystemJS


I work with Angular2, TypeScript, and SystemJS as module loader. I use SystemJs mostly here to keep files "as is" for debugging reasons. It basically works, but I have specific issue loading components. I read several answers here but found nothing comparable.

I have this structure for my App:

Folder Structure

Please notice the "index.d.ts" in the Components folder. The reason is simply a simplified import in the app.ts file, that goes like this:

import * as cmp from './Components';

The folder contains all my components and the 'index.d.ts' that looks like this:

export * from './shop-root';
export * from './shop-catalogs';
export * from './shop-edit-cat';
export * from './shop-about';
export * from './shop-contact';

I use my components like this, then (excerpt from @NgModule):

declarations: [
    cmp.ShopRootComponent,
    cmp.ShopCatalogs,
    cmp.ShopEditCat,
    cmp.ShopAbout,
    cmp.ShopContact
]

From the perspective of typescript this works fine, no issues here. My typescript compiler is happy and creates an 'app.js' file that contains the import:

var cmp = require('./Components');

When SystemJS starts loading it fails loading the folder.

Loading of a folder fails

If I use single files everything works perfectly. So all base paths etc. are correct.

However, with eventually hundreds of components I think the single file loading is going to clutter my TypeScript code.

Using WebPack or other loader is not an answer. The problem I face is the improvement of writing nice looking TypeScript files and have the compiled JavaScript handy for debugging (and learning).

The current System.config.js looks like this:

(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'npm:': 'assets/js/lib/'
        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder
            app: 'assets/js/app',
            // angular bundles
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
            '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
            '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
            // other libraries
            'rxjs': 'npm:rxjs',
            'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                main: './app.js',
                defaultExtension: 'js'
            },
            rxjs: {
                defaultExtension: 'js'
            },
            'angular-in-memory-web-api': {
                main: './index.js',
                defaultExtension: 'js'
            }
        }
    });
})(this);

Again, for components refererred to as single files this config is working perfectly.

Question

How can I load components from a folder/directory using SystemJS? I suspect it's mainly a SystemJs configuration issue but can't find the setting.


Solution

  • Try to rename index.d.ts to index.ts