storybook-framework-cedarjs
Version:
Storybook for CedarJS
59 lines (58 loc) • 2.5 kB
JavaScript
import { createRequire } from "node:module";
import path from "node:path";
import { mergeConfig } from "vite";
import { nodePolyfills } from "vite-plugin-node-polyfills";
import { getPaths } from "@cedarjs/project-config";
import { autoImports } from "./plugins/auto-imports.js";
import { mockAuth } from "./plugins/mock-auth.js";
import { mockRouter } from "./plugins/mock-router.js";
import { reactDocgen } from "./plugins/react-docgen.js";
function getAbsolutePath(input) {
const createdRequire = createRequire(import.meta.url);
return path.dirname(
createdRequire.resolve(path.join(input, "package.json"), {
paths: [getPaths().base]
})
);
}
const core = {
builder: getAbsolutePath("@storybook/builder-vite"),
renderer: getAbsolutePath("@storybook/react")
};
const previewAnnotations = (entries = []) => {
const createdRequire = createRequire(import.meta.url);
return [...entries, createdRequire.resolve("./preview.js")];
};
const cedarProjectPaths = getPaths();
const viteFinal = async (config) => {
const { plugins = [] } = config;
plugins.unshift(reactDocgen());
plugins.unshift(nodePolyfills());
return mergeConfig(config, {
// This is necessary as it otherwise just points to the `web` directory,
// but it needs to point to `web/src`
root: cedarProjectPaths.web.src,
plugins: [mockRouter(), mockAuth(), autoImports],
resolve: {
alias: {
"~__REDWOOD__USER_ROUTES_FOR_MOCK": cedarProjectPaths.web.routes,
"~__REDWOOD__USER_WEB_SRC": cedarProjectPaths.web.src
}
},
optimizeDeps: {
// Without this, on first run, Vite throws: `The file does not exist at
// "{project path}/web/node_modules/.cache/sb-vite/deps/DocsRenderer-NNNQARDV-DEXCJJZJ.js?v=c640a8fa"
// which is in the optimize deps directory.`
// This refers to @storybook/addon-docs, which is included as part of @storybook/addon-essentials.
// the docs addon then includes itself here: https://github.com/storybookjs/storybook/blob/a496ec48c708eed753a5251d55fa07947a869e62/code/addons/docs/src/preset.ts#L198C3-L198C27
// which I believe gets included by the builder here: https://github.com/storybookjs/storybook/blob/a496ec48c708eed753a5251d55fa07947a869e62/code/builders/builder-vite/src/optimizeDeps.ts#L117
// TODO: Figure out why this error is being thrown so that this can be removed.
exclude: ["@storybook/addon-docs"]
}
});
};
export {
core,
previewAnnotations,
viteFinal
};