import resolve from '@rollup/plugin-node-resolve';
import babel from '@rollup/plugin-babel';
import copy from 'rollup-plugin-copy';
import { terser } from 'rollup-plugin-terser';
import cleanup from 'rollup-plugin-cleanup';
import commonjs from '@rollup/plugin-commonjs';
import minifyHTML from 'rollup-plugin-minify-html-literals';

export default [
  /* real browsers */ {
    input: 'src/zui-bundle.js',
    output: {
      dir: 'dist',
      chunkFileNames: '[name].js',
      format: 'esm',
    },
    plugins: [
      resolve({
        browser: true,
      }),
      minifyHTML(),
      babel({
        babelHelpers: 'bundled',
        presets: [
          [
            '@babel/preset-env',
            {
              bugfixes: true,
            },
          ],
        ],
      }),
      copy({
        targets: [
          { src: 'src/zui-loader.js', dest: 'dist' },
          { src: '../../../node_modules/@webcomponents/webcomponentsjs/bundles', dest: 'dist/wc/' },
          {
            src: '../../../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js',
            dest: 'dist/wc',
          },
        ],
      }),
      terser(),
      cleanup({
        comments: 'none',
      }),
    ],
  },
  /* IE 11 */ {
    input: 'src/zui-bundle.es5.js',
    inlineDynamicImports: true,
    output: {
      file: 'dist/zui-bundle.es5.js',
      format: 'iife',
    },
    plugins: [
      resolve({
        browser: true,
      }),
      commonjs(),
      minifyHTML(),
      babel({
        babelHelpers: 'bundled',
        exclude: [/node_modules\/(core-js)/],
        presets: [
          [
            '@babel/preset-env',
            {
              targets: 'IE 11',
              corejs: 3,
              useBuiltIns: 'usage',
              modules: false,
            },
          ],
        ],
      }),
      terser(),
      cleanup({
        comments: 'none',
      }),
    ],
  },
];
