Skip to content

[WC-3670]: share mobx and mobx-react-lite between widgets - #2524

Open
r0b1n wants to merge 2 commits into
mainfrom
feat/shared-mobx-bundle
Open

r0b1n wants to merge 2 commits into
mainfrom
feat/shared-mobx-bundle

Conversation

@r0b1n

@r0b1n r0b1n commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Pull request type

Dependency changes (any modification to dependencies in package.json)


Description

Every widget that uses mobx inlines its own copy (~57 KB per widget). A page with a grid, three filters, sorting, gallery and selection helper downloads it 7 times.

This adds withSharedLibs to @mendix/rollup-web-widgets. The helper builds mobx and mobx-react-lite once, as standalone bundles. The charts already share Plotly the same way (com/mendix/shared/charts/), and the same mechanism has run in Studio Pro since Mendix 9.6. The lowest minimum version among the migrated widgets is 9.17.

Migrated: datagrid, the 4 datagrid filters, dropdown-sort, gallery, selection-helper, file-uploader, image-cropper, date-time-picker, maps. Not migrated yet: charts / custom-chart.

How it works

com/mendix/widget/web/datagrid/Datagrid.mjs   → ../../../shared/mobx/6.12.3/esm/mobx.mjs
com/mendix/shared/mobx/6.12.3/{amd,esm}/      ← shared bundle + dependencies.{json,txt}
com/mendix/shared/mobx-react-lite/4.0.7/...   → ../../../mobx/6.12.3/...
  • Widgets on the same library version ship byte-identical files at the same path. When they're deployed together, the client loads one copy.
  • The version is part of the path, so an older MPK can't overwrite the files a newer widget needs. Different versions sit side by side.
  • Only the AMD and ESM runtime bundles change. editorPreview and editorConfig keep bundling mobx.
  • One source of truth: the shared bundles are built from the patched mobx that @mendix/rollup-web-widgets depends on. The build fails if a widget resolves a different version.
  • Mixed apps keep working: the existing mobx patch (__mx_isolated_mobxGlobals) makes old inlined copies and the shared copy use the same global state.

Usage:

// rollup.config.mjs (+ "@mendix/rollup-web-widgets": "workspace:*" in devDependencies)
import { withSharedLibs } from "@mendix/rollup-web-widgets/sharedLibs.mjs";

export default (args) => withSharedLibs(args, args.configDefaultConfig);

Only stable third-party libraries used by many widgets should be shared, not @mendix/widget-plugin-*. To share another library, add it to LIBRARIES in sharedLibs.mjs and as a dependency of @mendix/rollup-web-widgets.

datagrid-web/rollup.config.js is renamed to .mjs so it can import the helper.

Licensing

A widget's own dependencies.json no longer lists mobx, the same way it never listed React. Each shared library gets its own dependencies.{json,txt} instead. SBOM_Generator.jar reads every dependencies.json in the MPK. On file-uploader it reports the same 16 components on main and on this branch, mobx and mobx-react-lite included.

Results (production, uncompressed ESM)

Widget Before After
datagrid 232 KB 174 KB
datagrid text / number filter 112 KB 56 KB
datagrid dropdown filter 144 KB 86 KB
gallery 125 KB 67 KB
dropdown-sort 68 KB 8 KB
selection-helper 58 KB 2 KB
file-uploader 274 KB 217 KB

Grid + 3 filters + sort + gallery + selection helper: 851 KB → 516 KB, including the shared files (~63 KB, loaded once). Each MPK now carries the shared files (~37 KB zipped).

Verified

  • Unit tests (node --test, run by CI) for withSharedLibs: import paths for a normal and a moved (maps-web) output, mobx-react-lite → mobx, existing externals preserved, the version-mismatch error, and invalid library lists.
  • All 12 widgets build in production and watch mode. Their shared files are byte-identical, and no runtime bundle inlines mobx.
  • Headless Chromium smoke test with all 12 outputs merged into one folder: every widget loads through ESM and through require.js, each shared file is requested once, and a single mobx instance drives observer re-renders.
  • The OSS BOM tool lists the shared libraries (see Licensing).

What should be covered while testing?

  • React client and Dojo client: a Data Widgets page (grid + filters + gallery), file-uploader, maps.
  • Data Widgets module MPK.
  • pnpm start with MX_PROJECT_PATH.
  • An app that mixes an old MPK (inlined mobx) with a new one.

Add withSharedLibs to @mendix/rollup-web-widgets. It builds mobx and
mobx-react-lite as standalone AMD/ESM bundles at a versioned path
(com/mendix/shared/<lib>/<version>/) inside each MPK and points the
widget bundles at them instead of inlining a copy. Widgets with the same
library version ship identical files at the same path, so they collapse
into one copy when deployed together; different versions sit side by
side instead of overwriting each other.

Use it in the Data Widgets widgets, file-uploader, image-cropper,
date-time-picker and maps.
@r0b1n
r0b1n force-pushed the feat/shared-mobx-bundle branch 2 times, most recently from ac2ec62 to cc23c18 Compare November 17, 2026 09:31
@r0b1n r0b1n changed the title build: share mobx and mobx-react-lite between widgets [WC-3670]: share mobx and mobx-react-lite between widgets Nov 17, 2026
@r0b1n
r0b1n marked this pull request as ready for review November 17, 2026 09:40
@r0b1n
r0b1n requested a review from a team as a code owner November 17, 2026 09:40
@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

AI Code Review

✅ Approved — no issues found


What was reviewed

Package Change
shared/rollup-web-widgets New sharedLibs.mjs (withSharedLibs), node --test unit tests, turbo.json, new dependencies
datagrid-web rollup.config.js renamed to .mjs and wrapped with withSharedLibs
Datagrid filters (date, dropdown, number, text), dropdown-sort-web, gallery-web, selection-helper-web, file-uploader-web New rollup.config.mjs and a @mendix/rollup-web-widgets devDependency
date-time-picker-web, image-cropper-web, maps-web Existing rollup configs wrapped with withSharedLibs

Skipped (out of scope): pnpm-lock.yaml

CI: pending: Run code quality check, Plan E2E matrix, sigridci · unrelated failures: security/snyk (external scanner, 17 tests; the code and license Snyk checks pass)


Questions

  • No widget CHANGELOG.md is touched. Is the change intentionally invisible to app developers? The bundles load from shared paths, and an app that mixes old and new MPKs relies on the existing mobx isolation patch. If you consider that user-visible, add an entry.

Positives

  • Shared files sit under a versioned path, and the build fails if a widget resolves a different mobx version than the shared bundle. This avoids collisions between MPKs.
  • Only the AMD and ESM runtime bundles change. editorPreview and editorConfig keep bundling mobx, so Studio Pro is unaffected.
  • withSharedLibs has unit tests for import paths, moved outputs, version mismatch and invalid library lists. The PR description also documents the licensing and BOM handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant