<?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 Sasha Nedopaka on Medium]]></title>
        <description><![CDATA[Stories by Sasha Nedopaka on Medium]]></description>
        <link>https://medium.com/@nedopaka?source=rss-11984707caea------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zD3GxCgIHW1nkFBYsCKL-g.png</url>
            <title>Stories by Sasha Nedopaka on Medium</title>
            <link>https://medium.com/@nedopaka?source=rss-11984707caea------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 05:49:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nedopaka/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[Setup a React Vite project with TypeScript, Prettier & Vitest [2024]]]></title>
            <link>https://medium.com/@nedopaka/setup-a-react-vite-project-with-typescript-prettier-vitest-2024-9bb6e919ac8f?source=rss-11984707caea------2</link>
            <guid isPermaLink="false">https://medium.com/p/9bb6e919ac8f</guid>
            <category><![CDATA[vitejs]]></category>
            <category><![CDATA[vitest]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[prettier]]></category>
            <dc:creator><![CDATA[Sasha Nedopaka]]></dc:creator>
            <pubDate>Tue, 13 Feb 2024 11:14:36 GMT</pubDate>
            <atom:updated>2024-02-13T17:41:09.856Z</atom:updated>
            <content:encoded><![CDATA[<h4>Easy instructions for quick setup</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0sNxsfVTQdoOZ5sU" /><figcaption>Photo by <a href="https://unsplash.com/@thomastastet?utm_source=medium&amp;utm_medium=referral">Thomas Tastet</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Even with React frameworks like Next.js gaining popularity and React <a href="https://react.dev/learn/start-a-new-react-project">documentation</a> advocating their use, still many developers tend to start with just using the React library without the additional tools available in the framework out of the box, as this extra layer introduces additional complexity that is unnecessary in many cases.</p><p>Since create-react-app is no longer supported, I’ve found that the best alternative for creating a new React app is to use Vite.</p><p>Below I pasted short description from the official <a href="https://vitejs.dev/guide/">documentation</a>.</p><p>Vite (French word for “quick”, pronounced /vit/, like “veet”) is a build tool that aims to provide a faster and leaner development experience for modern web projects. It consists of two major parts:</p><ul><li>A dev server that provides <a href="https://vitejs.dev/guide/features">rich feature enhancements</a> over <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules">native ES modules</a>, for example extremely fast <a href="https://vitejs.dev/guide/features#hot-module-replacement">Hot Module Replacement (HMR)</a>.</li><li>A build command that bundles your code with <a href="https://rollupjs.org/">Rollup</a>, pre-configured to output highly optimized static assets for production.</li></ul><p>Vite is opinionated and comes with sensible defaults out of the box.</p><p>In this setup we’re going to use TypeScript. TypeScript is a typed JavaScript superset that compiles into regular JavaScript.</p><p>With the growing popularity of TypeScript or TS for short, we can consider it as an improved version of JavaScript with additional features.</p><p>It is an open-source programming language for developing large and complex applications. It was developed by Microsoft in 2012, and in fact, the reason behind its creation was the need to handle large-scale applications. It helps software developers add type safety to their projects and provides features like type aliases, interfaces, abstract classes, encapsulation, inheritance, and function overloading.</p><p>Furthermore, Angular also uses TypeScript and is actually written in it. Lastly, TypeScript is very trendy among developers today.</p><p>So, let’s create a new React project with Vite by executing the following command:</p><pre>npm create vite@latest</pre><p>First, you’ll be asked for project name:</p><pre>npm create vite@latest<br>? Project name: › vite-project</pre><p>Then it asks for a framework to choose:</p><pre>npm create vite@latest<br>✔ Project name: … vite-project<br>? Select a framework: › - Use arrow-keys. Return to submit.<br>❯   Vanilla<br>    Vue<br>    React<br>    Preact<br>    Lit<br>    Svelte<br>    Solid<br>    Qwik<br>    Others</pre><p>Select React and hit Enter.</p><p>Then you need to choose whether to use JavaScript/TypeSprit with Babel/SWC:</p><pre>npm create vite@latest<br>✔ Project name: … vite-project<br>✔ Select a framework: › React<br>? Select a variant: › - Use arrow-keys. Return to submit.<br>❯   TypeScript<br>    TypeScript + SWC<br>    JavaScript<br>    JavaScript + SWC</pre><p>Let’s choose <strong>TypeScript</strong> considering to work with a big project.</p><p>It’s a final step, and then you should see something like this:</p><pre>npm create vite@latest<br>✔ Project name: … vite-project<br>✔ Select a framework: › React<br>✔ Select a variant: › TypeScript<br><br>Scaffolding project in /Users/Oleksandr_Nedopaka/Repos/vite-project...<br><br>Done. Now run:<br><br>  cd vite-project<br>  npm install<br>  npm run dev</pre><p>Now go to the project folder, install dependencies, start dev server to check that everything works fine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*D4R4VMfp9BkqFG1x.png" /><figcaption>Default page with App component</figcaption></figure><p>Let’s have a look what we’ve got in our package.json now:</p><pre>{<br>  &quot;name&quot;: &quot;vite-project&quot;,<br>  &quot;private&quot;: true,<br>  &quot;version&quot;: &quot;0.0.0&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;dev&quot;: &quot;vite&quot;,<br>    &quot;build&quot;: &quot;tsc &amp;&amp; vite build&quot;,<br>    &quot;lint&quot;: &quot;eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0&quot;,<br>    &quot;preview&quot;: &quot;vite preview&quot;<br>  },<br>  &quot;dependencies&quot;: {<br>    &quot;react&quot;: &quot;^18.2.0&quot;,<br>    &quot;react-dom&quot;: &quot;^18.2.0&quot;<br>  },<br>  &quot;devDependencies&quot;: {<br>    &quot;@types/react&quot;: &quot;^18.2.55&quot;,<br>    &quot;@types/react-dom&quot;: &quot;^18.2.19&quot;,<br>    &quot;@typescript-eslint/eslint-plugin&quot;: &quot;^6.21.0&quot;,<br>    &quot;@typescript-eslint/parser&quot;: &quot;^6.21.0&quot;,<br>    &quot;@vitejs/plugin-react&quot;: &quot;^4.2.1&quot;,<br>    &quot;eslint&quot;: &quot;^8.56.0&quot;,<br>    &quot;eslint-plugin-react-hooks&quot;: &quot;^4.6.0&quot;,<br>    &quot;eslint-plugin-react-refresh&quot;: &quot;^0.4.5&quot;,<br>    &quot;typescript&quot;: &quot;^5.2.2&quot;,<br>    &quot;vite&quot;: &quot;^5.1.0&quot;<br>  }<br>}</pre><p>Vite kindly preinstalls ESLint for us. In .eslintrc.cjs we already have necessary rules for the code quality, and it provides us error highlights in our code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GcOBpbFiQtFzDJNqQ4m_ng.png" /><figcaption>src/App.jsx</figcaption></figure><p>Keep in mind, that you should have installed ESLint plugin to make it work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Jeu6YHf-tHf0VZLq.png" /><figcaption>ESLint extension for VS Code</figcaption></figure><p>Now let’s install Prettier to make our code look nice.</p><pre>npm i -D -E prettier</pre><p>And then create .prettierrc with the following content:</p><pre>{<br>  &quot;semi&quot;: true,<br>  &quot;singleQuote&quot;: true,<br>  &quot;trailingComma&quot;: &quot;es5&quot;<br>}</pre><p>To prevent conflicts in rules between ESLint and Prettier let’s add the following plugin:</p><pre>npm i -D eslint-config-prettier</pre><p>Then make changes in .eslintrc.cjs to match the following:</p><pre>module.exports = {<br>  root: true,<br>  env: { browser: true, es2020: true },<br>  extends: [<br>    &#39;eslint:recommended&#39;,<br>    &#39;plugin:@typescript-eslint/recommended&#39;,<br>    &#39;plugin:react-hooks/recommended&#39;,<br>    // This disables the formatting rules in ESLint that Prettier is going to be responsible for handling.<br>    // Make sure it&#39;s always the last config, so it gets the chance to override other configs.<br>    &#39;prettier&#39;,<br>  ],<br>  ignorePatterns: [&#39;dist&#39;, &#39;.eslintrc.cjs&#39;],<br>  parser: &#39;@typescript-eslint/parser&#39;,<br>  plugins: [&#39;react-refresh&#39;],<br>  rules: {<br>    &#39;react-refresh/only-export-components&#39;: [<br>      &#39;warn&#39;,<br>      { allowConstantExport: true },<br>    ],<br>  },<br>}</pre><p>And then add the following script to our package.json:</p><pre>&quot;format&quot;: &quot;prettier --write ./src&quot;,</pre><p>Now we can do npm run format to format All our source code, but it would be great if we could format our files automatically on paste and save actions.</p><p>For this, install Prettier plugin for VS Code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ROfzGgdjahsM0P_u.png" /><figcaption>Prettier extension for VS Code</figcaption></figure><p>And then create .vscode/settings.json so we can use this project as a boilerplate for every our new React project.</p><pre>{<br>  &quot;editor.defaultFormatter&quot;: &quot;esbenp.prettier-vscode&quot;,<br>  &quot;editor.formatOnPaste&quot;: true, // required<br>  &quot;editor.formatOnType&quot;: false, // required<br>  &quot;editor.formatOnSave&quot;: true, // optional<br>  &quot;editor.formatOnSaveMode&quot;: &quot;file&quot;, // required to format on save<br>  &quot;files.autoSave&quot;: &quot;onFocusChange&quot; // optional but recommended<br>}</pre><p>Next, considering that we’re going to write unit tests for our code, let’s install the following dependencies:</p><pre>npm i -D vitest jsdom @testing-library/jest-dom @testing-library/react</pre><p>Then create src/__tests__/setup.ts and insert the following code:</p><pre>// jest-dom adds custom jest matchers for asserting on DOM nodes.<br>// allows you to do things like:<br>// expect(element).toHaveTextContent(/react/i)<br>// learn more: https://github.com/testing-library/jest-dom<br>import &#39;@testing-library/jest-dom&#39;;</pre><p>Also, then modify vite.config.ts to match:</p><pre>/// &lt;reference types=&quot;vitest&quot; /&gt;<br>import { defineConfig } from &#39;vite&#39;;<br>import react from &#39;@vitejs/plugin-react&#39;;<br><br>// https://vitejs.dev/config/<br>export default defineConfig({<br>  plugins: [react()],<br>  test: {<br>    globals: true,<br>    environment: &#39;jsdom&#39;,<br>    setupFiles: [&#39;src/__tests__/setup.ts&#39;],<br>  },<br>});</pre><p>Note the “triple slash command” in the first line. It is needed because the Vite config interface knows nothing about Vitest, and TS does not allow redundant properties (properties not defined by the type/interface). Therefore, Vitest must <a href="https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/config.ts">extend</a> Vite config (defined as a TS interface).</p><p>Alternatively, instead of “tripple slash command”, you can replace standard Vite config with Vite config extended by Vitest:</p><pre>import { defineConfig } from &#39;vitest/config&#39;;</pre><p>Now we’re ready to write our first test App.test.tsx:</p><pre>import { describe, it, expect, test } from &#39;vitest&#39;;<br>import { render } from &#39;@testing-library/react&#39;;<br>import App from &#39;./App&#39;;<br><br>test(&#39;demo&#39;, () =&gt; {<br>  expect(true).toBe(true);<br>});<br><br>describe(&#39;render&#39;, () =&gt; {<br>  it(&#39;renders the main page&#39;, () =&gt; {<br>    render(&lt;App /&gt;);<br>    expect(true).toBeTruthy();<br>  });<br>});</pre><p>Finally, add the following script in package.json:</p><pre>&quot;test&quot;: &quot;vitest&quot;</pre><p>Your package.json should look similar to:</p><pre>{<br>  &quot;name&quot;: &quot;vite-project&quot;,<br>  &quot;private&quot;: true,<br>  &quot;version&quot;: &quot;0.0.0&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;dev&quot;: &quot;vite&quot;,<br>    &quot;build&quot;: &quot;tsc &amp;&amp; vite build&quot;,<br>    &quot;format&quot;: &quot;prettier --write ./src&quot;,<br>    &quot;lint&quot;: &quot;eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0&quot;,<br>    &quot;preview&quot;: &quot;vite preview&quot;,<br>    &quot;test&quot;: &quot;vitest&quot;<br>  },<br>  &quot;dependencies&quot;: {<br>    &quot;react&quot;: &quot;^18.2.0&quot;,<br>    &quot;react-dom&quot;: &quot;^18.2.0&quot;<br>  },<br>  &quot;devDependencies&quot;: {<br>    &quot;@testing-library/jest-dom&quot;: &quot;^6.4.2&quot;,<br>    &quot;@testing-library/react&quot;: &quot;^14.2.1&quot;,<br>    &quot;@types/react&quot;: &quot;^18.2.55&quot;,<br>    &quot;@types/react-dom&quot;: &quot;^18.2.19&quot;,<br>    &quot;@typescript-eslint/eslint-plugin&quot;: &quot;^6.21.0&quot;,<br>    &quot;@typescript-eslint/parser&quot;: &quot;^6.21.0&quot;,<br>    &quot;@vitejs/plugin-react&quot;: &quot;^4.2.1&quot;,<br>    &quot;eslint&quot;: &quot;^8.56.0&quot;,<br>    &quot;eslint-config-prettier&quot;: &quot;^9.1.0&quot;,<br>    &quot;eslint-plugin-react-hooks&quot;: &quot;^4.6.0&quot;,<br>    &quot;eslint-plugin-react-refresh&quot;: &quot;^0.4.5&quot;,<br>    &quot;jsdom&quot;: &quot;^24.0.0&quot;,<br>    &quot;prettier&quot;: &quot;3.2.5&quot;,<br>    &quot;typescript&quot;: &quot;^5.2.2&quot;,<br>    &quot;vite&quot;: &quot;^5.1.0&quot;,<br>    &quot;vitest&quot;: &quot;^1.2.2&quot;<br>  }<br>}</pre><p>Run npm test to check if everything works as expected.</p><p>Finnaly, here’s what you should pay attention to. Our current .eslintrc.cjs contains only very basic recommended rules, and that’s fine if you need to develop a simple application without micro front-ends and extensive use of various APIs or if you are just starting to use TS. However, in most cases the current basic settings are not enough, and if you’re going to develop a complex production application, it’s better to update your configuration to include type-aware lint rules. Let’s do it!</p><p>Install React-specific linting rules for ESLint:</p><pre>npm i -D eslint-plugin-react</pre><p>Then make changes in .eslintrc.cjs to match the following:</p><pre>module.exports = {<br>  root: true,<br>  env: { browser: true, es2020: true },<br>  extends: [<br>    &#39;eslint:recommended&#39;,<br>    &#39;plugin:react/recommended&#39;,<br>    &#39;plugin:react/jsx-runtime&#39;,<br>    &#39;plugin:@typescript-eslint/recommended-type-checked&#39;,<br>    &#39;plugin:@typescript-eslint/stylistic-type-checked&#39;,<br>    &#39;plugin:react-hooks/recommended&#39;,<br>    // This disables the formatting rules in ESLint that Prettier is going to be responsible for handling.<br>    // Make sure it&#39;s always the last config, so it gets the chance to override other configs.<br>    &#39;prettier&#39;,<br>  ],<br>  ignorePatterns: [&#39;dist&#39;, &#39;.eslintrc.cjs&#39;],<br>  parser: &#39;@typescript-eslint/parser&#39;,<br>  parserOptions: {<br>    ecmaVersion: &#39;latest&#39;,<br>    sourceType: &#39;module&#39;,<br>    project: [&#39;./tsconfig.json&#39;, &#39;./tsconfig.node.json&#39;],<br>    tsconfigRootDir: __dirname,<br>  },<br>  plugins: [&#39;react-refresh&#39;],<br>  rules: {<br>    &#39;react-refresh/only-export-components&#39;: [<br>      &#39;warn&#39;,<br>      { allowConstantExport: true },<br>    ],<br>  },<br>};</pre><p>If you need more hard sort of rules you can replace plugin:@typescript-eslint/recommended-type-checked with plugin:@typescript-eslint/strict-type-checked to ensure strictly typed code.</p><p>Now you can use this project as a boilerplate for your next React application.</p><p>That’s it, folks! Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9bb6e919ac8f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setup a React Vite project with SWC, Prettier & Vitest [2024]]]></title>
            <link>https://medium.com/@nedopaka/setup-a-react-vite-project-with-swc-prettier-vitest-2024-62ecff357c7b?source=rss-11984707caea------2</link>
            <guid isPermaLink="false">https://medium.com/p/62ecff357c7b</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[vitejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vitest]]></category>
            <category><![CDATA[prettier]]></category>
            <dc:creator><![CDATA[Sasha Nedopaka]]></dc:creator>
            <pubDate>Mon, 12 Feb 2024 11:30:05 GMT</pubDate>
            <atom:updated>2024-03-22T18:47:50.964Z</atom:updated>
            <content:encoded><![CDATA[<h4>Easy instructions for quick setup</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NphneUdnuTFsNDod" /><figcaption>Photo by <a href="https://unsplash.com/@flowforfrank?utm_source=medium&amp;utm_medium=referral">Ferenc Almasi</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Even with React frameworks like Next.js gaining popularity and React <a href="https://react.dev/learn/start-a-new-react-project">documentation</a> advocating their use, still many developers tend to start with just using the React library without the additional tools available in the framework out of the box, as this extra layer introduces additional complexity that is unnecessary in many cases.</p><p>Since create-react-app is no longer supported, I’ve found that the best alternative for creating a new React app is to use Vite.</p><p>Below I pasted short description from the official <a href="https://vitejs.dev/guide/">documentation</a>.</p><p>Vite (French word for “quick”, pronounced /vit/, like “veet”) is a build tool that aims to provide a faster and leaner development experience for modern web projects. It consists of two major parts:</p><ul><li>A dev server that provides <a href="https://vitejs.dev/guide/features">rich feature enhancements</a> over <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules">native ES modules</a>, for example extremely fast <a href="https://vitejs.dev/guide/features#hot-module-replacement">Hot Module Replacement (HMR)</a>.</li><li>A build command that bundles your code with <a href="https://rollupjs.org/">Rollup</a>, pre-configured to output highly optimized static assets for production.</li></ul><p>Vite is opinionated and comes with sensible defaults out of the box.</p><p>So, let’s create a new React project with Vite by executing the following command:</p><pre>npm create vite@latest</pre><p>First, you’ll be asked for project name:</p><pre>npm create vite@latest<br>? Project name: › vite-project</pre><p>Then it asks for a framework to choose:</p><pre>npm create vite@latest<br>✔ Project name: … vite-project<br>? Select a framework: › - Use arrow-keys. Return to submit.<br>❯   Vanilla<br>    Vue<br>    React<br>    Preact<br>    Lit<br>    Svelte<br>    Solid<br>    Qwik<br>    Others</pre><p>Select React and hit Enter.</p><p>Then you need to choose whether to use JavaScript/TypeSprit with Babel/SWC:</p><pre>npm create vite@latest<br>✔ Project name: … vite-project<br>✔ Select a framework: › React<br>? Select a variant: › - Use arrow-keys. Return to submit.<br>❯   TypeScript<br>    TypeScript + SWC<br>    JavaScript<br>    JavaScript + SWC</pre><p>Let’s choose <strong>JavaScript + SWC</strong> considering to work with a small project without type checking. SWC is a free and open-source JavaScript transcompiler like Babel, but unlike Babel it can significantly speed up build and development time due to SWC’s fast conversion capabilities, however it may not support all Babel plugins.</p><p>SWC plays an integral role in the Vite ecosystem. Vite uses SWC to run the Babel transformation pipeline much faster, which is especially useful for large React projects.</p><p>It is worth mentioning that Vite also uses Rollup for efficient production builds and supports most Rollup plugins. Thus, Vite offers lightning-fast development and reliable performance in production.</p><p>It’s a final step, and then you should see something like this:</p><pre>npm create vite@latest<br>✔ Project name: … vite-project<br>✔ Select a framework: › React<br>✔ Select a variant: › JavaScript + SWC<br><br>Scaffolding project in /Users/Alex_Nedopaka/vite-project...<br><br>Done. Now run:<br><br>  cd vite-project<br>  npm install<br>  npm run dev</pre><p>Now go to the project folder, install dependencies, start dev server to check that everything works fine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XpyCGYy-69S5XSHR.png" /><figcaption>Default page with App component</figcaption></figure><p>Let’s have a look what we’ve got in our package.json now:</p><pre>{<br>  &quot;name&quot;: &quot;vite-project&quot;,<br>  &quot;private&quot;: true,<br>  &quot;version&quot;: &quot;0.0.0&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;dev&quot;: &quot;vite&quot;,<br>    &quot;build&quot;: &quot;vite build&quot;,<br>    &quot;lint&quot;: &quot;eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0&quot;,<br>    &quot;preview&quot;: &quot;vite preview&quot;<br>  },<br>  &quot;dependencies&quot;: {<br>    &quot;react&quot;: &quot;^18.2.0&quot;,<br>    &quot;react-dom&quot;: &quot;^18.2.0&quot;<br>  },<br>  &quot;devDependencies&quot;: {<br>    &quot;@types/react&quot;: &quot;^18.2.55&quot;,<br>    &quot;@types/react-dom&quot;: &quot;^18.2.19&quot;,<br>    &quot;@vitejs/plugin-react-swc&quot;: &quot;^3.5.0&quot;,<br>    &quot;eslint&quot;: &quot;^8.56.0&quot;,<br>    &quot;eslint-plugin-react&quot;: &quot;^7.33.2&quot;,<br>    &quot;eslint-plugin-react-hooks&quot;: &quot;^4.6.0&quot;,<br>    &quot;eslint-plugin-react-refresh&quot;: &quot;^0.4.5&quot;,<br>    &quot;vite&quot;: &quot;^5.1.0&quot;<br>  }<br>}</pre><p>Vite kindly preinstalls ESLint for us. In .eslintrc.cjs we already have necessary rules for the code quality, and it provides us error highlights in our code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yQBoRlhD5nLuBSbB_hMCvg.png" /><figcaption>src/App.jsx</figcaption></figure><p>Keep in mind, that you should have installed ESLint plugin to make it work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sBuNsN1RezAGwguW.png" /><figcaption>ESLint extension for VS Code</figcaption></figure><p>Looking on package.json it has come to my attention that there are 2 packages with type declarations, although we do not use TypeScript in our project. Let’s get rid of them with npm rm command.</p><pre>npm rm @types/react @types/react-dom</pre><p>Now let’s install Prettier to make our code look nice.</p><pre>npm i -D -E prettier</pre><p>And then create .prettierrc with the following content:</p><pre>{<br>  &quot;semi&quot;: true,<br>  &quot;singleQuote&quot;: true,<br>  &quot;trailingComma&quot;: &quot;es5&quot;<br>}</pre><p>To prevent conflicts in rules between ESLint and Prettier let’s add the following plugin:</p><pre>npm i -D eslint-config-prettier</pre><p>Then make changes in .eslintrc.cjs to match the following:</p><pre>module.exports = {<br>  root: true,<br>  env: { browser: true, es2020: true },<br>  extends: [<br>    &#39;eslint:recommended&#39;,<br>    &#39;plugin:react/recommended&#39;,<br>    &#39;plugin:react/jsx-runtime&#39;,<br>    &#39;plugin:react-hooks/recommended&#39;,<br>    // This disables the formatting rules in ESLint that Prettier is going to be responsible for handling.<br>    // Make sure it&#39;s always the last config, so it gets the chance to override other configs.<br>    &#39;prettier&#39;,<br>  ],<br>  ignorePatterns: [&#39;dist&#39;, &#39;.eslintrc.cjs&#39;],<br>  parserOptions: { ecmaVersion: &#39;latest&#39;, sourceType: &#39;module&#39; },<br>  settings: { react: { version: &#39;18.2&#39; } },<br>  plugins: [&#39;react-refresh&#39;],<br>  rules: {<br>    &#39;react/jsx-no-target-blank&#39;: &#39;off&#39;,<br>    &#39;react-refresh/only-export-components&#39;: [<br>      &#39;warn&#39;,<br>      { allowConstantExport: true },<br>    ],<br>  },<br>};</pre><p>And then add the following script to our package.json:</p><pre>&quot;format&quot;: &quot;prettier --write ./src&quot;,</pre><p>Now we can do npm run format to format All our source code, but it would be great if we could format our files automatically on paste and save actions.</p><p>For this, install Prettier plugin for VS Code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*itLQPnoB36MTpW9C.png" /><figcaption>Prettier extension for VS Code</figcaption></figure><p>And then create .vscode/settings.json so we can use this project as a boilerplate for every our new React project.</p><pre>{<br>  &quot;editor.defaultFormatter&quot;: &quot;esbenp.prettier-vscode&quot;,<br>  &quot;editor.formatOnPaste&quot;: true, // required<br>  &quot;editor.formatOnType&quot;: false, // required<br>  &quot;editor.formatOnSave&quot;: true, // optional<br>  &quot;editor.formatOnSaveMode&quot;: &quot;file&quot;, // required to format on save<br>  &quot;files.autoSave&quot;: &quot;onFocusChange&quot; // optional but recommended<br>}</pre><p>Next, considering that we’re going to write unit tests for our code, let’s install the following dependencies:</p><pre>npm i -D vitest jsdom @testing-library/jest-dom @testing-library/react</pre><p>Then create src/__tests__/setup.js and insert the following code:</p><pre>// jest-dom adds custom jest matchers for asserting on DOM nodes.<br>// allows you to do things like:<br>// expect(element).toHaveTextContent(/react/i)<br>// learn more: https://github.com/testing-library/jest-dom<br>import &#39;@testing-library/jest-dom&#39;;</pre><p>Also, then modify vite.config.js to match:</p><pre>import { defineConfig } from &#39;vite&#39;;<br>import react from &#39;@vitejs/plugin-react-swc&#39;;<br><br>// https://vitejs.dev/config/<br>export default defineConfig({<br>  plugins: [react()],<br>  test: {<br>    globals: true,<br>    environment: &#39;jsdom&#39;,<br>    setupFiles: [&#39;src/__tests__/setup.js&#39;],<br>  },<br>});</pre><p>Now we’re ready to write our first test App.test.jsx:</p><pre>import { describe, it, expect, test } from &#39;vitest&#39;;<br>import { render } from &#39;@testing-library/react&#39;;<br>import App from &#39;./App&#39;;<br><br>test(&#39;demo&#39;, () =&gt; {<br>  expect(true).toBe(true);<br>});<br><br>describe(&#39;render&#39;, () =&gt; {<br>  it(&#39;renders the main page&#39;, () =&gt; {<br>    render(&lt;App /&gt;);<br>    expect(true).toBeTruthy();<br>  });<br>});</pre><p>Finally, add the following script in package.json:</p><pre>&quot;test&quot;: &quot;vitest&quot;</pre><p>Your package.json should look similar to:</p><pre>{<br>  &quot;name&quot;: &quot;vite-project&quot;,<br>  &quot;private&quot;: true,<br>  &quot;version&quot;: &quot;0.0.0&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;dev&quot;: &quot;vite&quot;,<br>    &quot;build&quot;: &quot;vite build&quot;,<br>    &quot;format&quot;: &quot;prettier --write ./src&quot;,<br>    &quot;lint&quot;: &quot;eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0&quot;,<br>    &quot;preview&quot;: &quot;vite preview&quot;,<br>    &quot;test&quot;: &quot;vitest&quot;<br>  },<br>  &quot;dependencies&quot;: {<br>    &quot;react&quot;: &quot;^18.2.0&quot;,<br>    &quot;react-dom&quot;: &quot;^18.2.0&quot;<br>  },<br>  &quot;devDependencies&quot;: {<br>    &quot;@testing-library/jest-dom&quot;: &quot;^6.4.2&quot;,<br>    &quot;@testing-library/react&quot;: &quot;^14.2.1&quot;,<br>    &quot;@vitejs/plugin-react-swc&quot;: &quot;^3.5.0&quot;,<br>    &quot;eslint&quot;: &quot;^8.56.0&quot;,<br>    &quot;eslint-config-prettier&quot;: &quot;^9.1.0&quot;,<br>    &quot;eslint-plugin-react&quot;: &quot;^7.33.2&quot;,<br>    &quot;eslint-plugin-react-hooks&quot;: &quot;^4.6.0&quot;,<br>    &quot;eslint-plugin-react-refresh&quot;: &quot;^0.4.5&quot;,<br>    &quot;jsdom&quot;: &quot;^24.0.0&quot;,<br>    &quot;prettier&quot;: &quot;3.2.5&quot;,<br>    &quot;vite&quot;: &quot;^5.1.0&quot;,<br>    &quot;vitest&quot;: &quot;^1.2.2&quot;<br>  }<br>}</pre><p>Run npm test to check if everything works as expected.</p><p>Now you can use this project as a boilerplate for your next React application.</p><p>That’s it, folks! Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62ecff357c7b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setup a React Vite project with Prettier & Vitest [2024]]]></title>
            <link>https://medium.com/@nedopaka/setup-a-react-vite-project-with-prettier-vitest-2024-12e291669b4b?source=rss-11984707caea------2</link>
            <guid isPermaLink="false">https://medium.com/p/12e291669b4b</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[vitest]]></category>
            <category><![CDATA[prettier]]></category>
            <category><![CDATA[vitejs]]></category>
            <dc:creator><![CDATA[Sasha Nedopaka]]></dc:creator>
            <pubDate>Thu, 01 Feb 2024 21:19:10 GMT</pubDate>
            <atom:updated>2024-02-01T21:21:22.826Z</atom:updated>
            <content:encoded><![CDATA[<h4>Easy instructions for quick setup</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NOpx0JbZkGYKrW3P" /><figcaption>Photo by <a href="https://unsplash.com/@thomastastet?utm_source=medium&amp;utm_medium=referral">Thomas Tastet</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Even with React frameworks like Next.js gaining popularity and React <a href="https://react.dev/learn/start-a-new-react-project">documentation</a> advocating their use, still many developers tend to start with just using the React library without the additional tools available in the framework out of the box, as this extra layer introduces additional complexity that is unnecessary in many cases.</p><p>Since create-react-app is no longer supported, I’ve found that the best alternative for creating a new React app is to use Vite.</p><p>Below I pasted short description from the official <a href="https://vitejs.dev/guide/">documentation</a>.</p><p>Vite (French word for “quick”, pronounced /vit/, like “veet”) is a build tool that aims to provide a faster and leaner development experience for modern web projects. It consists of two major parts:</p><ul><li>A dev server that provides <a href="https://vitejs.dev/guide/features">rich feature enhancements</a> over <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules">native ES modules</a>, for example extremely fast <a href="https://vitejs.dev/guide/features#hot-module-replacement">Hot Module Replacement (HMR)</a>.</li><li>A build command that bundles your code with <a href="https://rollupjs.org/">Rollup</a>, pre-configured to output highly optimized static assets for production.</li></ul><p>Vite is opinionated and comes with sensible defaults out of the box.</p><p>So, let’s create a new React project with Vite by executing the following command:</p><pre>npm create vite@latest</pre><p>First, you’ll be asked for project name:</p><pre>npm create vite@latest<br>? Project name: › vite-project</pre><p>Then it asks for a framework to choose:</p><pre>npm create vite@latest<br>✔ Project name: … vite-project<br>? Select a framework: › - Use arrow-keys. Return to submit.<br>❯   Vanilla<br>    Vue<br>    React<br>    Preact<br>    Lit<br>    Svelte<br>    Solid<br>    Qwik<br>    Others</pre><p>Select React and hit Enter.</p><p>Then you need to choose whether to use JavaScript/TypeSprit with Babel/SWC:</p><pre>npm create vite@latest<br>✔ Project name: … vite-project<br>✔ Select a framework: › React<br>? Select a variant: › - Use arrow-keys. Return to submit.<br>❯   TypeScript<br>    TypeScript + SWC<br>    JavaScript<br>    JavaScript + SWC</pre><p>Let’s choose JavaScript considering to work with a small project.</p><p>It’s a final step, and then you should see something like this:</p><pre>npm create vite@latest<br>✔ Project name: … vite-project<br>✔ Select a framework: › React<br>✔ Select a variant: › JavaScript<br><br>Scaffolding project in /Users/Alex_Nedopaka/vite-project...<br><br>Done. Now run:<br><br>  cd vite-project<br>  npm install<br>  npm run dev</pre><p>Now go to the project folder, install dependencies, start dev server to check that everything works fine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uBmUd3A6pXph0frOc8VWTg.png" /><figcaption>Default page with App component</figcaption></figure><p>Let’s have a look what we’ve got in our package.json now:</p><pre>{<br>  &quot;name&quot;: &quot;vite-project&quot;,<br>  &quot;private&quot;: true,<br>  &quot;version&quot;: &quot;0.0.0&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;dev&quot;: &quot;vite&quot;,<br>    &quot;build&quot;: &quot;vite build&quot;,<br>    &quot;lint&quot;: &quot;eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0&quot;,<br>    &quot;preview&quot;: &quot;vite preview&quot;<br>  },<br>  &quot;dependencies&quot;: {<br>    &quot;react&quot;: &quot;^18.2.0&quot;,<br>    &quot;react-dom&quot;: &quot;^18.2.0&quot;<br>  },<br>  &quot;devDependencies&quot;: {<br>    &quot;@types/react&quot;: &quot;^18.2.43&quot;,<br>    &quot;@types/react-dom&quot;: &quot;^18.2.17&quot;,<br>    &quot;@vitejs/plugin-react&quot;: &quot;^4.2.1&quot;,<br>    &quot;eslint&quot;: &quot;^8.55.0&quot;,<br>    &quot;eslint-plugin-react&quot;: &quot;^7.33.2&quot;,<br>    &quot;eslint-plugin-react-hooks&quot;: &quot;^4.6.0&quot;,<br>    &quot;eslint-plugin-react-refresh&quot;: &quot;^0.4.5&quot;,<br>    &quot;vite&quot;: &quot;^5.0.8&quot;<br>  }<br>}</pre><p>Vite kindly preinstalls ESLint for us. In .eslintrc.cjs we already have necessary rules for the code quality, and it provides us error highlights in our code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ggBSxdlntdOi_w7WC-6Lew.png" /><figcaption>src/App.jsx</figcaption></figure><p>Keep in mind, that you should have installed ESLint plugin to make it work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xtrSrG1CkdAos6Hk5D7DAQ.png" /><figcaption>ESLint extension for VS Code</figcaption></figure><p>Looking on package.json it has come to my attention that there are 2 packages with type declarations, although we do not use TypeScript in our project. Let’s get rid of them with npm rm command.</p><pre>npm rm @types/react @types/react-dom</pre><p>Now let’s install Prettier to make our code look nice.</p><pre>npm i -D -E prettier</pre><p>And then create .prettierrc with the following content:</p><pre>{<br>  &quot;semi&quot;: true,<br>  &quot;singleQuote&quot;: true,<br>  &quot;trailingComma&quot;: &quot;es5&quot;<br>}</pre><p>To prevent conflicts in rules between ESLint and Prettier, and also to add linting of ES2015+ (ES6+) import/export syntax, let’s add the following plugins:</p><pre>npm i -D eslint-config-prettier eslint-plugin-prettier eslint-plugin-import</pre><p>Then make changes in .eslintrc.cjs to match the following:</p><pre>module.exports = {<br>  root: true,<br>  env: { browser: true, es2020: true },<br>  extends: [<br>    &#39;eslint:recommended&#39;,<br>    &#39;plugin:react/recommended&#39;,<br>    &#39;plugin:react/jsx-runtime&#39;,<br>    &#39;plugin:react-hooks/recommended&#39;,<br>    // This disables the formatting rules in ESLint that Prettier is going to be responsible for handling.<br>    // Make sure it&#39;s always the last config, so it gets the chance to override other configs.<br>    &#39;eslint-config-prettier&#39;,<br>  ],<br>  ignorePatterns: [&#39;dist&#39;, &#39;.eslintrc.cjs&#39;],<br>  parserOptions: { ecmaVersion: &#39;latest&#39;, sourceType: &#39;module&#39; },<br>  settings: { react: { version: &#39;18.2&#39; } },<br>  plugins: [&#39;react-refresh&#39;, &#39;import&#39;],<br>  rules: {<br>    &#39;react-refresh/only-export-components&#39;: [<br>      &#39;warn&#39;,<br>      { allowConstantExport: true },<br>    ],<br>    &#39;import/newline-after-import&#39;: [&#39;error&#39;, { count: 1 }],<br>  },<br>};</pre><p>And then add the following script to our package.json:</p><pre>&quot;format&quot;: &quot;prettier --write ./src&quot;,</pre><p>Now we can do npm run format to format All our source code, but it would be great if we could format our files automatically on paste and save actions.</p><p>For this, install Prettier plugin for VS Code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pnZVkfxXUHKz7ivpfQDzLg.png" /><figcaption>Prettier extension for VS Code</figcaption></figure><p>And then create .vscode/settings.json so we can use this project as a boilerplate for every our new React project.</p><pre>{<br>  &quot;editor.defaultFormatter&quot;: &quot;esbenp.prettier-vscode&quot;,<br>  &quot;editor.formatOnPaste&quot;: true, // required<br>  &quot;editor.formatOnType&quot;: false, // required<br>  &quot;editor.formatOnSave&quot;: true, // optional<br>  &quot;editor.formatOnSaveMode&quot;: &quot;file&quot;, // required to format on save<br>  &quot;files.autoSave&quot;: &quot;onFocusChange&quot; // optional but recommended<br>}</pre><p>Next, considering that we’re going to write unit tests for our code, let’s install the following dependencies:</p><pre>npm i -D vitest jsdom @testing-library/jest-dom @testing-library/react</pre><p>Then create src/__tests__/setup.js and insert the following code:</p><pre>// jest-dom adds custom jest matchers for asserting on DOM nodes.<br>// allows you to do things like:<br>// expect(element).toHaveTextContent(/react/i)<br>// learn more: https://github.com/testing-library/jest-dom<br>import &#39;@testing-library/jest-dom&#39;;</pre><p>Also, then modify vite.config.js to match:</p><pre>import { defineConfig } from &#39;vite&#39;;<br>import react from &#39;@vitejs/plugin-react&#39;;<br><br>// https://vitejs.dev/config/<br>export default defineConfig({<br>  plugins: [react()],<br>  test: {<br>    globals: true,<br>    environment: &#39;jsdom&#39;,<br>    setupFiles: [&#39;src/__tests__/setup.js&#39;],<br>  },<br>});</pre><p>Now we’re ready to write our first test App.test.jsx:</p><pre>import { describe, it, expect, test } from &#39;vitest&#39;;<br>import { render } from &#39;@testing-library/react&#39;;<br>import App from &#39;./App&#39;;<br><br>test(&#39;demo&#39;, () =&gt; {<br>  expect(true).toBe(true);<br>});<br><br>describe(&#39;render&#39;, () =&gt; {<br>  it(&#39;renders the main page&#39;, () =&gt; {<br>    render(&lt;App /&gt;);<br>    expect(true).toBeTruthy();<br>  });<br>});</pre><p>Finally, add the following script in package.json:</p><pre>&quot;test&quot;: &quot;vitest&quot;</pre><p>Your package.json should look similar to:</p><pre>{<br>  &quot;name&quot;: &quot;vite-project&quot;,<br>  &quot;private&quot;: true,<br>  &quot;version&quot;: &quot;0.0.0&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;dev&quot;: &quot;vite&quot;,<br>    &quot;build&quot;: &quot;vite build&quot;,<br>    &quot;format&quot;: &quot;prettier --write ./src&quot;,<br>    &quot;lint&quot;: &quot;eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0&quot;,<br>    &quot;preview&quot;: &quot;vite preview&quot;,<br>    &quot;test&quot;: &quot;vitest&quot;<br>  },<br>  &quot;dependencies&quot;: {<br>    &quot;react&quot;: &quot;^18.2.0&quot;,<br>    &quot;react-dom&quot;: &quot;^18.2.0&quot;<br>  },<br>  &quot;devDependencies&quot;: {<br>    &quot;@testing-library/jest-dom&quot;: &quot;^6.3.0&quot;,<br>    &quot;@testing-library/react&quot;: &quot;^14.1.2&quot;,<br>    &quot;@vitejs/plugin-react&quot;: &quot;^4.2.1&quot;,<br>    &quot;eslint&quot;: &quot;^8.55.0&quot;,<br>    &quot;eslint-config-prettier&quot;: &quot;^9.1.0&quot;,<br>    &quot;eslint-plugin-import&quot;: &quot;^2.29.1&quot;,<br>    &quot;eslint-plugin-prettier&quot;: &quot;^5.1.3&quot;,<br>    &quot;eslint-plugin-react&quot;: &quot;^7.33.2&quot;,<br>    &quot;eslint-plugin-react-hooks&quot;: &quot;^4.6.0&quot;,<br>    &quot;eslint-plugin-react-refresh&quot;: &quot;^0.4.5&quot;,<br>    &quot;jsdom&quot;: &quot;^24.0.0&quot;,<br>    &quot;prettier&quot;: &quot;3.2.4&quot;,<br>    &quot;vite&quot;: &quot;^5.0.8&quot;,<br>    &quot;vitest&quot;: &quot;^1.2.1&quot;<br>  }<br>}</pre><p>Run npm test to check if everything works as expected.</p><p>Now you can use this project as a boilerplate for your next React application.</p><p>That’s it, folks! Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=12e291669b4b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create new RoR project [2024]]]></title>
            <link>https://medium.com/@nedopaka/create-new-ror-project-2024-57551dd25ec6?source=rss-11984707caea------2</link>
            <guid isPermaLink="false">https://medium.com/p/57551dd25ec6</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails-development]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Sasha Nedopaka]]></dc:creator>
            <pubDate>Thu, 01 Feb 2024 13:56:14 GMT</pubDate>
            <atom:updated>2024-02-01T13:56:14.872Z</atom:updated>
            <content:encoded><![CDATA[<h4>All you need to know to get started</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*N3LOrWa8bLQI_gMc" /><figcaption>Photo by <a href="https://unsplash.com/@spacex?utm_source=medium&amp;utm_medium=referral">SpaceX</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Rails provides everything you need to create a web application. So, let’s dive into the topic with some practical examples.</p><p>To generate a new project with a basic Rails skeleton, use the following command:</p><pre>rails new APP_PATH [options]</pre><p>For a complete list of options use rails new -h in your terminal.</p><p>Keep in mind that you can change your project’s preferences any time you want during development process.</p><p>Rails is using SQLite database by default, but you can choose any from the following list:</p><pre>mysql/trilogy/postgresql/sqlite3/oracle/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc</pre><p>For example, let’s create a brand new my_blog application that utilises MySQL database. To do it use the following command:</p><pre>rails new my_blog -d mysql</pre><p>But again, if the current configuration doesn’t suite you anymore or eventually you decided to deploy your app to Heroku which uses Prostgre, — don’t worry, you can always change database later by switching to another option e.g. PostreSQL via the following command:</p><pre>rails db:system:change --to=postgresql</pre><p>Now you can run Puma server via rails s command, and open <a href="http://localhost:3000/">http://localhost:3000/</a> to check if everything looks good.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rTUoDZZoltAsRUdD9m41WA.png" /><figcaption>Index page served by Puma</figcaption></figure><p>Next, to generate a scaffold for our blog posts use:</p><pre>rails g scaffold post title:string content:text</pre><p>Under the hood, it does the following:</p><ul><li>generates new migration that creates Posts table at e.g. db/migrate/20240131185821_create_posts.rb</li><li>creates Post model at app/models/post.rb</li><li>creates PostsController at app/controllers/posts_controller.rb with index, show, new, edit actions to get posts data from the database, and create, update, destroy actions to modify data in the database (REST API) — 7 default actions for CRUD operations</li><li>creates respective views at app/views/posts with html.erb extension, and several files with json.jbuilder extension for the Posts JSON API that comes out of the box</li></ul><p>Now if you try to reload <a href="http://localhost:3000/">http://localhost:3000/</a>, you’ll get the following error:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*13EOV_MCOFdlXH6lYA56nQ.png" /><figcaption>ActiveRecord::PendingMigrationError</figcaption></figure><p>Migration is a technique that helps transition database schemas from their current state to a new desired state, whether that involves adding tables and columns, removing elements, splitting fields, or changing types and constraints.</p><p>Also, migrations allow you to “travel” through different states or so called “versions” of your app’s database. Keep in mind, Rails won’t allow you to run application if some migration is pending.</p><p>To check migrations status use:</p><pre>rails db:migrate:status</pre><p>You shell see a table with Status, Migration ID, Migration Name columns.</p><p>Status can be up or down. It’s down if the migration is NOT applied.</p><p>Migration ID is an autogenerated timestamp for the migration, and you can find it as a prefix in the migration’s filename.</p><p>Migration Name corresponds to the name of your migration’s class.</p><p>To apply All migrations use:</p><pre>rails db:migrate</pre><p>Now you should see <a href="http://localhost:3000/">http://localhost:3000/</a> without errors.</p><p>To migrate up or down until it gets to the specific version and then stop use:</p><pre>rails db:migrate VERSION=[timestamp]</pre><p>To undo All migrations use:</p><pre>rails db:migrate VERSION=0</pre><p>Keep in mind, there are a lot of ways to generate migration automatically with a new model via generate function e.g. rails g model NAME [field[:type][:index] field[:type][:index]] [options] or rails generate resource NAME [field[:type][:index] field[:type][:index]] [options] when you need to generate relation “one-to-many” etc.</p><p>If you need just a migration use:</p><pre>rails generate migration NAME [field[:type][:index] field[:type][:index]] [options]</pre><p>For example, to generate the Post model and its migration use:</p><pre>rails g model Post title:string content:text</pre><p>The default type for new columns is String, so you can skip explicit type declaration for the Title column in the command above.</p><p>You can check current schema of your app’s database at db/schema.rb file.</p><p>Now our Posts is a basic CRUD app created with the scaffold command at <a href="http://localhost:3000/posts">http://localhost:3000/posts</a>, where you can view All your posts and create/read/update/delete any post.</p><p>To make the title field mandatory just add validates_presence_of :title to your model at app/models/post.rb:</p><pre>class Post &lt; ApplicationRecord<br>  validates_presence_of :title<br>end</pre><p>Let’s add rich text editing capabilities to our blog with action_text package via:</p><pre>rails action_text:install</pre><p>It’ll add a little bit of JavaScript, CSS for our rich text editor, and a set of migrations to upload and store files using ActiveStorage. It also adds image_processing gem to our Gemfile.</p><p>To install it use bundle command.</p><p>Next, add rich text editing capabilities to the content field in app/models/post.rb:</p><pre>class Post &lt; ApplicationRecord<br>  validates_presence_of :title<br>  has_rich_text :content<br>end</pre><p>And in app/views/posts/_form.html.erb for the content instead of having form.text_area we change it to form.rich_text_area:</p><pre>...<br>  &lt;div&gt;<br>    &lt;%= form.label :content, style: &quot;display: block&quot; %&gt;<br>    &lt;%= form.rich_text_area :content %&gt;<br>  &lt;/div&gt;<br>...</pre><p>To fetch local-time npm package by running an API using JSPM use the following command:</p><pre>./bin/importmap pin local-time</pre><p>JSPM provides package management for import maps, allowing any package from npm to be loaded directly in the browser fully optimized without further tooling. It does’t need to have node/npm installed on your machine.</p><p>Now add the following lines to app/javascript/application.js:</p><pre>import LocalTime from &quot;local-time&quot;<br>LocalTime.start()</pre><p>Next add time_tag beneath the Title at app/views/posts/_post.html.erb:</p><pre>  &lt;p&gt;<br>    Posted &lt;%= time_tag post.created_at, &quot;data-local&quot;: &quot;time-ago&quot; %&gt;<br>  &lt;/p&gt;</pre><p>Verify that everything works by running rails s, and visiting <a href="http://localhost:3000/">http://localhost:3000/</a> in your browser.</p><p>That’s it, folks! Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57551dd25ec6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setup RoR project with MySQL DB on Mac with M1/M2/M3 running Sonoma (macOS 14) [2024]]]></title>
            <link>https://blog.devops.dev/setup-ror-project-with-mysql-db-on-mac-with-m1-m2-m3-running-sonoma-macos-14-2024-a52c588919fa?source=rss-11984707caea------2</link>
            <guid isPermaLink="false">https://medium.com/p/a52c588919fa</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[mac]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Sasha Nedopaka]]></dc:creator>
            <pubDate>Thu, 11 Jan 2024 21:02:21 GMT</pubDate>
            <atom:updated>2024-01-15T17:22:26.504Z</atom:updated>
            <content:encoded><![CDATA[<h4>Eliminating ‘Gem::Ext::BuildError: ERROR: Failed to build gem native extension’</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QieEfgNuqm_H1UQN" /><figcaption>Photo by <a href="https://unsplash.com/@nvte?utm_source=medium&amp;utm_medium=referral">Nathan Duck</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Today I decided to create a new Rails project using MySQL and ran into a problem installing the gem “mysql2” on my laptop with Apple silicon SoC.<br>Below I will give an example. I hope it’ll help you to spare some time to solve this problem and understand its reason, because this problem can occur not only with mysql2.</p><p>The mysql2 gem comes with native extensions, meaning it comes in two parts: the ruby gem and a native system library. The gem relies on the library to work, which is why it needs to be compiled on your machine.</p><p>Once <a href="https://medium.com/devops-dev/rvm-ror-installation-on-mac-with-m1-m2-m3-running-sonoma-macos-14-2024-77d4df86bd7c">RVM, Ruby and RoR</a> are installed, let’s do the following:</p><ol><li>Install Homebrew and execute brew install mysql</li><li>Run brew services start mysql to start MySQL. To secure root with password, run mysql_secure_installation</li><li>Verify ability to connect to MySQL via mysql -u root</li><li>Next create new project rails new my_blog -d mysql</li><li>Rails creates new folder my_blog with a lot of files, and then it runs bundle install to install dependencies. As the result you may catch the following error:</li></ol><pre>Installing mysql2 0.5.5 with native extensions<br>Gem::Ext::BuildError: ERROR: Failed to build gem native extension.<br>...<br>compiling statement.c<br>linking shared-object mysql2/mysql2.bundle<br>ld: library &#39;zstd&#39; not found<br>clang: error: linker command failed with exit code 1 (use -v to see invocation)<br>make: *** [mysql2.bundle] Error 1<br>...<br>make failed, exit code 2<br>An error occurred while installing mysql2 (0.5.5), and Bundler cannot continue.<br>In Gemfile:<br>  mysql2</pre><p>This error occurs because during mysql2 gem installation on Mac with M1 or higher, Ruby is trying to build mysql2 bundle from scratch by compiling native extensions that may use 3rd party libraries, like zstd in our case. And these 3rd party libraries may not be compatible with Apple Silicon (arm64e). And that’s the case!</p><p>To fix it we need to install mysql2 by hand:</p><pre>gem install mysql2 -- --with-mysql-dir=$(brew --prefix mysql)</pre><p>Or specify binary version explicitly:</p><pre>arch -x86_64 gem install mysql2 -- --with-mysql-dir=$(brew --prefix mysql)</pre><p>The arch command simply overrides the automatic selection of the most compatible binary by explicitly choosing which to run. Your can get more info via man arch.</p><p>Next, just do bundle inside your project folder, and if all goes well you should see something like this:</p><pre>Bundle complete! 15 Gemfile dependencies, 83 gems now installed.<br>Use `bundle info [gemname]` to see where a bundled gem is installed.</pre><p>Now you can edit config/database.yml and run rails db:create to create your database.</p><p>Verify that everything works by running rails s, and visiting <a href="http://localhost:3000/">http://localhost:3000/</a> in your browser.</p><p>That’s it, folks! Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a52c588919fa" width="1" height="1" alt=""><hr><p><a href="https://blog.devops.dev/setup-ror-project-with-mysql-db-on-mac-with-m1-m2-m3-running-sonoma-macos-14-2024-a52c588919fa">Setup RoR project with MySQL DB on Mac with M1/M2/M3 running Sonoma (macOS 14) [2024]</a> was originally published in <a href="https://blog.devops.dev/">DevOps.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RVM & RoR installation on Mac with M1/M2/M3 running Sonoma (macOS 14) [2024]]]></title>
            <link>https://blog.devops.dev/rvm-ror-installation-on-mac-with-m1-m2-m3-running-sonoma-macos-14-2024-77d4df86bd7c?source=rss-11984707caea------2</link>
            <guid isPermaLink="false">https://medium.com/p/77d4df86bd7c</guid>
            <category><![CDATA[mac]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[rvm]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Sasha Nedopaka]]></dc:creator>
            <pubDate>Mon, 01 Jan 2024 01:52:10 GMT</pubDate>
            <atom:updated>2024-01-02T17:57:28.370Z</atom:updated>
            <content:encoded><![CDATA[<h4>Eliminating ‘__rvm_make -j8’ errors during Ruby installation</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Pr50AIIrHMzT1BIwclzrrg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@theregisti?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash">TheRegisti</a> on <a href="https://unsplash.com/photos/a-close-up-of-an-apple-laptop-on-a-table-UIAGNeMUHdE?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash">Unsplash</a></figcaption></figure><p>I’ve run into a problem recently trying to clean install RVM with the latest stable RoR on my laptop with Apple silicon SoC. So, at first I just copied and tried to execute following command from <a href="https://rvm.io/rvm/install#any-other-system">the official RVM documentation</a>:</p><pre>\curl -sSL https://get.rvm.io | bash -s stable --rails</pre><p>But it failed with the following error at the Ruby compiling stage:</p><pre>ruby-3.0.0 - #compiling.................................................................|<br>Error running &#39;__rvm_make -j8&#39;,<br>please read /Users/Oleksandr_Nedopaka/.rvm/log/1704022949_ruby-3.0.0/make.log<br><br>There has been an error while running make. Halting the installation.</pre><p>So, after some experiments I’ve come with the following solution:</p><ol><li>Install just RVM via \curl -sSL https://get.rvm.io | bash</li><li>Next install any Ruby via rvm install 3.1.2 --autolibs=disable</li><li>Install RoR gem install rails -v 7.1.2</li></ol><p>That’s it, folks! Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77d4df86bd7c" width="1" height="1" alt=""><hr><p><a href="https://blog.devops.dev/rvm-ror-installation-on-mac-with-m1-m2-m3-running-sonoma-macos-14-2024-77d4df86bd7c">RVM &amp; RoR installation on Mac with M1/M2/M3 running Sonoma (macOS 14) [2024]</a> was originally published in <a href="https://blog.devops.dev/">DevOps.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>