【问题标题】:Angular2 lazy loading modules: how to create a build package with SystemJS Builder?Angular2 延迟加载模块:如何使用 SystemJS Builder 创建构建包?
【发布时间】:2016-11-04 12:32:16
【问题描述】:

我正在使用 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 可以做到...

【问题讨论】:

    标签: angular gulp lazy-loading systemjs systemjs-builder


    【解决方案1】:

    看看你是否可以通过提供所需模块的地图来安抚 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);
    

    【讨论】:

    • 这不起作用,因为 SystemJS Builder 的 buildStatic 会生成一个从 main.js 开始的 js 文件(如您在我的代码中所见)。在这种情况下,我如何映射延迟加载模块?
    • 看看你的意思。您可以按照 SO..stackoverflow.com/questions/38944257/… 上的这篇文章中的说明尝试捆绑算术。让我知道这是否有帮助
    • 我不需要丢弃任何依赖项。我只需要在 dist bundle 上使用延迟加载。
    • 我已经为你做了一个例子github.com/abidmix/SystemjsBuilderLazyLoad使用英雄教程应用程序.. 还可以使用现场演示。您可以对您的应用程序使用类似的方法。如果此捆绑工作流程适合您,将更新我的答案
    • 感谢您的回复,但正如我在那儿 (github.com/systemjs/builder/issues/729#issuecomment-259076482) 所说的那样,您并没有为生产创建一个捆绑包。您正在保持开发结构。
    【解决方案2】:

    为了让 bundle 为您的应用工作,您需要为 systemjs 定义“bundles”属性,如下所示:

        if (global.ENV === 'production') {
            config.transpiler = 'typescript';
            config.map = {
                'app': 'app',
                '@angular': 'node_modules/@angular',
            };
            config.packages = {
                'app': { main: 'main.js', defaultExtension: 'js' },
                '@angular/core': { main: 'index.js' },
                '@angular/common': { main: 'index.js' },
                '@angular/compiler': { main: 'index.js' },
                '@angular/forms': { main: 'index.js' },
                '@angular/http': { main: 'index.js' },
                '@angular/platform-browser': { main: 'index.js' },
                '@angular/platform-browser-dynamic': { main: 'index.js' },
                '@angular/router': { main: 'index.js' },
            };
            config.bundles = {
                'build/scripts/app.min.js': [
                    'app/modules/FIRST-SECTION/*',
                    'app/modules/SECOND-SECTION/*',
                    '@angular/core/index.js',
                    '@angular/common/index.js',
                    '@angular/compiler/index.js',
                    '@angular/platform-browser/index.js',
                    '@angular/platform-browser-dynamic/index.js',
                    '@angular/http/index.js',
                    '@angular/router/index.js',
                    '@angular/forms/index.js',
                ]
            }
    
    System.config(config);
    

    【讨论】:

    • 感谢您来这里回复我 :) 无论如何,您的解决方案不起作用。我仍然在调用延迟加载模块时收到 404。我将以下代码添加到我的systemjs.config.js:bundles: { 'build/scripts/app.min.js': ['app/modules/FIRST-MODULE/*', 'app/modules/SECOND-MODULE/*'] }
    • 我明白了,您正在创建一个静态 js 文件,所以我猜,使用静态 js 文件您不能使用 systemjs 动态加载它,如果您查看 systemjs 构建器站点,他们会说您使用静态包甚至不需要系统 js 来加载模块,我在解决方案中所做的是创建非静态包
    • 是的,你是对的。那么如何在没有 SystemJS(dist 文件夹中没有 systemjs.config.js 文件)和延迟加载模块的情况下创建生产版本?我看到 WebPack 可以做到...
    • 我已经更新了我的问题(见编辑)。也许它可以让你找出问题所在。
    • 如果整个应用程序只有一个静态 js 文件,则意味着您不需要延迟加载,如果您需要延迟加载,则意味着您需要多个捆绑包(js 文件)应用程序按模块,所以我建议你使用systemjs builder的“build”功能而不是“buildStatic”,这样你就可以拥有多个bundle然后你可以进行延迟加载,当然你需要系统js来动态加载它们
    猜你喜欢
    • 2017-04-04
    • 2016-07-19
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 2017-05-05
    • 2019-09-18
    相关资源
    最近更新 更多