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

Addon-docs: Replace source-loader with csf-plugin #19680

Merged
merged 23 commits into from
Nov 1, 2022
Merged

Conversation

shilman
Copy link
Member

@shilman shilman commented Oct 29, 2022

Issue: N/A

csf-plugin is a source-loader replacement that is dramatically simpler & easier to maintain.
It also supports both Webpack & Vite, providing a proof of concept for how we might do cross-builder addons
in the future.

In addition to the maintenance implications, addon-storysource and addon-docs both used source-loader, but in conflicting ways. So to use both at the same time, users needed to jump through configuration hoops.

What I did

  • Extended csf-tools to provide basic static source extraction
  • Introduced csf-plugin that provides that extraction for both Weback & Vite
  • Updated the documentation & MIGRATION.md
  • Added to ui storybook to test Vite

What I need

@joshwooding @IanVS let's figure out the best place to integrate it into Vite. Should addon-docs get a viteFinal?

How to test

  • CI passes
  • Test by hand
cd code
yarn storybook:blocks

Then navigate to the Controls stories & view source on the DocsPage

Copy link
Contributor

@JReinhold JReinhold left a comment

Choose a reason for hiding this comment

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

This looks good!
I'm not worried about the breaking change since it's fairly easy for users to migrate.

Comment on lines 11 to 16
const addParameter = template(`
%%key%%.parameters = { storySource: { source: %%source%% }, ...%%key%%.parameters };
`)({
key: t.identifier(key),
source: t.stringLiteral(source),
}) as t.Statement;
Copy link
Contributor

Choose a reason for hiding this comment

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

Without knowing too much about what goes on behind the scenes, on the surface this looks like something that could be handled with a simple template literal instead of requiring babel/template?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, this needs to be babel AST nodes. The other option is to do it without @babel/template is to construct the AST by hand, which is more verbose and not very human-readable. I was thinking of doing it as an optimization, and will create a telescoping PR to see if we like that better.

Copy link
Member Author

@shilman shilman Oct 29, 2022

Choose a reason for hiding this comment

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

See changes in #19684

Copy link
Contributor

@JReinhold JReinhold Oct 29, 2022

Choose a reason for hiding this comment

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

Yeah I see those changes. I don't know how much faster it is, but the raw AST is definitely less readable that template.

It might look okay now, but coming back to it in a year to do some modifications sounds like a nightmare.

code/lib/csf-plugin/README.md Outdated Show resolved Hide resolved
code/lib/csf-tools/src/enrichCsf.test.ts Outdated Show resolved Hide resolved
shilman and others added 2 commits October 29, 2022 19:49
Co-authored-by: Tom Coleman <tom@chromatic.com>
@@ -62,7 +66,7 @@ export async function webpack(
babelOptions,
mdxBabelOptions,
configureJSX = true,
sourceLoaderOptions = { injectStoryParameters: true },
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to still accept this property with the purpose of warning users/throwing an error and directing them to the migration docs?

Copy link
Member Author

Choose a reason for hiding this comment

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

Great idea! 💯

@shilman shilman marked this pull request as ready for review November 1, 2022 02:08
@shilman shilman merged commit e8427c9 into next Nov 1, 2022
@shilman shilman deleted the shilman/csf-plugin branch November 1, 2022 04:05
],
"scripts": {
"check": "../../../scripts/node_modules/.bin/tsc --noEmit",
"prep": "node ../../../scripts/prepare.js"
Copy link
Member

Choose a reason for hiding this comment

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

@shilman was there a reason not to use tsup for bundling this?

@@ -32,6 +33,10 @@ const config: StorybookConfig = {
core: {
disableTelemetry: true,
},
viteFinal: (vite) => ({
...vite,
plugins: [...(vite.plugins || []), csfPlugin({})],
Copy link
Contributor

Choose a reason for hiding this comment

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

@shilman coming back to this.
Why are we explicitly adding this to our UI SB instead of adding it per default through builder-vite ?

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.

5 participants