Ctrl K
ComunidadGuías…

rsbuild.config.ts

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
import { resolve } from "node:path";
import { defineConfig, type EnvironmentConfig, type RsbuildConfig } from "@rsbuild/core";
import { pluginReact } from "@rsbuild/plugin-react";
import { pluginSass } from "@rsbuild/plugin-sass";
import { config } from "dotenv";
import { ProductionDependenciesInstallerPlugin } from "./scripts/build/production-dependencies-installer";

const loadEnvironmentFile = () => {
  const result = config({
    path: resolve(__dirname, ".env"),
    override: false,
    quiet: false,
  });

  if (result.error) {
    throw result.error;
  }

  return result.parsed || {};
};

export default defineConfig(async ({ command }): Promise<RsbuildConfig> => {
  const isCommandDev = command === "dev";
  const isCommandBuild = command === "build";

  const port = 33077;
  const env = loadEnvironmentFile();
  const define = Object.entries(env).reduce(
    (define, [key, val]) => {
      define[`process.env.${key}`] = JSON.stringify(val);
      return define;
    },
    {} as Record<string, string>,
  );

  define["process.env.RENDERER_DEV_SERVER"] = isCommandDev ? `"http://localhost:${port}/renderer"` : "undefined";
  define["process.env.SOURCE_ROOT"] = isCommandDev ? `"${__dirname}"` : "undefined";

  const createMainEnvironment = () => {
    const copy: Array<Record<"from" | "to", string>> = [
      {
        from: "build",
        to: "build",
      },
      {
        from: "drizzle/migrations",
        to: "migrations",
      },
      {
        from: `node_modules/onnxruntime-node/bin/napi-v3/${process.platform}/${process.arch}`,
        to: `bin/onnxruntime/${process.platform}/${process.arch}`,
      },
    ];

    const externals: string[] = ["better-sqlite3", "@electric-sql/pglite", "sharp"];

    if (process.platform === "win32") {
      externals.push(`@lancedb/lancedb-win32-${process.arch}-msvc`);
      externals.push(`@napi-rs/canvas-win32-${process.arch}-msvc`);
    } else if (process.platform === "linux") {
      externals.push(`@lancedb/lancedb-linux-${process.arch}-gnu`);
      externals.push(`@napi-rs/canvas-linux-${process.arch}-gnu`);
    } else if (process.platform === "darwin") {
      externals.push(`@lancedb/lancedb-darwin-${process.arch}`);
      externals.push(`@napi-rs/canvas-darwin-${process.arch}`);
    }

    const config: EnvironmentConfig = {
      source: {
        entry: {
          main: "./src/main/main.ts",
        },
        define,
      },
      tools: {
        rspack: {
          target: "electron-main",
          output: {
            library: {
              type: "commonjs",
            },
            publicPath: "./",
          },
          plugins: [
            new ProductionDependenciesInstallerPlugin({
              externals,
              dev: isCommandDev,
              inspect: {
                port: port + 1,
              },
            }),
          ],
          externals,
        },
        htmlPlugin: false,
      },
      output: {
        target: "node",
        filename: {
          js: "[name].cjs",
        },
        copy,
      },
    };

    return config;
  };

  const createRendererEnvironment = () => {
    const config: EnvironmentConfig = {
      source: {
        entry: {
          index: "./src/renderer/index.tsx",
        },
        define,
      },
      output: {
        target: "web",
        distPath: {
          root: "output/renderer",
        },
      },
      tools: {
        rspack: {
          output: {
            publicPath: isCommandBuild ? "./" : undefined,
          },
        },
        htmlPlugin: {
          title: "5ire",
        },
      },
      dev: {
        writeToDisk: true,
      },
      plugins: [pluginReact(), pluginSass()],
    };

    return config;
  };

  const createPreloadEnvironment = () => {
    const config: EnvironmentConfig = {
      source: {
        entry: {
          preload: "./src/main/preload.ts",
        },
        define,
      },
      tools: {
        rspack: {
          target: "electron-preload",
          output: {
            library: {
              type: "module",
            },
          },
        },
        htmlPlugin: false,
      },
      output: {
        target: "node",
        filename: {
          js: "[name].js",
        },
      },
      dev: {
        writeToDisk: true,
      },
    };

    return config;
  };

  return {
    environments: {
      main: createMainEnvironment(),
      renderer: createRendererEnvironment(),
      preload: createPreloadEnvironment(),
    },
    server: {
      port: port,
    },
    resolve: {
      alias: {
        "@": resolve(__dirname, "src"),
        utils: resolve(__dirname, "src/utils"),
      },
    },
    dev: {
      writeToDisk: true,
    },
    output: {
      distPath: {
        root: "output",
      },
      cleanDistPath: true,
      // sourceMap: true,
      minify: false,
    },
    tools: {
      rspack: {
        ignoreWarnings: [
          (error) => {
            if (error.message?.includes("/node_modules/") || error.message?.includes("\\node_modules\\")) {
              return true;
            }

            if (error.message?.includes("Critical dependency")) {
              return true;
            }

            return false;
          },
        ],
      },
    },
  };
});