角5的SystemJS捆绑应用依赖

时间:2018-09-10 09:26:05

标签: angular systemjs bundling-and-minification systemjs-builder

我有Angular 5应用。而且我想尽可能地缩小和捆绑我的应用。

这是SystemJS的配置:

(function (global) {
    System.config({
        paths: {
            'npm:': "node_modules/"
        },
        map: {
            app: "dist",
            '@angular/animations': "npm:@angular/animations/bundles/animations.umd.min.js",
            '@angular/animations/browser': "npm:@angular/animations/bundles/animations-browser.umd.min.js",
            '@angular/common': "npm:@angular/common/bundles/common.umd.min.js",
            '@angular/common/http': "npm:@angular/common/bundles/common-http.umd.min.js",
            '@angular/compiler': "npm:@angular/compiler/bundles/compiler.umd.min.js",
            '@angular/core': "npm:@angular/core/bundles/core.umd.min.js",
            '@angular/forms': "npm:@angular/forms/bundles/forms.umd.min.js",
            '@angular/http': "npm:@angular/http/bundles/http.umd.min.js",
            '@angular/platform-browser': "npm:@angular/platform-browser/bundles/platform-browser.umd.min.js",
            '@angular/platform-browser/animations': "npm:@angular/platform-browser/bundles/platform-browser-animations.umd.min.js",
            '@angular/platform-browser-dynamic': "npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.min.js",
            '@angular/platform-server': "npm:@angular/platform-server/bundles/platform-server.umd.min.js",
            '@angular/router': "npm:@angular/router/bundles/router.umd.min.js",
            // other libraries
            'angular-froala-wysiwyg': "npm:angular-froala-wysiwyg/bundles/angular-froala-wysiwyg.umd.min.js",
            'ngx-bootstrap': "npm:ngx-bootstrap/bundles/ngx-bootstrap.umd.min.js",
            'ngx-infinite-scroll': "npm:ngx-infinite-scroll/bundles/ngx-infinite-scroll.umd.min.js",
            'ngx-moment': "npm:ngx-moment/bundles/ngx-moment.umd.min.js",
            'moment': "npm:moment/min/moment.min.js",
            'rxjs': "npm:rxjs",
            'tslib': "npm:tslib/tslib.js",
            'jszip': "npm:jszip",
            'pako': "npm:pako",
            '@progress': "npm:@progress",
            '@telerik': "npm:@telerik"
        },
        packages: {
            app: {
                main: "./main.js",
                defaultExtension: "js"
            },
            rxjs: {
                defaultExtension: "js",
            },
            jszip: {
                defaultExtension: 'js',
                main: './dist/jszip.js'
            },
            pako: {
                defaultExtension: 'js',
                main: './index.js'
            },
            'npm:@progress/kendo-angular-buttons': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-charts': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-dateinputs': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-dropdowns': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-dialog': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-grid': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-pdf-export': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-inputs': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-intl': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-l10n': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-excel-export': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-layout': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-scrollview': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-sortable': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-popup': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-resize-sensor': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-angular-upload': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-charts': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-data-query': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-date-math': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-drawing': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-file-saver': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-intl': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-ooxml': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@progress/kendo-popup-common': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@telerik/kendo-draggable': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@telerik/kendo-dropdowns-common': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@telerik/kendo-intl': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            },
            'npm:@telerik/kendo-inputs-common': {
                main: './dist/npm/main.js',
                defaultExtension: 'js'
            }
        }
    });
})(this);

gulpfile.js:

var gulp = require('gulp');
var tsc = require('gulp-typescript');
var systemjsBuilder = require('systemjs-builder');
var angularEmbed = require('gulp-angular-embed');
var del = require('del');

var tsProject = tsc.createProject('tsconfig.json');
var builder = new systemjsBuilder('', 'systemjs.config.js');

gulp.task('bundle-config', function () {
    return gulp.src('systemjs.config.js')
        .pipe(gulp.dest('dist/configs'));
});

gulp.task('embed-template', ['bundle-config'], function () {
    return gulp.src('app/**/*.ts')
        .pipe(angularEmbed())
        .pipe(gulp.dest('dist2'));
});

gulp.task('tsc', ['embed-template'], function () {
    return gulp.src('dist2/app/**/*.ts')
        .pipe(tsProject())
        .js.pipe(gulp.dest('dist'));
});

gulp.task('clean', ['tsc'], function () {
    return del(['dist2/**']);
});

gulp.task('bundle-app', ['clean'], function () {
    return builder
        .bundle('[dist/**/*]', 'production/app.bundle.min.js', {
                minify: true,
                mangle: true
            })
        .then(function () {
            console.log('Build complete');
        })
        .catch(function (err) {
            console.log('Build error');
            console.log(err);
        });
});

gulp.task('bundle-dependencies', ['clean'], function () {
    return builder
        .bundle('dist/**/* - [dist/**/*.js]', 'production/dependencies.bundle.min.js', {
                minify: true,
                mangle: true
            })
        .then(function () {
            console.log('Build complete');
        })
        .catch(function (err) {
            console.log('Build error');
            console.log(err);
        });
});

gulp.task('production', ['bundle-app', 'bundle-dependencies'], function () { });

和index.html包含以下内容:

<!DOCTYPE html>
<html>
<head>...</head>
<body>
  <my-app></my-app>

  <script src="node_modules/core-js/client/shim.min.js"></script>
  <script src="node_modules/zone.js/dist/zone.min.js"></script>
  <script src="node_modules/reflect-metadata/Reflect.js"></script>
  <script src="node_modules/systemjs/dist/system.js"></script>

  <script src="production/dependencies.bundle.min.js"></script>
  <script src="production/app.bundle.min.js"></script>
  <script>
      System.import('dist/configs/systemjs.config.js').then(function() {
          System.import('app').catch(function(err){ console.error(err); });
      });
  </script>
</body>
</html>

结果,我已经在“ dist”文件夹(包含html的js文件)中编译了应用程序。另外,我还有“生产”文件夹,其中所有应用程序文件都位于“ app.bundle.min.js”位置,所有我的依赖项(例如angular,kendo,rxjs框架)都位于“ dependencies.bundle.min.js”位置。 当我启动我的应用程序时,它将分别加载这2个缩小的文件和所有依赖项,例如:

/node_modules/rxjs/add/operator/mergeMap.js
/node_modules/@progress/kendo-angular-grid/dist/npm/data/data.iterators.js
/node_modules/@angular/animations/bundles/animations.umd.min.js

但是它们都在“ dependencies.bundle.min.js”中。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我只是将systemjs.config.js放在文件包文件之前,它对我有帮助:

...
<body>
    <ac-app></ac-app>

    <script src="systemjs.config.js"></script>
    <script src="production/dependencies.bundle.min.js"></script>
    <script src="production/app.bundle.min.js"></script>
    <script>
        System.import('app')
            .catch(function (err) {
                console.error(err);
            });
    </script>
</body>
...