avatarDaniel Craciun

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3278

Abstract

save for added convenience.</p><h2 id="d76d">Configuring Prettier Settings And Plugins</h2><ol><li>Create a new file called <code>prettier.config.<b>c</b>js</code> at the root of your project. Note that we’re using the <code>.<b>c</b>js</code> file extension because <b>Prettier</b> fully supports CommonJS configurations within a default Next.js 13 environment.</li><li>Copy the following code within your configuration file:</li></ol><div id="3ebf"><pre><span class="hljs-comment">/** <span class="hljs-doctag">@type</span> {import('prettier').Config} /</span> <span class="hljs-keyword">module</span>.<span class="hljs-keyword">exports</span> = { endOfLine: <span class="hljs-string">"lf"</span>, semi: <span class="hljs-literal">false</span>, singleQuote: <span class="hljs-literal">false</span>, tabWidth: <span class="hljs-number">2</span>, trailingComma: <span class="hljs-string">"es5"</span>, importOrder: [ <span class="hljs-string">"^(react/(.))|^(react)"</span>, <span class="hljs-string">"^(next/(.))|^(next)"</span>, <span class="hljs-string">"<THIRD_PARTY_MODULES>"</span>, <span class="hljs-string">""</span>, <span class="hljs-string">"^types"</span>, <span class="hljs-string">"^@/types/(.*)"</span>, <span class="hljs-string">"^@/config/(.)"</span>, <span class="hljs-string">"^@/lib/(.*)"</span>, <span class="hljs-string">"^@/hooks/(.)"</span>, <span class="hljs-string">"^@/components/ui/(.*)"</span>, <span class="hljs-string">"^@/components/(.)"</span>, <span class="hljs-string">"^@/styles/(.*)"</span>, <span class="hljs-string">"^@/app/(.*)$"</span>, <span class="hljs-string">""</span>, <span class="hljs-string">"^[./]"</span>, ], importOrderSeparation: <span class="hljs-literal">false</span>, importOrderSortSpecifiers: <span class="hljs-literal">true</span>, importOrderBuiltinModulesToTop: <span class="hljs-literal">true</span>, importOrderParserPlugins: [<span class="hljs-string">"typescript"</span>, <span class="hljs-string">"jsx"</span>, <span class="hljs-string">"decorators-legacy"</span>], importOrderMergeDuplicateImports: <span class="hljs-literal">true</span>, importOrderCombineTypeAndValueImports: <span class="hljs-literal">true</span>, plugins: [ <span class="hljs-string">"@ianvs/prettier-plugin-sort-imports"</span>, <span class="hljs-string">"prettier-plugin-tailwindcss"</span>, ], }</pre></div><ul><li>These are my <b>personal settings</b>, feel free to visit <a href="https://prettier.io/docs/en/configuration.html">this page</a> and incorporate other configurations to personalize your experience.</li><li>The <code>importOrder</code> option is specific to <a href="https://github.com/IanVS/prettier-plugin-sort-imports">this package</a>, I recommend reading through the <code>README.md</code> for help on further configuration.</li><li>The <code>@/</code> folder alias can be configured inside your <code>tsconfig.json</code> file as follows:</li></ul><div id="4a30"><pre><span class="hljs-attr">"compilerOptions"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> <span class="hljs-comment">// ...</span> <span class="hljs-attr">"paths"</span><span class=

Options

"hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> <span class="hljs-attr">"@/"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-string">"./src/"</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span> <span class="hljs-comment">// "@/: ["./"] <<< if not using the src/ directory</span> <span class="hljs-punctuation">}</span> <span class="hljs-comment">// ...</span> <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span></pre></div><h2 id="b366">Avoid Prettier Formatting For Certain Directories</h2><p id="ecab">Before we harness the power of <b>Prettier</b> formatting, we must ensure files that don’t require formatting, such as the <code>node_modules</code> files, are ignored by <b>Prettier</b>.</p><ol><li>We achieve this by creating a file called <code>.prettierignore</code> at the root of our Next.js project.</li><li>Inside this file, I recommend you add the following code:</li></ol><div id="867c"><pre>dist node_modules .<span class="hljs-built_in">next</span> build</pre></div><p id="3dfa">These directories contain code that shouldn’t be touched by <b>Prettier </b>since this may introduce bugs as well as increase formatting time.</p><p id="e2f3">3. To format all your files at once, run this command in the terminal:</p><div id="d73b"><pre>npm prettier -w . pnpm prettier -w . yarn prettier -w .</pre></div><h1 id="779b">Conclusion</h1><p id="1b06">And that’s about it!</p><p id="89ad">If you enjoyed this article, check out my profile for many more stories like this, and stay tuned for future articles! 👍</p><h2 id="ca8a">Further Reading 📖</h2><div id="2cc1" class="link-block"> <a href="https://blog.stackademic.com/effortless-pre-commit-linting-supercharging-your-next-js-13-project-with-husky-and-eslint-c79a26a10e96"> <div> <div> <h2>Effortless Pre-Commit Linting: Supercharging Your Next.js 13 Project with Husky and ESLint</h2> <div><h3>Dive into the world of streamlined development as we explore how to seamlessly integrate Husky and ESLint into your…</h3></div> <div><p>blog.stackademic.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*49lef4w2viMVBekAvQkPUw.png)"></div> </div> </div> </a> </div><div id="0329" class="link-block"> <a href="https://blog.stackademic.com/react-context-is-officially-dead-enter-zustand-state-management-bca413ca2bd1"> <div> <div> <h2>React Context is Officially Dead; Enter Zustand State Management.</h2> <div><h3>In the ever-evolving landscape of frontend development, React Context has long been a stalwart for state management…</h3></div> <div><p>blog.stackademic.co</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*Pyp04jHIAvfNiv7bu5WcHQ.png)"></div> </div> </div> </a> </div></article></body>

Effortless Code Formatting: Setting Up Prettier with Next.js 13

Prettier: source

In the ever-evolving world of web development, maintaining clean and consistent code is paramount.

If you’re diving into Next.js 13, you’re in for a treat. This article will guide you through the process of setting up Prettier, the popular code formatter, to streamline your coding experience.

Say goodbye to manual formatting hassles and hello to code that’s not just functional but also beautifully organized. Let’s dive into the world of code elegance with Next.js 13 and Prettier.

Table of Contents

  • Setup Next.js 13 Project Environment
  • Dependencies Installation
  • Prettier Visual Studio Code Setup
  • Configuring Prettier Settings And Plugins
  • Avoid Prettier Formatting For Certain Directories

Setup Next.js 13 Project Environment

To setup a Next.js project, please check out the instructions here. The installation process generally consists of running the following command in the terminal.

npx create-next-app@latest

Dependencies Installation

Open a terminal within your project directory, and execute one of the following commands to get started:

npm install prettier prettier-plugin-tailwindcss @ianvs/prettier-plugin-sort-imports -D
yarn add prettier prettier-plugin-tailwindcss @ianvs/prettier-plugin-sort-imports -D
pnpm add prettier prettier-plugin-tailwindcss @ianvs/prettier-plugin-sort-imports -D
  • prettier is the core package.
  • prettier-plugin-tailwindcss organizes tailwind classes automatically.
  • @ianvs/prettier-plugin-sort-imports organizes imports based on configuration you provide.

Prettier Visual Studio Code Setup

Prettier is best suited for visual studio code; if you happen to use an alternative editor, I suggest visiting this page to install the relevant extension.

  1. Inside your vs-code editor, press Ctrl-Shift-X to open up the extensions marketplace, and search Prettier.
  2. Install the extension and restart vs-code to save the changes.

Your extensions page should look something like this:

Prettier vs-code extension page

3. Click Ctrl+, and search default formatter; select Prettier.

4. Additionally, search format on save and enable it so Prettier runs on file save for added convenience.

Configuring Prettier Settings And Plugins

  1. Create a new file called prettier.config.cjs at the root of your project. Note that we’re using the .cjs file extension because Prettier fully supports CommonJS configurations within a default Next.js 13 environment.
  2. Copy the following code within your configuration file:
/** @type {import('prettier').Config} */
module.exports = {
  endOfLine: "lf",
  semi: false,
  singleQuote: false,
  tabWidth: 2,
  trailingComma: "es5",
  importOrder: [
    "^(react/(.*)$)|^(react$)",
    "^(next/(.*)$)|^(next$)",
    "<THIRD_PARTY_MODULES>",
    "",
    "^types$",
    "^@/types/(.*)$",
    "^@/config/(.*)$",
    "^@/lib/(.*)$",
    "^@/hooks/(.*)$",
    "^@/components/ui/(.*)$",
    "^@/components/(.*)$",
    "^@/styles/(.*)$",
    "^@/app/(.*)$",
    "",
    "^[./]",
  ],
  importOrderSeparation: false,
  importOrderSortSpecifiers: true,
  importOrderBuiltinModulesToTop: true,
  importOrderParserPlugins: ["typescript", "jsx", "decorators-legacy"],
  importOrderMergeDuplicateImports: true,
  importOrderCombineTypeAndValueImports: true,
  plugins: [
    "@ianvs/prettier-plugin-sort-imports",
    "prettier-plugin-tailwindcss",
  ],
}
  • These are my personal settings, feel free to visit this page and incorporate other configurations to personalize your experience.
  • The importOrder option is specific to this package, I recommend reading through the README.md for help on further configuration.
  • The @/ folder alias can be configured inside your tsconfig.json file as follows:
"compilerOptions": {
    // ...
    "paths": {
      "@/*": ["./src/*"],
      // "@/*: ["./*"] <<< if not using the src/ directory
    }
    // ...
  },

Avoid Prettier Formatting For Certain Directories

Before we harness the power of Prettier formatting, we must ensure files that don’t require formatting, such as the node_modules files, are ignored by Prettier.

  1. We achieve this by creating a file called .prettierignore at the root of our Next.js project.
  2. Inside this file, I recommend you add the following code:
dist
node_modules
.next
build

These directories contain code that shouldn’t be touched by Prettier since this may introduce bugs as well as increase formatting time.

3. To format all your files at once, run this command in the terminal:

npm prettier -w .
pnpm prettier -w .
yarn prettier -w .

Conclusion

And that’s about it!

If you enjoyed this article, check out my profile for many more stories like this, and stay tuned for future articles! 👍

Further Reading 📖

Nextjs
React
Software Development
Programming
Software Engineering
Recommended from ReadMedium