8

我正在使用 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 site with static bundles 所述,我们不需要 SystemJs 来加载模块。实际上,我正在创建一个静态包(使用buildStatic,而不是bundle),但是由于我排除了 SystemJ,因此似乎没有其他方法可以延迟加载模块。bundle那么如何使用然后使用延迟加载模块但没有 SystemJS(systemjs.config.js文件夹中没有文件)来创建生产构建dist?我看到 WebPack 可以做到...

4

2 回答 2

1

See if you can appease system.js by providing a map for the required module. So when system.js finds the path modules/FIRST-SECTION/first-section.module , you tell it : Hey , could you map this path to modules/FIRST-SECTION/first-section.module.js without screaming in the console. Hope this helps .

(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);
于 2016-11-07T01:42:00.373 回答
1

为了使捆绑软件适用于您的应用程序,您需要为 systemjs 定义“捆绑软件”属性,如下所示:

    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);
于 2016-11-08T05:23:59.133 回答