Search code examples
typescriptangularsystemjs

Angular SystemJS require adds extension to .html file


I'm building a simple Angular Application using Typescript. I'm using gulp to build it. I'm trying to save the html and css files for each component in seperate files.

Example:

components
    - app.component.ts
    - app.component.html
    - app.component.scss

I'm including the files like this

app.component.ts

import { Component } from '@angular/core';
@Component({
    selector: 'my-app',
    template: require('./app.component.html')
})
export class AppComponent { }

The Problem is, that it is trying to load the file ./app.component.html.js as shown on the network-tools in chrome. Chrome Network-Tools Request

If i open the requested url in my browser and remove the .js at the end, the file gets displayed correctly.

systemjs.config.js

(function(global) {
    // map tells the System loader where to look for things
    var map = {
        'app':                        '', // 'dist',
        '@angular':                   'libs/@angular',
        'angular2-in-memory-web-api': 'libs/angular2-in-memory-web-api',
        'rxjs':                       'libs/rxjs'
    };
    // packages tells the System loader how to load when no filename and/or no extension
    var packages = {
        'app':                        { main: 'main.js', defaultExtension: 'js' },
        'rxjs':                       { defaultExtension: 'js' },
        'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' }
    };
    var ngPackageNames = [
        'common',
        'compiler',
        'core',
        'forms',
        'http',
        'platform-browser',
        'platform-browser-dynamic',
        'router',
        'router-deprecated',
        'upgrade'
    ];
    // Individual files (~300 requests):
    function packIndex(pkgName) {
        packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
    }
    // Bundled (~40 requests):
    function packUmd(pkgName) {
        packages['@angular/'+pkgName] = { main: '/bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
    }
    // Most environments should use UMD; some (Karma) need the individual index files
    var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
    // Add package entries for angular packages
    ngPackageNames.forEach(setPackageConfig);
    var config = {
        map: map,
        packages: packages
    };
    System.config(config);
})(this);

If I'm storing the html and css directly in the components, everything works as expected.

Project Structure


Solution

  • According to cheatsheet Component configuration chapter you can pass external html and css as following:

    templateUrl: 'components/app.component.html', styleUrls: ['components/app.component.css']

    And you can make the path relative to component instead of relative to application root by adding moduleId: module.id to @Component directive.

    @Component({ moduleId: module.id, selector: 'my-app', templateUrl: 'app.component.html', styleUrls: ['app.component.css'] })

    You can find the full description here.

    But you also have to manage how you convert your sass files into css before.