import * as path from 'path' ; import { normalizePath } from 'vite' ; import { listStories } from './list-stories' ; import type { Options } from '@storybook/core-common' ; /**
 * This file is largely based on https://github.com/storybookjs/storybook/blob/d1195cbd0c61687f1720fefdb772e2f490a46584/lib/core-common/src/utils/to-importFn.ts
 */ /**
 * Paths get passed either with no leading './' - e.g. `src/Foo.stories.js`,
 * or with a leading `../` (etc), e.g. `../src/Foo.stories.js`.
 * We want to deal in importPaths relative to the working dir, so we normalize
 */ function toImportPath ( relativePath ) { return relativePath . startsWith ( '../' ) ? relativePath : `./${ relativePath }` ; } param stories async function toImportFn ( stories ) { const objectEntries = stories . map ( ( file ) => { return `  '${ toImportPath ( normalizePath ( path . relative ( process . cwd ( ) , file ) ) ) }': async () => import('/@fs/${ file }')` ; } ) ; return `
    const importers = {
      ${ objectEntries . join ( ',\n' ) }
    };

    export async function importFn(path) {
        return importers[path]();
    }
  ` ; } export async function generateImportFnScriptCode ( options ) { const stories = await listStories ( options ) ; return ( await toImportFn ( stories ) ) . trim ( ) ; }