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

[DRAFT] Add Storybook (Vite) framework package #10064

Open
wants to merge 49 commits into
base: main
Choose a base branch
from

Conversation

arimendelow
Copy link
Contributor

@arimendelow arimendelow commented Feb 25, 2024

Overview

This PR is the first step to migrating our Storybook integration from relying on Webpack to relying on Vite. Specifically, this PR:

  • Adds the Storybook framework package for RedwoodJS.
    • This follows the modern Storybook pattern for providing required configuration for getting Storybook to work with a given framework/metaframework.
    • For comparison, our current Webpack Storybook setup was written before framework packages were a thing, so it's architected very differently. See the [@redwoodjs/testing].(https://github.com/redwoodjs/redwood/tree/main/packages/testing) package for more information.
    • The eventual goal is for this package to move into the Storybook codebase and be "officially" supported.
    • This package was forked from the react-vite framework package.
  • Adds the storybook-vite CLI package, which is based on the existing storybook CLI package. The CLI package does two important things before running the Storybook command:
    • Checks if the necessary project-side config files exist yet, and creates them if they don't.
    • Creates the Mock Service Worker, which is necessary for Cell mocking.

Importantly, the architecture of this (including duplicating files, etc.) is based on the eventual goal of removing the older Storybook integration files.

Proof

This screen recording shows that the following works:

  • Router
  • Nested cells (waterfall)
  • Auth

sbv demo

Details

This framework package is based on the proof of concept of getting Storybook working on RedwoodJS with Vite, which is documented here.

Please see that and official Storybook framework package documentation to understand implementation details.

Outstanding

  • The console outputs are a bit noisy, and it seems like it's just noise. Do we want to address/silence this?
  • Testing?
  • Cleanup?
  • Finalize how we want to roll this out. For example:
    • Do we want to add a console message to the current CLI that it'll be removed in a future/next major?
    • How do we want to address the different CLI packages? Some options:
      • Temporarily keep the existing one as yarn redwood storybook, and make the new one yarn redwood storybook-vite.
      • Make the existing one yarn redwood storybook-webpack.
        • I'm concerned that folks will not be aware of this, causing friction.
  • Based on the above, write migration guide.
  • ???

Trying it out

First step is, of course, pulling down this branch.

I've been testing this with the generated test project. If you want to do that, from the framework repo, first run:

yarn run build:test-project ../for-testing-storybook-vite

Then, over in your project, run yarn rwfw project:tarsync so that the module resolutions point to the ones in this branch.

Then, I think because it hasn't yet been published anywhere, you'll need to manually add the new CLI package (yarn complains about not being able to find the packument when I try to get the CLI package to autoinstall):

yarn add -D @redwoodjs/cli-storybook-vite@7

Then, run:

yarn rw sbv

Your browser should then open to Storybook!

@arimendelow arimendelow changed the title [DRAFT] Add storybook framework package [DRAFT] Add Storybook (Vite) framework package Apr 14, 2024
@arimendelow arimendelow marked this pull request as ready for review May 19, 2024 19:17
import type { StorybookYargsOptions } from '../types'

/*
readFile and writeFile are somewhat duplicated from @redwoodjs/cli; I could not for the life of me get the package
Copy link
Member

Choose a reason for hiding this comment

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

@redwoodjs/cli is a bit strange. It's not ment to be imported from. Stuff that should be shared goes in cli-helpers. So maybe readFile and writeFile should live there. But since you modified writeFile anyways they might as well both live here, at least for now 🙂

import { useLocation, ParamsContext, parseSearch } from '@redwoodjs/router'

interface Props {
path?: string
Copy link
Member

@Tobbe Tobbe May 23, 2024

Choose a reason for hiding this comment

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

Did you intend for this to be allParams to match ParamsProvider in @redwoodjs/router?
Or could we maybe just get rid of it since it's not used in the Mock version?


import { MockParamsProvider } from './MockParamsProvider'

// Import the user's Router from `./web/src/Router.{tsx,jsx}`,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Import the user's Router from `./web/src/Router.{tsx,jsx}`,
// Import the user's Routes from `./web/src/Routes.{tsx,jsx}`,

import { MockParamsProvider } from './MockParamsProvider'

// Import the user's Router from `./web/src/Router.{tsx,jsx}`,
// we pass the `children` from the user's Router to `./MockRouter.Router`
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// we pass the `children` from the user's Router to `./MockRouter.Router`
// we pass the `children` from the user's Routes to `./MockRouter.Router`

// we pass the `children` from the user's Router to `./MockRouter.Router`
// so that we can populate the `routes object` in Storybook and tests.
// // @ts-expect-error - this comes from a Vite alias in main.ts
let UserRouterWithRoutes: React.FC
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let UserRouterWithRoutes: React.FC
let UserRoutes: React.FC

export const routes: { [routeName: string]: () => string } = {}

/**
* We overwrite the default `Router` export (see jest-preset). So every import
Copy link
Member

Choose a reason for hiding this comment

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

is jest-preset still the thing to look at?

import { reactDocgen } from './plugins/react-docgen'
import type { StorybookConfig } from './types'

const getAbsolutePath = <I extends string>(input: I): I =>
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain what's going on with the types here? I tried it without the narrowing to I and it seems to still work as expected. What am I missing?

const {
default: UserRouterWithRoutes,
} = require('~__REDWOOD__USER_ROUTES_FOR_MOCK')
let UserRouterWithRoutes: React.FC
Copy link
Member

Choose a reason for hiding this comment

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

Same naming questions here as above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants