Skip to content

oj dev: an editor's source-tagging plugin runs natively - #825

Closed
raphamorim wants to merge 1 commit into
mainfrom
rapha/native-source-tagging
Closed

raphamorim wants to merge 1 commit into
mainfrom
rapha/native-source-tagging

Conversation

@raphamorim

Copy link
Copy Markdown
Collaborator

What

oj dev runs an editor's source-tagging plugin natively. The one oj knows is the lovable-tagger package's lovable-plugin (1.3.x), a pre plugin whose work in a serve is two things:

  • it answers react/jsx-dev-runtime with a virtual module of its own, a wrapper of React's jsxDEV that records each element's source on its DOM node for the editor (resolveId + load);
  • it writes the app's resolved Tailwind theme to src/tailwind.config.lov.json at boot and when tailwind.config.ts or a v4 CSS entry changes, for the editor's color picker (buildStart + configureServer).

With this PR the plugin host leaves that plugin out and oj does both. A React app whose only other plugins are React's boots no plugin host at all.

How

  • Config extraction asks the instance what it does (sourceTagging in vite-extract.mjs), so no version of the plugin is assumed: its resolveId is called for react/jsx-dev-runtime from an importer of the app's and its load for the answer, and its configureServer is called with a stand-in server whose watcher records whether a change listener is registered (the Tailwind feature's only effect there). The answers travel in the extraction (ViteValues.source_tagging, cached with the config like every other value) to OjConfig.vite_source_tagging and the server state.
  • Only an instance with exactly the known hooks is taken over (isSourceTaggingPlugin in native-plugins.mjs: name, enforce, configResolved, buildStart, configureServer, resolveId, load). One with more, a later version's transform say, stays hosted. A build hosts it as before: the native path is for serve.
  • The JSX runtime import: the import rewrite sends react/jsx-dev-runtime from every module but the plugin's own to /@id/__x00__jsx-source/jsx-dev-runtime, Vite's URL for the \0 id, ahead of the pre-bundle as a pre plugin's resolveId runs ahead of vite:resolve. The server compiles the plugin's code like any module under a synthetic file, so its own import of the runtime reaches the pre-bundle.
  • The Tailwind theme: source-tagging-theme.mjs is the plugin's run() as an engine job with the app's own packages, resolved from the plugin's package first (esbuild and tailwindcss are its dependencies) and the app second: Tailwind v4 through @tailwindcss/node's design system with var()s resolved from :root, else v3 by bundling tailwind.config.ts with esbuild and tailwindcss/resolveConfig. It runs on a thread off the boot's path (the plugin's buildStart ran before Vite listened, but an editor reads the file on demand) and again after a change to a watched file, from the watch feed.

Tests

Each fails without the change.

  • vite_values_tests: the fact reaches the config; null leaves it unset.
  • engine_extraction_tests: a plugin of the known shape is asked (id, code, package, the Tailwind watch) and counts as native in a serve, hosted in a build; one with its features off is native with nothing to do; one with a hook oj does not know stays hosted.
  • host_filter_tests: a serve host leaves the plugin out and keeps a lookalike with a transform and the user's plugin; a build host keeps all three.
  • tests/dev/source_tagging: the served JSX module imports the plugin's URL, that URL serves the plugin's code with its runtime import on the pre-bundle, and no host boots; the v4 theme is written at boot and rewritten after a CSS edit (the fixture's Tailwind 4 and @tailwindcss/node); a v3 config with a relative import and a require is bundled by the fixture's esbuild and resolved by a resolveConfig stand-in, and the bundled file is removed.

Not in this PR

  • The dev-server bridge and the assets-proxy plugins that ship beside the tagger in the same configs: with those native too, such an app's serve boots no host. Next.
  • An SSR serve (oj dev --ssr, a TanStack Start app) does not redirect the runtime; the known plugin's wrapper passes through on the server anyway, and the configs that carry it are client apps.
  • The older vite-plugin-component-tagger (1.1.x), which tags elements with a transform: hosted as before.

Config extraction asks the instance what it does: its resolveId and load
for react/jsx-dev-runtime, and whether its configureServer watches the
Tailwind config. In a serve the plugin host leaves it out and oj does
both: the JSX dev runtime import goes to the plugin's module at Vite's
URL for its virtual id, compiled like any module so its own import of
the runtime reaches the pre-bundle; and the app's resolved Tailwind theme
is written to src/tailwind.config.lov.json at boot and on a Tailwind
config change, by an engine job with the app's own @tailwindcss/node
(v4) or esbuild and tailwindcss/resolveConfig (v3), off the boot's path.
An app whose other plugins are React's boots no plugin host. An instance
with a hook oj does not know, and every build, keep the host as before.
@raphamorim
raphamorim requested a review from a team as a code owner December 3, 2026 14:25
@raphamorim

Copy link
Copy Markdown
Collaborator Author

Closing in favour of emitting source tags in oj's own JSX transform (the data-tsd-source convention), which covers this plugin's case without emulating it. The branch stays for reference.

@raphamorim raphamorim closed this Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant