Skip to content
Open
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
21 changes: 21 additions & 0 deletions src/plugins/next-image/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,28 @@ export function vitePluginNextImage(
nextConfigResolver: PromiseWithResolvers<NextConfigComplete>,
) {
let isBrowser = !isVitestEnv;
let excludeSvg = false;

return {
name: "vite-plugin-storybook-nextjs-image",
enforce: "pre" as const,
async config(config, env) {
if (config.test?.browser?.enabled === true) {
isBrowser = true;
}

// Auto-detect SVGR plugin
const hasVitePluginSvgr = config.plugins?.some(
(plugin) =>
plugin &&
typeof plugin === "object" &&
"name" in plugin &&
(plugin.name === "vite-plugin-svgr" || plugin.name.includes("svgr")),
);
if (hasVitePluginSvgr) {
excludeSvg = true;
}

return {
resolve: {
alias: getAlias(isBrowser ? "browser" : "node"),
Expand All @@ -42,6 +57,12 @@ export function vitePluginNextImage(
return null;
}

// For SVG files, only process if they don't have ?react parameter and SVG processing is enabled
const isSvg = /\.svg$/.test(source);
if (isSvg && (excludeSvg || queryA === "react")) {
return null;
}

if (
includePattern.test(source) &&
!excludeImporterPattern.test(importer ?? "") &&
Expand Down