UNPKG

storybook-framework-cedarjs

Version:
60 lines (59 loc) 1.39 kB
import { extname } from "node:path"; import resolve from "resolve"; class ReactDocgenResolveError extends Error { // the magic string that react-docgen uses to check if a module is ignored code = "MODULE_NOT_FOUND"; constructor(filename) { super(`'${filename}' was ignored by react-docgen.`); } } const RESOLVE_EXTENSIONS = [ ".js", ".cts", ".mts", ".ctsx", ".mtsx", ".ts", ".tsx", ".mjs", ".cjs", ".mts", ".cts", ".jsx" ]; function defaultLookupModule(filename, basedir) { const resolveOptions = { basedir, extensions: RESOLVE_EXTENSIONS, // we do not need to check core modules as we cannot import them anyway includeCoreModules: false }; try { return resolve.sync(filename, resolveOptions); } catch (error) { const ext = extname(filename); let 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: [] }); } } export { RESOLVE_EXTENSIONS, ReactDocgenResolveError, defaultLookupModule };