Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,12 @@ export function viteMockPlugin(options: MockPluginOptions): Plugin[] {
name: 'storybook:mock-loader-preview',
transform(code, id) {
if (id === normalizedPreviewConfigPath) {
return rewriteSbMockImportCalls(code);
try {
return rewriteSbMockImportCalls(code);
} catch (e) {
logger.debug(`Could not transform sb.mock(import(...)) calls in ${id}: ${e}`);
return null;
}
}
return null;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import type { LoaderContext } from 'webpack';
import { logger } from 'storybook/internal/node-logger';

import type { LoaderDefinition } from 'webpack';

import { rewriteSbMockImportCalls } from '../../../mocking-utils/extract';

/**
* A Webpack loader that normalize sb.mock(import(...)) calls to sb.mock(...)
*
* @param {string} source The original source code of the preview config file.
* @this {LoaderContext<{}>} The Webpack loader context.
* @param source The original source code of the preview config file.
* @this The Webpack loader context.
*/
export default function storybookMockTransformLoader(this: LoaderContext<{}>, source: string) {
const result = rewriteSbMockImportCalls(source);
const storybookMockTransformLoader: LoaderDefinition = function mockTransformLoaderFn(
source,
sourceMap,
meta
) {
const callback = this.async();
callback(null, result.code, result.map || undefined);
}

try {
const result = rewriteSbMockImportCalls(source);
callback(null, result.code, result.map || undefined, meta);
} catch (error) {
const filePath = this.resourcePath;
logger.debug(`Could not transform sb.mock(import(...)) calls in ${filePath}: ${error}`);
callback(null, source, sourceMap, meta);
}
};

export default storybookMockTransformLoader;
Loading