ES6: Keep function from tree shaking

2020-03-26 javascript ecmascript-6 babeljs rollupjs tree-shaking

I'm using rollup and babel to transpile ES6 code. Now i need this Recaptcha Callback function to be triggered when the Captcha has been checked.

Problem is that tree shaking or another mechanism filters this function out during transpilation process.

How can i keep recaptchaCallback() in the final script to be triggered from outside?

import UIkit from 'uikit';

...

function recaptchaCallback() {
    document.getElementsByClassName('g-recaptcha')[0].style.border = 'none';
};

...

Answers

use sideEffects

A "side effect" is defined as code that performs a special behavior when imported, other than exposing one or more exports. An example of this are polyfills, which affect the global scope and usually do not provide an export.

{
  "name": "your-project",
  "sideEffects": [
    "./src/some-side-effectful-file.js"
  ]
}

Related