Skip to content

Commit 259067f

Browse files
Copilothotlong
andcommitted
fix: apply onwarn directly on merged config for reliable warning suppression
Instead of passing onwarn through mergeConfig (which may not properly merge function callbacks), apply it directly on the merged config object. This ensures the handler is always active regardless of how Storybook's internal Vite config is structured. Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 0fad35b commit 259067f

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

.storybook/main.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const config: StorybookConfig = {
1919
autodocs: "tag",
2020
},
2121
async viteFinal(config) {
22-
return mergeConfig(config, {
22+
const merged = mergeConfig(config, {
2323
define: {
2424
'process.env': {},
2525
'process.platform': '"browser"',
@@ -82,21 +82,30 @@ const config: StorybookConfig = {
8282
},
8383
build: {
8484
target: 'esnext',
85-
rollupOptions: {
86-
onwarn(warning, warn) {
87-
// Suppress "use client" directive warnings (from Radix UI, react-router, etc.)
88-
// and sourcemap resolution errors from dependencies with incomplete sourcemaps
89-
if (
90-
warning.code === 'MODULE_LEVEL_DIRECTIVE' ||
91-
warning.message?.includes("Can't resolve original location of error")
92-
) {
93-
return;
94-
}
95-
warn(warning);
96-
},
97-
},
9885
},
9986
});
87+
88+
// Apply onwarn directly to avoid mergeConfig potentially dropping function callbacks
89+
merged.build ??= {};
90+
merged.build.rollupOptions ??= {};
91+
const existingOnwarn = merged.build.rollupOptions.onwarn;
92+
merged.build.rollupOptions.onwarn = (warning, warn) => {
93+
// Suppress "use client" directive warnings (from Radix UI, react-router, etc.)
94+
// and sourcemap resolution errors from dependencies with incomplete sourcemaps
95+
if (
96+
warning.code === 'MODULE_LEVEL_DIRECTIVE' ||
97+
warning.message?.includes("Can't resolve original location of error")
98+
) {
99+
return;
100+
}
101+
if (existingOnwarn) {
102+
existingOnwarn(warning, warn);
103+
} else {
104+
warn(warning);
105+
}
106+
};
107+
108+
return merged;
100109
},
101110
};
102111
export default config;

0 commit comments

Comments
 (0)