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

Release: Prerelease 8.0.0-alpha.4 #25189

Merged
merged 129 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
129 commits
Select commit Hold shift + click to select a range
a275999
refactor: inherited 'react-element-to-jsx-string'.
zhyd1997 Nov 9, 2022
543bdf4
fix: passed undefined value into displayNameDefaults if getDocgenSect…
zhyd1997 Nov 9, 2022
3d25687
fix: used reassigned renderedJSX instead of code var.
zhyd1997 Nov 9, 2022
9d96efb
test: log renderedJSX for debug.
zhyd1997 Nov 9, 2022
20863a7
Merge branch 'storybookjs:next' into refactor/ea45781-yadong-jsx-deco…
zhyd1997 Nov 9, 2022
7797cda
fix: Cannot read properties of undefined (reading '__docgenInfo').
zhyd1997 Nov 10, 2022
ef1d14a
feat: added StrictMode support in source code snippet.
zhyd1997 Nov 10, 2022
946ab3f
Merge branch 'storybookjs:next' into refactor/ea45781-yadong-jsx-deco…
zhyd1997 Nov 11, 2022
1893bc1
Merge branch 'storybookjs:next' into refactor/ea45781-yadong-jsx-deco…
zhyd1997 Nov 15, 2022
4fbcd93
fix: added missing arguments.
zhyd1997 Nov 15, 2022
bd5bdb7
Merge branch 'next' into refactor/ea45781-yadong-jsx-decorator
ndelangen Jan 13, 2023
88c63b7
change the includes list of angular projects to reduce the warnings f…
ndelangen Oct 20, 2023
f1d16e6
Merge branch 'next' into norbert/fix-24487-angular-tsconfig
valentinpalkovic Nov 5, 2023
376029f
spike removing react as a peer dependency from addon-docs
JReinhold Nov 16, 2023
5317302
Merge branch 'next' of github.com:storybookjs/storybook into addon-do…
JReinhold Nov 16, 2023
7f6c1cc
update lockfile
JReinhold Nov 17, 2023
c85a698
remove unused join
JReinhold Nov 17, 2023
012e909
fix
JReinhold Nov 17, 2023
a27c2c7
Explain how to clean local files for contributers
bodograumann Oct 31, 2023
c863342
Add random attribute to bootstrapped selector
Marklb Nov 24, 2023
fce6e2a
Render colors in the same order as provided
kaelig Nov 27, 2023
bd40b6e
Merge branch 'release-8-0' into refactor/ea45781-yadong-jsx-decorator
ndelangen Nov 28, 2023
18be085
Merge branch 'next' into marklb/fix-angular-name-collisions
Marklb Nov 30, 2023
413f579
Refactor story's unique attribute to be more predictable
Marklb Dec 2, 2023
7893089
Merge branch 'next' into marklb/fix-angular-name-collisions
Marklb Dec 2, 2023
a79a362
SvelteKit: fix missing $app modules
paoloricciuti Dec 6, 2023
16722d6
Add tests for svelte-kit/skeleton-js too
paoloricciuti Dec 6, 2023
56a695e
Merge branch 'next' into fix-sveltekit-other-app-modules
paoloricciuti Dec 6, 2023
2785843
fix pr comments
paoloricciuti Dec 6, 2023
564e088
Merge branch 'next' into fix-sveltekit-other-app-modules
paoloricciuti Dec 6, 2023
9b09b1c
Merge branch 'next' into marklb/fix-angular-name-collisions
Marklb Dec 6, 2023
d707069
Merge branch 'next' into fix-sveltekit-other-app-modules
paoloricciuti Dec 8, 2023
791ba23
Merge branch 'next' into norbert/fix-24487-angular-tsconfig
valentinpalkovic Dec 11, 2023
163d347
Merge branch 'next' into docs-contributer-returns
jonniebigodes Dec 11, 2023
5982486
Merge branch 'next' into addon-docs-without-react
ndelangen Dec 12, 2023
297eca8
cleanup
ndelangen Dec 12, 2023
f5a7235
cleanup
ndelangen Dec 12, 2023
90e95bd
cleanup
ndelangen Dec 12, 2023
5d65a35
fixes
ndelangen Dec 12, 2023
8a9dce4
Merge pull request #24972 from Marklb/marklb/fix-angular-name-collisions
valentinpalkovic Dec 12, 2023
2ee9a72
Fix Angular sandbox generation
valentinpalkovic Dec 11, 2023
c44d4ee
fixes
ndelangen Dec 12, 2023
4282571
Merge branch 'next' into docs-contributer-returns
jonniebigodes Dec 12, 2023
c49a841
fix react-dom-shim
JReinhold Dec 12, 2023
e2374e5
Merge branches 'addon-docs-without-react' and 'addon-docs-without-rea…
JReinhold Dec 12, 2023
593a774
Merge pull request #24531 from storybookjs/norbert/fix-24487-angular-…
valentinpalkovic Dec 12, 2023
6c92162
remove the add-react automigration, do not add react as dep
ndelangen Dec 12, 2023
0e7ce81
Merge branch 'addon-docs-without-react' into norbert/not-add-react-init
ndelangen Dec 12, 2023
d2d27c4
add sandbox for react16
ndelangen Dec 12, 2023
441ccac
Update CONTRIBUTING.md
bodograumann Dec 12, 2023
4cb0495
Merge branch 'norbert/not-add-react-init' into norbert/add-internalre…
ndelangen Dec 12, 2023
72a0423
Merge branch 'next' into docs-contributer-returns
jonniebigodes Dec 12, 2023
b4acf3a
Merge pull request #24630 from bodograumann/docs-contributer-returns
jonniebigodes Dec 12, 2023
0fd75af
Merge branch 'next' into pr/kaelig/25001
ndelangen Dec 13, 2023
b6af032
fix linting
ndelangen Dec 13, 2023
f013ffe
Merge pull request #25001 from kaelig/patch-6
ndelangen Dec 13, 2023
171493e
add e2e test for resolved react version
JReinhold Dec 13, 2023
ba924cb
add prompt-only automigrate
ndelangen Dec 13, 2023
c590019
fix react-dom-shim in vite too
JReinhold Dec 13, 2023
dab5b63
add react alias to beginning of array
JReinhold Dec 13, 2023
e5b8f86
Merge branch 'next' into addon-docs-without-react
ndelangen Dec 13, 2023
dab576d
react alias plugin enforce pre
JReinhold Dec 13, 2023
f81a397
fix ResolvedReactVersion tsx
JReinhold Dec 13, 2023
46cb279
Merge branch 'addon-docs-without-react' of github.com:storybookjs/sto…
JReinhold Dec 13, 2023
e289b59
mention both react & react-dom
ndelangen Dec 13, 2023
15e365e
Build: Remove preact-webpack5 sandboxes
valentinpalkovic Dec 13, 2023
0e95602
Merge pull request #25217 from storybookjs/valentin/remove-preact5-we…
valentinpalkovic Dec 13, 2023
f85710c
Merge branch 'next' into fix-sveltekit-other-app-modules
JReinhold Dec 13, 2023
86ad08a
Docs: Fixed Typo in documentation template
Darth-koder007 Dec 13, 2023
a9824bc
Merge pull request #25132 from paoloricciuti/fix-sveltekit-other-app-…
JReinhold Dec 13, 2023
d0979bc
fix e2e test asserting react 17
JReinhold Dec 13, 2023
d3b01f3
Merge branch 'next' into patch-1
jonniebigodes Dec 13, 2023
6ea2365
don't attempt to resolve @mdx-js/react outside of addon-docs
JReinhold Dec 14, 2023
99a1b78
fix
ndelangen Dec 14, 2023
a6958dd
Merge branch 'addon-docs-without-react' into norbert/not-add-react-init
ndelangen Dec 14, 2023
678f669
Update code/lib/cli/src/automigrate/fixes/prompt-remove-react.ts
ndelangen Dec 14, 2023
b217193
Update code/lib/cli/src/automigrate/fixes/prompt-remove-react.ts
ndelangen Dec 14, 2023
5d3619a
Merge branch 'norbert/not-add-react-init' into norbert/prompt-removal…
ndelangen Dec 14, 2023
fb06d36
simplify
ndelangen Dec 14, 2023
303a5d6
Merge branch 'norbert/not-add-react-init' into norbert/add-internalre…
ndelangen Dec 14, 2023
aeec36d
add to daily
ndelangen Dec 14, 2023
389eaa9
Merge pull request #19785 from zhyd1997/refactor/ea45781-yadong-jsx-d…
ndelangen Dec 14, 2023
2131586
Docs: Remove documentation only components and MDX files
jonniebigodes Dec 14, 2023
a742b55
Fix snippets
jonniebigodes Dec 14, 2023
43af8b7
Merge pull request #25229 from storybookjs/docs_fix_install_controls_…
jonniebigodes Dec 14, 2023
523d280
Merge branch 'next' into patch-1
jonniebigodes Dec 14, 2023
276af47
Merge pull request #25221 from Darth-koder007/patch-1
jonniebigodes Dec 14, 2023
d96c62d
Docs: Fix Install troubleshooting heading
jonniebigodes Dec 14, 2023
d9d5453
Merge branch 'next' into docs_install_fix_heading
jonniebigodes Dec 14, 2023
70da7da
Merge pull request #25230 from storybookjs/docs_install_fix_heading
jonniebigodes Dec 14, 2023
01182e5
cleanup
JReinhold Dec 15, 2023
00f031b
Merge branch 'addon-docs-without-react' of github.com:storybookjs/sto…
JReinhold Dec 15, 2023
021bb56
embed `react-textarea-autosize` types
ndelangen Dec 15, 2023
ed302ce
export it
ndelangen Dec 15, 2023
37d3bf5
fix
ndelangen Dec 15, 2023
28051ed
Merge branch 'next' into addon-docs-without-react
JReinhold Dec 15, 2023
655aa07
Merge pull request #25235 from storybookjs/norbert/fix-18734-type-bun…
ndelangen Dec 15, 2023
ff88e04
API: Remove stories.json support
shilman Dec 15, 2023
4d5f965
Core: Bundle for node18 in SB8
shilman Dec 15, 2023
9d767ef
Update CHANGELOG.md for v7.6.5 [skip ci]
storybook-bot Dec 15, 2023
05efeae
Build: Removed changing `version` from SvelteKit environment story
shilman Dec 15, 2023
b3840f4
SvelteKit: new modules exports mocks
paoloricciuti Dec 15, 2023
5854e5a
Merge pull request #25241 from storybookjs/shilman/fix-sveltekit-envi…
shilman Dec 15, 2023
34d5354
Merge branch 'next' into new-sveltekit-modules
paoloricciuti Dec 15, 2023
482ef84
fix peer dep and update yarn lock
paoloricciuti Dec 15, 2023
29bf696
Docs: Adds list of available viewports
jonniebigodes Dec 17, 2023
152edae
Merge pull request #25236 from storybookjs/shilman/kill-stories-json
shilman Dec 18, 2023
2db44d7
Merge branch 'next' into addon-docs-without-react
JReinhold Dec 18, 2023
d17d493
Merge branch 'addon-docs-without-react' into norbert/not-add-react-init
JReinhold Dec 18, 2023
2c2c345
Merge branch 'norbert/not-add-react-init' into norbert/add-internalre…
JReinhold Dec 18, 2023
1ea28ea
Merge branch 'norbert/not-add-react-init' into norbert/prompt-removal…
JReinhold Dec 18, 2023
1aa7b1a
Merge pull request #25239 from storybookjs/shilman/bundle-for-node18
ndelangen Dec 18, 2023
bc3a098
Merge branch 'next' into docs_update_viewports
jonniebigodes Dec 18, 2023
0096e7b
Merge pull request #24881 from storybookjs/addon-docs-without-react
JReinhold Dec 18, 2023
ce5d8a1
Merge pull request #25196 from storybookjs/norbert/not-add-react-init
JReinhold Dec 18, 2023
9cd9f65
Merge pull request #25215 from storybookjs/norbert/prompt-removal-rea…
JReinhold Dec 18, 2023
bd2cf8e
Merge branch 'next' into new-sveltekit-modules
paoloricciuti Dec 18, 2023
5ad970d
Merge branch 'next' into docs_update_viewports
jonniebigodes Dec 18, 2023
5d61821
Merge branch 'next' into norbert/add-internalreact16-sandbox
JReinhold Dec 18, 2023
4ef8ba7
remove inDevelopment for react16-webpack
JReinhold Dec 18, 2023
e6ecbe0
fix parallelism count
yannbf Dec 18, 2023
d9dca67
improve cadence check message
yannbf Dec 18, 2023
405dfee
Merge pull request #25199 from storybookjs/norbert/add-internalreact1…
yannbf Dec 18, 2023
6cc0744
Merge branch 'next' into new-sveltekit-modules
paoloricciuti Dec 18, 2023
f77e80b
Merge branch 'next' into docs_update_viewports
jonniebigodes Dec 18, 2023
4c3db68
Merge pull request #25253 from storybookjs/docs_update_viewports
jonniebigodes Dec 18, 2023
862637c
Merge branch 'next' into new-sveltekit-modules
paoloricciuti Dec 19, 2023
aae68d4
Merge pull request #25244 from paoloricciuti/new-sveltekit-modules
JReinhold Dec 19, 2023
c0ab202
Write changelog for 8.0.0-alpha.4 [skip ci]
storybook-bot Dec 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 10 additions & 10 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -652,27 +652,27 @@ workflows:
requires:
- unit-tests
- create-sandboxes:
parallelism: 20
parallelism: 19
requires:
- build
- build-sandboxes:
parallelism: 20
parallelism: 19
requires:
- create-sandboxes
- chromatic-sandboxes:
parallelism: 17
parallelism: 16
requires:
- build-sandboxes
- e2e-production:
parallelism: 15
parallelism: 14
requires:
- build-sandboxes
- e2e-dev:
parallelism: 2
requires:
- create-sandboxes
- test-runner-production:
parallelism: 15
parallelism: 14
requires:
- build-sandboxes
- bench:
Expand Down Expand Up @@ -706,30 +706,30 @@ workflows:
requires:
- build
- create-sandboxes:
parallelism: 33
parallelism: 32
requires:
- build
# - smoke-test-sandboxes: # disabled for now
# requires:
# - create-sandboxes
- build-sandboxes:
parallelism: 33
parallelism: 32
requires:
- create-sandboxes
- chromatic-sandboxes:
parallelism: 30
parallelism: 29
requires:
- build-sandboxes
- e2e-production:
parallelism: 28
parallelism: 27
requires:
- build-sandboxes
- e2e-dev:
parallelism: 2
requires:
- create-sandboxes
- test-runner-production:
parallelism: 28
parallelism: 27
requires:
- build-sandboxes

Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 7.6.5

- Angular: Update Angular cli templates - [#25152](https://github.com/storybookjs/storybook/pull/25152), thanks [@Marklb](https://github.com/Marklb)!
- Blocks: Fix Subtitle block for unattached docs pages - [#25157](https://github.com/storybookjs/storybook/pull/25157), thanks [@kripod](https://github.com/kripod)!
- SvelteKit: Fix missing `$app` modules - [#25132](https://github.com/storybookjs/storybook/pull/25132), thanks [@paoloricciuti](https://github.com/paoloricciuti)!

## 7.6.4

- Angular: Fix CSF Plugin - [#25098](https://github.com/storybookjs/storybook/pull/25098), thanks [@valentinpalkovic](https://github.com/valentinpalkovic)!
Expand Down
15 changes: 15 additions & 0 deletions CHANGELOG.prerelease.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
## 8.0.0-alpha.4

- API: Remove stories.json support - [#25236](https://github.com/storybookjs/storybook/pull/25236), thanks [@shilman](https://github.com/shilman)!
- Addon Docs: Remove `react` peer dependency - [#24881](https://github.com/storybookjs/storybook/pull/24881), thanks [@JReinhold](https://github.com/JReinhold)!
- Addon-docs: Support `<StrictMode />` and `<Suspense />` in source viewer - [#19785](https://github.com/storybookjs/storybook/pull/19785), thanks [@zhyd1997](https://github.com/zhyd1997)!
- Angular: Add random attribute to bootstrapped selector - [#24972](https://github.com/storybookjs/storybook/pull/24972), thanks [@Marklb](https://github.com/Marklb)!
- Angular: Reduce the warnings from `ts-loader` via stricter list of `includes` - [#24531](https://github.com/storybookjs/storybook/pull/24531), thanks [@ndelangen](https://github.com/ndelangen)!
- Blocks: Render colors in the same order as provided - [#25001](https://github.com/storybookjs/storybook/pull/25001), thanks [@kaelig](https://github.com/kaelig)!
- CLI: Add prompt-only automigrate asking for react-removal - [#25215](https://github.com/storybookjs/storybook/pull/25215), thanks [@ndelangen](https://github.com/ndelangen)!
- CLI: No longer add react in init - [#25196](https://github.com/storybookjs/storybook/pull/25196), thanks [@ndelangen](https://github.com/ndelangen)!
- Core: Set bundle target to `node18` - [#25239](https://github.com/storybookjs/storybook/pull/25239), thanks [@shilman](https://github.com/shilman)!
- SvelteKit: Fix missing `$app` modules - [#25132](https://github.com/storybookjs/storybook/pull/25132), thanks [@paoloricciuti](https://github.com/paoloricciuti)!
- SvelteKit: Support 2.0 modules with mocks - [#25244](https://github.com/storybookjs/storybook/pull/25244), thanks [@paoloricciuti](https://github.com/paoloricciuti)!
- UI: Embed `react-textarea-autosize` types - [#25235](https://github.com/storybookjs/storybook/pull/25235), thanks [@ndelangen](https://github.com/ndelangen)!

## 8.0.0-alpha.3

- Addon-docs: Fix storybook MDX check - [#24696](https://github.com/storybookjs/storybook/pull/24696), thanks [@shilman](https://github.com/shilman)!
Expand Down
7 changes: 7 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ If you run `yarn start` and encounter the following error, try rerunning `yarn s
```sh
> NX ENOENT: no such file or directory, open 'storybook/code/node_modules/nx/package.json'
```
If you are a Storybook contributor and still experience issues, it is recommended that you verify your local Storybook instance for any unintentional local changes. To do this, you can use the following command:

```sh
git clean -dx --dry-run
```

By executing this command, you will be able to see which untracked or ignored files and directories will be removed from your working directory if you run it with the `--force` flag. Before running the command with the `--force` flag, please commit any local changes that you want to keep. Otherwise they will be lost.

## Forked repos

Expand Down
7 changes: 7 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [Icons is deprecated](#icons-is-deprecated)
- [React-docgen component analysis by default](#react-docgen-component-analysis-by-default)
- [Removed postinstall](#removed-postinstall)
- [Removed stories.json](#removed-storiesjson)
- [Framework-specific changes](#framework-specific-changes)
- [Angular: Drop support for Angular \< 15](#angular-drop-support-for-angular--15)
- [Next.js: Drop support for version \< 13.5](#nextjs-drop-support-for-version--135)
Expand Down Expand Up @@ -473,6 +474,12 @@ For more information see: https://storybook.js.org/docs/react/api/main-config-ty

We removed the `@storybook/postinstall` package, which provided some utilities for addons to programmatically modify user configuration files on install. This package was years out of date, so this should be a non-disruptive change. If your addon used the package, you can view the old source code [here](https://github.com/storybookjs/storybook/tree/release-7-5/code/lib/postinstall) and adapt it into your addon.

#### Removed stories.json

In addition to the built storybook, `storybook build` generates two files, `index.json` and `stories.json`, that list out the contents of the Storybook. `stories.json` is a legacy format and we included it for backwards compatibility. As of 8.0 we no longer build `stories.json` by default, and we will remove it completely in 9.0.

In the meantime if you have code that relies on `stories.json`, you can find code that transforms the "v4" `index.json` to the "v3" `stories.json` format (and their respective TS types): https://github.com/storybookjs/storybook/blob/release-7-5/code/lib/core-server/src/utils/stories-json.ts#L71-L91

### Framework-specific changes

#### Angular: Drop support for Angular \< 15
Expand Down
8 changes: 2 additions & 6 deletions code/addons/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,17 @@
"@storybook/theming": "workspace:*",
"@storybook/types": "workspace:*",
"fs-extra": "^11.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"remark-external-links": "^8.0.0",
"remark-slug": "^6.0.0",
"ts-dedent": "^2.0.0"
},
"devDependencies": {
"@rollup/pluginutils": "^5.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"typescript": "^5.3.2",
"vite": "^4.0.4"
},
"peerDependencies": {
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
},
"publishConfig": {
"access": "public"
},
Expand Down
40 changes: 0 additions & 40 deletions code/addons/docs/src/ensure-react-peer-deps.ts

This file was deleted.

87 changes: 84 additions & 3 deletions code/addons/docs/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,26 @@ import type { JSXOptions, CompileOptions } from '@storybook/mdx2-csf';
import { global } from '@storybook/global';
import { loadCsf } from '@storybook/csf-tools';
import { logger } from '@storybook/node-logger';
import { ensureReactPeerDeps } from './ensure-react-peer-deps';

/**
* Get the resolvedReact preset, which points either to
* the user's react dependencies or the react dependencies shipped with addon-docs
* if the user has not installed react explicitly.
*/
const getResolvedReact = async (options: Options) => {
const resolvedReact = (await options.presets.apply('resolvedReact', {})) as any;
// resolvedReact should always be set by the time we get here, but just in case, we'll default to addon-docs's react dependencies
return {
react: resolvedReact.react ?? dirname(require.resolve('react/package.json')),
reactDom: resolvedReact.reactDom ?? dirname(require.resolve('react-dom/package.json')),
// In Webpack, symlinked MDX files will cause @mdx-js/react to not be resolvable if it is not hoisted
// This happens for the SB monorepo's template stories when a sandbox has a different react version than
// addon-docs, causing addon-docs's dependencies not to be hoisted.
// This might also affect regular users who have a similar setup.
// Explicitly alias @mdx-js/react to avoid this issue.
mdx: resolvedReact.mdx ?? dirname(require.resolve('@mdx-js/react/package.json')),
};
};

async function webpack(
webpackConfig: any = {},
Expand Down Expand Up @@ -90,6 +109,35 @@ async function webpack(
? require.resolve('@storybook/mdx1-csf/loader')
: require.resolve('@storybook/mdx2-csf/loader');

// Use the resolvedReact preset to alias react and react-dom to either the users version or the version shipped with addon-docs
const { react, reactDom, mdx } = await getResolvedReact(options);

let alias;
if (Array.isArray(webpackConfig.resolve?.alias)) {
alias = [...webpackConfig.resolve?.alias];
alias.push(
{
name: 'react',
alias: react,
},
{
name: 'react-dom',
alias: reactDom,
},
{
name: '@mdx-js/react',
alias: mdx,
}
);
} else {
alias = {
...webpackConfig.resolve?.alias,
react,
'react-dom': reactDom,
'@mdx-js/react': mdx,
};
}

const result = {
...webpackConfig,
plugins: [
Expand All @@ -99,7 +147,10 @@ async function webpack(
? [(await import('@storybook/csf-plugin')).webpack(csfPluginOptions)]
: []),
],

resolve: {
...webpackConfig.resolve,
alias,
},
module: {
...module,
rules: [
Expand Down Expand Up @@ -179,6 +230,25 @@ export const viteFinal = async (config: any, options: Options) => {
const { plugins = [] } = config;
const { mdxPlugin } = await import('./plugins/mdx-plugin');

// Use the resolvedReact preset to alias react and react-dom to either the users version or the version shipped with addon-docs
const { react, reactDom } = await getResolvedReact(options);

const reactAliasPlugin = {
name: 'storybook:react-alias',
enforce: 'pre',
config: () => ({
resolve: {
alias: {
react,
'react-dom': reactDom,
},
},
}),
};

// add alias plugin early to ensure any other plugins that also add the aliases will override this
// eg. the preact vite plugin adds its own aliases
plugins.unshift(reactAliasPlugin);
plugins.push(mdxPlugin(options));

return config;
Expand All @@ -192,7 +262,18 @@ const webpackX = webpack as any;
const indexersX = indexers as any;
const docsX = docs as any;

ensureReactPeerDeps();
/**
* If the user has not installed react explicitly in their project,
* the resolvedReact preset will not be set.
* We then set it here in addon-docs to use addon-docs's react version that always exists.
* This is just a fallback that never overrides the existing preset,
* but ensures that there is always a resolved react.
*/
export const resolvedReact = async (existing: any) => ({
react: existing?.react ?? dirname(require.resolve('react/package.json')),
reactDom: existing?.reactDom ?? dirname(require.resolve('react-dom/package.json')),
mdx: existing?.mdx ?? dirname(require.resolve('@mdx-js/react/package.json')),
});

const optimizeViteDeps = [
'@mdx-js/react',
Expand Down
13 changes: 13 additions & 0 deletions code/addons/docs/template/stories/docs2/ResolvedReact.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { version as reactVersion } from 'react';
import { version as reactDomVersion } from 'react-dom';
import { ResolvedReactVersion } from './ResolvedReactVersion';

## In MDX

<code>react</code>: <code data-testid="mdx-react">{reactVersion}</code>

<code>react-dom</code>: <code data-testid="mdx-react-dom">{reactDomVersion}</code>

## In `ResolvedReactVersion` component

<ResolvedReactVersion />
15 changes: 15 additions & 0 deletions code/addons/docs/template/stories/docs2/ResolvedReactVersion.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React, { version as reactVersion } from 'react';
import { version as reactDomVersion } from 'react-dom';

export const ResolvedReactVersion = () => {
return (
<>
<p>
<code>react</code>: <code data-testid="component-react">{reactVersion}</code>
</p>
<p>
<code>react-dom</code>: <code data-testid="component-react-dom">{reactDomVersion}</code>
</p>
</>
);
};
4 changes: 0 additions & 4 deletions code/addons/essentials/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@
"devDependencies": {
"typescript": "^5.3.2"
},
"peerDependencies": {
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
},
"publishConfig": {
"access": "public"
},
Expand Down
27 changes: 27 additions & 0 deletions code/e2e-tests/addon-docs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,31 @@ test.describe('addon-docs', () => {
await expect(stories.nth(1)).toHaveText('Basic');
await expect(stories.last()).toHaveText('Another');
});

test('should resolve react to the correct version', async ({ page }) => {
const sbPage = new SbPage(page);
await sbPage.navigateToUnattachedDocs('addons/docs/docs2', 'ResolvedReact');
const root = sbPage.previewRoot();

let expectedReactVersion = /^18/;
if (
templateName.includes('preact') ||
templateName.includes('react-webpack/17') ||
templateName.includes('react-vite/17')
) {
expectedReactVersion = /^17/;
} else if (templateName.includes('react16')) {
expectedReactVersion = /^16/;
}

const mdxReactVersion = await root.getByTestId('mdx-react');
const mdxReactDomVersion = await root.getByTestId('mdx-react-dom');
const componentReactVersion = await root.getByTestId('component-react');
const componentReactDomVersion = await root.getByTestId('component-react-dom');

await expect(mdxReactVersion).toHaveText(expectedReactVersion);
await expect(mdxReactDomVersion).toHaveText(expectedReactVersion);
await expect(componentReactVersion).toHaveText(expectedReactVersion);
await expect(componentReactDomVersion).toHaveText(expectedReactVersion);
});
});