import fs from 'fs';
import path from 'path';
import { logger } from '@storybook/node-logger';
import { Options } from 'ts-loader';

function resolveTsConfig(tsConfigPath: string): string | undefined {
  if (fs.existsSync(tsConfigPath)) {
    logger.info('=> Found custom tsconfig.json');
    return tsConfigPath;
  }
  return undefined;
}

export default function (configDir: string) {
  const tsLoaderOptions: Partial<Options> = {
    transpileOnly: true,
    compilerOptions: {
      emitDecoratorMetadata: true,
    },
  };

  const configFilePath = resolveTsConfig(path.resolve(configDir, 'tsconfig.json'));
  if (configFilePath) tsLoaderOptions.configFile = configFilePath;

  return tsLoaderOptions;
}

import fs from 'fs';
import path from 'path';
import { logger } from '@storybook/node-logger';

function resolveTsConfig(tsConfigPath: string): string | undefined {
  if (fs.existsSync(tsConfigPath)) {
    logger.info('=> Found custom tsconfig.json');
    return tsConfigPath;
  }
  return undefined;
}

export default function (configDir: string) {
  const configFilePath = resolveTsConfig(path.resolve(configDir, 'tsconfig.json'));
  return {
    transpileOnly: true,
    compilerOptions: {
      emitDecoratorMetadata: true,
    },
    configFile: configFilePath || undefined,
  };
}

--- 
+++ 
@@ -1,6 +1,7 @@
 import fs from 'fs';
 import path from 'path';
 import { logger } from '@storybook/node-logger';
+import { Options } from 'ts-loader';
 
 function resolveTsConfig(tsConfigPath: string): string | undefined {
   if (fs.existsSync(tsConfigPath)) {
@@ -11,12 +12,15 @@
 }
 
 export default function (configDir: string) {
-  const configFilePath = resolveTsConfig(path.resolve(configDir, 'tsconfig.json'));
-  return {
+  const tsLoaderOptions: Partial<Options> = {
     transpileOnly: true,
     compilerOptions: {
       emitDecoratorMetadata: true,
     },
-    configFile: configFilePath || undefined,
   };
+
+  const configFilePath = resolveTsConfig(path.resolve(configDir, 'tsconfig.json'));
+  if (configFilePath) tsLoaderOptions.configFile = configFilePath;
+
+  return tsLoaderOptions;
 }
Fix `configFile: undefined` in ts-loader options when not using a custom .storybook/tsconfig.json

Fixes https://github.com/storybookjs/storybook/issues/13381
