convert-tanstack-app-to-web-component

Refactors a standalone TanStack-based application (React/Vue/Solid) into a standard Web Component (Custom Element), enabling framework-agnostic injection into legacy apps or micro-frontends.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "convert-tanstack-app-to-web-component" with this command: npx skills add visio-ai/teste-skills/visio-ai-teste-skills-convert-tanstack-app-to-web-component

Step 1 — Install the required dependency

npm install @r2wc/react-to-web-component

Step 2 — Create src/App.tsx (body-level app component)

Extract the body content from src/routes/__root.tsx into a standalone React component. This component must NOT render <html>, <head>, or <body> — only the visual content (Header + router outlet). The original __root.tsx stays untouched for the SSR build.


Step 3 — Create src/widget.tsx (custom element entry)

  • Import App from src/App.tsx
  • Use @r2wc/react-to-web-component to wrap it as a custom element
  • Inject Tailwind CSS into the Shadow DOM using Vite's ?inline import:
import styles from './styles.css?inline'
// pass styles to r2wc options so they are adopted into the shadow root
  • Register the custom element, for %app-name get the name from the package.json name:
customElements.define('%app-name', TanstackAppElement)

Step 4 — Switch to Memory Router for the WC build

Browser history routing conflicts with the host page URL. In src/widget.tsx, create the router using createMemoryHistory:

import { createMemoryHistory } from '@tanstack/react-router'

const router = createRouter({
  routeTree,
  history: createMemoryHistory({ initialEntries: ['/'] }),
})

Update src/router.tsx to accept an optional history parameter so the same factory works for both SSR and WC builds.


Step 5 — Create vite.config.wc.ts (standalone lib build)

The WC build must NOT use TanStack Start's SSR plugins (tanstackStart(), nitro(), devtools()). Create a separate Vite config in lib mode:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import tsconfigPaths from 'vite-tsconfig-paths'

export default defineConfig({
  plugins: [react(), tailwindcss(), tsconfigPaths()],
  define: {
    'process.env.NODE_ENV': JSON.stringify('production'),
  },
  build: {
    outDir: 'webcomponent',
    lib: {
      entry: 'src/widget.tsx',
      name: 'TanstackAppWC',
      fileName: '%app-name',
      formats: ['iife'], // single self-executing bundle for easy <script> embedding
    },
  },
})

Add the webcomponent dir to .gitignore


Step 6 — Add build:wc script to package.json

"build:wc": "vite build --config vite.config.wc.ts"

Output: dist/tanstack-app.iife.js — a single file any host page can load:

<script src="tanstack-app.iife.js"></script>
<tanstack-app></tanstack-app>

Files to create / modify

FileActionReason
src/App.tsxCreateBody-level app (no html/head/body)
src/widget.tsxCreateCustom element definition + bootstrap
vite.config.wc.tsCreateLib build without SSR plugins
package.jsonModifyAdd build:wc script
src/router.tsxModifyAccept optional history param
src/routes/__root.tsxNo changeKept intact for SSR build
src/styles.cssNo changeImported via ?inline in WC entry

CSS strategy inside Shadow DOM

OptionHowTrade-off
Constructable Stylesheet (recommended)Bundle CSS as string via ?inline, inject via adoptedStyleSheetsClean isolation, no leakage
Light DOM (no Shadow DOM)Pass shadow: false to r2wcSimpler, but styles leak both ways

Result

The original TanStack Start SSR app remains fully functional. The WC build is an additive parallel output — same source, different entry and Vite config.

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

Maintenance

Maintenance makes home management simple. Record, search, and analyze your data with clear terminal output.

Registry SourceRecently Updated
General

ManualExpert

Professional translator for hardware and technical manuals delivering complete, page-by-page bilingual tables and Word exports for accurate DTP preparation.

Registry SourceRecently Updated
General

Semantic Router

让 AI 代理根据对话内容自动选择最合适的模型。四层识别(系统过滤→关键词→指示词→语义相似度),四池架构(高速/智能/人文/代理),五分支路由,全自动 Fallback 回路。支持 trigger_groups_all 非连续词组命中。

Registry SourceRecently Updated
General

Campaign

Campaign - command-line tool for everyday use

Registry SourceRecently Updated