Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Improve support for main.ts/preview.ts files #10099

Merged
merged 2 commits into from
Mar 12, 2020

Conversation

yannbf
Copy link
Member

@yannbf yannbf commented Mar 10, 2020

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 to main, 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:
image

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 or git checkout HEAD^1
4 - run yarn to uninstall @babel/register
5 - run yarn storybookand see the issue:

info @storybook/react v5.3.14
info 
info => Loading static files from: /Users/Dev/storybook-cra-ts/public .
WARN => File /Users/Dev/storybook-cra-ts/.storybook/main.ts is detected
WARN    but impossible to import loader for .ts
info => Loading presets
WARN => File /Users/Dev/storybook-cra-ts/.storybook/main.ts is detected
WARN    but impossible to import loader for .ts
info => Loading presets

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.

- it's useful to add support to load files within storybook config.
- with this you have support for .ts files!
@shilman
Copy link
Member

shilman commented Mar 11, 2020

cc @igor-dv

@shilman shilman requested review from ndelangen and Hypnosphi March 11, 2020 02:13
@shilman shilman added this to the 6.0.0 milestone Mar 11, 2020
Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I didn't test, but crossing my fingers 🤞 Great job 👏

@shilman shilman changed the title Add support for main.ts/preview.ts files Core: Improve support for main.ts/preview.ts files Mar 12, 2020
@shilman shilman merged commit 396f196 into storybookjs:next Mar 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants