{"id":13134,"library":"esbuild-plugin-rdi","title":"ESBuild Plugin to Remove Duplicate Requires","description":"esbuild-plugin-rdi is an ESBuild plugin designed to optimize JavaScript bundles by identifying and removing redundant `require` statements during the minification phase. This process helps to reduce the final bundle size, improving application load times and overall performance. The package is currently at version `0.0.0`, indicating an initial public release or very early development stage. As such, its API surface and internal behavior might be subject to rapid changes, and a formal release cadence is not yet established. Its primary differentiator lies in its targeted approach to deduplicating `require` calls, which can be particularly beneficial for projects that compile to or still rely on CommonJS modules, or when integrating with frameworks like React (versions 18 and 19) and Next.js (versions 14 and 15), especially in scenarios involving React Server Components where such optimizations are crucial. The plugin is intended for use within an `esbuild` build pipeline or through tools like `tsup` that leverage `esbuild`.","status":"active","version":"0.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/react18-tools/esbuild-plugin-remove-duplicate-require","tags":["javascript","React 18","React 19","Next.js","Next.js 14","Next.js 15","React server components","Customizable","Cutting-edge","typescript"],"install":[{"cmd":"npm install esbuild-plugin-rdi","lang":"bash","label":"npm"},{"cmd":"yarn add esbuild-plugin-rdi","lang":"bash","label":"yarn"},{"cmd":"pnpm add esbuild-plugin-rdi","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library ships TypeScript types and is primarily consumed via ES Modules. While `require` might work in some transpiled environments, `import` is the idiomatic way.","wrong":"const rdiPlugin = require('esbuild-plugin-rdi');","symbol":"rdiPlugin","correct":"import { rdiPlugin } from 'esbuild-plugin-rdi';"},{"note":"Type import for configuring the plugin. Options are 'coming soon' according to the README, but the type definition would follow this pattern.","symbol":"RdiPluginOptions","correct":"import type { RdiPluginOptions } from 'esbuild-plugin-rdi';"}],"quickstart":{"code":"import * as esbuild from 'esbuild';\nimport { rdiPlugin } from 'esbuild-plugin-rdi';\nimport * as path from 'path';\nimport * as fs from 'fs';\n\nconst srcDir = path.resolve(__dirname, 'src');\nconst entryPoint = path.join(srcDir, 'index.js');\nconst outputDir = path.resolve(__dirname, 'dist');\nconst outputFile = path.join(outputDir, 'bundle.js');\n\n// Create dummy files for demonstration\nif (!fs.existsSync(srcDir)) fs.mkdirSync(srcDir, { recursive: true });\nif (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });\n\nfs.writeFileSync(entryPoint, `\n  const depA = require('./commonDep');\n  const depB = require('./anotherDep');\n  const depA_again = require('./commonDep'); // This duplicate 'require' will be targeted\n  console.log(depA.message, depB.message, depA_again.message);\n`);\nfs.writeFileSync(path.join(srcDir, 'commonDep.js'), `exports.message = 'Hello from commonDep';`);\nfs.writeFileSync(path.join(srcDir, 'anotherDep.js'), `exports.message = 'Hello from anotherDep';`);\n\nasync function buildExample() {\n  try {\n    await esbuild.build({\n      entryPoints: [entryPoint],\n      bundle: true,\n      outfile: outputFile,\n      format: 'cjs', // Important: plugin targets 'require' statements\n      plugins: [rdiPlugin()],\n      minify: true, // Minification often exposes duplicate requires\n      logLevel: 'info'\n    });\n    console.log('Build successful! Output:', outputFile);\n    const content = fs.readFileSync(outputFile, 'utf-8');\n    console.log('\\n--- Generated Bundle Excerpt (first 500 chars) ---\\n', content.substring(0, 500));\n  } catch (e) {\n    console.error('ESBuild failed:', e);\n  } finally {\n    // Cleanup dummy files\n    fs.unlinkSync(entryPoint);\n    fs.unlinkSync(path.join(srcDir, 'commonDep.js'));\n    fs.unlinkSync(path.join(srcDir, 'anotherDep.js'));\n    fs.rmdirSync(srcDir);\n    // fs.rmdirSync(outputDir, { recursive: true }); // Careful with this in real projects\n  }\n}\n\nbuildExample();","lang":"typescript","description":"This quickstart demonstrates how to integrate `esbuild-plugin-rdi` into an `esbuild` configuration. It sets up a dummy project with duplicate `require` statements, runs `esbuild` with the plugin, and then outputs the (truncated) minified bundle to show its effect on CommonJS imports."},"warnings":[{"fix":"Exercise caution when using in production. Pin to exact versions and consult the GitHub repository for any breaking changes or updated documentation before upgrading.","message":"This package is currently at version `0.0.0`, indicating it's in a very early development stage. The API is subject to rapid change, and stability is not guaranteed across minor or even patch versions.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Ensure your project or the specific parts you want to optimize are using CommonJS `require` syntax for the plugin to be effective. For pure ESM projects, `esbuild`'s default optimizations are usually sufficient.","message":"The plugin specifically targets `require` statements for deduplication. It will not process or deduplicate ES module `import` statements, which are typically handled by `esbuild`'s native tree-shaking and optimization capabilities.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Thoroughly test the plugin with your exact `esbuild` setup and versions. Monitor bundle size changes and review the output to ensure it performs as expected. Report any unexpected behavior to the maintainers.","message":"The effectiveness of `esbuild-plugin-rdi` is tied to `esbuild`'s minification process and specific build configurations. Different `esbuild` versions or custom plugin interactions might alter its behavior or impact.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Run `npm install esbuild-plugin-rdi` or `pnpm add esbuild-plugin-rdi`. If using TypeScript, ensure your `tsconfig.json` has appropriate `moduleResolution` settings (e.g., `NodeNext` or `Bundler`).","cause":"The package is not installed, or the module resolution path is incorrect, or TypeScript cannot find its type definitions.","error":"Cannot find module 'esbuild-plugin-rdi' or its corresponding type declarations."},{"fix":"Ensure you are calling `rdiPlugin()` as a function to get the plugin object: `plugins: [rdiPlugin()]`. Do not pass `rdiPlugin` directly without invoking it.","cause":"The `rdiPlugin` function was called incorrectly, or there's a version mismatch with `esbuild`.","error":"Plugin 'rdiPlugin' is not a valid ESBuild plugin. Plugins must be an object with a 'name' and 'setup' property."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}