How can I concat TypeScript files with Angular2 like Rails assets:precompile?

て烟熏妆下的殇ゞ 提交于 2019-12-05 15:36:19

It sounds like you're looking for something like Webpack. Webpack will transpile your ts files to js and then create a single module bundle that you can include with a <script> tag. Not only will it transpile your code but it will also build in its dependencies including Angular 2 itself (and its dependencies).

Here's a simple starter project for Angular 2 and Webpack written by one of the Angular 2 developers: https://github.com/pkozlowski-opensource/ng2-webpack-play

It looks like typescript will support concatenating multiple files(with modules) from version 1.8 onwards(1.7.5 is current). There is also another way using webpack, but I don't know how it is done exactly.

I use JSPM to make my angular2 projects production ready. Other popular tools like JSPM include webpack, and browserfy. One of the important tasks that JSPM will accomplish is to bundle the various modules that make up your angular2 project. I also set the "selfExecutingBundle" flag to true and have JSPM make one bundled js file (e.g. myApp.bundled.js) and from there I minify it (myApp.bundled.min.js) and this one script reference is used in the index.html file.

<html>

<head>
    <title>Hello Angular World</title>
</head>
<body>
<div>
    <my-app>Loading...</my-app>
</div>
    <script src="/js/myApp.bundle.min.js"></script>
</body>

</html>

And that is all you need!

In the future when the HTTP/2 spec is more common, there may be less of a need to bundle your module-based projects, but for now I think bundling is needed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!