Core: Improve support for main.ts/preview.ts files #10099
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue:
It started when I was migrating storybook in my project from 5.2 to 5.3. I had
config.ts
working fine, so when I changed it tomain
, I figured it could as well be.ts
, however storybook then stopped working. After quite some debugging, I noticed that at this line,registerCompiler(moduleDescriptor)
, based on the file extension, tries to check if any of the following libs are present under node_modules, traversing from storybook/core folder and going up, until it reaches the user's project folder:If none of those are present, then it won't be able to load the file extension.
What I did
After talking with @shilman about it, we thought it could be an idea to have that dependency in storybook/core, so the issue is fixed automatically for everyone, rather than having a developer install one of those dependencies in case they would like ts support for those files. So this PR adds that dependency, and with that, any of the storybook configuration files can be used with typescript. The dependency weighs 241 bytes.
How to test
To test this in the storybook monorepo is a bit complicated, given that many libs have either @babel/register or ts-node as dependencies or have dependencies which have those as peer dependencies, which makes those dependencies available at the top level and it's enough to make the .ts files load without issues. I did test there though, but I had to remove all those dependencies.
An easier way:
1 - Checkout this sample repo
2 - run
yarn && yarn storybook
and see the storybook working fine.3 - checkout the broken main.ts version:
git checkout 1e98a7d
orgit checkout HEAD^1
4 - run
yarn
to uninstall@babel/register
5 - run
yarn storybook
and see the issue:Alternatively, you can go to the broken version, and rather than installing @babel/register at the project level, install it under
node_modules/@storybook/core
. It will work fine as well.