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

CLI: Improve init for svelte #14161

Merged
merged 2 commits into from
Mar 16, 2021
Merged

CLI: Improve init for svelte #14161

merged 2 commits into from
Mar 16, 2021

Conversation

j3rem1e
Copy link
Contributor

@j3rem1e j3rem1e commented Mar 7, 2021

Issue:

What I did

as discuted on Discord and here, I have updated the CLI init for svelte:

  • Added an addon dependencies on @storybook/addon-svelte-csf
  • Added *.stories.svelte
  • If the project uses a svelte preprocessor (through a svelte.config.js or with svelte-preprocess), then the preprocessor is added to the storybook configuration

today, this is not possible to extend the generation of .storybook/main.js: this configuration is added by patching directly the main.js file after the standard generation.

How to test

  • Is this testable with Jest or Chromatic screenshots? no
  • Does this need a new example in the kitchen sink apps? no
  • Does this need an update to the documentation? no

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 this looks so good 🔥 .... @tooppaaa can you review also?

let conf = fse.readFileSync('./.storybook/main.js').toString();

// add *.stories.svelte
conf = conf.replace(/js\|jsx/g, 'js|jsx|svelte');
Copy link
Contributor

Choose a reason for hiding this comment

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

This means we can break Svelte CLI while touching lib\cli\src\generators\configure.ts Do you think it's best to pass an option "extension" that defaults to js, jsx, ts, txs ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My first implementation was checking if "framework is svelte" (there is already a special case for angular in this file), my 2nd implementation was doing that (a special properties for extensions), and my third is this PR :)

conf = `${requirePreprocessor}\n\n${conf}`;
}

conf = conf.replace(/\],/, `],\n${svelteOptions}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is also not very robust. Same as the in the configure method of lib\cli\src\generators\baseGenerator.ts you have the capacity to add "extraMain" args to your main. With a small refactor, you won't need to know about ./.storybook/main.js in this file

Copy link
Member

Choose a reason for hiding this comment

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

That's why you're the MVP @tooppaaa ❤️

Thanks so much for the thoughtful review!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently, the main.js is generating from static properties (with JSON.stringify).

However, in my case, I want to inject :

  • An import statement / require
  • A Function (which can't be generated by JSON.stringify), so I have to format a string and format it correctly
const sveltePreprocess = require('svelte-preprocess');

module.exports = {
   ...
   svelteOptions: {
     preprocessor: sveltePreprocess()
  },
  ..
}

That's why, for me at least, it wasn't a "small refactor" but I have to rewrite most of the logic inside configure.js 😅
I didn't found jest test around this function, so this refactoring could be risky

I can work on that, but how do you see the implementation of configure for my case ?

@shilman shilman changed the title Improve CLI init for svelte CLI: Improve init for svelte Mar 16, 2021
@shilman shilman merged commit 41efaee into storybookjs:next Mar 16, 2021
This was referenced Mar 16, 2021
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