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: Migrate from express to polka #29230

Open
wants to merge 34 commits into
base: next
Choose a base branch
from

Conversation

43081j
Copy link
Contributor

@43081j 43081j commented Sep 27, 2024

Closes #29083

What I did

Migrates from express to polka as the internal web server.

Work in progress

Still WIP as there's some leftover changes from the old connect branch we can probably discard/revert now.

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

N/A yet

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This pull request has been released as version 0.0.0-pr-29230-sha-ffef33ce. Try it out in a new sandbox by running npx storybook@0.0.0-pr-29230-sha-ffef33ce sandbox or in an existing project with npx storybook@0.0.0-pr-29230-sha-ffef33ce upgrade.

More information
Published version 0.0.0-pr-29230-sha-ffef33ce
Triggered by @JReinhold
Repository 43081j/storybook
Branch without-express-polka
Commit ffef33ce
Datetime Thu Oct 3 12:43:50 UTC 2024 (1727959430)
Workflow run 11162208896

To request a new release of this pull request, mention the @storybookjs/core team.

core team members can create a new canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=29230

Greptile Summary

This PR migrates Storybook's internal web server from Express to Polka, aiming to reduce transitive dependencies and potentially improve performance.

  • Replaced Express with Polka in code/core/src/core-server/dev-server.ts, updating server initialization and middleware usage
  • Updated static file serving to use sirv instead of express.static in multiple files
  • Removed Express-specific types and methods, replacing them with more generic alternatives across the codebase
  • Modified package.json files to remove Express dependencies and add Polka-related packages
  • Adjusted response handling in various files to be compatible with Polka's API

43081j and others added 21 commits May 6, 2024 23:27
Migrates away from express, instead using connect and the underlying
node server directly.

Both express and connect support the same middleware structure, which
means most middleware packages work for both and have remained the same
in this change.

A few notes:

- Express' router has been replaced by basic connect mount points
(though the two are not equivalent exactly)
- Express static has been replaced by sirv

This trims a large sub-tree of dependencies from our packages which
express was pulling in. We in fact use very little of the functionality
express gives us, only really making use of the connect-style API
anyway.
Migrates from express to polka 1.x.
Copy link

nx-cloud bot commented Sep 27, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 66eddc5. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

@43081j
Copy link
Contributor Author

43081j commented Sep 27, 2024

@JReinhold this is where I got to with polka

I may not have much time over the next few days, so if you do get time, feel free to use and push to this branch

few things to note:

  • it may be possible to rename app back to router or whatever it was before, now that this uses polka
  • the places where we use sirv need manually testing
  • a few formatting changes are in here. I think they're right since, if I try revert them, the lint-staging process will just add them back (but they're in files I haven't otherwise touched)
  • haven't checked but I think the public interface exposes Polka.Polka which isn't ideal. maybe we should have a wrapper or dumbed down interface around it which we ship (so we're not tied into polka)

@JReinhold JReinhold self-assigned this Sep 27, 2024
@JReinhold
Copy link
Contributor

Thank you! 🙏

I'll make sure it gets over the finish line next week.

@JReinhold JReinhold changed the title Migrate from express to polka Core: Migrate from express to polka Sep 30, 2024
@JReinhold JReinhold added the core label Sep 30, 2024
@JReinhold
Copy link
Contributor

JReinhold commented Oct 1, 2024

Todo

  • Don't expose Polka types
  • Test the local dev server on Windows
  • Test static files
    • Windows
    • Paths outside of root should not be allowed
    • Favicon
    • Multiple staticDirs
  • Test custom middlewares with .storybook/middleware.js
  • Webpack
  • Vite
  • RSBuild
  • Modern Web Dev
  • Remove express from scripts

@storybook-bot
Copy link
Contributor

Failed to publish canary version of this pull request, triggered by @JReinhold. See the failed workflow run at: https://github.com/storybookjs/storybook/actions/runs/11161442195

@storybook-bot
Copy link
Contributor

Failed to publish canary version of this pull request, triggered by @JReinhold. See the failed workflow run at: https://github.com/storybookjs/storybook/actions/runs/11161893237

@JReinhold
Copy link
Contributor

Couldn't test rsbuild because of rspack-contrib/storybook-rsbuild#136, but looking at the source it doesn't look like they're depending on any express-specific behavior in the router of start():

https://github.com/rspack-contrib/storybook-rsbuild/blob/main/packages/builder-rsbuild/src/index.ts#L130-L193

Same story for Modern Web, they don't yet support Storybook 8 so I couldn't test it, but their start() code looks good too:

https://github.com/modernweb-dev/web/blob/master/packages/storybook-builder/src/index.ts#L60-L133

@JReinhold JReinhold marked this pull request as ready for review October 4, 2024 10:41
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

30 file(s) reviewed, 4 comment(s)
Edit PR Review Bot Settings

options.presets.apply('core'),
]);
const [server, core] = await Promise.all([getServer(options), options.presets.apply('core')]);
console.log('LOG: starting dev server');
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Remove this console.log statement before merging

Comment on lines +28 to +31
// app.use((req, res, next) => {
// console.log('LOG: ', { url: req.url, method: req.method });
// next();
// });
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Consider removing or uncommenting this debugging middleware

Comment on lines +96 to +97
app,
router: app, // back-compatability with express-based API
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Passing both app and router: app might be redundant. Consider removing one

@@ -143,7 +138,7 @@ export async function storybookDevServer(options: Options) {
const previewResult = await previewStarted;

// Now the preview has successfully started, we can count this as a 'dev' event.
doTelemetry(core, initializedStoryIndexGenerator, options);
doTelemetry(app, core, initializedStoryIndexGenerator, options);
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Update doTelemetry function to accept Polka app instead of Express

@shilman shilman mentioned this pull request Oct 6, 2024
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Migrate from express to polka
4 participants