UNPKG

storybook-react-rsbuild

Version:

Storybook for React and Rsbuild: Develop React components in isolation with Hot Reloading.

178 lines (172 loc) 7 kB
import CJS_COMPAT_NODE_URL_bce9f5a39a886437 from 'node:url'; import CJS_COMPAT_NODE_PATH_bce9f5a39a886437 from 'node:path'; import CJS_COMPAT_NODE_MODULE_bce9f5a39a886437 from "node:module"; var __filename = CJS_COMPAT_NODE_URL_bce9f5a39a886437.fileURLToPath(import.meta.url); var __dirname = CJS_COMPAT_NODE_PATH_bce9f5a39a886437.dirname(__filename); var require = CJS_COMPAT_NODE_MODULE_bce9f5a39a886437.createRequire(import.meta.url); // ------------------------------------------------------------ // end of CJS compatibility banner, injected by Storybook's esbuild configuration // ------------------------------------------------------------ import "../_node-chunks/chunk-HLWUFMVK.js"; // src/loaders/react-docgen-loader.ts import findUp from "find-up"; import MagicString from "magic-string"; import { builtinHandlers as docgenHandlers, builtinResolvers as docgenResolver, ERROR_CODES, makeFsImporter, parse, utils } from "react-docgen"; import { logger } from "storybook/internal/node-logger"; import * as TsconfigPaths from "tsconfig-paths"; // src/loaders/docgen-resolver.ts import { extname } from "node:path"; import resolve from "resolve"; var ReactDocgenResolveError = class extends Error { constructor(filename) { super(`'${filename}' was ignored by react-docgen.`); // the magic string that react-docgen uses to check if a module is ignored this.code = "MODULE_NOT_FOUND"; } }, RESOLVE_EXTENSIONS = [ ".js", ".cts", // These were originally not in the code, I added them ".mts", // These were originally not in the code, I added them ".ctsx", // These were originally not in the code, I added them ".mtsx", // These were originally not in the code, I added them ".ts", ".tsx", ".mjs", ".cjs", ".mts", ".cts", ".jsx" ]; function defaultLookupModule(filename, basedir) { let resolveOptions = { basedir, extensions: RESOLVE_EXTENSIONS, // we do not need to check core modules as we cannot import them anyway includeCoreModules: !1 }; try { return resolve.sync(filename, resolveOptions); } catch (error) { let ext = extname(filename), newFilename; switch (ext) { case ".js": case ".mjs": case ".cjs": newFilename = `${filename.slice(0, -2)}ts`; break; case ".jsx": newFilename = `${filename.slice(0, -3)}tsx`; break; default: throw error; } return resolve.sync(newFilename, { ...resolveOptions, // we already know that there is an extension at this point, so no need to check other extensions extensions: [] }); } } // src/loaders/react-docgen-loader.ts var { getNameOrValue, isReactForwardRefCall } = utils, actualNameHandler = function(documentation, componentDefinition) { if (documentation.set("definedInFile", componentDefinition.hub.file.opts.filename), (componentDefinition.isClassDeclaration() || componentDefinition.isFunctionDeclaration()) && componentDefinition.has("id")) documentation.set( "actualName", getNameOrValue(componentDefinition.get("id")) ); else if (componentDefinition.isArrowFunctionExpression() || componentDefinition.isFunctionExpression() || isReactForwardRefCall(componentDefinition)) { let currentPath = componentDefinition; for (; currentPath.parentPath; ) { if (currentPath.parentPath.isVariableDeclarator()) { documentation.set( "actualName", getNameOrValue(currentPath.parentPath.get("id")) ); return; } if (currentPath.parentPath.isAssignmentExpression()) { let leftPath = currentPath.parentPath.get("left"); if (leftPath.isIdentifier() || leftPath.isLiteral()) { documentation.set("actualName", getNameOrValue(leftPath)); return; } } currentPath = currentPath.parentPath; } documentation.set("actualName", ""); } }, defaultHandlers = Object.values(docgenHandlers).map((handler) => handler), defaultResolver = new docgenResolver.FindExportedDefinitionsResolver(), handlers = [...defaultHandlers, actualNameHandler], tsconfigPathsInitializeStatus = "uninitialized", resolveTsconfigPathsInitialingPromise, tsconfigPathsInitialingPromise = new Promise((resolve2) => { resolveTsconfigPathsInitialingPromise = resolve2; }), finishInitialization = () => { resolveTsconfigPathsInitialingPromise(), tsconfigPathsInitializeStatus = "initialized"; }, matchPath; async function reactDocgenLoader(source, map) { let callback = this.async(), options = this.getOptions() || {}, { debug = !1 } = options; if (tsconfigPathsInitializeStatus === "uninitialized") { tsconfigPathsInitializeStatus = "initializing"; let tsconfigPath = await findUp("tsconfig.json", { cwd: process.cwd() }), tsconfig = TsconfigPaths.loadConfig(tsconfigPath); tsconfig.resultType === "success" && (logger.info("Using tsconfig paths for react-docgen"), matchPath = TsconfigPaths.createMatchPath( tsconfig.absoluteBaseUrl, tsconfig.paths, ["browser", "module", "main"] )), finishInitialization(); } tsconfigPathsInitializeStatus === "initializing" && await tsconfigPathsInitialingPromise; try { let docgenResults = parse(source, { filename: this.resourcePath, resolver: defaultResolver, handlers, importer: getReactDocgenImporter(matchPath), babelOptions: { babelrc: !1, configFile: !1 } }), magicString = new MagicString(source); for (let info of docgenResults) { let { actualName, definedInFile, ...docgenInfo } = info; if (actualName && definedInFile === this.resourcePath) { let docNode = JSON.stringify(docgenInfo); magicString.append(`;${actualName}.__docgenInfo=${docNode}`); } } callback( null, magicString.toString(), map ?? magicString.generateMap({ hires: !0, source: this.resourcePath, includeContent: !0 }) ); } catch (error) { error.code === ERROR_CODES.MISSING_DEFINITION || (debug ? (logger.warn( `Failed to parse ${this.resourcePath} with react-docgen. Please use the below error message and the content of the file which causes the error to report the issue to the maintainers of react-docgen. https://github.com/reactjs/react-docgen` ), logger.error(error)) : logger.warn( `Failed to parse ${this.resourcePath} with react-docgen. Rerun Storybook with --loglevel=debug to get more info.` )), callback(null, source); } } function getReactDocgenImporter(matchingPath) { return makeFsImporter((filename, basedir) => { let mappedFilenameByPaths = matchingPath && matchingPath(filename) || filename, result = defaultLookupModule(mappedFilenameByPaths, basedir); if (RESOLVE_EXTENSIONS.find((ext) => result.endsWith(ext))) return result; throw new ReactDocgenResolveError(filename); }); } export { reactDocgenLoader as default, getReactDocgenImporter };