Repository navigation
fix(ui, preview): release the bundle compilers, and transpile TypeScript that resolves from node_modules - #10612
Conversation
…a time
`bit ci pr` was OOM-killed ("Received 'killed' signal") at the first preview
bundle after BundleUI. BundleUI bundled both UI roots (scope and workspace)
concurrently and never closed either rspack compiler, so two complete module
graphs - plus rspack's native side of them - stayed resident for the rest of the
build, which runs every remaining task, including a webpack preview bundle per
env, in the same process.
Close each compiler when its run finishes (the webpack bundler already does this)
and bundle the roots sequentially. Serial costs little here: a single rspack run
already saturates the cores, so the two in parallel mostly bought peak memory.
Correction to the previous commit. Closing each rspack compiler inside `build()`
meant closing one while the other UI root was still bundling in the same process,
and that run's second root failed to resolve modules the first had resolved
("Can't resolve '@teambit/cloud.hooks.use-cloud-scopes'"), which the run before
it - same code, no closes - bundled fine.
Keep the bundling shape that worked (both roots via Promise.all, no close in
between) and release the compilers in BundleUiTask's `finally`, once both roots
are done. That still drops the two UI module graphs before the preview bundles
that were getting the process OOM-killed.
`bit ci pr`'s BundleUI task died with two `Module parse failed: Expected '{',
got 'type'` errors: the bundle reached `@teambit/component-issues/index.ts` and
`@teambit/pkg.modules.semver-helper/semver-helper.ts` as raw TypeScript, and
both rspack configs excluded node_modules from the swc loader.
Those are workspace components, consumed through node_modules as injected pnpm
copies. An injected copy is taken from the package dir at install time, before
the compile that fills its `dist`, so the package entry (`dist/index.js`)
doesn't exist in the copy and resolution falls back to `index.ts`. Removing the
core envs is what routed the bundle through them: the UI root now pulls the
installed `@teambit/[email protected]` UI runtime, whose published dependency chain
re-enters the workspace copies of `@teambit/component` and friends.
Excluding node_modules is about not re-processing already-transpiled
third-party JavaScript. A `.ts` file is never valid bundler input no matter
where it resolves from, so exclude only JavaScript and let TypeScript through.
PR Summary by QodoFix rspack compiler cleanup and TS transpilation from node_modules
AI Description
Diagram
High-Level Assessment
Files changed (4)
|
Code Review by Qodo
1. allSettled/flatMap require newer lib
|
…the ones nobody claims Three problems with the previous commit, all from review: - BundleUiTask built both roots with `Promise.all`, which rejects as soon as one fails - the `finally` then closed compilers while the other root was still bundling, the exact hazard the deferral exists to avoid. It now waits with `allSettled` and surfaces the first failure afterwards. - `build()` pushed every compiler it created onto `openBuildCompilers`, but only BundleUiTask ever drains that list. `bit start`'s pre-bundle and watch rebuilds call `build()` too, so a module graph per build would have stayed referenced for the life of the process - worse than before. Tracking is now opt-in via `deferClose`; every other caller gets its compilers closed in `build()`'s finally. - Cleanup ran unguarded from a `finally`, so a throw from `compiler.close()` could replace the build error that sent us there. It is now per-compiler try/catch that logs and moves on, the same contract as the webpack bundler's cleanup.
|
All three review findings were real; fixed in c085fd1. 1. Premature compiler close. Correct, and it defeated the point of deferring the 2. Compilers retained outside the task. Also correct, and worse than the status 3. Cleanup can override errors. Fixed with per-compiler try/catch that logs and On the first alternative in the summary (close per root, bundle sequentially): I |
|
Code review by qodo was updated up to the latest commit c085fd1 |
…preview bundlers Review point: the predicate was defined twice, once per bundler, so the two could drift into handling TypeScript-in-node_modules differently. Both configs already import from @teambit/webpack (and nothing in that aspect imports back), so it moves there as a single export with the rationale attached.
|
The new finding (duplicate exclude helper) is fair — fixed in 12cd3ec.
Verified with the full |
|
Code review by qodo was updated up to the latest commit 12cd3ec |
…modules paths
Review point: the previous wording ('lets TypeScript through from anywhere, declaration
files excluded') could be read as excluding .d.ts globally. The predicate only ever
returns true for paths under node_modules; outside it, nothing is excluded and the
rule's own `test` decides.
|
Fair — fixed in 2adec24. The predicate only ever returns Behavior is unchanged - comment only. |
|
Code review by qodo was updated up to the latest commit 2adec24 |
…nent Per review, the helper belongs in its own component under teambit.webpack/modules. It is published as @teambit/webpack.modules.exclude-node-modules-js, so both bundler configs import it from there and the copy in the webpack aspect is gone, along with its re-export - nothing outside those two configs used it. Pinned at 0.0.1 in the workspace policy. No lockfile entry here: the install that adds it also rewrites a large amount of unrelated drift in pnpm-lock.yaml, which should not ride along in this PR.
|
Switched over to the released component in f7b4bf7.
Verified against the published package rather than by inspection: installed it and ran the full One deliberate omission: no |
|
Code review by qodo was updated up to the latest commit f7b4bf7 |
Adds @teambit/[email protected] for the three importers that now consume it (the workspace root, preview and ui-foundation).
|
Code review by qodo was updated up to the latest commit 6a968c8 |
Review: BundleUiTask found the first rejected root, threw its reason, and the catch
immediately replaced it with `new Error("Generating UI bundle failed")`. The actual
error only ever reached the debug log - diagnosing a failed BundleUI in CI meant
downloading the log artifact to find out what rspack actually said.
Now the thrown message names the roots that failed, every failing root is logged
(not only the one that ends the task), and the first reason is attached as `cause`.
The reason itself stays out of the message on purpose: for an rspack failure it is
the entire stats output, megabytes of it, which is why the message was generic in
the first place.
|
Both new findings are fair — fixed in a72bdf1. Error cause discarded. This one bit me for real while developing the PR: a Now the thrown message names the roots that failed, every failing root is logged rather than only Ambiguous failure ordering. Same fix covers it: the message lists every root that failed One deliberate limit: the reason itself stays out of the message. For an rspack failure it is the Note Finding 6 (ambiguous exclude helper docs) is moot on this branch now: that file was deleted when |
|
Code review by qodo was updated up to the latest commit a72bdf1 |
|
This last one is stale rather than open — no change needed, and I would rather say why than quietly It quotes The finding looks like it is being re-evaluated against an intermediate commit rather than the head. For the record, the point it raised was right, and the published component's own JSDoc already states |
Same issue as BundleUI, found by review: `buildPreBundlePreview` creates an rspack compiler, runs it, and never closes it - so the module graph and rspack native side stay resident. Both callers keep the process alive well past that point: the PreBundlePreview build task runs once per env inside a `bit build` that runs every task in one process, and `bit start` pre-bundles on demand in a server that then keeps running. Closed in a `finally`, so the error paths release it too, and the close itself is guarded - cleanup must not replace the build error that sent us there. No deferral needed here, unlike BundleUiTask: there is one compiler and nothing else bundling alongside it. Checked the other rspack call site while here: `ui-server.ts` hands its compiler to RspackDevServer, which needs it alive to watch, so it stays as is.
|
@GiladShoham asked whether
It is closed in a I checked the only other rspack call site in the repo while I was at it: |
|
Code review by qodo was updated up to the latest commit b2adf49 |
…g them closeRspackCompiler() dropped both the close() callback's error argument and any thrown error, so a compiler that never releases looked exactly like one that does. Both paths now log at debug, matching UiMain.closeCompilers(), and cleanup still never throws from the finally. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Code review by qodo was updated up to the latest commit 0cb497b |
|
Code review by qodo was updated up to the latest commit f16ab91 |
The lockfile committed alongside the exclude-node-modules-js addition was a full re-resolution: it moved several hundred packages within their ranges, among them @bitdev/react.generators.react-templates 6.0.3 -> 6.0.9, which no longer exports ReactWebpackTemplate. scopes/react/react/react.templates.ts imports it, so `tsc --noEmit` and the `declaration` build task both failed - that is the whole of generate_and_check_types and bit_pr going red. Regenerated from master's lockfile instead, so the only package this PR adds is @teambit/[email protected]. The rest of the diff is pnpm re-syncing importers with master's tree - #10610 added webpack-bundler, webpack-dev-server, html-webpack-plugin and webpack-assets-manifest to scopes/react/react and deleted components/sandbox/webpack-poc - and changes no package version. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Code review by qodo was updated up to the latest commit 291ca9a |
Conflict resolutions: - .circleci/config.yml: master's revert of --keep-lane (#10615) applied inside this branch's memory-sampler wrapper for the `bit ci pr` step. - scopes/react/react/react.env.ts, react.main.runtime.ts: stay deleted - this branch removes the core env sources from the workspace, so #10610's edits to them are moot. - scopes/preview/preview/rspack/rspack.config.ts: keep the mdxOptions parameter. The import master re-adds is what this branch removed on purpose: @teambit/mdx.modules.mdx-v3-options is ESM-only and has to reach the config through a native import() done by the caller. - scopes/preview/preview/pre-bundle.ts: keep master's closeRspackCompiler, which logs a close failure instead of swallowing it (#10612), and drop this branch's earlier copy. - workspace.jsonc: both sides - master's webpack-bundler/webpack-dev-server entries plus this branch's exclude-node-modules-js. - pnpm-lock.yaml: regenerated from master's lockfile with `bit install`, so the full-re-resolution lockfile this branch carried is gone. @teambit/react is now the only added package (1.0.1099, consumed from the registry); everything else is importers and duplicate versions dropping out with the core env sources. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Part 2 of #10596 — the workspace/scope dedupe. Builds both UI roots in **one** rspack compilation with an entry each, so the chunks they share are emitted once. Takes the shipped `@teambit/ui` pre-bundle from **24 MB to 16 MB**. > Stacked on #10628 and targets that branch. Retarget to `master` once #10628 merges. ## The roots are the same app The issue estimated a small win from file-level comparison ("only 25 files / 1.9 MB byte-identical"). At the *module* level they are all but identical: | | modules | bytes | | --- | --- | --- | | workspace root | 3,403 | 16.00 MB | | scope root | 3,403 | 16.00 MB | | **shared by both** | **3,402** | **15.96 MB** | | workspace-only | 1 | 40 KB | | scope-only | 1 | 40 KB | The one differing module is the generated root entry (`ui.root<hash>.js`) — same aspect graph, different root aspect id baked in. Both the `workspace` and `scope` aspects are already in *both* bundles; the root id only selects which one renders. So the duplicate is essentially total, and so is the saving. | | before | after | | --- | --- | --- | | browser chunks | 16.4 MB (2 copies) | **8.2 MB** (1 copy) | | ssr | 7.9 MB | 7.9 MB | | **artifact total** | **24 MB** | **16 MB** | The 6.53 MB vendor chunk and 0.53 MB CSS are now shared; each root adds ~40 KB of its own. Combined with #10628 this is 58 MB → 16 MB. It also lowers peak memory during `bit build` — the concern raised on #10612 — since there is one module graph instead of two, and the `BundleUI` task got slightly faster (17s vs 18-19s). ## Layout ``` artifacts/ui-bundle/ .hash JSON: { "<rootAspectId>": "<sha1>", … } public/bit/ workspace.html only the chunks the workspace entry needs scope.html only the chunks the scope entry needs asset-manifest.json static/js|css/… shared chunks, emitted once ssr/index.js scope only ``` Three things had to change to support it: 1. **`.hash` is now a map.** `shouldServeBundleUi` compares a *per-root* hash, but there is now one bundle. `.hash` holds one hash per root aspect id and `readBundleUiHash` looks up the root being served. A root missing from the map — or a bundle in the old layout — reads as "no pre-bundle" and falls back to a local build. 2. **No more `index.html`.** With two roots in one compilation there is no single default document, so the server falls back to `<root>.html`. This is the sharpest edge in the PR: get the name wrong and every *client-side* route 404s while the SSR-rendered ones keep working. There is a test for exactly that (below). 3. **The asset manifest is entry-aware.** `generateAssetManifest` hardcoded `entrypoints.main`, and it is shared with the preview aspect's rspack config, so renaming was not an option. It now also emits `entrypointsByName`; `entrypoints` keeps its old meaning for single-entry compilations, so the preview side is untouched. The SSR middleware prefers its root's entry and falls back to `entrypoints`. `build()` now builds every registered root rather than one, so `uiRootAspectIdOrName` only selects the output location. That is nearly free — the module graph is shared — and it keeps one layout everywhere instead of one for the build task and another for `bit start`. A bare scope registers only the scope root, so it emits just `scope.html`. `bit start --dev` is unaffected; the dev config is separate and still single-entry. ## Deletes machinery Because the two roots bundled concurrently, #10612 had to defer closing the compilers (`openBuildCompilers` + a `deferClose` option on `UiMain.build`) instead of closing inside `build()`. With one compiler there is no concurrent sibling, and it collapses into a single close in `build()`'s `finally`. `buildIfNoBundle` also stopped constructing a whole rspack config — resolving every root's aspects — just to read `output.path`. ## Testing Both roots verified in a real browser, on **both** paths that can serve them: - *local build* (`bit start --rebuild`): bare scope emits `scope.html` and SSRs every route; workspace emits both htmls with the 6.53 MB vendor chunk present once. - *pre-bundle* (the production path): built artifact placed in the bvm install, `bit start` logged `returned from ui bundle cache` and `bundle will be served from …/ui-bundle/public/bit`, with no local build directory created. Scope SSR renders (`/` 27 KB, `/ui/button` 52 KB); workspace serves `workspace.html` through the fallback for client-side routes. Workspace and scope home pages, component page, code tab and API reference all render with no console errors. New e2e assertion covers change 2 — `?rendering=client` makes the SSR middleware call `next()`, which is the only way to reach the history-api fallback. Verified it catches the bug: pointing the fallback back at `index.html` fails that test and only that test, which is precisely the failure mode described above.
Two independent problems in the bundling tasks, both found while getting
bit ci prthrough a build on #10465.
The UI bundle compilers were never released
BundleUiTaskbundles both UI roots (scope and workspace) andui.build()neverclosed either rspack compiler. A compiler keeps its whole module graph alive - plus
rspack's native side of it - and
bit buildruns every task in one process, so bothUI graphs stayed resident through every task that follows, a preview bundle per env
among them. The process was OOM-killed partway through those bundles.
The compilers are now closed once the task is done, rather than inside
build().The webpack bundler in this repo closes its compiler after each run the same way.
Why at the end of the task and not per build, stated with the confidence the
evidence actually supports: an earlier revision closed each compiler inside
build(), and that run had the second root fail to resolve a module the first hadresolved, while the same code with no closes bundled fine. But that run was also
bundling a component's raw sources, because the component's
distwas missing -a #10465-only condition (eight components had landed on a compiler-less default
env) that has since been fixed. With
distpresent the bundle does not walk intocomponent source directories at all, so I would not treat "closing mid-task breaks
the other root" as established. Deferring is the conservative choice while there
are two compilers in flight; it is not a constraint to design around.
Which matters for #10596: if the two roots become one rspack build with two entry
points, there is no "other root still bundling", and the tracking added here
(
openBuildCompilers+deferClose) collapses into a single close inbuild()'sfinally. This PR reduces what is held after the task; the peak during it isstill two full compilations of the same app, which is that issue's territory.
.tsreaching the bundler unhandledBoth rspack configs excluded
node_modulesfrom the swc loader, so a bundle thatreached TypeScript through
node_modulesdied on the firstexport typewithModule parse failed. That happens for real: a bit component consumed throughnode_modulescan resolve to its sources, because an injected pnpm copy is takenfrom the package directory before the compile fills its
dist, and the packageentry then falls back to
index.ts.Excluding
node_modulesis about not re-processing already-transpiled third-partyJavaScript. A
.tsfile is never valid bundler input wherever it resolves from, sothe exclusion now applies to JavaScript only, with
.d.tsstill excluded.Testing
On #10465 the compiler release is part of what took the measured container peak of
the
bit ci prbuild from 16373MB to 11695MB (sampled from the cgroup, 16384MBlimit), and
BundleUIsucceeds with both changes in place. The full e2e suite hasrun green with them four consecutive times.