<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Greg Magolan on Medium]]></title>
        <description><![CDATA[Stories by Greg Magolan on Medium]]></description>
        <link>https://medium.com/@gregmagolan?source=rss-e68e18577060------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*w0lUXSjpvP2FuupwkoJe3w@2x.jpeg</url>
            <title>Stories by Greg Magolan on Medium</title>
            <link>https://medium.com/@gregmagolan?source=rss-e68e18577060------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 07:52:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gregmagolan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Code-splitting and Lazy-loading with Bazel]]></title>
            <link>https://medium.com/@gregmagolan/code-splitting-and-lazy-loading-with-bazel-9b11ac4de20b?source=rss-e68e18577060------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b11ac4de20b</guid>
            <category><![CDATA[rollup]]></category>
            <category><![CDATA[bazel]]></category>
            <category><![CDATA[lazy-loading]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Greg Magolan]]></dc:creator>
            <pubDate>Mon, 10 Sep 2018 23:53:07 GMT</pubDate>
            <atom:updated>2018-09-25T03:13:47.349Z</atom:updated>
            <content:encoded><![CDATA[<p>In March, I <a href="https://medium.com/@gregmagolan/production-code-splitting-with-bazel-8a7da242bf83">wrote</a> about an unreleased experimental feature in the rules_nodejs rollup_bundle Bazel rule which allowed for code-splitting using the recently added code-splitting feature in <a href="https://github.com/rollup/rollup/pull/1841">rollup</a>. I’m happy to announce that this feature has now matured and has been released in <a href="https://github.com/bazelbuild/rules_nodejs">rules_nodejs</a> 0.12.3.</p><p>In this post we’ll go over how the new code-splitting feature of rollup_bundle works. We’ll also take a look at how this new feature is used in the canonical <a href="https://github.com/alexeagle/angular-bazel-example">angular-bazel-example</a> to setup lazy loaded routes in an Angular application.</p><p><a href="https://bazel.build/"><em>Bazel</em></a><em> is a Google open-source project. It is a fast, scalable, multi-language build system that is used to build nearly all of Google’s applications.</em></p><h4><strong>What is code-splitting?</strong></h4><p>Code-splitting allows you to split application bundle into multiple chunks. This approach can reduce the load time of your application significantly by only loading the chunks that are required for your application to start and allowing the remaining chunks to be loaded later, either lazily, when the user navigates to a route for which they are needed, or eagerly, in the background before they are needed.</p><h4>Documentation</h4><p>The Bazel documentation for the rollup_bundle rule can be found here: <a href="https://bazelbuild.github.io/rules_nodejs/rollup/rollup_bundle.html#rollup_bundle">https://bazelbuild.github.io/rules_nodejs/rollup/rollup_bundle.html#rollup_bundle</a></p><p>The canonical angular-bazel-example, which demonstrates how to use the rollup_bundle rule to setup lazy loaded Angular routes, is found here: <a href="https://github.com/alexeagle/angular-bazel-example">https://github.com/alexeagle/angular-bazel-example</a></p><h4>Configuration</h4><p>To enable code-splitting in the rollup_bundle rule, you simply specify one or more additional_entry_points in the rule.</p><pre>rollup_bundle(<br>    name = &quot;bundle&quot;,<br>    entry_point = &quot;src/main&quot;,<br>    additional_entry_points = [<br>        &quot;src/foo&quot;,<br>        &quot;src/bar&quot;,<br>    ],<br>    deps = [&quot;//src&quot;],<br>)</pre><p>The entry_point should be the main entry point to your application and additional_entry_points are the additional entry points that rollup will turn into separate entry chunks. Rollup will analyze all off the entry points specified and create an entry chunk for each one as well as one or more common chunks depending on how much shared code there is between the entry points.</p><p>The output of the rollup_bundle rule with code-splitting enabled is a number of folders which contain different variants of the code-split bundles in CommonJS format. The folder names start with the target name. For example, if the target name is bundle then the folders outputted would be:</p><ul><li>bundle.cs.es6 (es6)</li><li>bundle.cs (es5 downlevelled from es6 output)</li><li>bundle.cs.min (es5 minified with uglify)</li><li>bundle.cs.min_debug (es5 minified with uglify with debug flags turned on)</li></ul><p>The es6 variant is outputted by rollup and the remaining variants are derived from that output. Each variant will contain the entry point chunks, one or more common chunks and the corresponding source maps. In the above configuration, with the entry points src/main, src/foo and src/bar, the following files would be outputted to each variant folder:</p><ul><li>main.js</li><li>main.js.map</li><li>foo.js</li><li>foo.js.map</li><li>bar.js</li><li>bar.js.map</li><li>chunk-&lt;hash&gt;.js</li><li>chunk-&lt;hash&gt;.js.map</li></ul><p>The number of common chunk files, which are named chunk-&lt;hash&gt;.js, is variable and they are uniquely named with a hash of their content.</p><p>In addition to the code-split bundles, the rollup_bundle rule will also output a corresponding SystemJS boilerplate &amp; bootstrap file for each variant. These files configure the SystemJS map of entry points and bootstraps the main entry point. They are also named after the target name:</p><ul><li>bundle.es6.js</li><li>bundle.js</li><li>bundle.min.js</li><li>bundle.min_debug.js</li></ul><p>These scripts are optional to use as you can configure SystemJS yourself or use another loader in the browser. They are convenient as single scripts that can be loaded to configure SystemJS and bootstrap your code-split application:</p><pre>&lt;html&gt;<br>  &lt;head&gt;...&lt;/head&gt;<br>  &lt;body&gt;<br>    ...<br>    &lt;!-- Note: system.js must be loaded before bundle.min.js --&gt;<br>    &lt;script src=&quot;<a href="https://cdnjs.cloudflare.com/.../system.js">https://cdnjs.cloudflare.com/.../system.js</a>&quot;&gt;&lt;/script&gt;<br>    &lt;script src=&quot;/bundle.min.js&quot;&gt;&lt;/script&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><p>If the main entry_point is src/main and the additional_entry_points are src/foo and src/bar as shows above, then bundle.min.js will look like so:</p><pre>// SystemJS boilerplate/bootstrap for code-split rollup_bundle.<br>// GENERATED BY Bazel<br>(function(global) {<br>System.config({<br>  packages: {<br>    &#39;&#39;: {<br>      map: {<br>        &#39;./main&#39;: &#39;bundle.cs.min/main&#39;,<br>        &#39;./foo&#39;: &#39;bundle.cs.min/foo&#39;,<br>        &#39;./bar&#39;: &#39;bundle.cs.min/bar&#39;},<br>      defaultExtension: &#39;js&#39;,<br>      },<br>  }<br>});<br>System.import(&#39;main&#39;).catch(function(err) {<br>  console.error(err);<br>});<br>})(this);</pre><h4><strong>Lazy-loading with Angular</strong></h4><p>The canonical <a href="https://github.com/alexeagle/angular-bazel-example">angular-bazel-example</a> shows how to use rollup_bundle to generate code-split bundles that can be lazy-loaded using the Angular router.</p><p>For an Angular application, the additional_entry_points for the rollup_bundle rule are the ngfactory files of the modules corresponding to the lazy-loaded routes. In angular-bazel-example, these are src/hello-world/hello-world.module.ngfactory and src/todos/todos.module.ngfactory:</p><pre>rollup_bundle(<br>    name = &quot;bundle&quot;,<br>    additional_entry_points = [<br>        &quot;src/hello-world/hello-world.module.ngfactory&quot;,<br>        &quot;src/todos/todos.module.ngfactory&quot;,<br>    ],<br>    entry_point = &quot;src/main&quot;,<br>    deps = [&quot;//src&quot;],<br>)</pre><p>The main entry point src/main contains the Angular bootstrap code:</p><pre>import {enableProdMode} from &#39;<a href="http://twitter.com/angular/core">@angular/core</a>&#39;;<br>import {platformBrowser} from &#39;<a href="http://twitter.com/angular/platform-browser">@angular/platform-browser</a>&#39;;<br>import {AppModuleNgFactory} from &#39;./app.module.ngfactory&#39;;</pre><pre>enableProdMode();<br>platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);</pre><p>The production server is started with yarn serve-prod. When this is run, Bazel will build the code-split bundles and run the production server, which listens on port 8080.</p><pre>history-server listening on port 8080; Ctrl+C to stop</pre><p>The index.html file served in angular-bazel-example is /src/index.html. This html file is common in development and production modes and looks as follows:</p><pre>&lt;!doctype html&gt;<br>&lt;html&gt;<br>  &lt;head&gt;<br>    &lt;title&gt;Angular Bazel Example&lt;/title&gt;<br>    &lt;base href=&quot;/&quot;&gt;<br>  &lt;/head&gt;<br>  &lt;body&gt;<br>    &lt;app-component&gt;&lt;/app-component&gt;<br>    &lt;script src=&quot;/zone.min.js&quot;&gt;&lt;/script&gt;<br>    &lt;script src=&quot;/system.js&quot;&gt;&lt;/script&gt;<br>    &lt;script src=&quot;/bundle.min.js&quot;&gt;&lt;/script&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><p>Notice that we load /bundle.min.js, which contains the SystemJS boilerplate and bootstrap for the application:</p><pre>// SystemJS boilerplate/bootstrap for code-split rollup_bundle.<br>// GENERATED BY Bazel<br>(function(global) {<br>System.config({<br>  packages: {<br>    &#39;&#39;: {<br>      map: {<br>        &quot;./main&quot;: &quot;bundle.cs.min/main&quot;,<br>        &quot;./hello-world/hello-world.module.ngfactory&quot;: &quot;bundle.cs.min/hello-world.module.ngfactory&quot;,<br>        &quot;./todos/todos.module.ngfactory&quot;: &quot;bundle.cs.min/todos.module.ngfactory&quot;,<br>        },<br>      defaultExtension: &#39;js&#39;,<br>    },<br>  }<br>});<br>System.import(&#39;main&#39;).catch(function(err) {<br>  console.error(err);<br>});<br>})(this);</pre><p>After starting the production server, when you navigate to <a href="http://localhost:8080/">http://localhost:8080</a> you’ll see that the browser first loads zone.min.js and system.js. These scripts are required for the application to run. It then loads the bundle.min.js script which contains the SystemJS boilerplate and bootstrap code. The System.import(‘main’) line in bundle.min.js will cause the main entry point chunk, main.js, to be loaded. The main entry point will load the two common chunks that it requires (chunk-d4b9cc84.js and chunk-acf34f7e.js). Angular will then be bootstrapped in main.js and the Angular router will the lazy load the / route, which loads the hello-world.module.ngfactory.js entry chunk and the common chunk (chunk-8d93dd18.js1) that it requires.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/889/1*ucc2y2fFATFkv0_DrmF_lQ.png" /></figure><p><em>Note: In an real application you wouldn’t want to lazy load your main route (</em><em>/) but this is done here for demonstration purposes.</em></p><p><em>A</em>t this point the application has everything is needs to run the main page, but it hasn’t yet loaded the chunks for the /todos route. Only when you click on the Todos link does Angular request the todos.module.ngfactory.js script required for that route. At this point all common chunks have already been loaded so only the todos.module.ngfactory.js script is required for the /todos route. Once the todos entry script is loaded, Angular changes the current route to /todos and the Todos page is displayed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/885/1*zfF_fwihHu0wd3_xKaDGLg.png" /></figure><p><em>Note: The the behavior is different if you manually change the URL to </em><a href="http://localhost:8080/todos"><em>http://localhost:8080/todos</em></a><em> instead of clicking on the link which changes the route in Angular. In this case, the browser treats this as a full reload instead of an Angular route change.</em></p><h4>Not yet supported in development mode</h4><p>Code-splitting is not yet available in development mode using Bazel. The development server rule, ts_devserver, in <a href="https://github.com/bazelbuild/rules_typescript">rules_typescript</a> does not yet support code-splitting and does not serve a code-split bundle.</p><p>Development mode requires a fast RTT (round-trip-time) from when a developer makes a change to a source file to when the application is reloaded in the browser. The RTT should be short and proportional to the size of the change. Our intent is that the median RTT is less than two seconds, even for a large application. ts_devserver uses in-memory, on-the-fly bundling to generate the development bundle in order to accomplish this. Getting code-splitting to be fast and proportional to the size of the change in development mode is challenging and we have not developed a solution to this problem to date.</p><p><a href="https://github.com/alexeagle/angular-bazel-example">angular-bazel-example</a> shows how to setup an Angular application with Bazel which has a production build that makes use of code-splitting and lazy-loading and development build which does not.</p><h4>Bazel vs. Rollup vs. Webpack</h4><p>Bazel is a multi-language and multi-tool build system that can work with both rollup and webpack. An action in Bazel can run rollup, uglify, webpack, the typescript compiler or any other tool you would like it to using custom rules.</p><p>Webpack could be used very effectively with Bazel to bundle applications and perform code-splitting. With the webpack DLL plugin, webpack could even be used with Bazel to allow for the time to generate a production bundle to be proportional to the size of the change since the last build. This is something we are already thinking about at Angular labs.</p><h4>Bazel vs. Angular CLI</h4><p>Right now you can either use Bazel to build your Angular application or the Angular CLI. The two don’t work together very well.</p><p>At Angular labs, we are currently laying down the groundwork for the future so we can use Bazel under the hood of the Angular CLI. Angular CLI users will get all the benefits of the Angular CLI combined with the build performance improvements of Bazel. Stay tuned for more on this in the future.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b11ac4de20b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Production Code Splitting With Bazel]]></title>
            <link>https://medium.com/@gregmagolan/production-code-splitting-with-bazel-8a7da242bf83?source=rss-e68e18577060------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a7da242bf83</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[bazel]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[lazy-loading]]></category>
            <category><![CDATA[rollup]]></category>
            <dc:creator><![CDATA[Greg Magolan]]></dc:creator>
            <pubDate>Sat, 10 Mar 2018 00:39:39 GMT</pubDate>
            <atom:updated>2018-09-10T23:10:41.055Z</atom:updated>
            <content:encoded><![CDATA[<p>Rollup has recently added an experimental code splitting feature as of version 0.55.0 (<a href="https://github.com/rollup/rollup/pull/1841">https://github.com/rollup/rollup/pull/1841</a>). I updated the <a href="https://bazel.build/">Bazel</a> rules_nodejs production bundling rollup_bundle rule to make use of this feature and this blog post describes how this rule works with code splitting.</p><p>Code splitting allows you to split application code into various bundles. These can be loaded on demand (lazy loaded) or in parallel. This approach can reduce the load time of your application significantly. When used with Angular, as in this demonstration, the parts of your application that are not needed during the initial load can be lazy loaded when the user navigates to a specific routes or pre-loaded in the background after the initial application is started.</p><p><a href="https://bazel.build/"><em>Bazel</em></a><em> is a Google open-source project. It is a fast, scalable, multi-language build system that is based on Google’s internal build system used to build nearly all of Google’s applications.</em></p><p><strong>Disclaimer</strong></p><p>Code splitting with rollup_bundle is currently meant as a demonstration only as it has not yet been released and breaking changes will likely be made before it is. Further, the development server, ts_devserver, rule does not yet support code splitting so making use this rule for code splitting will mean that you’ll be debugging on the rollup_bundle output unless you use a custom development server. Code splitting support for ts_devserver is currently in the design phase and will be coming in the future.</p><p><strong>Code</strong></p><p>The demonstration is built on a fork of Alex Eagle’s canonical angular-bazel-example on this branch: <a href="https://github.com/gregmagolan/angular-bazel-example/tree/rollup-code-splitting">https://github.com/gregmagolan/angular-bazel-example/tree/rollup-code-splitting</a>.</p><p>The updated rollup_bundle rule is pulled from my fork of rules_nodejs: <a href="https://github.com/gregmagolan/rules_nodejs/tree/rollup-code-splitting">https://github.com/gregmagolan/rules_nodejs/tree/rollup-code-splitting</a>.</p><p><strong>Configuration</strong></p><p>The configuration for the rollup_bundle rule does not change much for code splitting.</p><p>Previously it was:</p><pre>rollup_bundle(<br>    name = &quot;bundle&quot;,<br>    entry_point = &quot;src/main&quot;,<br>    deps = [&quot;//src&quot;],<br>)</pre><pre>nodejs_binary(<br>    name = &quot;prodserver&quot;,<br>    args = [&quot;./src&quot;],<br>    data = [<br>        &quot;index.html&quot;,<br>        &quot;:bundle&quot;,<br>        &quot;:zone.js&quot;,<br>    ],<br>    entry_point = &quot;http-server/bin/http-server&quot;,<br>)</pre><p>Rollup would produce a minified bundle file and the prodserver target would serve it and all other files required using http-server. You can try out the current rollup_bundle without code splitting on Alex Eagle’s canonical <a href="https://github.com/alexeagle/angular-bazel-example">angular-bazel-example</a>.</p><p>With code splitting, the configuration is updated as follows:</p><pre>rollup_bundle(<br>    name = &quot;bundle&quot;,<br>    index_html_template = &quot;index.template.html&quot;,<br>    entry_points = [<br>        &quot;src/main&quot;,<br>        &quot;src/foo/foo.module.ngfactory&quot;,<br>    ],<br>    deps = [&quot;//src&quot;],<br>)</pre><pre>nodejs_binary(<br>    name = &quot;prodserver&quot;,<br>    args = [&quot;src&quot;],<br>    data = [<br>        &quot;:bundle&quot;,<br>        &quot;:zonejs&quot;,<br>    ],<br>    entry_point = &quot;history-server/modules/cli.js&quot;,<br>)</pre><p>You’ll notice that entry_point has been renamed to entry_points and now takes an array of entry points, with the first being the initial entry point for the application. Also, index.html is no longer a static file provided to prodserver, but rather a templated file. The rollup_bundle rule will expand this template with the necessary systemjs configuration required for code splitting and output an index.html file that is served by the prodserver target.</p><p>The lazy loaded entry points (src/main, src/foo/foo.module.ngfactory) need to be configured manually in the rollup_bundle rule for the time being. In the future, these will be automatically derived from metadata generated at build time.</p><p>The prodserver target has also been updated to use history-server instead of http-server as this supports lazy loading with the Angular router.</p><p><strong>Usage</strong></p><p>On my rollup-code-splitting branch of angular-bazel-example, the production server is started with bazel run src:prodserver.</p><p>Bazel will build the code split bundles and run the prodserver, which listens on port 8080.</p><pre>INFO: Running command line: dist/bin/src/prodserver src<br>history-server listening on port 8080; Ctrl+C to stop</pre><p>In Chrome devtools, when you navigate to <a href="http://localhost:8080/">http://localhost:8080</a>, you’ll see that the browser loads zone.min.js and system.js. These scripts are required for the application to run. It then loads main.js script, which is the main entry point to the application which was bundled by rollup. main.js will request the common chunk that it requires, chunk1.js, which is loaded as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/759/0*EuHyo5gP_JQQYVrQ." /></figure><p>At this point the application has everything is needs to run the main page but it hasn’t yet loaded the script for the `/foo` route. Only when you click on the Foo link does Angular request the foo.module.ngfactory.js script required for that route. The browser then loads foo.module.ngfactory.js and Angular navigates to the /foo route. The foo.module.ngfactory.js bundle also requires the common chunk chunk1.js but since it has already been downloaded by the browser it is not requested.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/0*Robm06ube966JdgG." /></figure><p>Note that the behavior is different if you manually change the URL to <a href="http://localhost:8080/foo">http://localhost:8080/foo</a> instead of clicking on the Foo link which tell Angular to change the route to /foo. In this manual case, the browser does a full reload and all the scripts are reloaded in the same order and the application is refreshed.</p><p><strong>Deep Dive</strong></p><p><strong>Lazy Loading Configuration in index.html</strong></p><p>The templated index.template.html that is passed to rollup_bundle contains a TEMPLATED_rollup_scripts tag. The rollup_bundle rule expands this tag into:</p><pre>&lt;script src=&quot;/system.js&quot;&gt;&lt;/script&gt;<br>&lt;script&gt;<br>  (function (global) {<br>    System.config({<br>      packages: {<br>        &#39;&#39; : {<br>          map: {&quot;./main&quot;: &quot;bundles.es5_min/main&quot;, &quot;./foo/foo.module.ngfactory&quot;: &quot;bundles.es5_min/foo.module.ngfactory&quot;},<br>          defaultExtension: &#39;js&#39;<br>        },<br>      }<br>    });<br>  })(this);<br>&lt;/script&gt;<br>&lt;script&gt;<br>  System.import(&#39;main&#39;).catch(function(err){ console.error(err); });<br>&lt;/script&gt;</pre><p>It adds these script tags, which configure systemjs in the browser to load the lazy loaded bundles when they are requested by the application, to the generated index.html. The rollup_bundle rule also outputs the system.js file so it is available for the prodserver to serve.</p><p><strong>Rollup Configuration</strong></p><p>The rollup_bundle rule produces a few different types of code split bundles during the build. For the purposes of this demonstration, these are all outputted. In the future, only bundles used by downstream rules would be generated.</p><p>First rollup is run on the ES6 compiled application sources and it outputs ES6 code split bundles to the bundles.es6 folder in your bazel-out folder. Next typescript is run to downlevel these to ES5 into the bundles.es5 folder. Finally, uglify is run twice, once without--beautify and once with --beautify and it outputs into the bundles.es5_min and bundles.es5_min_debug folders respectively. The generated index.html is configured by the rule to use the bundles.es5_min output, which is non-debug minified production output.</p><p>The experimental rollup code splitting feature only supports the cjs output format presently, so the rollup output is in cjs.</p><p>Rollup does not currently provide control over the output filenames. These will correspond to the entry point filenames: main.js and foo.module.ngfactory.js in the angular-bazel-example demonstration. Rollup outputs common code between entry point bundles into one or more chunkX.js files. In the angular-bazel-example, there is only one common chunk: chunk1.js. In larger applications there would be more common chunks, with each entry point bundle loading only the common chunks that it requires.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a7da242bf83" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>