【问题标题】:Angular2 Application Initial load issue. typescript.js file is 4.75MBAngular2 应用程序初始加载问题。 typescript.js 文件为 4.75MB
【发布时间】:2017-09-26 08:07:42
【问题描述】:

我将 Angular2 用于我的 J2EE 应用程序(Spring MVC)。我已经实现了延迟加载,但最初加载仍然需要更多时间(大约 2 分钟!!!)。

正在加载两个大文件:
typescript.js:4.75 MB
compiler.umd.js:0.98 MB

并且 typescript.js 文件没有缓存。每次重新加载它的下载。

我的 systemjs.config.js 文件是:

System
        .config({
            transpiler : 'ts',
            typescriptOptions : {
                "target" : "es5",
                "module" : "commonjs",
                "moduleResolution" : "node",
                "sourceMap" : true,
                "emitDecoratorMetadata" : true,
                "experimentalDecorators" : true,
                "lib" : [ "es2015", "dom" ],
                "noImplicitAny" : true,
                "suppressImplicitAnyIndexErrors" : true
            },
            meta : {
                'typescript' : {
                    "exports" : "ts"
                }
            },
            paths : {
                'npm:' : 'uploads/node_modules/'
            },
            map : {
                'app' : 'app',
                '@angular/animations' : 'npm:@angular/animations/bundles/animations.umd.js',
                '@angular/animations/browser' : 'npm:@angular/animations/bundles/animations-browser.umd.js',
                '@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/animations' : 'npm:@angular/platform-browser/bundles/platform-browser-animations.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/router/upgrade' : 'npm:@angular/router/bundles/router-upgrade.umd.js',
                '@angular/forms' : 'npm:@angular/forms/bundles/forms.umd.js',
                '@angular/upgrade' : 'npm:@angular/upgrade/bundles/upgrade.umd.js',
                '@angular/upgrade/static' : 'npm:@angular/upgrade/bundles/upgrade-static.umd.js',


                // other libraries using node_modules
                'rxjs' : 'npm:rxjs',
                'angular-in-memory-web-api' : 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
                'ts' : 'npm:plugin-typescript/lib/plugin.js',
                'typescript' : 'npm:typescript/lib/typescript.js',
                'angular2-datatable' : 'npm:angular2-datatable',
                'lodash' : 'npm:lodash/lodash.js',
                'angular2-datatable-pagination' : 'npm:angular2-datatable-pagination',
                'jquery' : 'lib/jquery/jquery-3.2.0.min.js',
                'ngx-pagination' : 'lib/ngxpagination/ngx-pagination.js',

            },
            packages : {
                app : {
                    defaultExtension : 'ts'
                },
                rxjs : {
                    defaultExtension : 'js'
                },
                'angular2-datatable' : {
                    main : 'index.js',
                    defaultExtension : 'js'
                }
            },
            "scripts" : [ "lib/jquery/jquery-3.2.0.min.js" ]
        });

有什么办法可以减小这两个文件(typescript.js,compiler.umd.js)的大小?

它是一个电子商务应用程序。所以我应该解决这个问题。请分享此问题的任何解决方案。

谢谢

【问题讨论】:

  • 你说它不是缓存,是选择的吗?或者你的客户问了什么?如果缓存可能会更快。
  • 感谢您的快速回复!客户将此报告为问题。缓存不能解决我的问题。加载需要 2 分钟。有什么办法可以减小 typescript.js 文件的大小?

标签: javascript node.js angular angular2-routing angular2-template


【解决方案1】:

我强烈建议您将 Angular 2 项目更新为 Angular 4 项目。 Angular 4 的特点之一是它最多可以缩小 60%。 Angular 2 和 4 是兼容的,有一些东西会破坏,比如 typeScript 和 rxjs,它们也需要更新。这需要大约 2 或 3 个小时才能完成。

您也可以使用Ahead Of Time Compiler 进行编译,这也会提高一点速度。

Here is an article to update to angular 4 正如文章所说,您可以运行命令:

(视窗)

npm install @angular/common@latest @angular/compiler@latest @angular/compiler-cli@latest @angular/core@latest @angular/forms@latest @angular/http@latest @angular/平台浏览器@latest @angular/platform-b​​rowser-dynamic@latest @angular/platform-server@latest @angular/router@latest @angular/animations@latest typescript@latest --save

(Linux)

npm install @angular/{common,compiler,compiler-cli,core,forms,http,platform-b​​rowser,platform-b​​rowser-dynamic,platform-server,router,animations}@latest typescript@latest --save

一切顺利!

【讨论】:

  • 非常感谢 KeaganFouche
【解决方案2】:

嗯,据我所知,你不能缩小 TypeScript。但是您可以在此处找到其他解决方案:

Is it possible to compile typescript into minified code?

但您还应该检查您的代码,并清理所有未使用的方法、导入...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多