Skip to content

Commit

Permalink
Merge pull request storybookjs#2 from eirslett/add-react-refresh-vite…
Browse files Browse the repository at this point in the history
…-plugin

Dynamic vite plugins import based on framework
  • Loading branch information
SasanFarrokh authored Apr 5, 2021
2 parents c1b7b04 + eb6c012 commit cbb9fdc
Show file tree
Hide file tree
Showing 44 changed files with 132 additions and 121 deletions.
4 changes: 2 additions & 2 deletions addons/a11y/src/a11yRunner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { document, window } from 'global';
import { document, window as globalWindow } from 'global';
import axe from 'axe-core';
import addons from '@storybook/addons';
import { EVENTS } from './constants';
Expand Down Expand Up @@ -66,7 +66,7 @@ const run = async (storyId: string) => {

/** Returns story parameters or default ones. */
const getParams = (storyId: string): A11yParameters => {
const { parameters } = window.__STORYBOOK_STORY_STORE__.fromId(storyId) || {};
const { parameters } = globalWindow.__STORYBOOK_STORY_STORE__.fromId(storyId) || {};
return (
parameters.a11y || {
config: {},
Expand Down
4 changes: 2 additions & 2 deletions addons/docs/src/blocks/DocsContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FunctionComponent, useEffect } from 'react';
import { document, window } from 'global';
import { document, window as globalWindow } from 'global';
import deprecate from 'util-deprecate';
import dedent from 'ts-dedent';
import { MDXProvider } from '@mdx-js/react';
Expand Down Expand Up @@ -46,7 +46,7 @@ export const DocsContainer: FunctionComponent<DocsContainerProps> = ({ context,
useEffect(() => {
let url;
try {
url = new URL(window.parent.location);
url = new URL(globalWindow.parent.location);
} catch (err) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions addons/google-analytics/src/register.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { window } from 'global';
import { window as globalWindow } from 'global';
import { addons } from '@storybook/addons';
import { STORY_CHANGED, STORY_ERRORED, STORY_MISSING } from '@storybook/core-events';

import ReactGA from 'react-ga';

addons.register('storybook/google-analytics', (api) => {
ReactGA.initialize(window.STORYBOOK_GA_ID, window.STORYBOOK_REACT_GA_OPTIONS);
ReactGA.initialize(globalWindow.STORYBOOK_GA_ID, globalWindow.STORYBOOK_REACT_GA_OPTIONS);

api.on(STORY_CHANGED, () => {
const { path } = api.getUrlState();
Expand Down
4 changes: 2 additions & 2 deletions app/angular/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

import './angular-polyfills';

window.STORYBOOK_ENV = 'angular';
globalWindow.STORYBOOK_ENV = 'angular';
4 changes: 2 additions & 2 deletions app/aurelia/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

window.STORYBOOK_ENV = 'aurelia';
globalWindow.STORYBOOK_ENV = 'aurelia';
6 changes: 3 additions & 3 deletions app/ember/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

window.STORYBOOK_NAME = process.env.STORYBOOK_NAME;
window.STORYBOOK_ENV = 'ember';
globalWindow.STORYBOOK_NAME = process.env.STORYBOOK_NAME;
globalWindow.STORYBOOK_ENV = 'ember';
6 changes: 3 additions & 3 deletions app/ember/src/client/preview/render.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { window, document } from 'global';
import { window as globalWindow, document } from 'global';
import dedent from 'ts-dedent';
import { RenderContext, ElementArgs, OptionsArgs } from './types';

declare let Ember: any;

const rootEl = document.getElementById('root');

const config = window.require(`${window.STORYBOOK_NAME}/config/environment`);
const app = window.require(`${window.STORYBOOK_NAME}/app`).default.create({
const config = globalWindow.require(`${globalWindow.STORYBOOK_NAME}/config/environment`);
const app = globalWindow.require(`${globalWindow.STORYBOOK_NAME}/app`).default.create({
autoboot: false,
rootElement: rootEl,
...config.APP,
Expand Down
4 changes: 2 additions & 2 deletions app/html/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

window.STORYBOOK_ENV = 'HTML';
globalWindow.STORYBOOK_ENV = 'HTML';
4 changes: 2 additions & 2 deletions app/marionette/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

window.STORYBOOK_ENV = 'marionette';
globalWindow.STORYBOOK_ENV = 'marionette';
4 changes: 2 additions & 2 deletions app/marko/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

window.STORYBOOK_ENV = 'marko';
globalWindow.STORYBOOK_ENV = 'marko';
4 changes: 2 additions & 2 deletions app/mithril/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

window.STORYBOOK_ENV = 'mithril';
globalWindow.STORYBOOK_ENV = 'mithril';
6 changes: 3 additions & 3 deletions app/preact/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

if (window) {
window.STORYBOOK_ENV = 'preact';
if (globalWindow) {
globalWindow.STORYBOOK_ENV = 'preact';
}
6 changes: 3 additions & 3 deletions app/rax/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

if (window) {
window.STORYBOOK_ENV = 'rax';
if (globalWindow) {
globalWindow.STORYBOOK_ENV = 'rax';
}
6 changes: 3 additions & 3 deletions app/react/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

if (window) {
window.STORYBOOK_ENV = 'react';
if (globalWindow) {
globalWindow.STORYBOOK_ENV = 'react';
}
4 changes: 2 additions & 2 deletions app/riot/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

window.STORYBOOK_ENV = 'riot';
globalWindow.STORYBOOK_ENV = 'riot';
4 changes: 2 additions & 2 deletions app/server/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

window.STORYBOOK_ENV = 'SERVER';
globalWindow.STORYBOOK_ENV = 'SERVER';
4 changes: 2 additions & 2 deletions app/svelte/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

window.STORYBOOK_ENV = 'svelte';
globalWindow.STORYBOOK_ENV = 'svelte';
6 changes: 3 additions & 3 deletions app/vue/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

window.STORYBOOK_REACT_CLASSES = {};
window.STORYBOOK_ENV = 'vue';
globalWindow.STORYBOOK_REACT_CLASSES = {};
globalWindow.STORYBOOK_ENV = 'vue';
6 changes: 3 additions & 3 deletions app/vue3/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

window.STORYBOOK_REACT_CLASSES = {};
window.STORYBOOK_ENV = 'vue3';
globalWindow.STORYBOOK_REACT_CLASSES = {};
globalWindow.STORYBOOK_ENV = 'vue3';
4 changes: 2 additions & 2 deletions app/web-components/src/client/preview/globals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { window } from 'global';
import { window as globalWindow } from 'global';

window.STORYBOOK_ENV = 'web-components';
globalWindow.STORYBOOK_ENV = 'web-components';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
```ts
// vue/src/client/preview/globals.ts

import { window } from 'global';
window.STORYBOOK_ENV = 'vue';
import { window as globalWindow } from 'global';
globalWindow.STORYBOOK_ENV = 'vue';
```
6 changes: 3 additions & 3 deletions examples/official-storybook/stories/addon-actions.stories.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable react/prop-types */
import { window, File } from 'global';
import { window as globalWindow, File } from 'global';
import React, { Fragment } from 'react';
import { action, actions, configureActions } from '@storybook/addon-actions';
import { Form } from '@storybook/components';
Expand Down Expand Up @@ -122,7 +122,7 @@ export const AllTypes = () => {
null,
undefined,
{ foo: 'bar' },
window
globalWindow
)
}
>
Expand All @@ -143,7 +143,7 @@ export const AllTypes = () => {
<Button onClick={() => action('Symbol')(Symbol('A_SYMBOL'))}>Symbol</Button>
<Button onClick={action('SyntheticMouseEvent')}>SyntheticEvent</Button>
<Button onClick={() => action('undefined')(undefined)}>undefined</Button>
<Button onClick={() => action('window')(window)}>Window</Button>
<Button onClick={() => action('window')(globalWindow)}>Window</Button>
</Fragment>
);
};
Expand Down
10 changes: 5 additions & 5 deletions lib/addons/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import window from 'global';
import { window as globalWindow } from 'global';
import { logger } from '@storybook/client-logger';
import {
FORCE_RE_RENDER,
Expand Down Expand Up @@ -140,10 +140,10 @@ const hookify = (fn: AbstractFunction) => (...args: any[]) => {
}
hooks.nextHookIndex = 0;

const prevContext = window.STORYBOOK_HOOKS_CONTEXT;
window.STORYBOOK_HOOKS_CONTEXT = hooks;
const prevContext = globalWindow.STORYBOOK_HOOKS_CONTEXT;
globalWindow.STORYBOOK_HOOKS_CONTEXT = hooks;
const result = fn(...args);
window.STORYBOOK_HOOKS_CONTEXT = prevContext;
globalWindow.STORYBOOK_HOOKS_CONTEXT = prevContext;

if (hooks.currentPhase === 'UPDATE' && hooks.getNextHook() != null) {
throw new Error(
Expand Down Expand Up @@ -196,7 +196,7 @@ const invalidHooksError = () =>
new Error('Storybook preview hooks can only be called inside decorators and story functions.');

function getHooksContextOrNull(): HooksContext | null {
return window.STORYBOOK_HOOKS_CONTEXT || null;
return globalWindow.STORYBOOK_HOOKS_CONTEXT || null;
}

function getHooksContextOrThrow(): HooksContext {
Expand Down
8 changes: 4 additions & 4 deletions lib/api/src/modules/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NAVIGATE_URL, STORY_ARGS_UPDATED, SET_CURRENT_STORY } from '@storybook/
import { queryFromLocation, navigate as queryNavigate, buildArgsParam } from '@storybook/router';
import { toId, sanitize } from '@storybook/csf';
import deepEqual from 'fast-deep-equal';
import { window } from 'global';
import { window as globalWindow } from 'global';

import { ModuleArgs, ModuleFn } from '../index';
import { PanelPositions } from './layout';
Expand Down Expand Up @@ -163,9 +163,9 @@ export const init: ModuleFn = ({ store, navigate, state, provider, fullAPI, ...r

let handleOrId: any;
fullAPI.on(STORY_ARGS_UPDATED, ({ args }) => {
if ('requestIdleCallback' in window) {
if (handleOrId) window.cancelIdleCallback(handleOrId);
handleOrId = window.requestIdleCallback(() => updateArgsParam(args), { timeout: 1000 });
if ('requestIdleCallback' in globalWindow) {
if (handleOrId) globalWindow.cancelIdleCallback(handleOrId);
handleOrId = globalWindow.requestIdleCallback(() => updateArgsParam(args), { timeout: 1000 });
} else {
if (handleOrId) clearTimeout(handleOrId);
setTimeout(updateArgsParam, 100, args);
Expand Down
1 change: 1 addition & 0 deletions lib/builder-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@storybook/core-common": "*",
"@types/node": "^14.0.10",
"@vitejs/plugin-vue": "^1.2.1",
"@vitejs/plugin-react-refresh": "^1.3.2",
"ejs": "^3.1.6",
"glob-promise": "^4.1.0",
"vite": "^2.1.5"
Expand Down
2 changes: 1 addition & 1 deletion lib/builder-vite/src/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const iframeMiddleware = async ({
return;
}
res.send(
await server.transformIndexHtml('/iframe.html', template({
await server.transformIndexHtml(req.url, template({
options: {},
files: {
css: [],
Expand Down
18 changes: 14 additions & 4 deletions lib/builder-vite/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { Server } from "http";
import path from 'path';
import { createServer, ViteDevServer } from 'vite';
import vue from '@vitejs/plugin-vue';
import { createServer, ViteDevServer } from "vite";
import type { Plugin } from 'vite';
import type { CLIOptions, LoadOptions } from "@storybook/core-common";
import { optimizeDeps } from './utils/optimizeDeps';
import { mockCoreJs } from './utils/mockCoreJs';

export async function createViteServer({ configDir, port }: LoadOptions & CLIOptions, devServer: Server): Promise<ViteDevServer> {
export async function createViteServer({ configDir, port, framework }: LoadOptions & CLIOptions, devServer: Server): Promise<ViteDevServer> {
// Lazy-load the proper Vite plugins, depending on which framework we use
const plugins: Plugin[] = [mockCoreJs()];
if (framework === 'vue') {
const vuePlugin = await import('@vitejs/plugin-vue').then(plugin => plugin.default());
plugins.push(vuePlugin);
} else if (framework === 'react') {
const reactPlugin = await import('@vitejs/plugin-react-refresh').then(plugin => plugin.default());
plugins.push(reactPlugin);
}

const server = await createServer({
configFile: false,
root: path.resolve(configDir, '..'),
Expand All @@ -23,7 +33,7 @@ export async function createViteServer({ configDir, port }: LoadOptions & CLIOpt
},
},
optimizeDeps,
plugins: [mockCoreJs(), vue()],
plugins,
});

return server;
Expand Down
16 changes: 8 additions & 8 deletions lib/channel-postmessage/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { window, document, location } from 'global';
import { window as globalWindow, document, location } from 'global';
import * as EVENTS from '@storybook/core-events';
import Channel, { ChannelEvent, ChannelHandler } from '@storybook/channels';
import { logger, pretty } from '@storybook/client-logger';
Expand Down Expand Up @@ -31,7 +31,7 @@ export class PostmsgTransport {
constructor(private readonly config: Config) {
this.buffer = [];
this.handler = null;
window.addEventListener('message', this.handleEvent.bind(this), false);
globalWindow.addEventListener('message', this.handleEvent.bind(this), false);

// Check whether the config.page parameter has a valid value
if (config.page !== 'manager' && config.page !== 'preview') {
Expand Down Expand Up @@ -129,8 +129,8 @@ export class PostmsgTransport {

return list.length ? list : this.getCurrentFrames();
}
if (window && window.parent && window.parent !== window) {
return [window.parent];
if (globalWindow && globalWindow.parent && globalWindow.parent !== globalWindow) {
return [globalWindow.parent];
}

return [];
Expand All @@ -143,8 +143,8 @@ export class PostmsgTransport {
];
return list.map((e) => e.contentWindow);
}
if (window && window.parent) {
return [window.parent];
if (globalWindow && globalWindow.parent) {
return [globalWindow.parent];
}

return [];
Expand All @@ -155,8 +155,8 @@ export class PostmsgTransport {
const list: HTMLIFrameElement[] = [...document.querySelectorAll('#storybook-preview-iframe')];
return list.map((e) => e.contentWindow);
}
if (window && window.parent) {
return [window.parent];
if (globalWindow && globalWindow.parent) {
return [globalWindow.parent];
}

return [];
Expand Down
4 changes: 2 additions & 2 deletions lib/components/src/Zoom/Zoom.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import window from 'global';
import { window as globalWindow } from 'global';
import { ZoomElement as Element } from './ZoomElement';
import { ZoomIFrame as IFrame } from './ZoomIFrame';

export const browserSupportsCssZoom = (): boolean => {
try {
return window.document.implementation.createHTMLDocument('').body.style.zoom !== undefined;
return globalWindow.document.implementation.createHTMLDocument('').body.style.zoom !== undefined;
} catch (error) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/components/src/Zoom/browserSupportsCssZoom.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import window from 'global';
import { window as globalWindow } from 'global';

export function browserSupportsCssZoom(): boolean {
try {
return window.document.implementation.createHTMLDocument('').body.style.zoom !== undefined;
return globalWindow.document.implementation.createHTMLDocument('').body.style.zoom !== undefined;
} catch (error) {
return false;
}
Expand Down
Loading

0 comments on commit cbb9fdc

Please sign in to comment.