Angular2 延迟加载模块 : how to create a build package with SystemJS Builder?

标签 angular gulp lazy-loading systemjs systemjs-builder

我正在使用 Angular2 和 SystemJS 创建 Web 应用程序。 我的应用程序和路由器配置中有一些模块,我使用延迟加载来打开它们。

这是我的应用程序的路由文件,它延迟加载了两个模块:

const appRoutes: Routes = [
    { path: '', component: MainComponent,
        canActivate: [AuthGuard],
        children: [
            { path: 'dashboard', component: DashboardComponent },
            { path: 'first-section', loadChildren: 'app/modules/FIRST-SECTION/first-section.module' },
            { path: 'second-section', loadChildren: 'app/modules/SECOND-SECTION/second-section.module' },
            { path: 'documents', component: DocumentsComponent },
            { path: 'users', component: UsersComponent },
            { path: '', redirectTo: 'dashboard', pathMatch: 'full' }
        ]
    }
];

我使用 Gulp 为开发服务器和生产构建创建任务。 对于构建,我使用 SystemJS Builder 为整个应用程序创建缩小的 JS 文件。

gulp.task('app-bundle', function() {
    var builder = new Builder('src', 'src/systemjs.config.js');

    return builder.buildStatic('app/main.js', 'build/scripts/app.min.js', { minify: true });
});

但是...如果我尝试在构建包上运行服务器,则应用程序在尝试运行延迟加载模块时无法运行。 它给了我以下错误:

GET http://127.0.0.1:8080/app/modules/FIRST-SECTION/first-section.module 404 (Not Found)

这是我的 systemjs.config.js 文件:

(function (global) {
    System.config({
        paths: {
            'npm:': './node_modules/',
        },
        map: {
            app: 'app',
            '@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',
            '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
            'rxjs': 'npm:rxjs',
            'lodash': 'npm:lodash/lodash.min.js',
            'jquery': 'npm:jquery/dist/jquery.min.js',
        },
        packages: {
            app: { main: './main.js', defaultExtension: 'js' },
            rxjs: { defaultExtension: 'js' },
        }
    });
})(this);

编辑:

如 SystemJs Builder 站点上的静态包所述,我们不需要 SystemJs 来加载模块。事实上,我正在创建一个静态包(使用 buildStatic,而不是 bundle),但由于我排除了 SystemJs,所以似乎没有其他方法可以延迟加载模块。 那么如何使用 bundle 创建生产构建,然后使用延迟加载模块但不使用 SystemJS(不使用 dist 中的 systemjs.config.js 文件)文件夹)?我看到 WebPack 可以做到...

最佳答案

看看您是否可以通过提供所需模块的映射来安抚 system.js。所以当 system.js 找到路径 modules/FIRST-SECTION/first-section.module 时,你告诉它:嘿,你能把这个路径映射到 modules/FIRST-SECTION/first- section.module.js 没有在控制台尖叫。希望这会有所帮助。

(function (global) {
    System.config({
        paths: {
            'npm:': './node_modules/',
        },
        map: {
            app: 'app',
            'modules/FIRST-SECTION/first-section.module':'modules/FIRST-SECTION/first-section.module.js',
          .........................
          your other mappings here
          .........................
            'lodash': 'npm:lodash/lodash.min.js',
            'jquery': 'npm:jquery/dist/jquery.min.js',
        },
        packages: {
            app: { main: './main.js', defaultExtension: 'js' },
            rxjs: { defaultExtension: 'js' },
        }
    });
})(this);

关于Angular2 延迟加载模块 : how to create a build package with SystemJS Builder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40422760/

相关文章:

Angular fire firestore 没有导出成员 'FirestoreSettingsToken'

javascript - typescript 推送到数组中的特定键

javascript - Angular 4 - 从服务到组件捕获错误对象

javascript - 在此 Gulp 任务中,什么负责创建文件名签名?

java - 如何显式加载 Hibernate 实体的 LAZY 获取成员

javascript - Angular 2 ngModel不绑定(bind)类定义中定义的对象属性

css - 在 SASS 中乘以无单位值时出现意外结果

node.js - 用于 Node 应用程序的 LibSass 的 SCSS 编译器替代品

android - 许多 AsyncTask 生成时出现 AsyncTask 性能问题。

android - 使用 ViewHolder 滚动 ListView 时图像被随机播放