storybook: [Bug]: experimental_indexers ignores importPath

Describe the bug

In the experimental indexer docs it says that an indexer can specify an importPath to a target CSF file, but doing so has no effect. Looking at the source code it looks like the importPath returned from an indexer is not used and instead the input source to the indexer will always be used as the importPath.

To Reproduce

https://github.com/storybookjs/storybook/blob/e942e71e1f5281f9197faab6d61625b0ac8cecfd/code/lib/core-server/src/utils/StoryIndexGenerator.ts#L302

System

Storybook Environment Info:

  System:
    OS: macOS 14.2.1
    CPU: (10) arm64 Apple M1 Pro
    Shell: 5.1.16 - /opt/homebrew/bin/bash
  Binaries:
    Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
    Yarn: 4.0.2 - ~/.nvm/versions/node/v20.10.0/bin/yarn <----- active
    npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm
  Browsers:
    Chrome: 120.0.6099.109
    Edge: 120.0.2210.121
    Safari: 17.2.1

Additional context

No response

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

I just ran into the same problem when I tried to implement virtual modules with a vite plugin, namely importPath being ignored. If this is not going to be addressed, it’d be great to mention this detail in the docs.

Thanks for the quick reply, @JReinhold! I checked the code again and indeed that’s the right place.

I’ve read the contribution documentation , but couldn’t figure out which branch to checkout (I imagine this is a small fix that will be released as a patch)

Should I checkout the latest-release branch?

Never mind, just checked the version on the code 🤦🏻

I’ll run the tests and send a PR in a few moments

This is wonderful news @fredriknils and exactly what we were hoping to enable with the indexer API. At some point I’d love to find a general solution to build into core. But in the meantime it’s great that you can hack it yourself. Kudos @JReinhold for making it happen!!

I had a look at fixing the “bug” and was testing it out on my local storybook. But after changing the behavior I realize that what I was hoping to achieve with the importPath will not work.

My first thinking with changing the import path was that I would avoid having to create my own builder and instead piggyback on the existing one. But after fixing the importPath I realize that the builder api specifies that the builders should read the stories array in the config not the store index entries. So, if I change the importPath the builder will not know what file I’m referring to. This can be solved by also including my generated file in the stories array but then I need to also create a secondary indexer, who’s only purpose would be to stop the generated file from being indexed, so that the builders has both the generated file and the source file, but the storybook index only has one version indexed. The whole solution feels like a hack and more complicated than just creating a builder, so I’m abandoning that approach in favor of you second suggestion of adding a builder like you did in you POC.

If anyone comes across this thread and want to have a look at how I solved generating regression variants, inspired by the original POC, I put my POC here Dynamic story variants POC. In the POC you specify what variants you want in the storybook setup phase. Then it will autogenerate the variants for each regression story automatically.

@shilman Thank you so much for taking the time to point me in the right direction! 👍