Skip to content

Tree‑shaking removes DOM‑initializing side‑effects, breaking element.bootstrapToggle() when imported via bundlers #334

Closed

Description

@palcarazm

Error Description

When bootstrap5-toggle is used via an ES module/bundler import instead of a CDN script tag, the DOM‑initializing side‑effect that registers bootstrapToggle on HTMLInputElement.prototype is being removed by tree‑shaking. As a result, code that previously worked, such as calling element.bootstrapToggle() (or this.$refs.toggle.bootstrapToggle() in Vue), now throws:

TypeError: element.bootstrapToggle is not a function

The workaround of directly instantiating new BootstrapToggle(element) still works, which indicates that the core class is present but the prototype patch is not being executed in bundled builds.

Steps to Reproduce

  1. Install bootstrap5-toggle via npm.

  2. Import the full bundle inside an ESM/bundler environment (Vite, Webpack, Rollup, Vue CLI, etc.):

    import 'bootstrap5-toggle/js/bootstrap5-toggle.ecmas.js';
  3. Render a component containing an input toggle, for example in Vue:

    <input ref="toggle" data-toggle="toggle">
  4. After mount, attempt to call:

    this.$refs.toggle.bootstrapToggle();
  5. Observe that the method is missing and the following error is thrown:

    TypeError: this.$refs.toggle.bootstrapToggle is not a function
    

Expected Behavior

When importing the full bundle, the library should always execute its DOM‑initializing side‑effects. As a result, the bootstrapToggle method should be registered on HTMLInputElement.prototype, allowing code such as:

element.bootstrapToggle();

to work reliably in any environment (CDN, ESM, bundlers) without requiring manual instantiation or additional configuration.

Actual Behavior

Instead of registering the bootstrapToggle method on HTMLInputElement.prototype, the imported bundle is tree‑shaken and the DOM‑initializing side‑effect is removed. As a result, the element received by the framework (e.g., Vue) is a plain HTMLElement without the expected method. Calling:

this.$refs.toggle.bootstrapToggle();

throws:

TypeError: this.$refs.toggle.bootstrapToggle is not a function

The class BootstrapToggle is still available, and manual instantiation works, which confirms that only the prototype‑patching side‑effect is being eliminated by the bundler.

Severity

Severe

Bootstrap 5 Toggle Version

5.4.0

Bootstrap version

^5.0.0

Other

The issue only appears in ESM/bundler environments (Vite, Webpack, Rollup, Vue CLI).
It does not occur when loading the library via CDN because the IIFE is always executed.

Bug Report Checklist

  • Confirm that you agree to follow the project's code of conduct.
  • Confirm that you have reviewed open and rejected anomalies and have not found the reported anomaly.

Activity

  1. github-actions commented on Jun 28, 2026

    @github-actions

    Hi! 👋
    Thanks for your issue. You are helping to improve Bootstrap 5 toggle.

  2. added this to the v5.4.1 milestone on Jun 28, 2026
  3. linked a pull request that will close this issuev5.4.1 #335on Jun 28, 2026
  4. moved this from Needs triage to Closed in Issue triageon Jun 28, 2026
  5. palcarazm commented on Jun 28, 2026

    @palcarazm
    OwnerAuthor

    Hi @danports

    I've released v5.4.1 with sideEffects configuration. Thanks for your help!

  6. self-assigned this
    on Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions