I'm currently bundling my Angular 2 app with WebPack. We are still spinning rapid cycles, so rather than adding delays to our build and application load process, we want to include the rarely-changing Angular 2 UMD CDN prepared bundles, e.g.:
<script src="https://npmcdn.com/@angular/core@2.0.0-rc.3/bundles/core.umd.min.js"></script>
<script src="https://npmcdn.com/@angular/common@2.0.0-rc.3/bundles/common.umd.min.js"></script>
<script src="https://npmcdn.com/@angular/compiler@2.0.0-rc.3/bundles/compiler.umd.min.js"></script>
- When I just let WebPack do its thing, the package runs fine but is a few MB, because it doesn't leverage pre-built bundles or separate out Angular 2 "vendor" code.
- When I use the Angular 2 WebPack Recommendations, e.g.:
plugins: [ new webpack.optimize.CommonsChunkPlugin("vendor", "vendor.bundle.js") ]
, my app bundle is small, but I manually build a separate, unique 1MB bundle containing most of the Angular 2 framework in it, every build. This file changes slightly each build depending on my application, and is not portable between versions of my applications or various applications, and doesn't have the benefit of "CDN". Of course I have to include this file for my app to run. - When I use the IgnorePlugin to filter
@angular|rxjs
, e.g.plugins: [ new webpack.IgnorePlugin(/\@angular|rxjs/) ]
, it excludes the vendor files, but inserts hard-coded exceptions / throws errors at the top of my application bundle. - When I use WebPack externals, e.g.
externals: ['@angular/core', ...
, I getfunction(module, exports) { module.exports = @angular/core; },
output in my app bundle, which obviously isn't functional. The WebPack documentation isn't terribly forthcoming, but I think I may be able to either specify alibraryTarget
or quoted resolve function, which would instruct WebPack to compile in module loading. - When I ditch WebPack altogether and use instead the TypeScript compiler bundler (as per this guide, which uses the UMD bundles), I get
System.register()
calls referring to the NPM namespaces I expected, e.g.System.register("myapp/boot", ['@angular/core', ...
, but I'm still working on SystemJS configuration to call the UMDs. As a side note, this file is an extra 25% in size relative to what WebPack is generating. - If I use SystemJS as in the prior item, I want this compilation to occur during build, or as a parallel process, rather than as part of file save. I guess SystemJS-Builder (see related questions here and here) would be the way to do this? Perhaps this also would produce smaller file sizes that the Typescript-integrated "bundler".
How can I build an application bundle that doesn't depend on a uniquely-repackaged Angular 2 bundle?
I'm currently building against RC3. My process is currently WebPack, as mentioned above, but I would move to another toolset if that makes it easier.
Doing some more research, I think I've been misled by WebPack's "Loader" terminology. I have to use a module loader, and it doesn't look like WebPack has one that will work for this.
To assign UMD bundles module namespaces (and wire up dependents) they can't be loaded in script tags. Instead they have to be evaluated with a given this
context to act as the module reference. That means that even if I want them all loaded synchronously, I still have to configure something else like SystemJS to load them over the wire, so their context is controlled/wrapped.
This Angular 2 plunker is near what I'm looking for. It uses the Angular 2 UMD bundles, but doesn't use an RxJs bundle, although that looks easy enough to change if I want the entire RxJs library.