const { ProvidePlugin } = require("webpack");

const disableEsLint = (e) => {
  return (
    e.module.rules
      .filter(
        (e) =>
          e.use &&
          e.use.some((e) => e.options && void 0 !== e.options.useEslintrc),
      )
      .forEach((s) => {
        e.module.rules = e.module.rules.filter((e) => e !== s);
      }),
    e
  );
};

module.exports = {
  stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
  addons: [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
    "@storybook/preset-create-react-app",
    "@react-theming/storybook-addon",
  ],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          {
            loader: "style-loader",
          },
          {
            loader: "css-loader",
            options: {
              sourceMap: true,
            },
          },
        ],
      },
    ],
  },
  webpackFinal: async (config) => {
    config.resolve.fallback = {
      buffer: require.resolve("buffer/"),
      crypto: require.resolve("crypto-browserify"),
      http: false,
      https: false,
      os: require.resolve("os-browserify/browser"),
      process: require.resolve("process/browser"),
      // "react/jsx-runtime": "react/jsx-runtime.js",
      stream: require.resolve("stream-browserify/"),
      url: false,
      util: false,
    };

    config.plugins = [
      ...(config.plugins || []),
      new ProvidePlugin({ Buffer: ["buffer", "Buffer"], process: ["process"] }),
    ];

    return disableEsLint(config);
  },
  core: {
    builder: "webpack5",
  },
};
