Skip to content

Conversation

@tobiasdiez
Copy link
Contributor

@tobiasdiez tobiasdiez commented Aug 3, 2024

What I did

While working on the Nuxt integration, I noticed a couple of issues with how the preview annotations are handled. Namely:

  • Vite seems to not analyze / transpile the preview imports: Not sure why this is the case, but I believe it's the origin of Could not resolve "virtual:nuxt-runtime-config" nuxt-modules/storybook#718. I've changed the dynamic imports to absolute ones. Now imports are correctly analyzed also in dev-mode, which made it necessary to include a few optimizeDeps (one of them is from Vite: Add jsdoc-type-pratt-parser to optimizeDeps #29179).
  • Deep imports in node_modules like node_modules/packageA/node_modules/storybook are truncated to the last package ( storybook in the example), which then cannot be resolved. This is fixed by always resolving the preview annotation paths to absolute paths and using them. The code contained a few remarks that vite appearently has problems with absolute paths, but as far as I'm aware vite handles absolute paths perfectly fine.
  • What results in errors, however, are absolute paths on Windows that use \ as path separator. This is fixed by using normalize from the pathe package, which converts this to /.

The code to create the preview annotation imports is inspired by https://github.com/nuxt/nuxt/blob/754fc30e5d0fe506ee3218f9c4a11fa047e3553f/packages/nuxt/src/core/templates.ts#L61-L76

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

I've run and added a few tests. Still have to figure out how to test the sandboxes.

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

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 PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

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

name before after diff z %
createSize 0 B 0 B 0 B - -
generateSize 79.7 MB 79.7 MB 0 B 1.34 0%
initSize 79.7 MB 79.7 MB 0 B 1.34 0%
diffSize 97 B 97 B 0 B - 0%
buildSize 7.3 MB 7.45 MB 144 kB 21.6 1.9%
buildSbAddonsSize 1.9 MB 1.9 MB 0 B 0.43 0%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 1.88 MB 1.88 MB 0 B -0.94 0%
buildSbPreviewSize 0 B 0 B 0 B - -
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 3.97 MB 3.97 MB 0 B -1.31 0%
buildPreviewSize 3.33 MB 3.47 MB 144 kB 35.98 4.2%
testBuildSize 0 B 0 B 0 B - -
testBuildSbAddonsSize 0 B 0 B 0 B - -
testBuildSbCommonSize 0 B 0 B 0 B - -
testBuildSbManagerSize 0 B 0 B 0 B - -
testBuildSbPreviewSize 0 B 0 B 0 B - -
testBuildStaticSize 0 B 0 B 0 B - -
testBuildPrebuildSize 0 B 0 B 0 B - -
testBuildPreviewSize 0 B 0 B 0 B - -
name before after diff z %
createTime 25.4s 25.3s -123ms 1.37 -0.5%
generateTime 21.6s 20.5s -1s -67ms 0.28 -5.2%
initTime 4.7s 5.1s 427ms 0.91 8.2%
buildTime 9.4s 9.4s 31ms -0.3 0.3%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 4.3s 4.9s 563ms -0.55 11.4%
devManagerResponsive 4.1s 4.7s 583ms -0.47 12.2%
devManagerHeaderVisible 692ms 649ms -43ms -1.1 -6.6%
devManagerIndexVisible 701ms 714ms 13ms -0.86 1.8%
devStoryVisibleUncached 1.7s 2.3s 607ms 1.66 🔺26.1%
devStoryVisible 780ms 1s 258ms 1.08 24.9%
devAutodocsVisible 674ms 1s 331ms 2.19 🔺32.9%
devMDXVisible 704ms 1.1s 444ms 3.57 🔺38.7%
buildManagerHeaderVisible 620ms 804ms 184ms 0.79 22.9%
buildManagerIndexVisible 670ms 821ms 151ms 0.55 18.4%
buildStoryVisible 607ms 794ms 187ms 1.01 23.6%
buildAutodocsVisible 473ms 664ms 191ms 0.43 28.8%
buildMDXVisible 477ms 709ms 232ms 1.38 🔺32.7%

Greptile Summary

Improved handling of preview annotations in the Vite builder for Storybook, addressing path normalization and import issues.

  • code/builders/builder-vite/package.json: Added knitwork, pathe, and slash dependencies for better path handling.
  • code/builders/builder-vite/src/codegen-modern-iframe-script.ts: Updated to generate absolute paths for preview annotations and improved HMR logic.
  • code/builders/builder-vite/src/utils/process-preview-annotation.ts: Replaced path with pathe for normalization and added deprecation warning for object-based annotations.
  • code/builders/builder-vite/src/codegen-modern-iframe-script.test.ts: Added unit tests for generateModernIframeScriptCodeFromPreviews.
  • code/builders/builder-vite/src/utils/process-preview-annotation.test.ts: Updated tests to ensure correct path handling, especially for Windows paths.

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.

LGTM

5 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings

@nx-cloud
Copy link

nx-cloud bot commented Aug 3, 2024

View your CI Pipeline Execution ↗ for commit 78e6c4c.

Command Status Duration Result
nx run-many -t build --parallel=3 ✅ Succeeded 1m 57s View ↗

☁️ Nx Cloud last updated this comment at 2025-02-28 15:05:57 UTC

@ndelangen ndelangen added the ci:daily Run the CI jobs that normally run in the daily job. label Aug 5, 2024
@ndelangen
Copy link
Member

Thank you for the PR @tobiasdiez.

Could you have a look at why the CI is failing?

@tobiasdiez
Copy link
Contributor Author

Could you have a look at why the CI is failing?

Tests are passing now.

@ndelangen ndelangen changed the title Vite: improve handling of preview annotations Vite: Improve handling of preview annotations Aug 14, 2024
@ndelangen
Copy link
Member

@tobiasdiez I think I may have broken something when i merged in the base branch, but I'm not sure what.

Can you assist?

@tobiasdiez
Copy link
Contributor Author

@tobiasdiez I think I may have broken something when i merged in the base branch, but I'm not sure what.

Can you assist?

Thanks for your help!

The tests should pass now, but there are ts errors during build. I have no idea where they are coming from. Do you?

@valentinpalkovic valentinpalkovic self-assigned this Feb 10, 2025
Comment on lines +11 to +15
// TODO: Remove this once the new version of Nuxt is released that removes this workaround:
// https://github.com/nuxt-modules/storybook/blob/a2eec6e898386f76c74826842e8e007b185c3d35/packages/storybook-addon/src/preset.ts#L279-L306
if (path.bare != null && path.absolute === '') {
return path.bare;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@tobiasdiez I had to make this workaround to support Nuxt for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! The issues mentioned in that link were exactly the reasons why I created this PR here in the first place.

Once you release a new version, I'll remove the above workaround in the nuxt-module.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, will release a new alpha soon!

@kasperpeulen kasperpeulen merged commit bc4d314 into storybookjs:next Feb 28, 2025
54 checks passed
@github-project-automation github-project-automation bot moved this from Empathy Queue (prioritized) to Done in Core Team Projects Feb 28, 2025
@tobiasdiez tobiasdiez deleted the previewAnno branch March 7, 2025 11:06
JReinhold added a commit that referenced this pull request Mar 13, 2025
This reverts commit bc4d314, reversing
changes made to d5f066c.
JReinhold added a commit that referenced this pull request Mar 14, 2025
This reverts commit 2d3d220.

It should never have been reverted in the first place, it was a mistake.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants