import replace from "@rollup/plugin-replace";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import babel from "rollup-plugin-babel";
import livereload from "rollup-plugin-livereload";
import { terser } from "rollup-plugin-terser";
import serve from "rollup-plugin-serve";
import scss from "rollup-plugin-scss";
import postcss from "rollup-plugin-postcss";
import { uglify } from "rollup-plugin-uglify";
import react from "react";
import reactDom from "react-dom";
import image from "rollup-plugin-img";
import url from "rollup-plugin-url";

const isProd = process.env.NODE_ENV === "production";
const extensions = [".js", ".ts", ".tsx"];

export default {
  input: "src/index.ts",
  output: {
    file: "public/index.js",
    format: "iife",
  },
  jest: {
    transform: {
      "^.+\\.jsx?$": "babel-jest",
    },
  },
  onwarn: function (warning) {
    // Skip certain warnings

    // should intercept ... but doesn't in some rollup versions
    if (warning.code === "THIS_IS_UNDEFINED") {
      return;
    }
  },
  plugins: [
    serve("public"),
    livereload("src"),
    image({
      output: `public/`, // default the root
      extensions: /\.(png|jpg|jpeg|gif|svg)$/, // support png|jpg|jpeg|gif|svg, and it's alse the default value
      limit: 8192, // default 8192(8k)
      exclude: "node_modules/**",
    }),
    resolve({
      extensions,
    }),
    commonjs({
      include: "node_modules/**",
      namedExports: {
        "node_modules/react/index.js": [
          "Component",
          "PureComponent",
          "Fragment",
          "Children",
          "createElement",
          "prettier",
        ],
        react: Object.keys(react),
        "react-dom": Object.keys(reactDom),
      },
    }),
    url({
      // by default, rollup-plugin-url will not handle font files
      include: ["**/*.woff", "**/*.woff2", "**/*.eot", "**/*.ttf"],
      // setting infinite limit will ensure that the files
      // are always bundled with the code, not copied to /dist
      limit: Infinity,
    }),
    replace({
      "process.env.NODE_ENV": JSON.stringify(
        isProd ? "production" : "development"
      ),
    }),
    scss({
      output: true,
      output: "public/index.css",
    }),
    postcss({
      modules: true,
    }),
    uglify(),
    babel({
      extensions,
      exclude: /node_modules/,
      babelrc: false,
      runtimeHelpers: true,
      presets: [
        [
          "@babel/preset-env",
          {
            targets: {
              node: "current",
            },
          },
        ],
        "@babel/preset-react",
        "@babel/preset-typescript",
        "next/babel",
      ],
      env: {
        test: {
          presets: [["@babel/preset-env"]],
        },
      },
      plugins: [
        "react-require",
        "@babel/plugin-syntax-dynamic-import",
        "@babel/plugin-proposal-class-properties",
        [
          "@babel/plugin-proposal-object-rest-spread",
          {
            useBuiltIns: true,
          },
        ],
        [
          "@babel/plugin-transform-runtime",
          {
            corejs: 2,
            helpers: true,
            regenerator: true,
            useESModules: false,
          },
        ],
      ],
    }),
    isProd && terser(),
  ],
};
