Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion frontend/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,29 @@ import { defineConfig, loadEnv } from "vite";
import svgr from "vite-plugin-svgr";

const EMPTY_MODULE_ID = "\0optional-plugin-empty";
const EMPTY_ASSET_MODULE_ID = "\0optional-plugin-empty-asset";

const ASSET_EXTENSIONS = new Set([
".svg",
".png",
".jpg",
".jpeg",
".gif",
".webp",
".ico",
".bmp",
".tiff",
]);

// Rollup plugin that resolves missing optional plugin imports to an empty
// module instead of failing the build. This lets the existing
// `try { await import("./plugins/...") } catch {}` pattern work at build
// time: Rollup will bundle an empty module for any plugin path that does
// not exist on disk, and the catch block handles the rest at runtime.
//
// Asset imports (images, SVGs, etc.) are resolved to a module that exports
// an empty string as default, so static `import logo from "..."` statements
// don't break the build.
function optionalPluginImports() {
return {
name: "optional-plugin-imports",
Expand All @@ -20,7 +37,10 @@ function optionalPluginImports() {
// Only handle relative imports
if (!source.startsWith(".")) return null;

const resolved = path.resolve(path.dirname(importer), source);
// Strip query strings and hashes (e.g. "./logo.svg?react" → "./logo.svg")
// so path.extname and fs.existsSync work correctly.
const sourcePath = source.split("?")[0].split("#")[0];
const resolved = path.resolve(path.dirname(importer), sourcePath);

// Only handle imports that resolve within a plugins directory.
// This covers both cross-plugin imports (e.g. "../plugins/foo")
Expand All @@ -37,6 +57,11 @@ function optionalPluginImports() {
);

if (!exists) {
// Asset files need a default export so static imports work.
const ext = path.extname(sourcePath).toLowerCase();
if (ASSET_EXTENSIONS.has(ext)) {
return EMPTY_ASSET_MODULE_ID;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
return EMPTY_MODULE_ID;
}

Expand All @@ -46,6 +71,9 @@ function optionalPluginImports() {
if (id === EMPTY_MODULE_ID) {
return "throw new Error('Optional plugin not available');";
}
if (id === EMPTY_ASSET_MODULE_ID) {
return "export default '';";
}
return null;
},
};
Expand Down
Loading