gpt4 book ai didi

typescript - 如何将 Angular2 TypeScript 应用程序编译为单个文件?

转载 作者:太空狗 更新时间:2023-10-29 17:06:40 25 4
gpt4 key购买 nike

我意识到我可以使用 tsc my_app.ts --target system 编译我的应用程序,它会为每个导入的模块生成一个 SystemJS 包装的文件,这很棒,但它会生成匿名 (无名) 函数,所以我不能将它们连接到一个文件中。

我想提出这个问题“如何将 TypeScript 编译为命名的 SystemJS 模块”,但我的目标只是将我的 Angular2 应用程序编译为单个文件,无论是否为 SystemJS。

最佳答案

自 TypeScript 1.8 以来,--outFile 选项与 --module 选项(适用于 AMD 和 SystemJS)一起使用,因此这可以在 native 完成。 Here's the changelog . TypeScript 还会自动拉取所有不驻留在外部模块中的依赖项,因此只需编译主应用程序文件就足够了。

如果 Angular 2 没有从 SystemJS 切换出去,那么将应用程序捆绑在单个文件中的方法应该像使用 tsc app.ts --target ES5 --module system - 这样的选项编译它一样简单。 -experimentalDecorators --moduleResolution node --emitDecoratorMetadata --outFile app.js

之后应该可以使用类似以下内容来引导应用程序:

<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>

关于typescript - 如何将 Angular2 TypeScript 应用程序编译为单个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33201005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com