LocatorJS Documentation

repository·master·Indexed 23 days ago

https://github.com/infi-pc/locatorjs

A developer tool that enables click-to-source functionality, allowing users to click a UI component in the browser to open its source code in an IDE. Includes the @locator/webpack-loader for Webpack and Turbopack, specifically designed for environments with restricted Babel plugin access like Next.js projects using SWC. Supports React, Preact, and SolidJS.

Tokens
9.1K
Snippets
28
Records
67
Agent score
83%

What's inside LocatorJS

  1. What is LocatorJS

    master
    LocatorJS is a developer tool that allows you to click on a UI component directly in your browser to automatically open its corresponding source code in your IDE. This streamlines the workflow between visual debugging and code editing.
  2. Requirements for LocatorJS to work

    master

    LocatorJS works automatically in development mode for most modern stacks (NextJS, Create React App, Vite, etc.).

    To function correctly, your project must include babel-plugin-transform-react-jsx-source (often included via babel-preset-react). Non-Babel stacks use similar alternatives to provide source information. If your environment does not provide this, you must set it up manually.

  3. How @locator/webpack-loader works

    master

    The loader enables component location tracking by using Babel's transform API to apply the @locator/babel-jsx plugin to JSX/TSX files.

    It works by:

    1. Parsing JSX/TSX files via Babel.
    2. Adding data-locatorjs attributes containing the full file path and location (e.g., data-locatorjs="/path/to/file.tsx:line:column").
    3. Returning transformed code with sourcemaps.

    Key Benefits:

    • Server Components Support: Unlike other methods, it does not require window.__LOCATOR_DATA__, making it compatible with React Server Components where JavaScript execution is limited.
    • Automatic Filtering: It automatically skips node_modules and middleware files.
    • Framework Compatibility: Works with React, Preact, and SolidJS.
  4. Preserve component state during HMR

    master

    Hot Module Replacement (HMR) in this template does not preserve local component state by default due to the inherent complexities of state preservation in Svelte HMR plugins.

    To ensure important data is not lost when a component reloads during development, move that state into an external Svelte store. External stores reside outside the component lifecycle and are not replaced when the component is re-rendered via HMR.

    // store.js
    // An extremely simple external store
    import { writable } from 'svelte/store'
    export default writable(0)
  5. Configure Next.js 15+ with Turbopack

    master

    To use LocatorJS with Next.js 15+ using Turbopack, add the loader to the turbopack.rules section in your next.config.ts or next.config.js. Target .tsx and .jsx files.

    import type { NextConfig } from "next";
    
    const nextConfig: NextConfig = {
      turbopack: {
        rules: {
          "**/*.{tsx,jsx}": {
            loaders: [
              {
                loader: "@locator/webpack-loader",
                options: {
                  env: "development",
                },
              },
            ],
          },
        },
      },
    };
    
    export default nextConfig;
  6. Set up a Svelte + Vite project

    master

    This template provides a minimal starting point for developing with Svelte using Vite. It is designed to provide a good developer experience with Hot Module Replacement (HMR) and Intellisense while remaining lightweight.

    If you require a full-featured framework with routing and serverless capabilities, consider using SvelteKit instead. This template is structured similarly to SvelteKit to facilitate easy migration if your project requirements grow.

  7. Setup the Vite + Solid template

    master

    To use this template, clone the repository and install dependencies using your preferred package manager. While a pnpm-lock.yaml is included because dependencies are maintained via pnpm, you can use npm or yarn instead. If you use a different package manager, you can safely remove the pnpm-lock.yaml file.

    $ npm install # or pnpm install or yarn install
  8. Configure Next.js with Webpack or SWC

    master

    For Next.js projects using Webpack (or those using SWC), inject the loader into the webpack configuration function within next.config.js. Ensure the loader is only applied to the client-side (!isServer) to avoid issues with server-side rendering.

    module.exports = {
      webpack: (config, { isServer }) => {
        if (!isServer) {
          config.module.rules.push({
            test: /\.(tsx|ts|jsx|js)$/,
            exclude: /node_modules/,
            use: [
              {
                loader: "@locator/webpack-loader",
                options: {
                  env: "development",
                },
              },
            ],
          });
        }
        return config;
      },
    };