How to bundle and minify all the packages in a single file using systemjs-builder

756 Views Asked by At

I have an angular2 app. I use the following configuration to bundle the app. I have all the libraries under the lib folder, and bundle to the js folder.

const Builder = require('systemjs-builder');
gulp.task('build-minify-angular', function () {
    var builder = new Builder();
    builder.reset();
    builder.loader.defaultJSExtensions = true;
    builder.config({
        packages: {
            'rxjs': {
                main: 'rxjs/rx',
                defaultExtension: 'js'
            }
        },
        map: {
            'myapp': "myapp/app.js",
            '@angular/core': '@angular/core/bundles/core.umd.js',
            '@angular/common': '@angular/common/bundles/common.umd.js',
            '@angular/compiler': '@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': '@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': '@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': '@angular/http/bundles/http.umd.js',
            '@angular/router': '@angular/router/bundles/router.umd.js',
            '@angular/forms': '@angular/forms/bundles/forms.umd.js',
            '@ng-bootstrap/ng-bootstrap': '@ng-bootstrap/ng-bootstrap/bundles/ng-bootstrap.js',
        },
        baseURL: 'wwwroot/lib'
    });
    builder.bundle('myapp', 'wwwroot/js/bundle.js', { minify: true });
});
```

The app works fine if I have the following configuration.

window.onload = () => {
                System.config({
                    packages: {
                        'myapp': {
                            main: 'app', defaultExtension: 'js'
                        },
                        '': {
                            defaultExtension: 'js'
                        }
                    },
                    map: {
                        'rxjs': '../../js/rxjs/',
                        '@@angular/core': '../../js/@@angular/core/bundles/core.umd.min.js',
                        '@@angular/common': '../../js/@@angular/common/bundles/common.umd.min.js',
                        '@@angular/compiler': '../../js/@@angular/compiler/bundles/compiler.umd.min.js',
                        '@@angular/platform-browser': '../../js/@@angular/platform-browser/bundles/platform-browser.umd.min.js',
                        '@@angular/platform-browser-dynamic': '../../js/@@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.min.js',
                        '@@angular/http': '../../js/@@angular/http/bundles/http.umd.min.js',
                        '@@angular/router': '../../js/@@angular/router/bundles/router.umd.min.js',
                        '@@angular/forms': '../../js/@@angular/forms/bundles/forms.umd.min.js',
                        '@@ng-bootstrap/ng-bootstrap': '../../js/@@ng-bootstrap/ng-bootstrap/bundles/ng-bootstrap.js',
                    }
                });
                System.import("myapp");
            }

The app does not work if I remove the map from the configuration.

I expected the systemjs-builder to bundle the myapp and all the angular libraries in a single bundle.js file. But that is not the case, and looking for packages in various packages folder.

How can I bundle the myapp and it's dependencies in a single file?

1

There are 1 best solutions below

0
On

Change

builder.bundle('myapp', 'wwwroot/js/bundle.js', { minify: true });

to be

builder.buildStatic('myapp', 'wwwroot/js/bundle.js', { minify: true });

This will build a single self-executing file you can include on your index.html page