<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Stanley Sathler</title>
    <description>The latest articles on DEV Community by Stanley Sathler (@stanleysathler).</description>
    <link>https://dev.to/stanleysathler</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F58766%2Feab53486-3b48-491f-b8de-61c91121a295.jpeg</url>
      <title>DEV Community: Stanley Sathler</title>
      <link>https://dev.to/stanleysathler</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stanleysathler"/>
    <language>en</language>
    <item>
      <title>Enabling styled-components' debugging options in your CRA app without ejecting</title>
      <dc:creator>Stanley Sathler</dc:creator>
      <pubDate>Sat, 11 Apr 2020 00:08:28 +0000</pubDate>
      <link>https://dev.to/stanleysathler/enabling-styled-components-debugging-options-in-your-cra-app-without-ejecting-16c1</link>
      <guid>https://dev.to/stanleysathler/enabling-styled-components-debugging-options-in-your-cra-app-without-ejecting-16c1</guid>
      <description>&lt;p&gt;I have a project using CRA (&lt;a href="https://github.com/facebook/create-react-app"&gt;&lt;code&gt;create-react-app&lt;/code&gt;&lt;/a&gt;), TypeScript and &lt;a href="https://styled-components.com/"&gt;&lt;code&gt;styled-components&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;styled-components&lt;/code&gt; has a useful feature when you are developing your components: a debugging option called &lt;code&gt;displayName&lt;/code&gt; that shows the components' name in the generated CSS classes. It helps you to identify each component in your DevTools.&lt;/p&gt;

&lt;p&gt;According to the &lt;a href="https://styled-components.com/docs/tooling#babel-plugin"&gt;docs&lt;/a&gt;, to enable it, you need to use a &lt;strong&gt;Babel plugin&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;a href="https://styled-components.com/docs/tooling#typescript-plugin"&gt;docs&lt;/a&gt; says we need a different plugin if we are using TypeScript. However, it is &lt;strong&gt;not the case&lt;/strong&gt;, even if your project uses CRA + TypeScript, as mine does.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, CRA doesn't have a &lt;code&gt;.babelrc&lt;/code&gt; file. It seems that its Babel configuration is given through the webpack config.&lt;/p&gt;

&lt;p&gt;It brings another problem: CRA doesn't support a custom webpack config unless you "hack it".&lt;/p&gt;

&lt;p&gt;So we need to follow two steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;1. Find a way to modify CRA's default webpack config.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;2. Add the Babel plugin.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Customizing CRA's default webpack config
&lt;/h3&gt;

&lt;p&gt;There is an awesome package called &lt;a href="https://github.com/timarney/react-app-rewired"&gt;&lt;code&gt;react-app-rewired&lt;/code&gt;&lt;/a&gt; that allows you to extend the default webpack config.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn add -D react-app-rewired
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You create a file named &lt;code&gt;config-override.js&lt;/code&gt; (at the root path) that acts similarly to the webpack config file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// config-override.js&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// `config` is the default config. Do whatever you want.&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we have the default webpack config, all we have to do now is to find out which rule we should modify to add the extra Babel plugin.&lt;/p&gt;

&lt;p&gt;However, there is also a tiny package that makes this task easier: the &lt;code&gt;customize-cra&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Injecting the Babel plugin
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://github.com/arackaf/customize-cra"&gt;&lt;code&gt;customize-cra&lt;/code&gt;&lt;/a&gt; package is a set of utility functions that helps you extend some config from CRA. Let's say these functions basically "change the right object properties for you".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn add -D customize-cra
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then update your &lt;code&gt;config-override.js&lt;/code&gt; file to inject the Babel plugin:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;override&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;addBabelPlugin&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;customize-cra&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;override&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;addBabelPlugin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;babel-plugin-styled-components&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="c1"&gt;// any extra config from babel-plugin-styled-components&lt;/span&gt;
    &lt;span class="p"&gt;}]&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Updating your dev / build scripts
&lt;/h3&gt;

&lt;p&gt;The final step is simple: you need the modify the default npm scripts in your &lt;code&gt;package.json&lt;/code&gt; so they can use &lt;code&gt;config-override.js&lt;/code&gt; rather than the default webpack config.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"your-app"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"0.1.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"private"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;omitted&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;brevity&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;purposes&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react-app-rewired start"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react-app-rewired build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react-app-rewired test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"eject"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react-scripts eject"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;No&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;need&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;to&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;change&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;it&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;`eject`&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;command&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Well, that's it! 🚀 I don't fully understand how webpack works internally, so even this simple thing took me some time to figure out. I hope it helps someone else.&lt;/p&gt;

</description>
      <category>styledcomponents</category>
      <category>debugging</category>
      <category>typescript</category>
      <category>createreactapp</category>
    </item>
  </channel>
</rss>
