/* eslint-disable @bigbinary/neeto/no-dangling-constants */
const WRONG_CODE = /^if\s\(module\.hot\)[\s\S]+/gm;

const reactGiphyPlugin = () => ({
  name: "esbuild-plugin-react-giphy-searchbox",
  setup(build) {
    build.onLoad(
      {
        filter: /node_modules\/react-giphy-searchbox\/es\/style\/useStyle\.js/,
      },
      async args => {
        const fs = require("fs").promises;
        const contents = await fs.readFile(args.path, "utf8");

        return { contents: contents.replace(WRONG_CODE, ""), loader: "js" };
      }
    );
  },
});

module.exports = { reactGiphyPlugin };
