Ugeen
Ugeen

Reputation: 31

SystemJS builder: Resolve module registration path correctly when bundling the app

I'm playing with Angular2 tutorial project https://angular.io/docs/ts/latest/tutorial/ and used SystemJS Builder for app bundling. However, I'm struggling with paths in generated bundle code registering modules. Here is my folder structure:

wwwroot
|-dist
  |-configs
  |  |-systemjs.config.js
  |    
  |-js
  | |-bundle.js //generated output bundle
  |
  |-main.js
  |-app.module.js
  |-app.component.js
......................

systemjs.config.js file:

(function (global) {
    System.config({
        baseURL:'wwwroot',
        paths: {
            // paths serve as alias
            'res:': 'lib/',
            'ng:': 'lib/@angular/'
        },
        // map tells the System loader where to look for things
        map: {
            // !!! the app is within the dist folder !!!
            'app': 'dist',
            // angular bundles
            '@angular/core': 'ng:core/bundles/core.umd.js',
            // other Angular 2 stuff...
            '@angular/upgrade': 'ng:upgrade/bundles/upgrade.umd.js',
            // other libraries
            'rxjs': 'res:rxjs',
            'angular2-in-memory-web-api': 'res:angular2-in-memory-web-api',
        },      

        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                //app entry point
                main: './main.js',
                format: 'register',
                defaultExtension: 'js'    
            },
            rxjs: {
                defaultExtension: 'js'
            },
            'angular2-in-memory-web-api': {
                main: './index.js',
                defaultExtension: 'js'
            }
        }
    });
})(this);

Gulp task:

var systemjsBuilder = require('systemjs-builder');

gulp.task('bundle-app', function () {

var builder = new systemjsBuilder('', 'wwwroot/dist/configs/systemjs.config.js');

return builder
    .bundle('wwwroot/dist/**/*', PATH.root + PATH.dist + '/js/bundle.min.js', {
        minify: false,
        mangle: true
    })
    .then(function () {
        console.log('Build complete');
    })
    .catch(function (err) {
        console.log('Build error');
        console.log(err);
    });

});

In generated bundle file all of the modules registered via System.registerDynamic('wwwroot/dist/main.js',...), which does not work. However, if I manually delete all 'wwwroot/' from registration paths, the app starts working (it's mapped to 'dist' in the systemjs.config.js file).

How should I configure the builder (or system config file) to omit 'wwwroot' folder adding or resolve it correctly? Any advice will be appreciated.

Thank you!

Upvotes: 1

Views: 735

Answers (1)

Ugeen
Ugeen

Reputation: 31

Finally found it! Just needed to:

  • add root base URL to builder instance, e.g. new systemjsBuilder('wwwroot', 'wwwroot/dist/configs/systemjs.config.js');
  • change .bundle('wwwroot/dist/**/*',... to .bundle('[dist/**/*]', ... (square brackets remove dependencies)
  • in systemjs.config.js file either change app package format from register to cjs (my JS modules are in CommonJS format) or remove the format completely to enable module auto detection.

Upvotes: 1

Related Questions