Presets: Use .js files when .cjs files are passed for entries that should be ESM
#31556
+21
−5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes storybookjs/addon-kit#79
🤞 Unblocks igrlk/storybook-addon-test-codegen#29
What I did
In Storybook 9 prereleases, we would always load any found
preview.cjsfiles as preview annotations, even though that is not valid to pass to the browser. There were multiple reasons for this:require.resolve()from a CJS file, and Node.js will prefer to return CJS files for that if it finds them. We rarely/never ran into that problem internally, because because the core addons are not"type": "module", they don't have.cjsfiles but rather.mjsfiles which we automatically prefer. Butaddon-kitand derivatives are"type:" "module", they distribute bothpreview.jsandpreview.cjsto be compatible with portable stories in Jest. In those situations, we would wrongly prefer thepreview.cjsfiles.storybook-addon-kit/preview, and the Vite builder would use that. However in Vite: Improve handling of preview annotations #28798 we changed that, so that Vite only uses the bare name if the absolute name is empty. But in most cases with addons, Storybook passes both a bare and an absolute path, and so in that situation Vite would now prefer the absolute path to thepreview.cjsfile which was invalid.To fix this, we're now improving the "prefer MJS" logic in preset loading, so that it not only prefers
.mjsfiles over.jsfiles, but also.jsfiles over.cjsfiles. So now, if there is bothpreview.cjsandpreview.js, it will preferpreview.js.Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
We need to test this out in:
getAbsolutePathhelperThis section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!
Documentation
MIGRATION.MD
Checklist for Maintainers
When this PR is ready for testing, make sure to add
ci:normal,ci:mergedorci:dailyGH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found incode/lib/cli-storybook/src/sandbox-templates.tsMake 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-31556-sha-5f0bee85. Try it out in a new sandbox by runningnpx [email protected] sandboxor in an existing project withnpx [email protected] upgrade.More information
0.0.0-pr-31556-sha-5f0bee85jeppe/fix-preview-cjs-resolution5f0bee851748006610)To request a new release of this pull request, mention the
@storybookjs/coreteam.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=31556