-
Notifications
You must be signed in to change notification settings - Fork 40
Production: ReferenceError: Cannot access '…' before initialization in BaseTooltipPluginView (circular import via extensions/index) #1035
Description
Description
In a production bundle (e.g. Next.js next build + client navigation), loading the WYSIWYG editor can throw:
ReferenceError: Cannot access '' before initialization at Module.BaseTooltipPluginView (…)
Dev mode may work; the failure shows up after minification/chunk ordering changes module evaluation order.
Root cause (analysis)
build/*/plugins/BaseTooltip/index.js imports getReactRendererFromState from ../../extensions/index.js (barrel).
While extensions/index.js is still evaluating, it pulls the YFM tree, which loads e.g. YfmNoteTooltipPlugin, which imports BaseTooltipPluginView from plugins/BaseTooltip/index.js again — a circular dependency. Depending on bundler order, the binding for getReactRendererFromState can still be in the TDZ when BaseTooltip runs, causing the error above.
Other parts of the package already import getReactRendererFromState directly from extensions/behavior/ReactRenderer/index.js, which avoids the cycle.
Suggested fix
In plugins/BaseTooltip (source), change the import from:
../../extensions/../../extensions/index.js
to:
../../extensions/behavior/ReactRenderer(or the correct relative path toReactRendererfrom BaseTooltip)
Apply to both ESM and CJS build outputs if they are generated separately.
Environment
@gravity-ui/markdown-editor: 15.32.0- Bundler: Next.js production client chunks (Webpack/Turbopack)
Reproduction
- Use an app that lazy-loads or routes to a view with the markdown editor (WYSIWYG) using the default/YFM-related extensions.
- Run production build (
next build) and production server (next start). - Navigate to the view — runtime error in console; React error boundary may catch it.
Workaround (downstream)
We patched the published build/esm and build/cjs plugins/BaseTooltip/index.js locally via patch-package to import from ../../extensions/behavior/ReactRenderer/index.js instead of the barrel. That removes the cycle and fixes the crash.
Happy to open a PR if you agree with this direction.