Copy required polyfills from node_modules to the build directory
function copyPolyfills(pluginConfig, outputConfig) {
  if (
    !pluginConfig.polyfillDynamicImports &&
    !pluginConfig.polyfillWebcomponents &&
    !pluginConfig.polyfillBabel
  ) {
    return;
  }
  const polyfillsDir = `${outputConfig.dir.replace('/legacy', '')}/polyfills`;
  mkdirp.sync(polyfillsDir);

  if (pluginConfig.polyfillDynamicImports) {
    copyFileSync(
      path.resolve(pathJoin(__dirname, '../../src/dynamic-import-polyfill.js')),
      `${polyfillsDir}/dynamic-import-polyfill.js`,
    );
  }

  if (pluginConfig.polyfillWebcomponents) {
    copyFileSync(
      require.resolve('@webcomponents/webcomponentsjs/webcomponents-bundle.js'),
      `${polyfillsDir}/webcomponent-polyfills.js`,
    );
  }

  if (pluginConfig.polyfillBabel) {
    copyFileSync(
      require.resolve('@babel/polyfill/browser.js'),
      `${polyfillsDir}/babel-polyfills.js`,
    );
  }
}