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

Fix SSR in external docs via node exports #20083

Merged
merged 7 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test-results
/sandbox
/bench
.verdaccio-cache
.next

# Yarn stuff
/**/.yarn/*
Expand Down
1 change: 1 addition & 0 deletions code/addons/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"license": "MIT",
"exports": {
".": {
"node": "./dist/index.js",
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
Expand Down
1 change: 1 addition & 0 deletions code/addons/docs/preview.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// eslint-disable-next-line import/export
export * from './dist/preview';
17 changes: 9 additions & 8 deletions code/addons/docs/src/DocsRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import ReactDOM from 'react-dom';
import type { Renderer, Parameters, DocsContextProps, DocsRenderFunction } from '@storybook/types';
import { Docs, CodeOrSourceMdx, AnchorMdx, HeadersMdx } from '@storybook/blocks';
import { MDXProvider } from '@mdx-js/react';
Copy link
Member Author

Choose a reason for hiding this comment

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

This is an ESM only package so must be imported async from CJS

Copy link
Member

@ndelangen ndelangen Dec 5, 2022

Choose a reason for hiding this comment

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

Is this a big package? would it be possible to have be bundled in by tsup?

You can still have react & react-dom be external if you do that. FYI

But there's probably some singleton module pattern in this for hooking up context? Or would that be coming from react? Probably.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm open to it if you want to discussion @ndelangen. Still it is not a big deal to do it the way I am doing it here.


// TS doesn't like that we export a component with types that it doesn't know about (TS4203)
export const defaultComponents: Record<string, any> = {
Expand Down Expand Up @@ -31,13 +30,15 @@ export class DocsRenderer<TRenderer extends Renderer> {
...docsParameter?.components,
};

ReactDOM.render(
<MDXProvider components={components}>
<Docs key={Math.random()} context={context} docsParameter={docsParameter} />
</MDXProvider>,
element,
callback
);
import('@mdx-js/react').then(({ MDXProvider }) => {
ReactDOM.render(
<MDXProvider components={components}>
<Docs key={Math.random()} context={context} docsParameter={docsParameter} />
</MDXProvider>,
element,
callback
);
});
};

this.unmount = (element: HTMLElement) => {
Expand Down
1 change: 1 addition & 0 deletions code/addons/essentials/src/docs/preview.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// eslint-disable-next-line import/export
export * from '@storybook/addon-docs/dist/preview';
1 change: 1 addition & 0 deletions code/lib/theming/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"sideEffects": false,
"exports": {
".": {
"node": "./dist/index.js",
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
Expand Down
1 change: 1 addition & 0 deletions code/ui/blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"sideEffects": false,
"exports": {
".": {
"node": "./dist/index.js",
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
Expand Down
20 changes: 10 additions & 10 deletions code/ui/components/src/Zoom/ZoomElement.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global MutationObserver */
import type { ReactElement, RefObject } from 'react';
import React, { useEffect, useRef, useState, useMemo, useCallback } from 'react';
import React, { useEffect, useRef, useState, useCallback } from 'react';
import { styled } from '@storybook/theming';
import { browserSupportsCssZoom } from './browserSupportsCssZoom';

Expand Down Expand Up @@ -29,20 +29,20 @@ const useMutationObserver = ({
options: MutationObserverInit;
callback: MutationCallback;
}): void => {
const observer = useMemo(
() =>
new MutationObserver((mutationRecord, mutationObserver) => {
callback(mutationRecord, mutationObserver);
}),
[callback]
);
const observer = useRef<MutationObserver>();

useEffect(() => {
if (!observer.current) {
observer.current = new MutationObserver((mutationRecord, mutationObserver) => {
callback(mutationRecord, mutationObserver);
});
}

if (element?.current) {
observer.observe(element.current, options);
observer.current.observe(element.current, options);
}

return () => observer.disconnect();
return () => observer.current.disconnect();
}, [element, observer, options]);
};

Expand Down
9 changes: 4 additions & 5 deletions test-storybooks/external-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"@storybook/addon-essentials": "7.0.0-alpha.43",
"@storybook/blocks": "7.0.0-alpha.43",
"@storybook/components": "7.0.0-alpha.43",
"@storybook/csf": "0.0.2--canary.53.4879818.0",
"@storybook/preview-web": "7.0.0-alpha.43",
"@storybook/react": "7.0.0-alpha.43",
"@storybook/react-webpack5": "7.0.0-alpha.43",
Expand Down Expand Up @@ -77,8 +76,8 @@
"@storybook/web-components-vite": "portal:../../code/frameworks/web-components-vite",
"@storybook/web-components-webpack5": "portal:../../code/frameworks/web-components-webpack5",
"@storybook/addons": "portal:../../code/lib/addons",
"@storybook/api": "portal:../../code/lib/api",
"@storybook/blocks": "portal:../../code/lib/blocks",
"@storybook/manager-api": "portal:../../code/lib/manager-api",
"@storybook/blocks": "portal:../../code/ui/blocks",
"@storybook/builder-manager": "portal:../../code/lib/builder-manager",
"@storybook/builder-vite": "portal:../../code/lib/builder-vite",
"@storybook/builder-webpack5": "portal:../../code/lib/builder-webpack5",
Expand All @@ -91,7 +90,7 @@
"@storybook/client-api": "portal:../../code/lib/client-api",
"@storybook/client-logger": "portal:../../code/lib/client-logger",
"@storybook/codemod": "portal:../../code/lib/codemod",
"@storybook/components": "portal:../../code/lib/components",
"@storybook/components": "portal:../../code/ui/components",
"@storybook/core-client": "portal:../../code/lib/core-client",
"@storybook/core-common": "portal:../../code/lib/core-common",
"@storybook/core-events": "portal:../../code/lib/core-events",
Expand All @@ -102,7 +101,7 @@
"@storybook/instrumenter": "portal:../../code/lib/instrumenter",
"@storybook/node-logger": "portal:../../code/lib/node-logger",
"@storybook/postinstall": "portal:../../code/lib/postinstall",
"@storybook/preview-web": "portal:../../code/lib/preview-web",
"@storybook/preview-api": "portal:../../code/lib/preview-api",
"@storybook/router": "portal:../../code/lib/router",
"@storybook/source-loader": "portal:../../code/lib/source-loader",
"@storybook/store": "portal:../../code/lib/store",
Expand Down
Loading