vite-plugin-storybook-nextjs
Version:
This is a Vite plugin that allows you to use Next.js features in Vite. It is the basis for `@storybook/experimental-nextjs-vite` and should be used when running portable stories in Vitest.
54 lines (51 loc) • 1.87 kB
JavaScript
import Loadable from 'next/dist/shared/lib/loadable.shared-runtime.js';
import React from 'react';
// src/plugins/next-mocks/alias/dynamic/index.tsx
function convertModule(mod) {
return { default: mod?.default || mod };
}
function noSSR() {
throw new Error("noSSR is not implemented in Storybook");
}
function dynamic(dynamicOptions, options) {
const loadableFn = Loadable;
let loadableOptions = {
// A loading component is not required, so we default it
loading: ({ error, isLoading, pastDelay }) => {
if (!pastDelay) return null;
if (process.env.NODE_ENV !== "production") {
if (isLoading) {
return null;
}
if (error) {
return /* @__PURE__ */ React.createElement("p", null, error.message, /* @__PURE__ */ React.createElement("br", null), error.stack);
}
}
return null;
}
};
if (dynamicOptions instanceof Promise) {
loadableOptions.loader = () => dynamicOptions;
} else if (typeof dynamicOptions === "function") {
loadableOptions.loader = dynamicOptions;
} else if (typeof dynamicOptions === "object") {
loadableOptions = { ...loadableOptions, ...dynamicOptions };
}
loadableOptions = { ...loadableOptions, ...options };
const loaderFn = loadableOptions.loader;
const loader = () => loaderFn != null ? loaderFn().then(convertModule) : Promise.resolve(convertModule(() => null));
if (loadableOptions.loadableGenerated) {
loadableOptions = {
...loadableOptions,
...loadableOptions.loadableGenerated
};
delete loadableOptions.loadableGenerated;
}
if (typeof loadableOptions.ssr === "boolean" && !loadableOptions.ssr) {
delete loadableOptions.ssr;
delete loadableOptions.webpack;
delete loadableOptions.modules;
}
return loadableFn({ ...loadableOptions, loader });
}
export { dynamic as default, noSSR };