如何将Angular2 TypeScript应用程序编译为单个文件? [英] How to compile an Angular2 TypeScript application to a single file?

查看:145
本文介绍了如何将Angular2 TypeScript应用程序编译为单个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到我可以使用tsc my_app.ts --target system编译我的应用程序,它将为每个导入的模块生成一个SystemJS包装的文件,这很了不起,但是它会生成匿名(无名)函数,因此我不能只是将它们串联起来到一个文件.

I realize that I can compile my application with tsc my_app.ts --target system and it will generate a SystemJS-wrapped file for each imported module, which is awesome, but it generates anonymous (nameless) functions, so I can't just concatenate them to a single file.

我曾想过要问这个问题如何将TypeScript编译为命名的SystemJS模块",但我的目标只是将Angular2应用编译为单个文件,无论是否为SystemJS.

I thought about making this question "how to compile TypeScript to named SystemJS modules", but my goal is just to compile my Angular2 app to a single file, SystemJS or not.

推荐答案

从TypeScript 1.8开始,--outFile选项与--module选项(对于AMD和SystemJS)一起使用,因此可以本地完成. 此处是更改日志. TypeScript还会自动提取不存在于外部模块中的所有依赖项,因此仅编译主应用程序文件就足够了.

The --outFile option works with the --module option (for AMD and SystemJS) since TypeScript 1.8, so this can be done natively. Here's the changelog. TypeScript also automatically pulls all the dependencies that don't reside in external modules, so it's enough to only compile the main app file.

如果Angular 2并未脱离SystemJS,将应用程序捆绑到一个文件中的方法应该像使用tsc app.ts --target ES5 --module system --experimentalDecorators --moduleResolution node --emitDecoratorMetadata --outFile app.js

If Angular 2 doesn't switch away from SystemJS, the way to bundle your app in a single file should be as simple as compiling it with options like tsc app.ts --target ES5 --module system --experimentalDecorators --moduleResolution node --emitDecoratorMetadata --outFile app.js

之后,应该可以使用以下方式引导应用程序:

After that it should be possible to bootstrap the application with something like:

<script src="/bower_components/system.js/dist/system-register-only.js"></script>
<script src="/node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="/app.js"></script>
<script>
    System.import('app');
</script>

这篇关于如何将Angular2 TypeScript应用程序编译为单个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆