Bootstrap 5 and Vite: Does a Side-Effect Import Initialize Data-Attribute Plugins?
0 reputation · 27 Jan 2024, 11:08 UTC
0 reputation · 27 Jan 2024, 11:08 UTC
A project uses Bootstrap 5.x for CSS and JavaScript with a Vite-based ES-module build. The goal is to establish whether one documented import boundary can support dropdowns, tooltips, and popovers consistently in development and production, while leaving the choice between CDN and self-hosted assets open.
The unresolved behavior is whether loading bootstrap/js as a side-effect import retains Bootstrap’s data-attribute auto-initialization, or whether the module build requires explicit component instantiation. CSS-only imports, tree-shaking, and the availability of Popper may affect that boundary. Bootstrap 5’s removal of jQuery does not by itself resolve the initialization question.
The integration contract should be version-specific and checked against the selected Bootstrap 5.x and Vite releases, including a production build. CDN delivery introduces constraints involving module format, third-party availability, and Subresource Integrity; self-hosting transfers bundling and cache-header responsibilities to the project.
bootstrap/js initialize existing data-bs-* components without explicit instantiation in the production bundle?37775 reputation · 27 Jan 2024, 15:52 UTC
Yes. A side-effect import of bootstrap/js (e.g., import 'bootstrap/js/dist/bootstrap.bundle.min.js') registers event listeners that auto-initialize any element with a data-bs-* attribute. Explicit component instantiation is not required, provided the bundle executes after the DOM is ready and Vite does not tree-shake the import away.
<body> or loaded with defer).Bootstrap’s bundle attaches a DOMContentLoaded listener that scans the document for data-bs-* attributes and initializes the corresponding components. If the import is executed before that event fires, auto-initialization occurs. If the import is deferred until after DOMContentLoaded (e.g., via dynamic import or a separate chunk), the listener may miss the initial scan and components will not initialize without manual instantiation.
Pin the following versions to guarantee consistent behavior across CDN and self-hosted delivery:
Using a CDN requires the same Bootstrap version and a matching Subresource Integrity hash. Self-hosting bundles the same file through Vite, transferring cache-header responsibility to your build.
vite build --mode production
# then search the output for "bootstrap.bundle.min.js" or "data-bs-toggle"<button data-bs-toggle="tooltip" title="Test">Hover</button>Load the page and verify the tooltip appears without calling new bootstrap.Tooltip(...).Bootstrap is not defined or Popper errors, which indicate version mismatches or missing bundles.package.json or via /* @__PURE__ */ comments.import('bootstrap/js/dist/bootstrap.bundle.min.js')) or code-splitting that loads the bundle after DOMContentLoaded will break auto-initialization.Confirm that the side-effect import is not being dynamically imported or split into a separate chunk that loads after DOMContentLoaded. If it is, you must either move the import to the entry point or manually initialize components after the dynamic load completes.
Use comments to ask for clarification. Post a solution as an answer.
6,990 reputation · 27 Jan 2024, 21:30 UTC
It is important to distinguish between importing the full bundle and importing individual components. A side-effect import of the complete bootstrap.bundle.min.js executes the internal Data.getOrCreateInstance logic for all data-bs-* attributes. However, if you import only specific modules (e.g., import Dropdown from 'bootstrap/js/dist/dropdown'), the global auto-initialization routine is not triggered.
To ensure consistency across Vite 5.0+ and Bootstrap 5.3.x, verify that the production build does not tree-shake the initialization code. You can validate this by adding a data-bs-toggle="tooltip" element to a page; if it fails to initialize without a manual new bootstrap.Tooltip() call, the side-effect import was likely stripped or replaced by a modular import.