-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Vue3: Add support for Global Apps install #23772
Changes from 13 commits
0a5ee9e
466850a
e45779e
d0a16cc
1f23839
fe71e39
886dfdf
69b9155
6dd671e
f3ae06f
be7d7a0
33d067c
07de8db
6d53c68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { global } from '@storybook/global'; | ||
import type { App } from 'vue'; | ||
import type { StoryContext } from './public-types'; | ||
|
||
const { window: globalWindow } = global; | ||
|
||
globalWindow.STORYBOOK_ENV = 'vue3'; | ||
globalWindow.PLUGINS_SETUP_FUNCTIONS ||= new Set< | ||
(app: App<any>, context: StoryContext) => unknown | ||
>(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/* eslint-disable local-rules/no-uncategorized-errors */ | ||
/* eslint-disable no-param-reassign */ | ||
import type { App } from 'vue'; | ||
import { createApp, h, reactive, isVNode, isReactive } from 'vue'; | ||
|
@@ -16,18 +17,17 @@ export const render: ArgsStoryFn<VueRenderer> = (props, context) => { | |
return () => h(Component, props, getSlots(props, context)); | ||
}; | ||
|
||
// set of setup functions that will be called when story is created | ||
const setupFunctions = new Set<(app: App, storyContext?: StoryContext<VueRenderer>) => void>(); | ||
/** add a setup function to set that will be call when story is created a d | ||
* | ||
* @param fn | ||
*/ | ||
export const setup = (fn: (app: App, storyContext?: StoryContext<VueRenderer>) => void) => { | ||
setupFunctions.add(fn); | ||
export const setup = (fn: (app: App, storyContext?: StoryContext<VueRenderer>) => unknown) => { | ||
globalThis.PLUGINS_SETUP_FUNCTIONS ??= new Set(); | ||
globalThis.PLUGINS_SETUP_FUNCTIONS.add(fn); | ||
Comment on lines
+21
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ndelangen I thought you mentioned at some point that we prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as i know globalThis was introduced to give access to global scope regardless the system Node/Browser, it is consistency and standard rather than using window / global There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and that what @storybook/global provides the global scope so if we do |
||
}; | ||
|
||
const runSetupFunctions = (app: App, storyContext: StoryContext<VueRenderer>) => { | ||
setupFunctions.forEach((fn) => fn(app, storyContext)); | ||
const runSetupFunctions = async ( | ||
app: App, | ||
storyContext: StoryContext<VueRenderer> | ||
): Promise<any> => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (globalThis && globalThis.PLUGINS_SETUP_FUNCTIONS) | ||
await Promise.all([...globalThis.PLUGINS_SETUP_FUNCTIONS].map((fn) => fn(app, storyContext))); | ||
Comment on lines
+29
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
}; | ||
|
||
const map = new Map< | ||
|
@@ -38,7 +38,7 @@ const map = new Map< | |
} | ||
>(); | ||
|
||
export function renderToCanvas( | ||
export async function renderToCanvas( | ||
{ storyFn, forceRemount, showMain, showException, storyContext, id }: RenderContext<VueRenderer>, | ||
canvasElement: VueRenderer['canvasElement'] | ||
) { | ||
|
@@ -80,7 +80,7 @@ export function renderToCanvas( | |
}); | ||
|
||
vueApp.config.errorHandler = (e: unknown) => showException(e as Error); | ||
runSetupFunctions(vueApp, storyContext); | ||
await runSetupFunctions(vueApp, storyContext); | ||
vueApp.mount(canvasElement); | ||
|
||
showMain(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
declare var STORYBOOK_ENV: 'vue3'; | ||
declare var PLUGINS_SETUP_FUNCTIONS = new Set<(app, context) => unknown>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You still need the
typeof module !== 'undefined'
check right?Maybe: