<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>Rsdoctor Blog</title>
        <link>https://rsdoctor.rs/</link>
        <description>Build analyzer for Rspack ecosystem</description>
        <lastBuildDate>Fri, 13 Nov 2026 03:42:37 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>en</language>
        <copyright>Copyright © ByteDance</copyright>
        <item>
            <title><![CDATA[Duplicate dependency problem]]></title>
            <link>https://rsdoctor.rs/blog/topic/duplicate-pkg-problem</link>
            <guid isPermaLink="false">/blog/topic/duplicate-pkg-problem</guid>
            <content:encoded><![CDATA[<!--$--><h1 class="rp-toc-include" id="duplicate-dependency-problem"><a href="#duplicate-dependency-problem" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Duplicate dependency problem<!-- --> </h1>
<p>Rsdoctor will report cases where multiple duplicate dependencies exist in the same bundler&#x27;s artifact.</p>
<img src="https://assets.rspack.rs/others/assets/rsdoctor/bundle-alerts.png"/>
<h2 class="rp-toc-include" id="hazards-of-duplicate-packages"><a href="#hazards-of-duplicate-packages" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Hazards of duplicate packages</h2>
<ul>
<li><strong>Security</strong>
<ul>
<li>Many packages adopt singleton mode and are only loaded once by default. Examples include core-js, react, and some component libraries. Having multiple versions coexist can cause runtime errors.</li>
</ul>
</li>
<li><strong>Runtime Performance</strong>
<ul>
<li>Increases artifact size, slowing down network transmission</li>
<li>Same code for the same functionality runs multiple times</li>
</ul>
</li>
</ul>
<h2 class="rp-toc-include" id="how-to-solve-duplicate-package-problems"><a href="#how-to-solve-duplicate-package-problems" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>How to solve duplicate package Problems?</h2>
<p>To solve the issue of multiple versions of dependencies, you can address it from both the dependency and build aspects.</p>
<h3 class="rp-toc-include" id="dependency-aspect"><a href="#dependency-aspect" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Dependency aspect</h3>
<h4 class="rp-toc-include" id="1-use-the-npm-dedupe-command"><a href="#1-use-the-npm-dedupe-command" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>1. Use the npm dedupe command</h4>
<p>Generally, package managers try to install the same version of a package based on the semver range. However, long-term projects may have some duplicate dependencies due to the existence of lock files.</p>
<p>Package managers provide the <code>dedupe</code> command, such as <code>npm/yarn/pnpm dedupe</code>, to optimize duplicate dependencies within the correct semver range.</p>
<h4 class="rp-toc-include" id="2-use-resolutions"><a href="#2-use-resolutions" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>2. Use resolutions</h4>
<p>Under the constraints of semver, the effectiveness of the dedupe command may not be ideal. For example, if the artifact contains dependencies <code>debug@4.3.4</code> and <code>debug@3.0.0</code>, where they are respectively depended on by <code>&quot;debug&quot;: &quot;^4&quot;</code> and another package&#x27;s <code>&quot;debug&quot;: &quot;^3&quot;</code>.</p>
<p>In this case, you can try using the <code>resolutions</code> feature of the package manager, such as pnpm&#x27;s <a href="https://pnpm.io/settings#overrides" target="_blank" rel="noopener noreferrer" class="rp-link"><strong>pnpm.overrides</strong></a>, <a href="https://pnpm.io/pnpmfile" target="_blank" rel="noopener noreferrer" class="rp-link"><strong>.pnpmfile.cjs</strong></a>, or <strong>yarn&#x27;s resolutions</strong>.</p>
<p>The advantage of these features is that they can break free from the constraints of semver and change the version declared in <code>package.json</code> during installation to precisely control the installed version.</p>
<p>However, before using them, it is important to consider the compatibility between package versions and evaluate whether optimization is necessary. For example, whether the logic changes between different versions of the same package will affect the functionality of the project.</p>
<h3 class="rp-toc-include" id="build-aspect"><a href="#build-aspect" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Build aspect</h3>
<h4 class="rp-toc-include" id="use-resolvealias"><a href="#use-resolvealias" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Use <a href="https://rspack.rs/config/resolve#resolvealias" target="_blank" rel="noopener noreferrer" class="rp-link">resolve.alias</a></h4>
<p>Almost all bundlers support customizing the resolution paths for npm packages. Therefore, we can eliminate duplicate dependencies by manually specifying the resolve paths for packages during compilation. For example, using <strong>Rspack</strong>, if <code>lodash</code> is duplicated in the build, we can configure it as follows to specify the resolve paths for all <code>lodash</code> packages to the <code>node_modules</code> directory in the current directory.</p>
<div class="rp-codeblock language-js"><div class="rp-codeblock__title">rspack.config.js</div><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="js" data-title="rspack.config.js"><code><span class="line"><span style="color:var(--shiki-token-keyword)">const</span><span style="color:var(--shiki-token-constant)"> path</span><span style="color:var(--shiki-token-keyword)"> =</span><span style="color:var(--shiki-token-function)"> require</span><span style="color:var(--shiki-foreground)">(</span><span style="color:var(--shiki-token-string-expression)">&#x27;path&#x27;</span><span style="color:var(--shiki-foreground)">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:var(--shiki-token-constant)">module</span><span style="color:var(--shiki-foreground)">.</span><span style="color:var(--shiki-token-constant)">exports</span><span style="color:var(--shiki-token-keyword)"> =</span><span style="color:var(--shiki-foreground)"> {</span></span>
<span class="line"><span style="color:var(--shiki-token-comment)">  //...</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  resolve</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-foreground)"> {</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">    alias</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-foreground)"> {</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">      lodash</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-token-constant)"> path</span><span style="color:var(--shiki-token-function)">.resolve</span><span style="color:var(--shiki-foreground)">(__dirname</span><span style="color:var(--shiki-token-punctuation)">,</span><span style="color:var(--shiki-token-string-expression)"> &#x27;node_modules/lodash&#x27;</span><span style="color:var(--shiki-foreground)">)</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">    }</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  }</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">};</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="Toggle code wrap" aria-label="Toggle code wrap"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="Copy code"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div>
<p>This method also requires attention to the compatibility between package versions.</p>
<h2 class="rp-toc-include" id="cases-of-handling-duplicate-packages"><a href="#cases-of-handling-duplicate-packages" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Cases of handling duplicate packages</h2>
<h3 class="rp-toc-include" id="duplicate-packages-in-pnpm-workspace"><a href="#duplicate-packages-in-pnpm-workspace" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Duplicate packages in pnpm-workspace</h3>
<p>In this project, the <code>web</code> app depends on <code>react@18.2.0</code> and imports <code>component</code> using <code>&quot;component&quot;: &quot;workspace:*&quot;</code>. The <code>component</code> package, in turn, depends on <code>react@18.1.0</code>. The project structure is as follows:</p>
<div class="rp-codeblock language-txt"><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="txt"><code><span class="line"><span>├── node_modules</span></span>
<span class="line"><span>├── apps</span></span>
<span class="line"><span>│   └── web</span></span>
<span class="line"><span>│       ├── node_modules</span></span>
<span class="line"><span>│       │   ├── component  -&gt; ../../packages/component</span></span>
<span class="line"><span>│       │   └── react      -&gt; ../../../node_modules/.pnpm/react@18.2.0</span></span>
<span class="line"><span>│       ├── src</span></span>
<span class="line"><span>│       │   └── index.js</span></span>
<span class="line"><span>│       ├── rspack.config.js</span></span>
<span class="line"><span>│       └── package.json</span></span>
<span class="line"><span>└── packages</span></span>
<span class="line"><span>    └── component</span></span>
<span class="line"><span>        ├── node_modules</span></span>
<span class="line"><span>        │   └── react      -&gt; ../../../node_modules/.pnpm/react@18.1.0</span></span>
<span class="line"><span>        ├── src</span></span>
<span class="line"><span>        │   └── index.js</span></span>
<span class="line"><span>        └── package.json</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="Toggle code wrap" aria-label="Toggle code wrap"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="Copy code"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div>
<p>When executing <code>rspack build</code> under <code>apps/web</code>, the code in the <code>web</code> directory will be resolved to <code>react@18.2.0</code>, and then the code in the <code>component</code> directory will be resolved to <code>react@18.1.0</code>. This results in the output of the web project containing two versions of <code>React</code>.</p>
<h4 class="rp-toc-include" id="solution"><a href="#solution" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Solution</h4>
<p>This issue can be resolved using the <code>resolve.alias</code> configuration of the bundler, such as Rspack. By specifying the resolve path for <code>React</code> to only resolve to <code>apps/web/node_modules/react</code>, you can ensure that only one version of <code>React</code> is included in the output. Here is an example code:</p>
<div class="rp-codeblock language-javascript"><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="javascript"><code><span class="line"><span style="color:var(--shiki-token-comment)">// apps/web/rspack.config.js</span></span>
<span class="line"><span style="color:var(--shiki-token-keyword)">const</span><span style="color:var(--shiki-token-constant)"> path</span><span style="color:var(--shiki-token-keyword)"> =</span><span style="color:var(--shiki-token-function)"> require</span><span style="color:var(--shiki-foreground)">(</span><span style="color:var(--shiki-token-string-expression)">&#x27;path&#x27;</span><span style="color:var(--shiki-foreground)">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:var(--shiki-token-constant)">module</span><span style="color:var(--shiki-foreground)">.</span><span style="color:var(--shiki-token-constant)">exports</span><span style="color:var(--shiki-token-keyword)"> =</span><span style="color:var(--shiki-foreground)"> {</span></span>
<span class="line"><span style="color:var(--shiki-token-comment)">  //...</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  resolve</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-foreground)"> {</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">    alias</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-foreground)"> {</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">      react</span><span style="color:var(--shiki-token-keyword)">:</span><span style="color:var(--shiki-token-constant)"> path</span><span style="color:var(--shiki-token-function)">.dirname</span><span style="color:var(--shiki-foreground)">(</span><span style="color:var(--shiki-token-constant)">require</span><span style="color:var(--shiki-token-function)">.resolve</span><span style="color:var(--shiki-foreground)">(</span><span style="color:var(--shiki-token-string-expression)">&#x27;react/package.json&#x27;</span><span style="color:var(--shiki-foreground)">))</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">    }</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">  }</span><span style="color:var(--shiki-token-punctuation)">,</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">};</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="Toggle code wrap" aria-label="Toggle code wrap"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="Copy code"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div>
<h4 class="rp-toc-include" id="duplicate-packages-caused-by-peer-dependency"><a href="#duplicate-packages-caused-by-peer-dependency" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Duplicate packages caused by peer dependency</h4>
<p>This handling method also applies to projects with duplicate packages caused by multiple instances of <strong><a href="https://pnpm.io/how-peers-are-resolved" target="_blank" rel="noopener noreferrer" class="rp-link">peerDependencies</a></strong> in <code>pnpm workspace</code>. The project directory structure is as follows:</p>
<div class="rp-codeblock language-txt"><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="txt"><code><span class="line"><span>├── node_modules</span></span>
<span class="line"><span>├── apps</span></span>
<span class="line"><span>│   └── web</span></span>
<span class="line"><span>│       ├── node_modules</span></span>
<span class="line"><span>│       │   ├── component  -&gt;  ../../packages/component</span></span>
<span class="line"><span>│       │   ├── axios      -&gt;  ../../../node_modules/.pnpm/axios@0.27.2_debug@4.3.4</span></span>
<span class="line"><span>│       │   └── debug      -&gt;  ../../../node_modules/.pnpm/debug@4.3.4</span></span>
<span class="line"><span>│       ├── src</span></span>
<span class="line"><span>│       │   └── index.js</span></span>
<span class="line"><span>│       ├── rspack.config.js</span></span>
<span class="line"><span>│       └── package.json</span></span>
<span class="line"><span>└── packages</span></span>
<span class="line"><span>    └── component</span></span>
<span class="line"><span>        ├── node_modules</span></span>
<span class="line"><span>        │   └── axios      -&gt;  ../../../node_modules/.pnpm/axios@0.27.2</span></span>
<span class="line"><span>        ├── src</span></span>
<span class="line"><span>        │   └── index.js</span></span>
<span class="line"><span>        └── package.json</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="Toggle code wrap" aria-label="Toggle code wrap"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="Copy code"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div>
<p>In this project, when executing <code>rspack build</code> under <code>apps/web</code>, the code in the <code>web</code> directory will be resolved to <code>axios@0.27.2_debug@4.3.4</code>, and then the code in the <code>packages/component</code> directory will be resolved to <code>axios@0.27.2</code>.</p>
<p>Although they are the same version, they have different paths, resulting in two copies of <code>axios</code> in the output.</p>
<p>The solution is to configure the <code>web</code> project to only resolve the <code>axios</code> package under the <code>web</code> directory&#x27;s <code>node_modules</code>.</p>
<div class="rp-codeblock language-javascript"><div class="rp-codeblock__content"><div class="rp-codeblock__content__scroll-container rp-scrollbar rp-scrollbar--always"><pre class="shiki css-variables" style="background-color:var(--shiki-background);color:var(--shiki-foreground)" tabindex="0" data-lang="javascript"><code><span class="line"><span style="color:var(--shiki-token-comment)">// apps/web/rspack.config.js</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">alias</span><span style="color:var(--shiki-token-punctuation)">:</span><span style="color:var(--shiki-foreground)"> {</span></span>
<span class="line"><span style="color:var(--shiki-token-string-expression)">  &#x27;axios&#x27;</span><span style="color:var(--shiki-foreground)">: </span><span style="color:var(--shiki-token-constant)">path</span><span style="color:var(--shiki-token-function)">.resolve</span><span style="color:var(--shiki-foreground)">(__dirname</span><span style="color:var(--shiki-token-punctuation)">,</span><span style="color:var(--shiki-token-string-expression)"> &#x27;node_modules/axios&#x27;</span><span style="color:var(--shiki-foreground)">)</span></span>
<span class="line"><span style="color:var(--shiki-foreground)">}</span></span></code></pre></div><div class="rp-code-button-group"><button class="rp-code-button-group__button rp-code-wrap-button" title="Toggle code wrap" aria-label="Toggle code wrap"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrapped"><path fill="currentColor" d="M21 5H3v2h18zM3 19h7v-2H3zm0-6h15c1 0 2 .43 2 2s-1 2-2 2h-2v-2l-4 3 4 3v-2h2c2.95 0 4-1.27 4-4 0-2.72-1-4-4-4H3z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-button-group__icon--wrap"><path fill="currentColor" d="M16 7H3V5h13zM3 19h13v-2H3zm19-7-4-3v2H3v2h15v2z"></path></svg></button><button class="rp-code-button-group__button rp-code-copy-button" title="Copy code"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--copy"><path fill="currentColor" d="M20 8v12H8V8zm0-2H8a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2"></path><path fill="currentColor" d="M4 16H2V4a2 2 0 0 1 2-2h12v2H4Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" class="rp-code-button-group__icon rp-code-copy-button__icon rp-code-copy-button__icon--success"><path fill="currentColor" d="m9.55 18-5.7-5.7 1.425-1.425L9.55 15.15l9.175-9.175L20.15 7.4z"></path></svg></button></div></div></div><!--/$-->]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Loader analysis and optimization]]></title>
            <link>https://rsdoctor.rs/blog/topic/loader-optimization</link>
            <guid isPermaLink="false">/blog/topic/loader-optimization</guid>
            <content:encoded><![CDATA[<!--$--><h1 class="rp-toc-include" id="loader-analysis-and-optimization"><a href="#loader-analysis-and-optimization" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Loader analysis and optimization<!-- --> </h1>
<!-- -->
<p>Optimizing loaders is a common way to improve the performance of Rspack compilation. In most cases, besides replacing the loader with a faster one, we can also reduce execution by setting <a href="https://rspack.rs/config/module-rules#rulesexclude" target="_blank" rel="noopener noreferrer" class="rp-link"><code>module.rules[].exclude</code></a> for the loader.</p>
<p>Rsdoctor provides two core modules, <a href="/guide/usage/loaders-timeline" class="rp-link">Loader Overall</a> and <a href="/guide/usage/loaders-analysis" class="rp-link">Loader Analysis</a>, to help you optimize based on the loader&#x27;s invocation information.</p>
<h2 class="rp-toc-include" id="how-to-analyze"><a href="#how-to-analyze" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>How to analyze?</h2>
<p>Regardless of the optimization method used for loaders, we need to obtain data information about the loaders, such as <strong>which files a loader compiles</strong> and <strong>time-consuming information for compiling certain files</strong>, in order to optimize more accurately.</p>
<h3 class="rp-toc-include" id="file-tree-structure"><a href="#file-tree-structure" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>File tree structure</h3>
<p>With the Loader Analysis module provided by Rsdoctor, we can see <strong>a tree structure composed of all files that pass through the loaders during the entire compilation process</strong>, as shown in the following figure:</p>
<img src="https://assets.rspack.rs/others/assets/rsdoctor/loader-analysis-all.png" width="600px" style="margin:auto"/>
<h3 class="rp-toc-include" id="analyzing-directories"><a href="#analyzing-directories" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Analyzing directories</h3>
<p>By <strong>clicking on a directory</strong>, you can see the <strong>time-consuming statistics of all loaders</strong> under the currently <strong>selected directory</strong> on the right side of the file tree, such as the number of files processed by a loader and <strong>the estimated time</strong> consumption. The content of the &quot;Statistics of ***&quot; card is shown in the following figure:</p>
<img src="https://assets.rspack.rs/others/assets/rsdoctor/loader-analysis-table.png" width="300px" height="400px" style="margin:auto"/>
<p>We can intuitively know <strong>the time-consuming data of this folder</strong>, such as the number of files processed by a certain loader and the estimated time consumption, as well as the sum of all data, which further helps us make decisions on how to optimize the loader.</p>
<div class="rp-callout rp-callout--tip"><div class="rp-callout__title">Optimization Tips</div><div class="rp-callout__content"><p>
Usually, in the directories of third-party libraries inside <strong><code>node_modules</code></strong>, we can easily determine whether it is necessary to set <a href="https://rspack.rs/config/module-rules#rulesexclude" target="_blank" rel="noopener noreferrer" class="rp-link"><code>module.rules[].exclude</code></a> for this directory based on the time-consuming information of the loader, in order to reduce the execution of loaders with long execution time, such as the common <code>babel-loader</code>.</p></div></div>
<h3 class="rp-toc-include" id="analyzing-files"><a href="#analyzing-files" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Analyzing files</h3>
<p>By <strong>clicking on a file</strong>, a mask layer will pop up, showing a list of all loaders that have been executed on the currently clicked file. By <strong>clicking on a loader</strong>, you can see the <strong>input</strong>, <strong>output</strong>, and <strong>parameter data information of the target loader</strong> when it is called.</p>
<img src="https://assets.rspack.rs/others/assets/rsdoctor/loader-analysis-code-change.png" width="600px" style="margin:auto"/>
<ul>
<li>Parameter Data Information: Click on &quot;<strong>show more</strong>&quot; or the expand button in the upper left corner to view the corresponding parameter information.</li>
</ul>
<img src="https://assets.rspack.rs/others/assets/rsdoctor/loader-analysis-options.png" width="600px" style="margin:auto"/>
<p>We can use the loader information of individual files here to troubleshoot issues and decide whether to add them to <a href="https://rspack.rs/config/module-rules#rulesexclude" target="_blank" rel="noopener noreferrer" class="rp-link"><code>module.rules[].exclude</code></a>.</p>
<div class="rp-callout rp-callout--tip"><div class="rp-callout__title">Optimization Tips</div><div class="rp-callout__content"><p>
Usually, for internal packages inside the project (i.e., outside the cwd, usually the workspace), we can determine whether it is necessary to set <a href="https://rspack.rs/config/module-rules#rulesexclude" target="_blank" rel="noopener noreferrer" class="rp-link"><code>module.rules[].exclude</code></a> for this directory based on <strong>the loader time-consuming information</strong> of the directory and <strong>the input-output information</strong> of individual files (because these internal packages may already have good compatibility with ES syntax).</p></div></div>
<h2 class="rp-toc-include" id="learn-more"><a href="#learn-more" class="rp-header-anchor rp-link" aria-hidden="true" tabindex="-1">#</a>Learn more</h2>
<p>You may want to learn more about the usage of the Loader Analysis module:</p>
<div class="next-steps-ox_kfz"><a class="step-ZdacDG" href="/guide/usage/loaders-timeline"><p class="title-yqulXf">Loader Timeline Usage</p><p class="description-JD1BgV">Learn how to use the Loader Timeline in Rsdoctor</p></a><a class="step-ZdacDG" href="/guide/usage/loaders-analysis"><p class="title-yqulXf">Loader Analysis Usage</p><p class="description-JD1BgV">Learn how to operate and use the Loader Analysis in Rsdoctor</p></a></div><!--/$-->]]></content:encoded>
        </item>
    </channel>
</rss>