Manual Installation: Vite & Webpack
Relevant source files
The following files were used as context for generating this wiki page:While the recommended method for installing react-grab is using the CLI command npx grab@latest init, manual installation is fully supported for projects using Vite or Webpack as their bundler. The core principle for both is to conditionally load react-grab only in the development environment, ensuring it is never included in the production bundle.
This is achieved by checking an environment variable (import.meta.env.DEV for Vite, process.env.NODE_ENV for Webpack) before dynamically importing the library. This approach keeps your production application lean while providing the full power of react-grab during development.
Sources: README.md:23, packages/cli/src/utils/transform.ts:49-59, packages/cli/src/commands/init.ts:101-104
Vite
For Vite-based projects, react-grab is integrated by adding a script to the main index.html file. This method leverages Vite's built-in environment variables to conditionally load the library.
Sources: README.md:65-81, packages/cli/src/utils/transform.ts:303-343
Installation Process
The process involves locating the <head> section of your index.html and inserting a <script type="module"> block. This script checks if the application is running in development mode and, if so, dynamically imports react-grab.
The following diagram illustrates the manual installation flow for a Vite project.
This flowchart shows the steps to manually instrument a Vite project by modifying index.html.
Sources: packages/cli/src/utils/transform.ts:303-343, packages/vite-playground/index.html:8-16
Code Examples
Basic Installation
Add the following script inside the <head> of your index.html. If you are using the grab package (an alias for react-grab), you may need to install it first (npm i grab).
<!-- packages/website/components/install-tabs.tsx:160-179 -->
<!doctype html>
<html lang="en">
<head>
<script type="module">
// first npm i react-grab
// then in head:
if (import.meta.env.DEV) {
import("react-grab");
}
</script>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
Sources: packages/website/components/install-tabs.tsx:160-179, packages/vite-playground/index.html:8-12
With Agent Integration
To integrate with a coding agent, you must also import the specific agent client package. The VITE_SCRIPT_WITH_AGENT template demonstrates this.
// packages/cli/test/templates.test.ts:49-55
const script = VITE_SCRIPT_WITH_AGENT("opencode");
expect(script).toContain("react-grab");
expect(script).toContain("@react-grab/opencode");
Sources: packages/cli/test/templates.test.ts:49-55, packages/cli/src/utils/transform.ts:159-198
With Custom Options
To customize react-grab's behavior, import the init function from react-grab/core and call it with your desired options.
<!-- packages/website/components/install-tabs.tsx:144-159 -->
<script type="module">
// first npm i react-grab
// then in head:
if (import.meta.env.DEV) {
const { init } = await import("react-grab/core");
init({ activationKey: { key: "g", metaKey: true } });
}
</script>
Sources: packages/website/components/install-tabs.tsx:144-159, packages/react-grab/README.md:321-344
Webpack
For Webpack-based projects, installation is a two-step process: installing the react-grab package and then adding a conditional import to your application's main entry file.
Sources: README.md:83-93, packages/cli/src/utils/transform.ts:345-388
Installation Process
First, install the package using your preferred package manager. Then, find your project's main entry point (e.g., src/index.tsx, src/main.js) and add a code block at the top to conditionally import react-grab based on process.env.NODE_ENV. The transformWebpack utility in the CLI searches for these common entry files to automate this process.
The following diagram illustrates the manual installation flow for a Webpack project.
This flowchart shows the two-step process for manually setting up react-grab in a Webpack project.
Sources: packages/cli/src/utils/transform.ts:345-388, packages/cli/test/transform.test.ts:192-206
Code Examples
Basic Installation
-
Install the package:
npm install react-grabSources: README.md:86-88
-
Add the following code to the top of your main entry file (e.g.,
src/index.tsx):// packages/website/components/install-tabs.tsx:216-228 import React from "react"; import ReactDOM from "react-dom/client"; import App from "./App"; if (process.env.NODE_ENV === "development") { import("react-grab"); } const root = ReactDOM.createRoot( document.getElementById("root") as HTMLElement ); root.render( <React.StrictMode> <App /> </React.StrictMode> );Sources: packages/website/components/install-tabs.tsx:216-228, README.md:90-93
With Agent Integration
Agent integration follows the same pattern, adding a dynamic import for the agent's client package. The WEBPACK_IMPORT_WITH_AGENT template is used for this.
// packages/cli/test/templates.test.ts:74-79
const importBlock = WEBPACK_IMPORT_WITH_AGENT("claude-code");
expect(importBlock).toContain("react-grab");
expect(importBlock).toContain("@react-grab/claude-code");
Sources: packages/cli/test/templates.test.ts:74-79, packages/cli/src/utils/transform.ts:199-238
With Custom Options
To pass custom options, use the init function from react-grab/core.
// packages/website/components/install-tabs.tsx:199-214
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
if (process.env.NODE_ENV === "development") {
import("react-grab/core").then(({ init }) => {
init({ activationKey: { key: "g", metaKey: true } });
});
}
// ... rest of your app
Sources: packages/website/components/install-tabs.tsx:199-214, packages/react-grab/README.md:321-344
Installation Summary
The table below provides a quick reference for the manual installation process for both Vite and Webpack.
| Framework | File to Modify | Method | Code Snippet |
|---|---|---|---|
| Vite | index.html | Add a <script> tag to the <head>. | if (import.meta.env.DEV) { import("react-grab"); } |
| Webpack | Entry file (e.g., src/index.tsx) | 1. npm install react-grab2. Add a conditional import. | if (process.env.NODE_ENV === "development") { import("react-grab"); } |
Sources: README.md:65-93, packages/website/components/install-tabs.tsx:141-228
Conclusion
Manual installation for Vite and Webpack is straightforward, centered on the key principle of conditional, development-only imports. This ensures that react-grab provides its powerful debugging capabilities during development without affecting the performance or size of the production application. While these manual steps are effective, the grab init CLI command is the recommended approach as it automates this entire process, including framework detection and file modification.