<?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: Spencer Carney</title>
    <description>The latest articles on DEV Community by Spencer Carney (@spencercarnage).</description>
    <link>https://dev.to/spencercarnage</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%2F1085260%2Fddb46b43-532c-4f38-83d3-08854de972fc.jpeg</url>
      <title>DEV Community: Spencer Carney</title>
      <link>https://dev.to/spencercarnage</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/spencercarnage"/>
    <language>en</language>
    <item>
      <title>Scaling Your Codebase with Tailwind CSS's @apply Directive</title>
      <dc:creator>Spencer Carney</dc:creator>
      <pubDate>Thu, 15 Jun 2023 04:58:23 +0000</pubDate>
      <link>https://dev.to/spencercarnage/scaling-your-codebase-with-tailwind-csss-apply-directive-4jg5</link>
      <guid>https://dev.to/spencercarnage/scaling-your-codebase-with-tailwind-csss-apply-directive-4jg5</guid>
      <description>&lt;p&gt;When it comes to rapidly building features, Tailwind CSS's utility-first approach enables engineers to move fast. However, as a code base grows in size and complexity, that utility-first approach can encounter challenges with scaling. One approach to circumvent these issues is to use Tailwind's &lt;code&gt;@apply&lt;/code&gt; directive to create custom classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Utility-first Approach
&lt;/h2&gt;

&lt;p&gt;A site can have page headers, sub-headers, and standard body copy. These may require some combination of &lt;code&gt;color&lt;/code&gt;, &lt;code&gt;font-face&lt;/code&gt;, &lt;code&gt;font-size&lt;/code&gt;, &lt;code&gt;font-weight&lt;/code&gt; and &lt;code&gt;line-height&lt;/code&gt; styles. Using Tailwind, we could implement a "large sub-header" like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h2&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"font-sans font-semibold text-lg text-stone-600"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Scaling this is problematic. Copying the 4 classes above and pasting them anywhere you need a large sub-header is not optimal. &lt;/p&gt;

&lt;h2&gt;
  
  
  Using the &lt;code&gt;@apply&lt;/code&gt; directive
&lt;/h2&gt;

&lt;p&gt;Tailwind's documentation describes the &lt;code&gt;@apply&lt;/code&gt; directive as providing the ability to &lt;a href="https://tailwindcss.com/docs/functions-and-directives#apply"&gt;"to inline any existing utility classes into your own custom CSS."&lt;/a&gt; Using the &lt;code&gt;@apply&lt;/code&gt; directive, we can create a custom large sub-header class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sass"&gt;&lt;code&gt;&lt;span class="nc"&gt;.large-sub-header&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;@apply&lt;/span&gt; &lt;span class="nt"&gt;font-sans&lt;/span&gt; &lt;span class="nt"&gt;font-semibold&lt;/span&gt; &lt;span class="nt"&gt;text-lg&lt;/span&gt; &lt;span class="nt"&gt;text-stone-600&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the same tokens used for the classes, we can add those styles to our custom class name. We can now use &lt;code&gt;.large-sub-header&lt;/code&gt; class anywhere we need a large sub-header.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h2&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"large-sub-header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the inevitable change comes to update the styles for large sub-headers, we only have to make the change in one place.&lt;/p&gt;

&lt;h2&gt;
  
  
  State Variants
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;@apply&lt;/code&gt; allows you to use state variants. Here is a "link" class that uses the &lt;code&gt;hover:&lt;/code&gt; variant:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sass"&gt;&lt;code&gt;&lt;span class="nc"&gt;.link&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;@apply&lt;/span&gt; &lt;span class="nt"&gt;text-orange-600&lt;/span&gt; &lt;span class="nt"&gt;hover&lt;/span&gt;&lt;span class="nd"&gt;:text-orange-900&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All state variants are available to the &lt;code&gt;@apply&lt;/code&gt; directive. For &lt;code&gt;.large-subheader&lt;/code&gt;, we want to apply &lt;code&gt;text-md&lt;/code&gt; on small screens.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sass"&gt;&lt;code&gt;&lt;span class="nc"&gt;.large-sub-header&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;@apply&lt;/span&gt; &lt;span class="nt"&gt;font-sans&lt;/span&gt; &lt;span class="nt"&gt;font-semibold&lt;/span&gt; &lt;span class="nt"&gt;text-lg&lt;/span&gt; &lt;span class="nt"&gt;text-stone-600&lt;/span&gt; &lt;span class="nt"&gt;xs&lt;/span&gt;&lt;span class="nd"&gt;:text-md&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By adding &lt;code&gt;xs:text-md&lt;/code&gt; to our custom class, we made it responsive without losing the benefits that come with using Tailwind.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;!important&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;One limitation of the &lt;code&gt;@apply&lt;/code&gt; directive is that the styles are added without &lt;code&gt;!important&lt;/code&gt;. This is done to avoid specificity issues. To apply &lt;code&gt;!important&lt;/code&gt; to an &lt;code&gt;@apply&lt;/code&gt; directive is done the same way you would apply &lt;code&gt;!important&lt;/code&gt; to CSS property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sass"&gt;&lt;code&gt;&lt;span class="nc"&gt;.large-sub-header&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;@apply&lt;/span&gt; &lt;span class="nt"&gt;font-sans&lt;/span&gt; &lt;span class="nt"&gt;font-semibold&lt;/span&gt; &lt;span class="nt"&gt;text-lg&lt;/span&gt; &lt;span class="nt"&gt;text-stone-600&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nt"&gt;important&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;While Tailwind CSS is great at providing all of the utility classes one needs, over reliance upon them can result in issues down the road. A well implemented site is one that is easy to update. Using Tailwind's &lt;code&gt;@apply&lt;/code&gt; to combine your patterns into custom classes can help with those scaling efforts.&lt;/p&gt;

</description>
      <category>tailwindcss</category>
      <category>css</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Yarn Modern with Plug’n’Play and "Zero-Installs"</title>
      <dc:creator>Spencer Carney</dc:creator>
      <pubDate>Fri, 26 May 2023 03:22:51 +0000</pubDate>
      <link>https://dev.to/spencercarnage/yarn-modern-with-plugnplay-and-zero-installs-6k8</link>
      <guid>https://dev.to/spencercarnage/yarn-modern-with-plugnplay-and-zero-installs-6k8</guid>
      <description>&lt;p&gt;Ever since Facebook released Yarn in 2016, it has become an essential tool for developers. According to Stack Overflow’s 2022 survey of 70,000 developers, 27% of those respondents use Yarn. Introducing a lock file and parallel downloads, Yarn pushed the envelope of what a JavaScript dependency management tool can do. The release of version 2 (known as Yarn Modern or Yarn Berry) introduced &lt;a href="https://yarnpkg.com/features/pnp"&gt;Plug'n'Play&lt;/a&gt; with &lt;a href="https://yarnpkg.com/features/zero-installs"&gt;Zero-Installs&lt;/a&gt;. In this article I will walk you through creating a project using the Yarn Modern, adding Plug'n'Play and Zero-Installs as we go.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Plug'n'Play and Zero-Installs?
&lt;/h2&gt;

&lt;p&gt;Plug'n'Play is a strategy for installing a project’s dependencies. Zero-Installs is a collection of Yarn's features used to make your project as fast and stable as possible.&lt;/p&gt;

&lt;p&gt;Have you ran into a problem with a project’s dependencies that was only solved with &lt;code&gt;rm -rf node_modules&lt;/code&gt;? Or running a node script on your machine works but that same script fails to run on a coworker's? The Yarn docs calls this the &lt;a href="https://yarnpkg.com/features/pnp/#the-node_modules-problem"&gt;“node_modules problem”&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;The “node_modules problem” refers to the inefficiencies caused by using the &lt;a href="https://nodejs.org/api/modules.html#all-together"&gt;Node Resolution Algorithm&lt;/a&gt; to look up a project’s dependencies. The Node Resolution Algorithm looks for files that are &lt;code&gt;require&lt;/code&gt;’d or &lt;code&gt;import&lt;/code&gt;’d, recursively (and indiscriminately) searching all the way to the home directory of the computer it is being run on. With the volume of files installed, and  different &lt;code&gt;stat&lt;/code&gt; and &lt;code&gt;readdir&lt;/code&gt; calls, this traditional approach is costly in space and time. Every minute spent on CI / CD installing node modules and bootstrapping your application is money coming out of someone’s pocket.&lt;/p&gt;

&lt;p&gt;Plug'n'Play and Zero-Installs provide a solution: replace large &lt;code&gt;node_modules&lt;/code&gt; downloads with zipped cached files that unzip at runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up a new project with Yarn Modern
&lt;/h2&gt;

&lt;p&gt;First, we need to upgrade to the latest version of Yarn.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i yarn -g
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are going to create a new folder and set up a git repo using Yarn inside of it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir yarn-modern
cd yarn-modern
git init
yarn init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;ls -l&lt;/code&gt; should show us the following files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.editorconfig 
.gitignore
README.md
package.json
yarn.lock
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to set up our project to install our dependencies in the &lt;code&gt;node_modules&lt;/code&gt; folder. We can do that by running &lt;code&gt;yarn config set nodeLinker: node-modules&lt;/code&gt; from the terminal. That shows us the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;➤ YN0000: Successfully set nodeLinker to 'node-modules'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running that command creates a &lt;code&gt;.yarnrc.yml&lt;/code&gt; file with the following contents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nodeLinker: node-modules
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We want to commit these files. Doing so will allow us to run &lt;code&gt;git status&lt;/code&gt; to see subsequent changes in isolation. Before doing that, we need to make a change to &lt;code&gt;.gitignore&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Let's look at the contents of &lt;code&gt;.gitignore&lt;/code&gt; first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# Swap the comments on the following lines if you don't wish to use Zero-Installs
# Documentation here: https://yarnpkg.com/features/Zero-Installs
!.yarn/cache
#.pnp.*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a number of &lt;code&gt;.yarn&lt;/code&gt; folders that we do not want to ignore. They can serve a purpose but they are beyond the scope of this article. Looking at the bottom section, there is the following comment:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Swap the comments on the following lines if you don't wish to use Zero-Installs&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By following those instructions we will turn off Zero-Installs, which was set up for us when we ran &lt;code&gt;yarn init -y&lt;/code&gt;. Turning it off now will help us better understand how Zero-Installs works when we enable it. &lt;/p&gt;

&lt;p&gt;Update the bottom of the &lt;code&gt;.gitignore&lt;/code&gt; to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!.yarn/cache
.pnp.*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add &lt;code&gt;node_modules&lt;/code&gt; to &lt;code&gt;.gitignore&lt;/code&gt; as well.&lt;/p&gt;

&lt;p&gt;When that is done, commit the changes.&lt;/p&gt;

&lt;p&gt;Let's add &lt;code&gt;lodash&lt;/code&gt; as a dependency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add lodash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;git status&lt;/code&gt; we should see a familiar sight:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Changes not staged for commit:
  (use "git add &amp;lt;file&amp;gt;..." to update what will be committed)
  (use "git restore &amp;lt;file&amp;gt;..." to discard changes in working directory)
    modified:   package.json
    modified:   yarn.lock
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;ls -l&lt;/code&gt;, we see a &lt;code&gt;node_modules&lt;/code&gt; folder. We also have a &lt;code&gt;.yarn&lt;/code&gt; folder. The &lt;code&gt;.yarn&lt;/code&gt; folder has a &lt;code&gt;cache&lt;/code&gt; folder along with a &lt;code&gt;install-state.gz&lt;/code&gt; folder. We won't be covering what &lt;code&gt;install-state.gz&lt;/code&gt; does but &lt;a href="https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignored"&gt;you can find out more about the files and folders that can end up in your &lt;code&gt;.yarn&lt;/code&gt; folder here.&lt;/a&gt; &lt;code&gt;.yarn/cache&lt;/code&gt; holds a local, cached copy of our project dependencies. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;ls -ls .yarn/cache&lt;/code&gt; will show us something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lodash-npm-4.17.21-6382451519-eb835a2e51.zip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we were to delete &lt;code&gt;node_modules&lt;/code&gt; and re-run &lt;code&gt;yarn install&lt;/code&gt;, it would load it from &lt;code&gt;.yarn/cache&lt;/code&gt; instead of fetching it from the remote repository. That is a nice touch.&lt;/p&gt;

&lt;p&gt;Next we will create a small file in our project's root that loads &lt;code&gt;lodash&lt;/code&gt;, &lt;code&gt;index.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&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;lodash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;camelCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;FOO_BAR_BAZ&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;node index.js&lt;/code&gt; we get the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fooBarBaz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As expected, we are loading &lt;code&gt;lodash&lt;/code&gt; from &lt;code&gt;node_modules&lt;/code&gt;. Commit this change. Next we are going to enable Plug'n'Play.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Plug'n'Play
&lt;/h2&gt;

&lt;p&gt;To add Plug'n'Play, we need to change the &lt;code&gt;nodeLinker&lt;/code&gt; config value. Run the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn config set nodeLinker pnp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Done successfully, that will output the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;➤ YN0000: Successfully set nodeLinker to 'pnp'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also change this setting manually by opening  &lt;code&gt;.yarnrc.yml&lt;/code&gt; and changing &lt;code&gt;nodeLinker&lt;/code&gt; to &lt;code&gt;pnp&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nodeLinker: pnp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;yarn install&lt;/code&gt;. You should see the following line somewhere in the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;➤ YN0031: │ One or more node_modules have been detected and will be removed. This operation may take some time.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;ls -l&lt;/code&gt; should show that there is no longer a &lt;code&gt;node_modules&lt;/code&gt; directory. Lets make sure our &lt;code&gt;index.js&lt;/code&gt; file is still working:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Uh oh! We got an error, that looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node:internal/modules/cjs/loader:942
  throw err;
  ^

Error: Cannot find module 'lodash'
Require Stack:
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using Plug'n'Play requires using the &lt;code&gt;yarn&lt;/code&gt; binary, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn node index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works! Running &lt;code&gt;yarn node index.js&lt;/code&gt;, we can see &lt;code&gt;console.log&lt;/code&gt; from our &lt;code&gt;index.js&lt;/code&gt; file. Instead of &lt;code&gt;node_modules&lt;/code&gt;, our dependencies are loading from &lt;code&gt;.yarn/cache&lt;/code&gt; instead. &lt;/p&gt;

&lt;h2&gt;
  
  
  Zero-Installs
&lt;/h2&gt;

&lt;p&gt;To achieve zero-install state with Plug'n'Play, we need make an update to &lt;code&gt;.gitignore&lt;/code&gt;, replacing its contents with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.yarn/*
!.yarn/cache
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;git status&lt;/code&gt;, you see the previously ignored Yarn cache files along with a &lt;code&gt;.pnp.cjs&lt;/code&gt; showing up as untracked files. We will commit these files to the repo. This how we achieve Zero-Installs, by adding our &lt;code&gt;.yarn/cache&lt;/code&gt; files to the repo along with the &lt;code&gt;.pnp.cjs&lt;/code&gt; file. &lt;a href="https://yarnpkg.com/features/pnp#fixing-node_modules"&gt;Yarn's website has a good description of what the &lt;code&gt;.pnp.cjs&lt;/code&gt; file is used for&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The .pnp.cjs file contains various maps: one linking package names and versions to their location on the disk and another one linking package names and versions to their list of dependencies. With these lookup tables, Yarn can instantly tell Node where to find any package it needs to access, as long as they are part of the dependency tree, and as long as this file is loaded within your environment.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Adding your dependencies to your repo may feel strange at first but you quickly get used to it.  &lt;/p&gt;




&lt;p&gt;If you are adding Plug'n'Play to a new project, you may run into some challenges. Plug'n'Play is more strict compared to other JavaScript package managers. This is by design. 3rd party packages may not have listed their dependencies in a manner that works with the strict nature of Plug'n'Play. This is either a feature or bug of how other package managers work. It all depends on how you look at it. Yarn Modern provides tools to tackle these challenges: &lt;code&gt;yarn patch&lt;/code&gt;, &lt;code&gt;yarn patch-commit&lt;/code&gt;, and &lt;code&gt;packageDependencies&lt;/code&gt;. &lt;a href="https://yarnpkg.com/getting-started/editor-sdks#gatsby-focus-wrapper"&gt;There is also some additional work needed to get your IDE to work with Plug'n'Play.&lt;/a&gt; I will cover these topics in a future post. &lt;/p&gt;

&lt;p&gt;Yarn with Plug'n'Play and Zero-Installs is an exciting addition to the package manager landscape. Skipping the need to install node modules on every build results in faster deployments. Getting the latest dependencies is as simple as running &lt;code&gt;git pull&lt;/code&gt;. While it may seem unconventional at first, once adopted, it can increase a team's overall productivity. &lt;/p&gt;

</description>
      <category>yarn</category>
      <category>plugnplay</category>
      <category>javascript</category>
      <category>tooling</category>
    </item>
  </channel>
</rss>
