<?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 Hiep Tran on Medium]]></title>
        <description><![CDATA[Stories by Hiep Tran on Medium]]></description>
        <link>https://medium.com/@hiepxanh?source=rss-f6562d19f273------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*FNOLc3m35kIKwgPp.</url>
            <title>Stories by Hiep Tran on Medium</title>
            <link>https://medium.com/@hiepxanh?source=rss-f6562d19f273------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 17:34:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hiepxanh/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[Angular with Vite is crazy fast (Angular 16 Example)]]></title>
            <link>https://medium.com/@hiepxanh/angular-vite-example-is-crazy-fast-3ee4d730020c?source=rss-f6562d19f273------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ee4d730020c</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[vitejs]]></category>
            <dc:creator><![CDATA[Hiep Tran]]></dc:creator>
            <pubDate>Wed, 19 Oct 2022 09:05:38 GMT</pubDate>
            <atom:updated>2023-09-17T03:07:16.888Z</atom:updated>
            <content:encoded><![CDATA[<p>updated: 2023/09/17</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*wmiHccq6imyMUakUZV_f6g.png" /><figcaption>Angular with Vite example tutorial with Angular 15</figcaption></figure><p>Since angular 16 release <a href="https://blog.angular.io/angular-v16-is-here-4d7a28ec680d">https://blog.angular.io/angular-v16-is-here-4d7a28ec680d</a><br>All you need to do is very simple, just change angular-cli build command and done:</p><pre>...<br>&quot;architect&quot;: {<br>  &quot;build&quot;: {<br>    &quot;builder&quot;: &quot;@angular-devkit/build-angular:browser-esbuild&quot;,<br>...</pre><p>detail from docs here: <a href="https://angular.io/guide/esbuild">https://angular.io/guide/esbuild</a></p><p>Below information is the custom Code in angular 15</p><p>Hello, I love Angular and I have worked from AngularJS to Angular 15 now. I always wonder how fast I can work with Angular and I see Vite out there with amazing speed. Let’s use Angular with Vite to push it to the limit.</p><p>example repo here if you are lazy:</p><p><a href="https://github.com/hiepxanh/angular-vite-example">https://github.com/hiepxanh/angular-vite-example</a></p><p>and Stackblitz if you are super lazy:</p><p><a href="https://stackblitz.com/github/hiepxanh/angular-vite-example">https://stackblitz.com/github/hiepxanh/angular-vite-example</a></p><p><strong>Getting Started</strong></p><p>pnpm init &amp;&amp; git init to create a new folder</p><p>pnpm i -D typescript@4.8.4 vite@4.3.3 to make my vanilla project work</p><p>add HTML file now at ./index.html:</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;head&gt;<br>&lt;meta charset=&quot;UTF-8&quot; /&gt;<br>&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;<br>&lt;title&gt;Vite + Angular&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>&lt;div id=&quot;app&quot;&gt;&lt;/div&gt;<br>&lt;app-root&gt;&lt;/app-root&gt;<br>&lt;script type=&quot;module&quot; src=&quot;/src/main.ts&quot;&gt;&lt;/script&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>add ./src/main.ts file now:</p><pre>document.querySelector&lt;HTMLDivElement&gt;(&#39;#app&#39;)!.innerHTML = &#39;Hello world&#39;;</pre><p>enjoy your Vite</p><pre>pnpm vite</pre><p>That it’s, elegant and simple.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/297/1*cC8ZFNVZdAZYWbSi74o40A.png" /></figure><p><strong>Add Angular To Vite</strong></p><p>we need Vite to work with Angular. Since Angular 14, we have had a standalone component that allows us to make Angular work with any kind of framework or even vanilla javascript. Let’s build it</p><pre>pnpm i @angular/compiler@16.0.0-rc.3 @angular/platform-browser@16.0.0-rc.3 @angular/core@16.0.0-rc.3 zone.js@0.13.0 rxjs@7.5.7</pre><p>update your src/main.ts to use angular:</p><pre>import &quot;@angular/compiler&quot;;<br>import &quot;zone.js&quot;;<br>import { bootstrapApplication } from &#39;@angular/platform-browser&#39;;<br>import { AppComponent } from &#39;./app.component&#39;;<br>bootstrapApplication(AppComponent);</pre><p>let’s add /src/app.component.ts now:</p><pre>import { ChangeDetectionStrategy, Component } from &#39;@angular/core&#39;;<br>@Component({<br>selector: &#39;app-root&#39;,<br>standalone: true,<br>imports: [],<br>template: `<br>&lt;p&gt; hello, this is angular and vite &lt;/p&gt;<br>`,<br>styles: [`<br>:host {<br>display: block;<br>}`,<br>],<br>changeDetection: ChangeDetectionStrategy.OnPush,<br>})<br>export class AppComponent {<br>constructor() {}<br>ngOnInit(): void {}<br>}</pre><p>remember, angular using @ as decorator, we need tsconfig.json to enable it, create ./tsconfig.js now</p><pre>{<br>&quot;compilerOptions&quot;: {<br>&quot;target&quot;: &quot;ESNext&quot;,<br>&quot;useDefineForClassFields&quot;: true,<br>&quot;module&quot;: &quot;ESNext&quot;,<br>&quot;lib&quot;: [&quot;ESNext&quot;, &quot;DOM&quot;],<br>&quot;moduleResolution&quot;: &quot;Node&quot;,<br>&quot;strict&quot;: true,<br>&quot;resolveJsonModule&quot;: true,<br>&quot;isolatedModules&quot;: true,<br>&quot;esModuleInterop&quot;: true,<br>&quot;noEmit&quot;: true,<br>&quot;noUnusedLocals&quot;: true,<br>&quot;noUnusedParameters&quot;: true,<br>&quot;noImplicitReturns&quot;: true,<br>&quot;skipLibCheck&quot;: true,<br>&quot;types&quot;: [&quot;vite/client&quot;], // for vite<br>&quot;experimentalDecorators&quot;: true, // for angular<br>},<br>&quot;include&quot;: [&quot;src&quot;]<br>}</pre><p>enjoy it again with pnpm vite</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*gfVg54CE5VkEuBZdGBCffg.png" /></figure><p>How about routing? ok let’s do it</p><p><strong>Add Angular Routing for Vite</strong></p><p>we need more packages here:</p><pre>pnpm i @angular/common@15.0.0-next.6 @angular/router@15.0.0-next.6</pre><p>create ./app.routing.ts to make us move:</p><pre>import { Route, provideRouter } from &#39;@angular/router&#39;;<br>const routes: Route[] = [<br>{<br>path: &#39;&#39;,<br>loadComponent: () =&gt; import(&#39;./home.component&#39;).then(c =&gt; c.HomeComponent)<br>},<br>{<br>path: &#39;about&#39;,<br>loadComponent: (): any =&gt; import(&#39;./about.component&#39;).then (c =&gt; c.AboutComponent)<br>}<br>];<br>export const appRouting = [<br>provideRouter(routes)<br>]</pre><p>then create ./src/home.component.ts and ./src/about.component.ts</p><pre>import { ChangeDetectionStrategy, Component } from &#39;@angular/core&#39;;<br>import { CommonModule } from &#39;@angular/common&#39;;<br>@Component({<br>selector: &#39;app-home&#39;,<br>standalone: true,<br>imports: [CommonModule],<br>template: `<br>&lt;p&gt; hello, this is home page &lt;/p&gt;<br>`,<br>})<br>export class HomeComponent {}</pre><p>then /src/about.component.ts file:</p><pre>import { ChangeDetectionStrategy, Component } from &#39;@angular/core&#39;;<br>import { CommonModule } from &#39;@angular/common&#39;;<br>@Component({<br>selector: &#39;app-about&#39;,<br>standalone: true,<br>imports: [CommonModule],<br>template: `<br>&lt;p&gt; hello, this is about us page &lt;/p&gt;<br>`,<br>})<br>export class AboutComponent {}</pre><p>That&#39;s good, keep moving and finish moving with an update on app.component.ts</p><pre>import { ChangeDetectionStrategy, Component } from &#39;@angular/core&#39;;<br>import { CommonModule } from &#39;@angular/common&#39;;<br>import { RouterLink, RouterOutlet } from &#39;@angular/router&#39;;<br>@Component({<br>selector: &#39;app-root&#39;,<br>standalone: true,<br>imports: [CommonModule, RouterOutlet, RouterLink],<br>template: `<br>&lt;p&gt; hello, this is angular, avite &lt;/p&gt;<br>&lt;a routerLink=&quot;/&quot; href=&quot;#&quot; class=&quot;text-xl font-bold no-underline hover:underline ...&quot;&gt;Home | &lt;/a&gt;<br>&lt;a routerLink=&quot;/about&quot; href=&quot;#&quot; class=&quot;text-xl font-bold no-underline hover:underline ...&quot;&gt;About us&lt;/a&gt;<br>&lt;hr&gt;<br>&lt;router-outlet&gt;&lt;/router-outlet&gt;<br>`,<br>styles: [<br>`<br>:host {<br>display: block;<br>}<br>`,<br>],<br>changeDetection: ChangeDetectionStrategy.OnPush,<br>})<br>export class AppComponent {}</pre><p>and another update on src/main.ts to make sure our route running:</p><pre>import &quot;@angular/compiler&quot;;<br>import &quot;zone.js&quot;;<br>import { bootstrapApplication } from &#39;@angular/platform-browser&#39;;<br>import { AppComponent } from &#39;./app.component&#39;;<br>import { appRouting } from &quot;./app.routing&quot;;<br>bootstrapApplication(AppComponent, {providers: [...appRouting]});</pre><p>ok let’s go:</p><pre>pnpm vite</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*GIQfEjTXZOhRqhS-LRTw5A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/294/1*q-0uusGPM26Xx_1J68gkEw.png" /></figure><p>goooooooodddd. Just a little Tailwind and you will have a beautiful angular</p><pre>pnpm install -D tailwindcss@3.1.8 postcss@8.4.18 autoprefixer@10.4.12 &amp;&amp; pnpx tailwindcss init -p</pre><p>update your /tailwind.config.js to:</p><pre>/** @type {import(&#39;tailwindcss&#39;).Config} */<br>module.exports = {<br>content: [<br>&quot;./index.html&quot;,<br>&quot;./src/**/*.{js,ts,jsx,tsx}&quot;,<br>],<br>theme: {<br>extend: {},<br>},<br>plugins: [],<br>}</pre><p>add /src/index.css like this:</p><pre>@tailwind base; @tailwind components; @tailwind utilities;</pre><p>then import it to main.ts :</p><pre>import &quot;@angular/compiler&quot;;<br>import &quot;zone.js&quot;;<br>import { bootstrapApplication } from &#39;@angular/platform-browser&#39;;<br>import { AppComponent } from &#39;./app.component&#39;;<br>import { appRouting } from &quot;./app.routing&quot;;<br>bootstrapApplication(AppComponent, {providers: [...appRouting]});<br>import &#39;./index.css&#39;</pre><p>and you are done</p><pre>pnpm vite</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/293/1*hi-WihN6ZJYYQ-oIhnvEZA.png" /></figure><p>Well, play.</p><p>Don&#39;t forget to add .gitignore /node_modules to have a beautiful commitment.</p><p>I’m currently working on my own. if you want to get help on your Angular Project feel free to contact me at: hiepxanh@gmail.com<br>I provide services:</p><ul><li>upgrade angular to the new version</li><li>angular universal for your project</li><li>move your Nestjs universal to the new version</li></ul><p>check my angular 15 websites here: <a href="https://awread.vn/">https://awread.vn</a><br>and my GitHub profile here: <a href="https://github.com/hiepxanh">https://github.com/hiepxanh</a></p><p>Please help me UPVOTE this issue, I tested vite with angular but it cannot work without zone, we need this to make Angular more close to Vite Eco system:</p><p><a href="https://github.com/angular/angular/issues/47538">Ability to pass `BootstrapOptions` when bootstrapping standalone application · Issue #47538 · angular/angular (github.com)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ee4d730020c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular 14 standalone component help reduce bundle size and the story behind you may not know]]></title>
            <link>https://medium.com/@hiepxanh/angular-14-standalone-component-help-reduce-bundle-size-and-the-story-behind-you-may-not-know-44ae866b8840?source=rss-f6562d19f273------2</link>
            <guid isPermaLink="false">https://medium.com/p/44ae866b8840</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[lazy-load]]></category>
            <category><![CDATA[standalone]]></category>
            <dc:creator><![CDATA[Hiep Tran]]></dc:creator>
            <pubDate>Mon, 23 May 2022 13:11:19 GMT</pubDate>
            <atom:updated>2022-06-18T02:26:06.985Z</atom:updated>
            <content:encoded><![CDATA[<h3>Angular 14 reduce bundle size and the story behind you may not know</h3><p>Hi, this is my experience on my fulltime project <a href="https://awread.vn./">https://awread.vn</a> which is using angular 13. This is my production website with 100 users daily. I’m working to migrate this to angular 14 and have discover this problem.</p><p>In this post, I will show you. Why main.js bundle is huge? why lazyload is not working? the Import Barrel index.ts problem ?. Then we will have solution for it.</p><h3><strong>The import barrel problem? Why your angular main.js is huge? how to reduce bundle size?</strong></h3><p>g-zip and ng build --prod is two solution that very useful to reduce main.js bundle size. That will help you alot. But what if, you did anything, tried your best. and it still not work? using lazyload module. still not work?</p><p>Here is the answer for you. Currently I’m upgrade my Angular 13 to Angular 14 project using nx and I find out the “barrel side effect” cause your main.js bundle increase. The solution is simple.</p><p>full explain is here <a href="https://webpack.js.org/guides/tree-shaking/#conclusion">https://webpack.js.org/guides/tree-shaking/#conclusion</a> and here <a href="https://github.com/angular/angular-cli/issues/16799">https://github.com/angular/angular-cli/issues/16799</a> and here <a href="https://stackoverflow.com/a/59353152/5748537">https://stackoverflow.com/a/59353152/5748537</a>. But I can make it short for you:</p><p>you have module “A is small”, and “B is Big”. you write them in UI library and export both of them in index.ts file. you import A in your project. final bundle still includes B which you dont want.</p><p>Why?</p><p>because commonjs is not smart. using es2020 is better, they understand which is use and which is not. But they not sure should it be remove or not. you add sideEffects: false to tell them: &quot;clear dead codeis safe&quot;. and they did it. Then your bundle is small. this can use in any modern JS framework include React, VueJS</p><p>read the issue in angular project about barrel side effect here: <a href="https://github.com/angular/angular-cli/issues/16799">https://github.com/angular/angular-cli/issues/16799</a></p><p>So I have a solution for you:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tcQIM9Fh5P54msqs3ETVrA.png" /></figure><ol><li>change your tsconfig.json build module from &quot;module&quot;: &quot;commonjs&quot; to &quot;module&quot;: &quot;es2020&quot; or esnext (you should use es2020 explain here <a href="https://angular.io/guide/migration-update-module-and-target-compiler-options">https://angular.io/guide/migration-update-module-and-target-compiler-options</a>)</li><li>add “sideEffects”: false to package.json</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BEXDCN2zS3eCte5ux_sBUg.png" /></figure><p>I can reduce 390kb for main.js bundle size just using sideEffects: false on production project using 613 component with design-system in mindset.</p><h3><strong>Lazyload is just not lazyload!</strong></h3><p>I have example in the image below. CoreModule is empty, You have TestModule which import something heavy like mat-datepicker. And you think you can reduce main.js bundle size by lazyload?</p><p>The answer is: NO.</p><p>Event if I use Angular 14 with routing loadComponent ?: NO it still not.</p><p>Why?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/973/0*rQC9jly1OVXZzUcO.png" /></figure><p>this was discuss in <a href="https://github.com/angular/angular-cli/issues/15787">https://github.com/angular/angular-cli/issues/15787</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qzTilEstOeNGiMwI4nkHvw.png" /></figure><p>So the explain is:</p><blockquote>That’s totally wrong assumption, code splitting is based on module, not value, so that @angular/core can only exist in one bundle, in this case main.js. Any lazy bundle could increase the non-tree-shakable parts of @angular/core, resulting in bigger @angular/core remaining, thus bigger main.js bundle size.</blockquote><blockquote>As long as your lazy module make any additional usage of @angular/core, @angular/common or rxjs, size of main.js would certainly increased, and the stats seems expected so far.</blockquote><p>What is the solution for you? Since the issue was closed and cannot comment. But it just not help me to reduce bundle size. Let see how we can fix it.</p><h3><strong>solution 1: only use library one time in one page…</strong></h3><blockquote>“You cannot import library in two location without increase main.js”</blockquote><p>Why? For example, mat-datepicker module import in lazyload route A and lazyload route B. how it should be?<br>webpack detect that it exist in both component. so it decide to put it in common place in this case is main.js. If you only use it in route B which is lazyload. It know that it should not put in main.js and decide to move datepicker to route B instead of common file. So we got the rule:</p><blockquote>webpack rule: “Any file will be move to main.js if it was import in two place”</blockquote><p>So, if you only use 3rd party library in one page only. it good, webpack and sideEffects false could move that code into lazyload module (or component in v14). Here is my profile page. it’s bundle increase from 151kb to 466kb. Since it using only datepicker.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/773/1*ZXRVO7hI1vpubYevD05IZQ.png" /></figure><p>But what if I have two lazy page, both of them are using datepicker?. ok, we should go to solution 2.</p><h3><strong>Solution 2: lazyload the component which is heavy</strong></h3><p>How it work and why I have to lazyload component?</p><p>If you import heavy lib like datepicker in two place, you will go to “webpack rule”. So what if we lazyload it? It mean you will not import anything and if nothing being import in the build time. it will not go to main.js bundle.</p><p>is that true? is that real? Yes. this image I import date picker in two difference place and lazyload it. Using angular 14 and standalone: true. The result of main.js with angular material reduce from 200kb to 39kb. Yes it true</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/991/1*1dDc09YjAEh58dViZJ-ZNg.png" /></figure><p>So how to do this? First wrap datepicker in a standalone component</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l9CSvvgUnwxI1f39rJrrRw.png" /></figure><p>Second, lazyload it:<br>```</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dec1dd81194f59b07c07150b54813d7d/href">https://medium.com/media/dec1dd81194f59b07c07150b54813d7d/href</a></iframe><p>here is the result. before, it stick with profile page</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jKJtvdpdc6Q_m2Zm8Tc-gg.png" /></figure><p>after it separate a single file instead mix with other and mix with profile page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LgmVn4BA-J9jPJxhA4HSug.png" /></figure><p>safe from main.js. safe from page. so we have very fast angular.</p><p>Welcome to angular 14. Thank you for reading my first article.</p><p>about me:</p><p>I have 7 years experience in Angular. And currently working on my fulltime project <a href="https://awread.vn/">https://awread.vn</a></p><p>contact me: hiepxanh@gmail.com or skype: live:hiepxanh if you need.<br>I’m looking for a remote job and I’m working at GMT+7 timezone. If you need someone help a hand on your Angular project. just DM me ❤</p><p>Have a good day, developer</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=44ae866b8840" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>