<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator><link href="https://blog.oatto.dev/feed.xml" rel="self" type="application/atom+xml" /><link href="https://blog.oatto.dev/" rel="alternate" type="text/html" /><updated>2026-10-24T15:19:25+00:00</updated><id>https://blog.oatto.dev/feed.xml</id><title type="html">Viva!</title><subtitle>Welcome to very cool blog which you will find a lot of things here. Especially about programming and my life ;)
</subtitle><entry xml:lang="th"><title type="html">สร้าง Skill ให้ AI ทำงานซ้ำได้ ด้วยพรอมป์ต์เดียว</title><link href="https://blog.oatto.dev/2026/10/24/create-ai-skill-with-prompt.html" rel="alternate" type="text/html" title="สร้าง Skill ให้ AI ทำงานซ้ำได้ ด้วยพรอมป์ต์เดียว" /><published>2026-10-24T00:00:00+00:00</published><updated>2026-10-24T00:00:00+00:00</updated><id>https://blog.oatto.dev/2026/10/24/create-ai-skill-with-prompt</id><content type="html" xml:base="https://blog.oatto.dev/2026/10/24/create-ai-skill-with-prompt.html"><![CDATA[<p style="text-align: center;">
    <img src="/assets/ai-skills/create-skill-with-prompt.png" alt="ภาพประกอบการสอน AI ให้ทำงานตามขั้นตอนด้วย Skill" />
</p>

<p>Skill คือชุดคำแนะนำที่บอก AI ว่าควรทำงานแบบไหนและทำตามขั้นตอนใด เช่น สรุปบทความหรือจัดรูปแบบรายงาน คำแนะนำหลักอยู่ในไฟล์ <code class="language-plaintext highlighter-rouge">SKILL.md</code> ส่วนไฟล์ตัวอย่างหรือสคริปต์จะเพิ่มเมื่อจำเป็นก็ได้ <a href="https://developers.openai.com/codex/skills/">เอกสาร OpenAI</a></p>

<p>ข้อดีคือไม่ต้องพิมพ์กติกาเดิมซ้ำทุกครั้งที่ให้ AI ทำงานแบบเดิม และยังช่วยให้งานที่ทำบ่อยมีแนวทางสม่ำเสมอขึ้น</p>

<p>ถ้าใช้ Codex และต้องการให้ Skill ใช้ได้ในโปรเจกต์เดียว ให้วางไว้ที่ <code class="language-plaintext highlighter-rouge">.agents/skills/&lt;ชื่อ-skill&gt;/SKILL.md</code> ของโปรเจกต์นั้น ๆ ส่วน Skill ส่วนตัวที่ใช้ข้ามโปรเจกต์อยู่ที่ <code class="language-plaintext highlighter-rouge">~/.agents/skills/&lt;ชื่อ-skill&gt;/SKILL.md</code></p>

<h2 id="สร้าง-skill-ด้วยพรอมป์ต์">สร้าง Skill ด้วยพรอมป์ต์</h2>

<p>ใช้ <code class="language-plaintext highlighter-rouge">$skill-creator</code> ใน Codex หรือ <code class="language-plaintext highlighter-rouge">@skill-creator</code> ใน ChatGPT Work แล้วอธิบายว่า Skill นี้มีไว้ทำอะไร ควรเริ่มใช้เมื่อไร และอยากได้คำตอบแบบไหน</p>

<p>ถ้าอยากให้ AI สรุปบทความภาษาไทยให้อ่านเร็ว ลองใช้พรอมป์ต์นี้ดูครับ:</p>

<div class="language-text highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$skill-creator
สร้าง Skill ชื่อ thai-article-summary

จุดประสงค์: สรุปบทความภาษาไทยให้อ่านเข้าใจเร็ว
ใช้เมื่อ: ผู้ใช้ส่งบทความหรือลิงก์และขอให้สรุป
ขั้นตอน: หาใจความหลัก แยกประเด็นสำคัญ และเก็บตัวเลขหรือชื่อเฉพาะให้ครบ
รูปแบบคำตอบ: สรุป 3–5 ข้อ ปิดท้ายด้วยใจความสำคัญหนึ่งประโยค
ข้อควรระวัง: อย่าแต่งข้อมูลเพิ่ม ถ้าบทความไม่มีข้อมูลให้บอกว่าไม่ระบุ

เริ่มจากคำแนะนำใน SKILL.md สั้น ๆ ก่อน ยังไม่ต้องเพิ่มสคริปต์
</code></pre></div></div>

<h2 id="ลองใช้แล้วค่อยปรับ">ลองใช้แล้วค่อยปรับ</h2>

<p>สร้างเสร็จแล้ว ลองส่งทั้งบทความสั้นและยาวให้ AI ดูครับ เช็กว่าเรียกใช้ Skill ตอนที่เหมาะ สรุปได้ครบ และไม่เติมข้อมูลที่ต้นฉบับไม่ได้บอกหรือเปล่า ถ้าผลยังไม่ตรงที่ต้องการ ก็ปรับคำแนะนำหรือเพิ่มตัวอย่าง แล้วลองอีกครั้ง</p>]]></content><author><name></name></author><summary type="html"><![CDATA[]]></summary></entry><entry xml:lang="en"><title type="html">Recap 2025</title><link href="https://blog.oatto.dev/2026/01/01/recap-2025.html" rel="alternate" type="text/html" title="Recap 2025" /><published>2026-01-01T00:00:00+00:00</published><updated>2026-01-01T00:00:00+00:00</updated><id>https://blog.oatto.dev/2026/01/01/recap-2025</id><content type="html" xml:base="https://blog.oatto.dev/2026/01/01/recap-2025.html"><![CDATA[<h1 id="hi">Hi,</h1>

<p>First of all, I’d like to say <strong>Happy new year!</strong> You are very strong that you can survive till now. And today, it is time for every year that we are writing the recap what we encountered last year. Let’s take a look at what the year was like for me!</p>

<h1 id="8-years-of-software-engineer">8 years of software engineer</h1>

<p>It is surprising that I have been working in this field for eight years. Moreover, I am still with the same company I joined after graduating. While eight years seems like a long time, it hasn’t felt that long to me. I would say that this year, I didn’t learn many new things on the coding side; instead, I focused more on design and presentations, design is okay but documenting I didn’t enjoy that much.</p>

<p>However, I realize this is inevitable for professional growth. Although I wasn’t promoted this year, I learned a lot about how to prepare for those higher-level responsibilities. Let’s see if I can move up next year.</p>

<h1 id="ai-is-comming-fast">AI is comming fast</h1>

<p>In 2022, a company called OpenAI launched a product called ChatGPT, an AI-powered chatbot capable of understanding and generating human-like text through natural conversation. It was highly hyped at the time, and everyone began using it. I tried it myself and was very surprised by its capabilities. However, I didn’t expect it to develop this quickly. In just three years, it has evolved to the point where many major companies are integrating it into their business operations. I can’t imagine what will happen in the next few years.</p>

<p>I might get laid off because AI can code so well these days! (Haha). However, I am going to focus on learning AI so I can improve my productivity and remain competitive in this field.</p>

<h1 id="in-chocolate-chip-no-relationship-heh">In chocolate chip, No! relationship heh</h1>

<p>I never thought it would actually come true. I started using dating apps back in 2024, but I wasn’t in any serious relationships. Eventually, I realized that finding a committed partner that way would be very tough. You don’t even know the basic background of the person you’re talking to—for instance, is she actually single? So many questions kept coming up. Because of that, I decided to stop using apps and go back to the traditional way: meeting people through friends.</p>

<p>I actually met her in 2023, but at the time, she was just a friend of a friend. There was a trip last Christmas that she happened to join. While I was driving home, we had our first real conversation that went beyond just saying “hi” or asking how she was doing. I think that was the moment I became interested in her.</p>

<p>After that trip, we started dating. A lot has happened since then—both good and bad—and we went through a bit of a “situationship” phase. But finally, we are officially in a relationship! We’ve only been together for a couple of months, but for some reason, it feels like it’s been a year. I really hope it continues to go well in the future.</p>

<p>This is my recap 2025.</p>

<p>See you next time!</p>

<p style="text-align: center;">
    <img src="/assets/recap-2025/cat.png" max-height="400px" alt="cat" />
</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Hi,]]></summary></entry><entry xml:lang="th"><title type="html">Send message to Telegram chat by Telegram bot</title><link href="https://blog.oatto.dev/2025/07/12/send-message-to-telegram.html" rel="alternate" type="text/html" title="Send message to Telegram chat by Telegram bot" /><published>2025-07-12T00:00:00+00:00</published><updated>2025-07-12T00:00:00+00:00</updated><id>https://blog.oatto.dev/2025/07/12/send-message-to-telegram</id><content type="html" xml:base="https://blog.oatto.dev/2025/07/12/send-message-to-telegram.html"><![CDATA[<p>สวัสดีครับ ในวันนี้ผมจะมาพูดถึงวิธีการที่เราจะสร้าง bot ใน telegram เพื่อจะส่งข้อความบางอย่างให้เรา แต่ก่อนหลาย ๆ คนอาจจะใช้ตัว Line Notify เอาไว้ส่งข้อความ เข้า group Line หรือส่งส่วนตัว แต่ว่าตอนนี้ Line Notify ได้ปิดให้บริการไปเรียนร้อยแล้ว Telegram ก็จะเป็นทางเลือกอีกทางหนึ่ง ที่สำคัญคือเป็นบริการที่ฟรี ซึ่งคงเป็นตัวเลือกที่ดีที่จะใช้แทนตัว Line Notify</p>

<blockquote>
  <p>ในตัวอย่างนี้ผมจะใช้ Bun เป็นตัวอย่างในการส่งข้อความนะครับ</p>
</blockquote>

<h1 id="สิ่งที่ต้องเตรียมก่อนเริ่ม">สิ่งที่ต้องเตรียมก่อนเริ่ม</h1>

<ol>
  <li>บัญชี Telegram</li>
  <li>Bun - Javascript runtime</li>
</ol>

<h1 id="วิธีทำ">วิธีทำ</h1>

<ol>
  <li>
    <p>สร้างบัญชี Telegram ผ่าน Application Telegram บน iOS, Android หรือ ผ่าน store ของ OS นั้น ๆ</p>
  </li>
  <li>
    <p>หลังจากสมัครแล้ว น่าจะเห็นตามรูปด้านล่าง</p>

    <p style="text-align: center;">
    <img src="/assets/telegram/home.png" alt="home" />
</p>
  </li>
  <li>
    <p>ไปที BotFather แล้วใช้คำสั่ง `/newbot’ เพื่อสร้างบอทของตัวเอง แล้วใส่ชือที่อยากได้ลงไป</p>

    <p style="text-align: center;">
    <img src="/assets/telegram/newbot.png" alt="newbot" />
</p>
  </li>
  <li>
    <p>จากนั้นตั้ง username ของบอท โดยชื่อบอทต้องลงท้ายด้วย <code class="language-plaintext highlighter-rouge">Bot</code> หรือ <code class="language-plaintext highlighter-rouge">_bot</code></p>

    <p style="text-align: center;">
    <img src="/assets/telegram/username.png" alt="username" />
</p>
  </li>
  <li>
    <p>เสร็จแล้วจะได้บอท token มา เราจะใช้ในการส่ง message อันจะเป็น secret ควรที่จะหาที่เก็บที่ปลอดภัย</p>

    <p style="text-align: center;">
    <img src="/assets/telegram/token.png" alt="token" />
</p>
  </li>
  <li>
    <p>ถ้าเราไปดูเอกสารของ Telegram API นอกจาก bot token แล้วเรายังต้องมีอีกอย่างนึงคือ chat id ซึ่งวิธีง่าย ๆ คือให้เรา search หาชื่อ bot ตัวเอง แล้วส่งข้อความอะไรก็ได้ลงไป จากนั้นให้ไปที่ <code class="language-plaintext highlighter-rouge">https://api.telegram.org/bot&lt;YourBOTToken&gt;/getUpdates</code> แล้วเราจะได้ chat id มา</p>

    <p style="text-align: center;">
    <img src="/assets/telegram/chatid.png" alt="chatId" />
</p>
  </li>
  <li>
    <p>พอได้ ิ bot token กับ chat id มาเรียบร้อย ตอนนี้ก็ถึงเวลาที่เรื่มทำตัวแอปที่ไว้ใช้ส่งข้อความ เมื่อเราลง bun และสร้างโปรเจคเสร็จเรียบร้อยแล้ว ให้แก้ไฟล์ <code class="language-plaintext highlighter-rouge">index.ts</code> ตามนี้ (อย่าลืมแก้ botToken กับ chatId ด้วยนะครับ)</p>

    <div class="language-typescript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">const</span> <span class="nx">chatId</span> <span class="o">=</span> <span class="o">&lt;</span><span class="nx">chatId</span><span class="o">&gt;</span><span class="p">;</span>
<span class="kd">const</span> <span class="nx">botToken</span> <span class="o">=</span> <span class="dl">"</span><span class="s2">&lt;botToken&gt;</span><span class="dl">"</span><span class="p">;</span>
<span class="kd">const</span> <span class="nx">url</span> <span class="o">=</span> <span class="s2">`https://api.telegram.org/bot</span><span class="p">${</span><span class="nx">botToken</span><span class="p">}</span><span class="s2">/sendMessage`</span><span class="p">;</span>

<span class="k">await</span> <span class="nf">fetch</span><span class="p">(</span><span class="nx">url</span><span class="p">,</span> <span class="p">{</span>
  <span class="na">method</span><span class="p">:</span> <span class="dl">"</span><span class="s2">POST</span><span class="dl">"</span><span class="p">,</span>
  <span class="na">body</span><span class="p">:</span> <span class="nx">JSON</span><span class="p">.</span><span class="nf">stringify</span><span class="p">({</span>
    <span class="na">text</span><span class="p">:</span> <span class="dl">"</span><span class="s2">Hello, this is a test message from your bot!</span><span class="dl">"</span><span class="p">,</span>
    <span class="na">chat_id</span><span class="p">:</span> <span class="nx">chatId</span><span class="p">,</span>
    <span class="na">parse_mode</span><span class="p">:</span> <span class="dl">"</span><span class="s2">HTML</span><span class="dl">"</span><span class="p">,</span>
  <span class="p">}),</span>
  <span class="na">headers</span><span class="p">:</span> <span class="p">{</span>
    <span class="dl">"</span><span class="s2">Content-Type</span><span class="dl">"</span><span class="p">:</span> <span class="dl">"</span><span class="s2">application/json</span><span class="dl">"</span><span class="p">,</span>
  <span class="p">},</span>
<span class="p">}).</span><span class="k">catch</span><span class="p">((</span><span class="nx">err</span><span class="p">:</span> <span class="nx">unknown</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="k">if </span><span class="p">(</span><span class="nx">err</span> <span class="k">instanceof</span> <span class="nb">Error</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">console</span><span class="p">.</span><span class="nf">log</span><span class="p">(</span><span class="dl">"</span><span class="s2">telegram notify failed</span><span class="dl">"</span><span class="p">,</span> <span class="nx">err</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">});</span>
</code></pre></div>    </div>
  </li>
  <li>
    <p>ใช้คำสั่ง <code class="language-plaintext highlighter-rouge">bun index.ts</code> จะได้ผลลัพธ์ตามนี้</p>

    <p style="text-align: center;">
    <img src="/assets/telegram/message.png" alt="message" />
</p>
  </li>
</ol>

<p>อันนี้จะเป็นตัวโปรเจคเผื่อถ้ารันไม่ได้ยังไงก็สามารถโคลนตัวโค้ดจาก <a href="https://github.com/oat9002/telegram-message-api-demo">github</a> นี้ไปได้เลยครับ</p>]]></content><author><name></name></author><summary type="html"><![CDATA[สวัสดีครับ ในวันนี้ผมจะมาพูดถึงวิธีการที่เราจะสร้าง bot ใน telegram เพื่อจะส่งข้อความบางอย่างให้เรา แต่ก่อนหลาย ๆ คนอาจจะใช้ตัว Line Notify เอาไว้ส่งข้อความ เข้า group Line หรือส่งส่วนตัว แต่ว่าตอนนี้ Line Notify ได้ปิดให้บริการไปเรียนร้อยแล้ว Telegram ก็จะเป็นทางเลือกอีกทางหนึ่ง ที่สำคัญคือเป็นบริการที่ฟรี ซึ่งคงเป็นตัวเลือกที่ดีที่จะใช้แทนตัว Line Notify]]></summary></entry><entry xml:lang="en"><title type="html">Recap 2024</title><link href="https://blog.oatto.dev/2025/01/04/recap-2025.html" rel="alternate" type="text/html" title="Recap 2024" /><published>2025-01-04T00:00:00+00:00</published><updated>2025-01-04T00:00:00+00:00</updated><id>https://blog.oatto.dev/2025/01/04/recap-2025</id><content type="html" xml:base="https://blog.oatto.dev/2025/01/04/recap-2025.html"><![CDATA[<p style="text-align: center;">
    <img src="/assets/recap-2024/car.jpg" alt="car" />
</p>

<p>It has become a tradition every year for many people to reflect on their experiences in the past year. A lot happened in 2024, both good and bad. All of these experiences have shaped me in some way.</p>

<h1 id="get-to-know-more-people">Get to know more people</h1>

<p>Starting from late 2023, I began using dating apps. Before that, I thought dating apps weren’t good. However, it wasn’t that bad; I’m not sure if I was just lucky, but I met many wonderful people. This helped me understand my likes and dislikes better. Even though I haven’t been successful in a relationship yet, I’ve learned a lot from the experience. Let’s see what this year brings as I turn 30. Hahaha.</p>

<h1 id="back-to-exercise">Back to exercise</h1>

<p>It had been a very long time since I exercised regularly. I tried multiple times to go to the gym and play my Nintendo Ring Fit, but I failed. Thanks to a friend, I was reintroduced to the world of badminton. I used to play badminton when I was young, more than 10 years ago. It’s a really fun sport when you play with friends. Moreover, it burns a lot of calories. Because of this, I was able to cure my NAFLD (Non-alcoholic fatty liver disease). I’m becoming healthier, bit by bit.</p>

<h1 id="spend-to-something-a-little-big">Spend to something a little big</h1>

<p>I had been contemplating this for a couple of years but never made a decision. It was about buying a car. I really wanted to buy one, but I was obstructed by the lack of parking space at home, as my parents’ car occupied the space. However, in September, the car I wanted went on sale at an auction. That made me think, “Okay, this is the time.” It might be very difficult to get a chance like this again. So, I decided to buy it and was able to manage parking space. It wasn’t factory new; it was a 4-year-old car in very good condition. Since I won’t be driving it every day, there was no point in buying a factory-new one. Additionally, second-hand prices are much more valuable if you can find a car with low mileage. I bought it, and I hope it will last at least 5 years.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[]]></summary></entry><entry xml:lang="en"><title type="html">Javascript Bangkok 2.0.0</title><link href="https://blog.oatto.dev/2024/10/19/js-bkk-2-0-0.html" rel="alternate" type="text/html" title="Javascript Bangkok 2.0.0" /><published>2024-10-19T00:00:00+00:00</published><updated>2024-10-19T00:00:00+00:00</updated><id>https://blog.oatto.dev/2024/10/19/js-bkk-2-0-0</id><content type="html" xml:base="https://blog.oatto.dev/2024/10/19/js-bkk-2-0-0.html"><![CDATA[<p>Hi, long time no see! It’s been a while since I’ve joined a tech meetup event. I saw that JavaScript BKK 2.0.0 is happening this year, so I’m excited to participate. I used to attend JavaScript BKK 1.0.0 a long time ago. There’s no way I’m missing this one!</p>

<p>Below are some short notes I took during the event. Please note that they don’t cover everything, and they’re handwritten, so they might be a bit messy. Hopefully, you can still get some value from them.</p>

<p>
    <img src="/assets/js-bkk-2.0.0/1.jpg" alt="page 1" />
    <img src="/assets/js-bkk-2.0.0/2.jpg" alt="page 2" />
    <img src="/assets/js-bkk-2.0.0/3.jpg" alt="page 3" />
    <img src="/assets/js-bkk-2.0.0/4.jpg" alt="page 4" />
    <img src="/assets/js-bkk-2.0.0/5.jpg" alt="page 5" />
    <img src="/assets/js-bkk-2.0.0/6.jpg" alt="page 6" />
    <img src="/assets/js-bkk-2.0.0/7.jpg" alt="page 7" />
    <img src="/assets/js-bkk-2.0.0/8.jpg" alt="page 8" />
    <img src="/assets/js-bkk-2.0.0/9.jpg" alt="page 9" />
    <img src="/assets/js-bkk-2.0.0/10.jpg" alt="page 10" />
</p>
<p style="text-align: center;">
    <img src="/assets/js-bkk-2.0.0/badge.jpg" width="300px" alt="badge" />
</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Hi, long time no see! It’s been a while since I’ve joined a tech meetup event. I saw that JavaScript BKK 2.0.0 is happening this year, so I’m excited to participate. I used to attend JavaScript BKK 1.0.0 a long time ago. There’s no way I’m missing this one!]]></summary></entry><entry xml:lang="en"><title type="html">How to find live partner not prom date</title><link href="https://blog.oatto.dev/2024/04/23/How-to-find-live-partner-not-prom-date.html" rel="alternate" type="text/html" title="How to find live partner not prom date" /><published>2024-04-23T00:00:00+00:00</published><updated>2024-04-23T00:00:00+00:00</updated><id>https://blog.oatto.dev/2024/04/23/How-to-find-live-partner-not-prom-date</id><content type="html" xml:base="https://blog.oatto.dev/2024/04/23/How-to-find-live-partner-not-prom-date.html"><![CDATA[<p>Hi, welcome back to my blog. This article doesn’t relate to tech at all. I did read one book named, <strong>How to not die alone</strong>, by Logan Ury, a behavioral scientist turned dating coach, dismantles the common pitfalls people fall into while looking for love. By using insights from psychology and practical exercises, the book equips you to overcome these obstacles and make smarter decisions throughout your dating journey, ultimately helping you find and build the relationship you desire.</p>

<p>I grab 2 key takeaways which will shift your perspective as you embark on your next romantic adventure!</p>

<h1 id="what-matters-less-than-we-think">What matters less than we think</h1>

<p><strong>Money:</strong> Important but not everything. Prioritize compatibility over wealth.</p>

<p><strong>Good looks:</strong> Look beyond physical attraction. Focus on long-term factors over lust.</p>

<p><strong>Similar personality:</strong> Find someone who complements you, not someone exactly like you.</p>

<p><strong>Shared hobbies:</strong> Enjoying different activities is okay. Give each other space for your own hobbies.</p>

<h1 id="what-matters-more-than-we-think">What matters more than we think</h1>

<p><strong>Emotional stability and kindness:</strong> Look for how they treat others and handle stress.</p>

<p><strong>Loyalty:</strong> See if they have long-term friendships and if people rely on them.</p>

<p><strong>Growth mindset:</strong> See how they approach challenges, setbacks, and learning.</p>

<p><strong>Personality that brings out the best in you:</strong> Feel energized, challenged, and happy around them.</p>

<p><strong>Skills to fight well:</strong> Focus on being heard, making repair attempts, and fighting productively.</p>

<p><strong>Ability to make hard decisions together:</strong> Make real decisions together to see how you work as a team.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Hi, welcome back to my blog. This article doesn’t relate to tech at all. I did read one book named, How to not die alone, by Logan Ury, a behavioral scientist turned dating coach, dismantles the common pitfalls people fall into while looking for love. By using insights from psychology and practical exercises, the book equips you to overcome these obstacles and make smarter decisions throughout your dating journey, ultimately helping you find and build the relationship you desire.]]></summary></entry><entry xml:lang="en"><title type="html">Recap 2023</title><link href="https://blog.oatto.dev/2024/01/01/recap-2023.html" rel="alternate" type="text/html" title="Recap 2023" /><published>2024-01-01T00:00:00+00:00</published><updated>2024-01-01T00:00:00+00:00</updated><id>https://blog.oatto.dev/2024/01/01/recap-2023</id><content type="html" xml:base="https://blog.oatto.dev/2024/01/01/recap-2023.html"><![CDATA[<p>Hello!, it is this time again for every year. Time to recap what I have done so far in the last year. I feel like last year pass quite fast. Not sure if I’m really enjoy or something else lol. As always, I will summarize only 3 bullets point (Just lazy to do it more). Let’
s see what are those things.</p>

<h1 id="almost-going-to-be-3-ish-years-old">Almost going to be 3 ish years-old</h1>

<p>Yeah, this year I’m going to be 30 years old. In the past, I really thought that by the time I turned 30, I would be very mature. In fact, I feel no different when I turned into 25, lol. My colleagues who are around my age feel the same way. Perhaps it’s good that we still feel young.</p>

<p>However, our bodies don’t feel as young. I’ve started to hear my body saying, ‘Please take better care of me.’ My health check results aren’t that good, so I think it’s time to be serious about my health.</p>

<h1 id="finally-got-a-macbook">Finally got a macbook</h1>

<p>Yeah, again. it was time to change my laptop. Back then, I was using the Dell Latitude 7240, a really nice laptop with a 12.5-inch screen that was very easy to carry. I had a strong desire to buy a MacBook at that time, but I also missed Linux, and switching to macOS would mean giving that up. So, I decided to go with the Lenovo ThinkPad X1 with a 2K screen with Arch Linux and Gnome desktop.</p>

<p>After purchasing it, I discovered that the 2K resolution of the screen presented a problem with Linux. Surprisingly, it wasn’t the resolution itself but rather the size of the screen, which was 14 inches. Normally, you would need to scale it to around 150% to see things comfortably. However, Linux, especially with the Gnome desktop, couldn’t handle fractional scaling between 100% and 200%, making it challenging. While there was a workaround involving adjusting the font size, an experimental feature for fractional scaling was available, but it proved to be quite horrible.</p>

<p>I used the ThinkPad for almost four years, and now it was time to change my laptop again. This time, without hesitation, I opted for macOS. Even though I miss Linux, I’m content and happy with macOS.</p>

<h1 id="keep-reading-and-coding">Keep reading and coding</h1>

<p>This is the year I started reading books, something I used to despise. My aversion was mainly because I had to read for exams. However, this time, I’m reading purely for enjoyment or to explore topics I’m genuinely interested in. The experience is so much better when it’s not driven by exam pressure; it’s a refreshing change.</p>

<p>Additionally, when it comes to coding, I have my own personal program that I implemented a couple of years ago. I’m still maintaining it and adding some new features.</p>

<p>These are very breifly what is going on to me in 2023. And if you see that this is written quite good. Thanks to chatGPT for helping me correcting grammar hahaha. See ya!</p>

<p>
    <img src="/assets/recap-2023/mountains.jpeg" alt="initialize project" />
</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Hello!, it is this time again for every year. Time to recap what I have done so far in the last year. I feel like last year pass quite fast. Not sure if I’m really enjoy or something else lol. As always, I will summarize only 3 bullets point (Just lazy to do it more). Let’ s see what are those things.]]></summary></entry><entry xml:lang="en"><title type="html">Short note from DevFest Bangkok 2023</title><link href="https://blog.oatto.dev/2023/11/04/short-note-devfest-2023.html" rel="alternate" type="text/html" title="Short note from DevFest Bangkok 2023" /><published>2023-11-04T00:00:00+00:00</published><updated>2023-11-04T00:00:00+00:00</updated><id>https://blog.oatto.dev/2023/11/04/short-note-devfest-2023</id><content type="html" xml:base="https://blog.oatto.dev/2023/11/04/short-note-devfest-2023.html"><![CDATA[<h2 id="hi">Hi</h2>

<p>Welcome back to my blog. For this topic, I want to write in English so please bare with me a bit lol. Today, I have a chance to join developer event called <strong>Devfest Bangkok 2023</strong> which is set up by GDG Bangkok. This is my very first time join to this event. By the way, because I don’t really use Flutter or create application. So in some session which relates to that I don’t really much play attention to it lol. Let’s see what we have.</p>

<h2 id="sessions">Sessions</h2>

<p>There are 10 sessions</p>

<ol>
  <li>View transition API the next generation of web transition</li>
  <li>Better UX on the Web with Core Web Vital</li>
  <li>Firestore Advanced Queries</li>
  <li>Build a generative AI chatbot with PaLM API and Cloud Functions for Firebase 2nd Gen</li>
  <li>Ship faster with Feature Flags</li>
  <li>Managing design system in Flutter</li>
  <li>Make for Large, Make for Fold with Flutter</li>
  <li>Diversity in AI</li>
  <li>Test Strategy for Kotlin Multiplatform Apps</li>
</ol>

<h2 id="view-transition-api-the-next-generation-of-web-transition">View transition API the next generation of web transition</h2>

<ul>
  <li>Pain point for web developer is transition between page in web</li>
  <li>Not as smooth as mobile app</li>
  <li>There are some restricition
    <ul>
      <li>Only support SPA</li>
      <li>Safariy has not been supported yet T_T</li>
    </ul>
  </li>
</ul>

<h2 id="better-ux-on-the-web-with-core-web-vital">Better UX on the Web with Core Web Vital</h2>

<ul>
  <li>UX -&gt; Look, Feel, <strong>Usability</strong></li>
  <li>Loading Performance, Visual Stability, Input Responsiveness
    <ul>
      <li>LCP &lt;= 2.5 s</li>
      <li>CLS &lt;= 0.1 s</li>
      <li>FID &lt;= 100 ms</li>
      <li>INP &lt;= 200 ms</li>
    </ul>
  </li>
  <li>We can use Web Vital chrome extension to check these things
    <ul>
      <li>page speed inside</li>
      <li>chrome ux report</li>
      <li>unlighthouse</li>
    </ul>
  </li>
  <li>Ensure LCP has prioritize in resource</li>
  <li>Use CDN to optimize doc and resource TTFB</li>
  <li>Ensure web is eligible for bfcache</li>
  <li>Avoid animation/transition which uses css-induction</li>
  <li>Avoid or break up long task</li>
  <li>Void unnecessary javascripot</li>
  <li>Avoid large rendering update</li>
</ul>

<h2 id="firestore-advanced-quries">Firestore Advanced Quries</h2>

<ul>
  <li>One view, one collection</li>
</ul>

<h2 id="build-a-generative-ai-chatbot-with-palm-api-and-cloud-functions-for-firebase-2nd-gen">Build a Generative AI Chatbot with PaLM API and Cloud functions for Firebase 2nd Gen</h2>

<ul>
  <li>Firebase extension
    <ul>
      <li>Group of lib</li>
      <li>transaction is one of this lib</li>
    </ul>
  </li>
  <li>Palm API extension
    <ul>
      <li>Vertex AI - wrapper from google</li>
      <li>Chat bison</li>
    </ul>
  </li>
</ul>

<h2 id="ship-faster-with-feature-flag">Ship faster with Feature flag</h2>

<ul>
  <li>How to deploy code under development</li>
  <li>Deploy Vs Release
    <ul>
      <li>Deploy -&gt; user get new binary</li>
      <li>Release -&gt; user get access to new feature</li>
    </ul>
  </li>
  <li><strong>Done</strong> means <strong>Release</strong>, not just work on my machine</li>
  <li>Feature flag
    <ul>
      <li>Allow us to frequently merge code to master without exposing it</li>
    </ul>
  </li>
  <li>Testing
    <ul>
      <li>from master</li>
      <li>enabled flag for tester</li>
    </ul>
  </li>
  <li>Monitoring
    <ul>
      <li>Small change between release easy to debug and fix</li>
    </ul>
  </li>
</ul>

<h2 id="managing-design-system-in-flutter">Managing design system in Flutter</h2>

<ul>
  <li>Why need design system?
    <ul>
      <li>Common language</li>
      <li>Speed up development</li>
      <li>Ensure consistency and familarity</li>
    </ul>
  </li>
  <li>Colors (Don’t use Primary and Secondary palette directly)
    <ul>
      <li>Primary palette</li>
      <li>Secondary palette</li>
      <li>Usage palette</li>
    </ul>
  </li>
</ul>

<h2 id="make-for-large-make-for-fold-with-flutter">Make for large Make for fold with Flutter</h2>

<ul>
  <li>23% more on iOS</li>
  <li>9% more on Android</li>
  <li>Large Screen
    <ul>
      <li>Continuity
        <ul>
          <li>Rotations</li>
          <li>Folding-Unfold</li>
          <li>Multi-Window mode</li>
        </ul>
      </li>
    </ul>
  </li>
  <li>For the best app experience
    <ul>
      <li>No orientation restriction</li>
      <li>Resizable</li>
    </ul>
  </li>
</ul>

<h2 id="diversity-in-ai">Diversity in AI</h2>

<ul>
  <li>Bias
    <ul>
      <li>Age</li>
      <li>Gender</li>
      <li>Race</li>
    </ul>
  </li>
  <li>They are from <strong>Data</strong></li>
  <li>Resulting in unfaire product</li>
  <li>How to make AI fair
    <ul>
      <li>Aware of bias(es)</li>
      <li>LIT (Language Interpreting tool) in NLP</li>
      <li>Know your data</li>
    </ul>
  </li>
  <li>Responsible AI
    <ul>
      <li>AI should be
        <ul>
          <li>Transpaency</li>
          <li>Fairness</li>
          <li>Accountability</li>
          <li>Privacy</li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

<blockquote>
  <p>Trust is hard to earn, easy to lose</p>
</blockquote>

<h2 id="testing-strategy-for-kotlin-multiplatform-projects">Testing Strategy for Kotlin multiplatform projects</h2>

<ul>
  <li>I haven’t had any project on Kotlin so I didn’t get much out of it lol.</li>
</ul>]]></content><author><name></name></author><summary type="html"><![CDATA[Hi]]></summary></entry><entry xml:lang="th"><title type="html">Format code via GitHub Actions</title><link href="https://blog.oatto.dev/2023/07/15/format-code-via-github-actions.html" rel="alternate" type="text/html" title="Format code via GitHub Actions" /><published>2023-07-15T00:00:00+00:00</published><updated>2023-07-15T00:00:00+00:00</updated><id>https://blog.oatto.dev/2023/07/15/format-code-via-github-actions</id><content type="html" xml:base="https://blog.oatto.dev/2023/07/15/format-code-via-github-actions.html"><![CDATA[<p>
  <img src="/assets/format-code-via-github-actions/cover.png" alt="initialize project" />
</p>

<h1 id="hi">Hi</h1>

<p>บทความในวันนี้จะเป็นเรื่องของ github actions ครับ ถ้าเพื่อน ๆ ได้ใช้ github เป็นที่เก็บโค้ดก็น่าจะเคยได้ยินบริการตัวนี้จาก github มาบ้าง github actions เป็นบริการสำหรับการทำ CI/CD pipeline ข้อดีของมันคือเราสารถใช้ได้ฟรีได้ประมาณนึง แล้วก็เราไม่จำเป็นต้องลงอะไรให้วุ่นวาย ให้ github เป็นคนจัดการให้ทั้งหมด ซึ่งในบทความนี้จะแชร์เรื่องว่าถ้าเราอยากจะ format code ให้อัตโนมัติในทุก ๆ pull request โดยอัตโนมัติ เพื่อที่ให้โค้ดของเรามีการ indent ตาม config ของเราอยู่เสมอ</p>

<p>ตัวอย่างในบทความนี้จะใช้ scala3 โดยใช้ library scalafmt ในการ format code ส่วน IDE จะเป็น IntelliJ Idea CE</p>

<h2 id="สิ่งที่ต้องเตรียม">สิ่งที่ต้องเตรียม</h2>

<ol>
  <li><a href="https://www.jetbrains.com/idea/download/">IntelliJ Idea CE</a></li>
  <li><a href="https://adoptium.net/temurin/releases/">java</a></li>
  <li><a href="https://www.scala-sbt.org/download.html">sbt</a></li>
  <li>บัญชี github</li>
</ol>

<h2 id="เตรียมโค้ดที่ใช้ทดสอบ">เตรียมโค้ดที่ใช้ทดสอบ</h2>

<ul>
  <li>
    <p>เปิดโปรแกรม IntelliJ Idea แล้วสร้างโปรเจกต์ตามนี้</p>

    <p>
    <img src="/assets/format-code-via-github-actions/intialize-project.png" alt="initialize project" />
</p>
  </li>
  <li>
    <p>สร้างไฟล์ <code class="language-plaintext highlighter-rouge">Main.scala</code> ไว้ในโฟล์เดอร์ <code class="language-plaintext highlighter-rouge">src</code> แล้วพิมพ์โค้ดตามนี้</p>

    <div class="language-scala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">object</span> <span class="nc">Main</span> <span class="k">extends</span> <span class="nc">App</span> <span class="o">{</span>
  <span class="k">val</span> <span class="nv">data</span> <span class="k">=</span> <span class="nc">Data</span><span class="o">(</span><span class="s">"Hello scala"</span><span class="o">)</span>
  <span class="nf">println</span><span class="o">(</span><span class="nv">data</span><span class="o">.</span><span class="py">content</span><span class="o">)</span>
<span class="o">}</span>

<span class="k">case</span> <span class="k">class</span> <span class="nc">Data</span><span class="o">(</span><span class="n">content</span><span class="k">:</span> <span class="kt">String</span><span class="o">)</span>

</code></pre></div>    </div>
  </li>
  <li>
    <p>สร้างไฟล์ <code class="language-plaintext highlighter-rouge">plugins.sbt</code> ที่โฟล์เดอร์ <code class="language-plaintext highlighter-rouge">project</code> เพิ่ม plugin <strong>scalafmt</strong> ตามนี้</p>

    <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")
</code></pre></div>    </div>
  </li>
  <li>
    <p>สร้างไฟล์ <code class="language-plaintext highlighter-rouge">.scalafmt.conf</code> ไว้ที่ root โฟล์เดอร์ ใส่ config ตามนี้</p>

    <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>version = 3.5.9
runner.dialect = scala3
maxColumn = 100
</code></pre></div>    </div>
  </li>
  <li>
    <p>จากนั้นให้ลองเทสโดยการแก้ format โค้ดใน <code class="language-plaintext highlighter-rouge">Main.scala</code> ตามนี้</p>

    <div class="language-scala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">object</span> <span class="nc">Main</span> <span class="k">extends</span> <span class="nc">App</span> <span class="o">{</span>
  <span class="k">val</span> <span class="nv">data</span> <span class="k">=</span> <span class="nc">Data</span><span class="o">(</span><span class="s">"Hello scala"</span><span class="o">)</span>
  <span class="nf">println</span><span class="o">(</span><span class="nv">data</span><span class="o">.</span><span class="py">content</span><span class="o">)</span>
<span class="o">}</span>

<span class="k">case</span> <span class="k">class</span> <span class="nc">Data</span><span class="o">(</span>
                  
                <span class="n">content</span><span class="k">:</span> <span class="kt">String</span><span class="o">)</span>
</code></pre></div>    </div>
  </li>
  <li>
    <p>เปิด sbt shell ขึ้นมาโดยใช้คำสั่ง <code class="language-plaintext highlighter-rouge">scamafmtAll</code> หรือถ้าไม่ได้ใช้ sbt shell ใช้เป็น terminal ธรรมดา ให้ใช้คำสั่ง <code class="language-plaintext highlighter-rouge">sbt scalafmtAll</code></p>
  </li>
  <li>
    <p>ลองเข้าไปดูไฟล์ <code class="language-plaintext highlighter-rouge">Main.scala</code> อีกรอบ จะได้ผลลัพธ์ตามนี้</p>

    <div class="language-scala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">object</span> <span class="nc">Main</span> <span class="k">extends</span> <span class="nc">App</span> <span class="o">{</span>
  <span class="k">val</span> <span class="nv">data</span> <span class="k">=</span> <span class="nc">Data</span><span class="o">(</span><span class="s">"Hello scala"</span><span class="o">)</span>
  <span class="nf">println</span><span class="o">(</span><span class="nv">data</span><span class="o">.</span><span class="py">content</span><span class="o">)</span>
<span class="o">}</span>

<span class="k">case</span> <span class="k">class</span> <span class="nc">Data</span><span class="o">(</span><span class="n">content</span><span class="k">:</span> <span class="kt">String</span><span class="o">)</span>
</code></pre></div>    </div>
  </li>
  <li>
    <p>จากนั้นให้แก้ไฟล์ให้ไปอยู่ในรูปแบบเดิมก่อนที่จะถูก format</p>
  </li>
  <li>
    <p>ในการที่เราจะใช้ github actions นั้น เราต้องสร้างโฟล์เดอร์ <code class="language-plaintext highlighter-rouge">.github</code> ไว้ที่ root โฟล์เดอร์แล้วก็ข้างในให้สร้างโฟล์เดอร์ <code class="language-plaintext highlighter-rouge">workflows</code></p>

    <p>
  <img src="/assets/format-code-via-github-actions/workflow-folder.png" alt="workflow folder" />
</p>
  </li>
  <li>
    <p>จากนี้นให้สร้างไฟล์ <code class="language-plaintext highlighter-rouge">pr_build.yml</code> ไว้ในโฟล์เดอร์ <code class="language-plaintext highlighter-rouge">workflows</code> แล้วใส่โค้ดตามนี้</p>

    <div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">name</span><span class="pi">:</span> <span class="s">pr_build</span>

<span class="na">on</span><span class="pi">:</span>
  <span class="na">pull_request</span><span class="pi">:</span>
    <span class="na">branches</span><span class="pi">:</span> <span class="pi">[</span> <span class="nv">main</span> <span class="pi">]</span>

<span class="na">jobs</span><span class="pi">:</span>
  <span class="na">formatCode</span><span class="pi">:</span>
    <span class="na">runs-on</span><span class="pi">:</span> <span class="s">ubuntu-latest</span>
    <span class="na">if</span><span class="pi">:</span> <span class="s">${{ !contains(github.actor, 'github-actions[bot]') }}</span> <span class="c1"># เพื่อไม่ให้ commit ของ bot มารัน job ตัวนี้อีก</span>
    <span class="na">steps</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="na">uses</span><span class="pi">:</span> <span class="s">actions/checkout@v3</span>
      <span class="na">with</span><span class="pi">:</span>
        <span class="na">ref</span><span class="pi">:</span> <span class="s">${{ github.head_ref }}</span>
    <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Set up JDK </span><span class="m">20</span>
      <span class="na">uses</span><span class="pi">:</span> <span class="s">actions/setup-java@v3</span>
      <span class="na">with</span><span class="pi">:</span>
        <span class="na">distribution</span><span class="pi">:</span> <span class="s1">'</span><span class="s">temurin'</span>
        <span class="na">java-version</span><span class="pi">:</span> <span class="s1">'</span><span class="s">20'</span>
    <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Set Git Username</span>
      <span class="na">run</span><span class="pi">:</span> <span class="pi">|</span>
        <span class="s">git config --global user.name "github-actions[bot]"</span>
        <span class="s">git config --global user.email "github-actions[bot]@users.noreply.github.com"</span>
    <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Format code</span>
      <span class="na">run</span><span class="pi">:</span> <span class="pi">|</span>
        <span class="s">sbt scalafmtAll</span>
    <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Check for changes</span>
      <span class="na">run</span><span class="pi">:</span> <span class="pi">|</span>
        <span class="s">if git diff-index --quiet HEAD; then</span>
          <span class="s">echo "IS_CHANGED=false" &gt;&gt; $GITHUB_ENV</span>
        <span class="s">else</span>
          <span class="s">echo "IS_CHANGED=true" &gt;&gt; $GITHUB_ENV</span>
        <span class="s">fi</span>
    <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Push code if format is needed</span>
      <span class="na">if</span><span class="pi">:</span> <span class="s">${{ env.IS_CHANGED == 'true' }}</span>
      <span class="na">run</span><span class="pi">:</span> <span class="pi">|</span>
        <span class="s">git add --all</span>
        <span class="s">git commit -m ":recycle: format code"</span>
        <span class="s">git push origin</span>
</code></pre></div>    </div>
  </li>
</ul>

<p>จนถึงขั้นตอนนี้เราก็มีโค้ดที่เตรียมพร้อมสำหรับที่จะใช้ใน github actions แล้ว</p>

<h2 id="สร้างโปรเจกต์บน-github">สร้างโปรเจกต์บน github</h2>

<ul>
  <li>
    <p>ไปที่เว็บ github ล็อคอินบัญชีของเรา แล้วสร้างโปรเจกต์ชื่อว่า <code class="language-plaintext highlighter-rouge">scalafmt-test</code> แล้วก็ให้เพิ่ม <code class="language-plaintext highlighter-rouge">README.md</code> ไฟล์ไปด้วยเลย</p>
  </li>
  <li>
    <p>จากนั้นไปที่ <code class="language-plaintext highlighter-rouge">Settings</code> แล้วเลือกไปที่ <code class="language-plaintext highlighter-rouge">Actions -&gt; General</code></p>

    <p>
  <img src="/assets/format-code-via-github-actions/github-setting-1.png" />
</p>
  </li>
  <li>
    <p> อนุญาติ permission ให้ <code class="language-plaintext highlighter-rouge">GITHUB_TOKEN</code> สามารถ write ไปยัง reporisitory ของเราได้</p>

    <p>
  <img src="/assets/format-code-via-github-actions/github-setting-2.png" />
</p>
  </li>
  <li>
    <p>จากนั้นกลับมาที่โค้ดของเรา ให้เชื่อมโค้ดเราเข้ากับ repo ที่เราสร้างขึ้น จากนั้นให้สร้าง branch ที่ชื่อว่า <code class="language-plaintext highlighter-rouge">feature/github-actions</code></p>
  </li>
  <li>
    <p>แล้วก็ให้ push โค้ดของเราขึ้น github ไป</p>

    <p><em>ถ้าเห็นว่าไฟล์ที่จะ push ขึ้นไปมีเยอะผิดปกติ ให้เพื่มไฟล์ <code class="language-plaintext highlighter-rouge">.gitignore</code> ไว้ที่ root  แล้วใส่ชื่อไฟล์ตามด้านล่างนี้</em></p>

    <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  .DS_Store
  *.class
  *.log
  *~

  # sbt specific
  dist/*
  target/
  lib_managed/
  src_managed/
  project/boot/
  project/plugins/project/
  project/local-plugins.sbt
  .history
  .bsp

  # Scala-IDE specific
  .scala_dependencies
  .cache
  .classpath
  .project
  .settings
  classes/

  # idea
  .idea
  .idea_modules
  /.worksheet/

  # Dotty-IDE
  .dotty-ide-artifact
  .dotty-ide.json

  # Visual Studio Code
  .vscode

  .env
  src/main/resources/application.local.conf
</code></pre></div>    </div>
  </li>
  <li>
    <p>จากนั้นไปที่เว็บ github ของเรา ให้ไปสร้าง pull request เป็นชื่ออะไรก็ได้ แล้วก็รอดูผลลัพธ์</p>

    <p><em>จะเห็นได้ว่าถ้าตัว github actions รันเสร็จแล้ว มันจะมี commit ให้เพิ่มขึ้นมา เป็นอันว่าเสร็จเรียบร้อย</em></p>

    <p>
  <img src="/assets/format-code-via-github-actions/result.png" />
</p>
  </li>
</ul>

<p>จากนี้ทุก ๆ pull request ที่มีการสร้างขึ้นมาก็จะถูก format ตาม config ของเราตลอดเวลา</p>]]></content><author><name></name></author><summary type="html"><![CDATA[]]></summary></entry><entry xml:lang="th"><title type="html">Migrate from CRA to Vite.js</title><link href="https://blog.oatto.dev/2023/05/07/migrate-from-cra-to-vitejs.html" rel="alternate" type="text/html" title="Migrate from CRA to Vite.js" /><published>2023-05-07T00:00:00+00:00</published><updated>2023-05-07T00:00:00+00:00</updated><id>https://blog.oatto.dev/2023/05/07/migrate-from-cra-to-vitejs</id><content type="html" xml:base="https://blog.oatto.dev/2023/05/07/migrate-from-cra-to-vitejs.html"><![CDATA[<p align="center">
    <img src="/assets/cra-to-vitejs/vitejs.png" alt="vitejs" />
</p>

<h1 id="มาเริ่มกันเลย-">มาเริ่มกันเลย !</h1>

<p>เมื่อไม่นานมานี้ในเว็บ react official ได้เอา <strong>CRA (Create React App)</strong> ออกจากเว็บไซต์ ซึ่งมันก็เป็นสัญญาณบ่งบอกได้ว่าให้ย้ายออกไปใช้ตัวอื่นแทนได้แล้วนะ หลังจากนั้นผมก็ลอง ๆ ไปหาดูว่าควรจะเปลี่ยนไปใช้อะไรดั แล้วก็ได้ไปเจอตัวนี้ซึ่งก็คือ <strong><a href="https://vitejs.dev/">Vite.js</a></strong> เหมือนแต่เดิมเค้าทำให้ Vue.js แต่ตอนนี้เค้าก็ support React แล้วก็เลยตัดสินใจลองเปลี่ยนมาใช้ตัวนี้ดู ซึ่งสำหรับผมการ migrate นี้ทำได้ค่อนข้างง่าย ไม่ยุ่งยากเหมือนที่ผมเคยคิดไว้</p>

<p>ปล. ตัวอย่างในที่นี้จะใช้ Typescript นะครับ</p>

<p>ปล2. Vite.js เท่าที่ผมรู้จะไม่ได้ support testing by default อาจจะต้องไปหาวิธีการเพิ่มต่อ</p>

<h1 id="ขั้นตอนการ-migrate">ขั้นตอนการ migrate</h1>

<ol>
  <li>
    <p>ไปที่โปรเจกต์แล้วรันคำสั่งนี้เพื่อเพิ่ม Vite.js lib</p>

    <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="nv">$ </span>yarn add <span class="nt">--dev</span> vite @vitejs/plugin-react vite-tsconfig-paths vite-plugin-svgr
</code></pre></div>    </div>
  </li>
  <li>
    <p>เสร็จเเล้วให้เข้าไปแก้ไฟล์ <code class="language-plaintext highlighter-rouge">package.json</code> จาก</p>

    <div class="language-json highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="w"> </span><span class="err">scripts:</span><span class="w"> </span><span class="p">{</span><span class="w">
     </span><span class="nl">"start"</span><span class="p">:</span><span class="w"> </span><span class="s2">"react-scripts start"</span><span class="p">,</span><span class="w">
     </span><span class="nl">"build"</span><span class="p">:</span><span class="w"> </span><span class="s2">"react-scripts build"</span><span class="p">,</span><span class="w">
     </span><span class="nl">"test"</span><span class="p">:</span><span class="w"> </span><span class="s2">"react-scripts test --env=jsdom"</span><span class="p">,</span><span class="w">
     </span><span class="nl">"eject"</span><span class="p">:</span><span class="w"> </span><span class="s2">"react-scripts eject"</span><span class="w">
     </span><span class="err">..</span><span class="w">
     </span><span class="err">..</span><span class="w">
 </span><span class="p">}</span><span class="w">
</span></code></pre></div>    </div>

    <p>เป็น</p>

    <div class="language-json highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="w"> </span><span class="err">scripts:</span><span class="w"> </span><span class="p">{</span><span class="w">
     </span><span class="nl">"start"</span><span class="p">:</span><span class="w"> </span><span class="s2">"vite"</span><span class="p">,</span><span class="w">
     </span><span class="nl">"build"</span><span class="p">:</span><span class="w"> </span><span class="s2">"tsc &amp;&amp; vite build"</span><span class="p">,</span><span class="w">
     </span><span class="nl">"serve"</span><span class="p">:</span><span class="w"> </span><span class="s2">"vite preview"</span><span class="w">
     </span><span class="err">..</span><span class="w">
     </span><span class="err">..</span><span class="w">
 </span><span class="p">}</span><span class="w">
</span></code></pre></div>    </div>
  </li>
  <li>
    <p>แก้ไฟล์ <code class="language-plaintext highlighter-rouge">tsconfig.json</code> บางจุดตามนี้</p>

    <div class="language-json highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="w"> </span><span class="p">{</span><span class="w">
     </span><span class="nl">"compilerOptions"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
         </span><span class="nl">"target"</span><span class="p">:</span><span class="w"> </span><span class="s2">"ESNext"</span><span class="p">,</span><span class="w">
         </span><span class="nl">"lib"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
             </span><span class="s2">"dom"</span><span class="p">,</span><span class="w">
             </span><span class="s2">"dom.iterable"</span><span class="p">,</span><span class="w">
             </span><span class="s2">"esnext"</span><span class="w">
         </span><span class="p">],</span><span class="w">
         </span><span class="nl">"types"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">"vite/client"</span><span class="p">,</span><span class="w"> </span><span class="s2">"vite-plugin-svgr/client"</span><span class="p">],</span><span class="w">
         </span><span class="nl">"allowJs"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
         </span><span class="nl">"skipLibCheck"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
         </span><span class="nl">"esModuleInterop"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
         </span><span class="err">..</span><span class="w">
         </span><span class="err">..</span><span class="w">
 </span><span class="p">}</span><span class="w">
</span></code></pre></div>    </div>
  </li>
  <li>
    <p>เพิ่มไฟล์ <code class="language-plaintext highlighter-rouge">vite.config.ts</code> ไว้ที่ root โฟล์เดอร์</p>

    <div class="language-typescript highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="k">import</span> <span class="nx">react</span> <span class="k">from</span> <span class="dl">"</span><span class="s2">@vitejs/plugin-react</span><span class="dl">"</span><span class="p">;</span>
 <span class="k">import</span> <span class="p">{</span> <span class="nx">defineConfig</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">"</span><span class="s2">vite</span><span class="dl">"</span><span class="p">;</span>
 <span class="k">import</span> <span class="nx">svgrPlugin</span> <span class="k">from</span> <span class="dl">"</span><span class="s2">vite-plugin-svgr</span><span class="dl">"</span><span class="p">;</span>
 <span class="k">import</span> <span class="nx">viteTsconfigPaths</span> <span class="k">from</span> <span class="dl">"</span><span class="s2">vite-tsconfig-paths</span><span class="dl">"</span><span class="p">;</span>

 <span class="c1">// https://vitejs.dev/config/</span>
 <span class="k">export</span> <span class="k">default</span> <span class="nf">defineConfig</span><span class="p">({</span>
     <span class="na">plugins</span><span class="p">:</span> <span class="p">[</span><span class="nf">react</span><span class="p">(),</span> <span class="nf">viteTsconfigPaths</span><span class="p">(),</span> <span class="nf">svgrPlugin</span><span class="p">()],</span>
     <span class="na">build</span><span class="p">:</span> <span class="p">{</span>
         <span class="na">outDir</span><span class="p">:</span> <span class="dl">"</span><span class="s2">build</span><span class="dl">"</span><span class="p">,</span>
     <span class="p">},</span>
 <span class="p">});</span>
</code></pre></div>    </div>
  </li>
  <li>
    <p>ย้ายไฟล์ <code class="language-plaintext highlighter-rouge">index.html</code> ที่อยู่ในโฟล์เดอร์ <code class="language-plaintext highlighter-rouge">public</code> ออกมาไว้ที่ root แล้วข้างในไฟล์ให้ลบ <code class="language-plaintext highlighter-rouge">%PUBLIC_URL%</code> ออก แล้วก็ใน <code class="language-plaintext highlighter-rouge">&lt;body&gt;</code> ให้เพิ่ม <code class="language-plaintext highlighter-rouge">&lt;script type="module" src="/src/index.tsx"&gt;&lt;/script&gt;</code> ไว้บรรทัดด้านล่าง <code class="language-plaintext highlighter-rouge">&lt;div id="root"&gt;&lt;/div&gt;</code> ก็จะได้ประมาณนี้</p>

    <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="nt">&lt;body&gt;</span>
     <span class="nt">&lt;noscript&gt;</span> You need to enable JavaScript to run this app. <span class="nt">&lt;/noscript&gt;</span>
     <span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">"root"</span><span class="nt">&gt;&lt;/div&gt;</span>
     <span class="nt">&lt;script </span><span class="na">type=</span><span class="s">"module"</span> <span class="na">src=</span><span class="s">"/src/index.tsx"</span><span class="nt">&gt;&lt;/script&gt;</span>
     <span class="c">&lt;!--
     This HTML file is a template.
     If you open it directly in the browser, you will see an empty page.
     To begin the development, run `npm start` or `yarn start`.
     To create a production bundle, use `npm run build` or `yarn build`.
     --&gt;</span>
 <span class="nt">&lt;/body&gt;</span>
</code></pre></div>    </div>
  </li>
  <li>
    <p>แก้ evnvironment variable prefix จาก <code class="language-plaintext highlighter-rouge">REACT_APP</code> เป็น <code class="language-plaintext highlighter-rouge">VITE</code></p>
  </li>
  <li>
    <p>สร้างไฟลฺ์ <code class="language-plaintext highlighter-rouge">evn.d.ts</code> ใว้ในโฟล์เดอรฺ์ <code class="language-plaintext highlighter-rouge">src</code> แล้วให้สร้าง interface ตามนี้ที่มี property ตาม environment variable ของเรา</p>

    <div class="language-typescript highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="kr">interface</span> <span class="nx">ImportMetaEnv</span> <span class="p">{</span>
     <span class="k">readonly</span> <span class="o">&lt;</span><span class="nx">variable</span> <span class="nx">name</span><span class="o">&gt;</span><span class="p">:</span> <span class="o">&lt;</span><span class="nx">varibale</span> <span class="kd">type</span><span class="o">&gt;</span><span class="p">;</span> <span class="c1">// ex. readonly VITE_SERVER_URL: string;</span>
     <span class="p">..</span>
     <span class="p">..</span>
 <span class="p">}</span>

 <span class="kr">interface</span> <span class="nx">ImportMeta</span> <span class="p">{</span>
     <span class="k">readonly</span> <span class="nx">env</span><span class="p">:</span> <span class="nx">ImportMetaEnv</span><span class="p">;</span>
 <span class="p">}</span>
</code></pre></div>    </div>
  </li>
  <li>
    <p>แก้โค้ดที่เรียกใช้ environment variable จาก <code class="language-plaintext highlighter-rouge">process.evn.REACT_....</code> เป็น <code class="language-plaintext highlighter-rouge">import.meta.evn.VITE_...</code></p>
  </li>
  <li>
    <p>จากนั้นลองสตาร์ท web app ด้วย <code class="language-plaintext highlighter-rouge">yarn start</code> ถ้า web app สตาร์ทได้ปกติก็เป็นอันเสร็จเรียบร้อย</p>
  </li>
</ol>

<p><strong><em>ตัวอย่าง commit ของการ migrate</em></strong> <em><a href="https://github.com/oat9002/GoldPriceTracking/commit/c91413e23e99e6ce573f56dffc1c695a6c398901">Click Here</a></em></p>

<h2 id="ไว้เจอกันใหม่-">ไว้เจอกันใหม่ !</h2>]]></content><author><name></name></author><summary type="html"><![CDATA[]]></summary></entry></feed>