Skip to content

feat(workspace | preview): migrate from webpack to rspack - #10187

Merged
luvkapur merged 35 commits into
masterfrom
perf/rspack-migration
Feb 10, 2026
Merged

luvkapur merged 35 commits into
masterfrom
perf/rspack-migration

Conversation

@luvkapur

@luvkapur luvkapur commented Feb 5, 2026 •

Copy link
Copy Markdown
Member

This PR migrates the UI server and Preview pre-bundle pipelines from Webpack to Rspack.

  • Replaced Webpack bundling paths for:

    • teambit.ui-foundation/ui (dev/prod/ssr UI bundling)
    • teambit.preview/preview pre-bundle generation
  • Added/standardized Rspack config flow:

    • SWC-based transpilation
    • Rspack CSS pipeline (experiments.css)
    • Rspack asset modules
    • shared Rspack manifest generation (asset-manifest.json)
  • Reduced classic loader usage and moved core work to Rspack built-ins where supported.

luvkapur and others added 17 commits February 2, 2026 16:21
Reduces code duplication between rspack.dev.config.ts and
rspack.browser.config.ts by extracting shared rules (SWC loader,
source-map loader, style rules, font rules) and constants
(moduleFileExtensions, RspackManifestPlugin) into a common module.

Also fixes regex backtracking vulnerability in navigateFallbackDenylist
flagged by SonarQube (changed [^/?]+ to [^/.?]+ to prevent overlapping
character classes).

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@luvkapur luvkapur changed the title feat(workspace): rspack feat(workspace | preview): migrate from webpack to rspack Feb 6, 2026
luvkapur and others added 11 commits February 6, 2026 14:29
…k configs

Extract shared resolveAlias(), resolveFallback, and resolveFallbackDev
into rspack.common.ts to eliminate duplicated blocks across browser,
dev, and SSR configs. Net -32 lines.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Export RspackManifestPlugin from @teambit/ui and import it in the
pre-bundle rspack config instead of maintaining an identical copy.
Fixes SonarCloud duplicated lines density flag.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…undle

Extract RspackManifestPlugin into its own file so that exporting it
from @teambit/ui index does not pull rspack.common.ts and its
require.resolve calls for loaders into the preview bundle graph.
Fixes Can not resolve os/http/vm errors during --rebuild.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Revert RspackManifestPlugin export from @teambit/ui index to avoid
  pulling build-time code into the browser bundle (caused runtime
  .replace error on undefined)
- Give pre-bundle its own local copy of RspackManifestPlugin in a
  separate file (rspack-manifest-plugin.ts)
- Fix import/default lint errors: use named import for rspack
- Fix TS errors: Stats.toString needs args, proxy type cast
- All lint and type checks pass

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@luvkapur
luvkapur marked this pull request as ready for review February 9, 2026 22:19
@sonarqubecloud

Copy link
Copy Markdown

@luvkapur
luvkapur enabled auto-merge (squash) February 10, 2026 18:12
@luvkapur
luvkapur merged commit 225881d into master Feb 10, 2026
2 checks passed
@luvkapur
luvkapur deleted the perf/rspack-migration branch February 10, 2026 19:04
luvkapur added a commit that referenced this pull request Feb 12, 2026
Reverts the following commits:
- 9527d01 fix(rspack): migrate to built in manifest plugin (#10194)
- 225881d feat(workspace | preview): migrate from webpack to rspack (#10187)
- 93b656a feat(workspace): enable graphql batching by default (#10188)

Restores webpack-based dev server and preview bundling.
Disables graphql batching (reverts to non-batched default).

Co-Authored-By: Claude Opus 4.6 <[email protected]>
GiladShoham added a commit that referenced this pull request Aug 18, 2026
…10628)

Part 1 of #10596. Cuts the shipped `@teambit/ui` pre-bundle from **58 MB
to 24 MB**, and fixes scope SSR, which turned out to have been throwing
on every request for months.

The workspace/scope single-compilation dedupe (~8 MB) is deliberately
left for a follow-up PR — it needs an artifact-layout and `.hash`
redesign, and mixing it in would make the SSR fix hard to verify.

## Size: 58 MB → 24 MB

| | before | after |
| --- | --- | --- |
| `ui-bundle/scope/public/bit/ssr/index.js` | 37 MB | **6.8 MB** |
| `ui-bundle/scope` | 50 MB | 16 MB |
| `ui-bundle/workspace` | 8.2 MB | 8.2 MB |
| **total** | **58 MB** | **24 MB** |

Two causes, both in `rspack.ssr.config.ts`:

- `devtool: 'eval-cheap-module-source-map'` wrapped every module in
`eval('…')` with an inlined base64 source map — 2,566 of them, 23.4 MB,
60% of the file. It entered as `'eval-cheap-source-map', // TODO` and
was later retuned in #7147 as a *development* sourcemap change; it was
never a deliberate choice for a shipped artifact. Now
`shouldUseSourceMap ? 'source-map' : false`, matching the browser
config's opt-in.
- The SSR build had no `optimization.minimizer` at all. It now mirrors
the browser build's `SwcJsMinimizerRspackPlugin`.

## Scope SSR was dead, and it took three fixes

Only `scope.ui-root.ts` sets `ssr: true`. The middleware catches any
render error and calls `next()`, falling through to the static
`index.html` — which looks completely normal in a browser. So the
failure was invisible: `curl /` and `curl '/?rendering=client'` returned
byte-identical 1,027-byte responses with an empty `<div
id="root"></div>`.

Three independent defects were stacked:

1. **`cjs` missing from the SSR asset catch-all.** The browser config
excludes `/\.(cjs|js|mjs|jsx|ts|tsx)$/`; the SSR config omitted `cjs`,
so every `.cjs` module was emitted as an `asset/resource` whose module
value is the file's URL. A component imported from one reached React as
a tag name → `Minified React error #65` = *"Invalid tag:
/public/ssr/&lt;hash&gt;.cjs"*. Introduced by the webpack→rspack
migration (#10187), which replaced a shared base config with two
hand-written ones and added `cjs` to only one.
2. **`use-user-agent` present at two versions** (0.0.199 and 0.0.200).
Each copy calls `createContext`, so `ui.ui.runtime.tsx` provided
`ssrBrowserContext` on one instance while `Tooltip`'s `useUserAgent`
read the other, got `undefined`, took the browser fallback and
dereferenced `window` on the server. Fixed by adding it to
`resolveAlias` — the list that exists for exactly this class of bug.
Aliasing the hook fixes every consumer, so the duplicated `Tooltip`
copies need no aliasing of their own.
3. **`window` in a `useEffect` dependency array** — `useCurrentUser` had
`}, [window.location.href]);`. The effect *body* never runs on the
server, but a dependency array is evaluated on every render, including
the server one.

All routes now render server-side and the log shows zero SSR failures:

| route | before | after |
| --- | --- | --- |
| `/` | 1,027 B (fallback) | 27,178 B |
| `/ui/button` | 1,027 B (fallback) | 51,726 B |
| `/ui/button/~code` | 1,027 B (fallback) | 61,246 B |

A visible side effect: the document title was the build-time placeholder
(`bit-local-88bfe855`) and is now the actual scope name.

## Browser runtime effect

This turns SSR on for the first time in months, so it is a real runtime
change. Bare scope on localhost, 7 runs, median, SSR vs
`?rendering=client`:

| | SSR | client | delta |
| --- | --- | --- | --- |
| FCP, warm cache | **72 ms** | 384 ms | **−312 ms** |
| FCP, cold cache | 584 ms | 376 ms | +208 ms |
| TTFB | 24 ms | 2 ms | +22 ms |

SSR is ~5× faster to first paint on a repeat visit, and ~200 ms slower
on a cold first load where the 6.4 MB of JS dominates and the larger
HTML delays stylesheet discovery. Server startup is unchanged (~1.77 s);
the first request drops from 0.79 s to 0.49 s while now doing a real
render instead of throwing. Note localhost has ~0 latency, which
flatters client rendering — over a network SSR's margin widens.

## Testing

New e2e `e2e/harmony/ui-ssr.e2e.ts` asserts on the served HTML, since a
browser cannot distinguish a working SSR render from the fallback.

It runs `bit start --rebuild`, which matters: `getBundleUiPath` resolves
through `getAspectDirFromBvm`, so without `--rebuild` the server serves
the pre-bundle from the installed bvm version and the test would assert
on whatever bit release happens to be installed rather than on this
code. `HttpHelper` gained an optional extra-args parameter for this.

Verified the test actually catches the bug — with the `cjs` fix
reverted, 2 of the 3 assertions fail. (The third, an assertion that the
scope name appears anywhere in the document, passed in both states
because `--rebuild` puts the scope name in the static `<title>`; it now
asserts inside `#root` instead.)

Manual verification on both UI roots, with the freshly built artifact
swapped into the bvm install and the released `.hash` files preserved (a
`.hash` mismatch makes `bit start` silently rebuild locally instead of
serving the artifact). Confirmed the new bundle was genuinely served by
diffing content-hashed asset names against the originals. Both roots
checked in a real browser: workspace and scope home, component page,
code tab, API reference — no console errors. The one error found
(`componentChanged` subscription missing on a bare scope) reproduces
identically on the original bundle and is pre-existing.

## Bundle analysis tooling

`BIT_UI_BUNDLE_STATS=1` makes the UI build write an rspack stats file
per compilation, and `scripts/analyze-bundle.mjs` (`npm run
analyze-bundle`) summarizes assets plus the heaviest packages and
workspace scopes. No new dependency. This is what found defect 2 above.

It also surfaced that **81–86 packages appear at more than one version**
in a single bundle (`@teambit/design.ui.tooltip` at eight). That is only
~0.6 MB (~3% of module bytes), so it is a React-context correctness risk
rather than a size lever — worth tracking separately.

## Not in scope

The six env preview pre-bundles
(`@teambit/{react,node,mdx,env,aspect,readme}/artifacts/env-template`)
are 37.9 MB with only 6.4 MB of unique content — 31.5 MB is
byte-identical across the six. That is now the largest single remaining
item, bigger than the workspace/scope dedupe, and is not covered by
#10596 as written.
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.

2 participants