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

Vue3: Add support for Global Apps install #23772

Merged
merged 14 commits into from
Aug 23, 2023

Conversation

chakAs3
Copy link
Contributor

@chakAs3 chakAs3 commented Aug 9, 2023

Closes #23844

What I did

How to test

Checklist

  • Make sure your changes are tested (stories and/or unit, integration, or end-to-end tests)
  • Make sure to add/update documentation regarding your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

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.

["cleanup", "BREAKING CHANGE", "feature request", "bug", "build", "documentation", "maintenance", "dependencies", "other"]

🦋 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>

@kasperpeulen kasperpeulen marked this pull request as draft August 14, 2023 07:29
@chakAs3 chakAs3 added the bug label Aug 21, 2023
@chakAs3 chakAs3 assigned chakAs3 and unassigned chakAs3 Aug 21, 2023
Copy link
Contributor

@kasperpeulen kasperpeulen left a comment

Choose a reason for hiding this comment

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

Can you fill in "What I did" and "How to test"? @chakAs3

module.hot.decline();
}
} catch (e) {
console.log(' e:', e);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to log this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not need to log but we need to wrap it in try catch. , it will break in vite ( it is webpack specific )

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, let's remove the console.log then


async function installGlobalPlugins(app: App<any>, storyContext: StoryContext<VueRenderer>) {
if (window.APPLY_PLUGINS_FUNC) {
await window.APPLY_PLUGINS_FUNC(app, storyContext);
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the difference with the setup function? Should this function be set by the user?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

using setup is not safe, in case your package manager allows 2 versions of the same package, @storybook/vue3 can't be a singleton, so calling setup. seems not working for users
using window. APPLY_PLUGINS_FUNC will insure the singleton.
it should be. awaited as well in case. registering the plugin is async

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can also solve this by putting this on the window:

const setupFunctions = new Set<(app: App, storyContext?: StoryContext<VueRenderer>) => void>();

I think it is better have one API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes exactly i'm working on that idea

@kasperpeulen kasperpeulen marked this pull request as draft August 21, 2023 13:41
@chakAs3 chakAs3 marked this pull request as ready for review August 21, 2023 14:07
@JReinhold JReinhold marked this pull request as draft August 21, 2023 14:11
@chakAs3 chakAs3 marked this pull request as ready for review August 22, 2023 05:24
Copy link
Contributor

@kasperpeulen kasperpeulen left a comment

Choose a reason for hiding this comment

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

Looks good! Some small stuff.

Comment on lines 8 to +12
// optimization: stop HMR propagation in webpack
if (typeof module !== 'undefined') module?.hot?.decline();
try {
if (module?.hot?.decline) {
module.hot.decline();
}
Copy link
Contributor

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:

if (typeof module !== 'undefined') module?.hot?.decline?.();

code/renderers/vue3/src/index.ts Outdated Show resolved Hide resolved
Comment on lines +21 to +22
globalThis.PLUGINS_SETUP_FUNCTIONS ??= new Set();
globalThis.PLUGINS_SETUP_FUNCTIONS.add(fn);
Copy link
Contributor

Choose a reason for hiding this comment

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

@ndelangen I thought you mentioned at some point that we prefer @storybook/global over globalThis but I don't know the reasons anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
declare var STORYBOOK_ENV:'type' in type.d.ts file
will be safely available on globalThis as global module

Comment on lines +29 to +30
if (globalThis && globalThis.PLUGINS_SETUP_FUNCTIONS)
await Promise.all([...globalThis.PLUGINS_SETUP_FUNCTIONS].map((fn) => fn(app, storyContext)));
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe Promise.allSettled as I think we don't want to short-circuit here?

const runSetupFunctions = async (
app: App,
storyContext: StoryContext<VueRenderer>
): Promise<any> => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Promise<void>

Copy link
Contributor

@kasperpeulen kasperpeulen left a comment

Choose a reason for hiding this comment

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

LGTM!

@kasperpeulen kasperpeulen merged commit fd9aa21 into next Aug 23, 2023
@kasperpeulen kasperpeulen deleted the chaks/vue3-global-app-install branch August 23, 2023 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: setup function imported from '@storybook/vue3' is not working
3 participants