import ReactTechStack from 'dumi/dist/techStacks/react'; import fs from 'fs'; import sylvanas from 'sylvanas'; import { dependencies, devDependencies } from '../../package.json'; /** * extends dumi internal tech stack, for customize previewer props */ export class AntVReactTechStack extends ReactTechStack { generatePreviewerProps(...[props, opts]: any) { props.pkgDependencyList = { ...devDependencies, ...dependencies }; props.jsx ??= ''; if (opts.type === 'code-block') { props.jsx = opts?.entryPointCode ? sylvanas.parseText(opts.entryPointCode) : ''; } if (opts.type === 'external') { // try to find md file with the same name as the demo tsx file const locale = opts.mdAbsPath.match(/[a-z-]+\.([a-z-]+)\.md$/i)?.[1]; const mdPath = opts.fileAbsPath!.replace(/\.\w+$/, '.md'); const md = fs.existsSync(mdPath) ? fs.readFileSync(mdPath, 'utf-8') : ''; const codePath = opts.fileAbsPath!.replace(/\.\w+$/, '.tsx'); const code = fs.existsSync(codePath) ? fs.readFileSync(codePath, 'utf-8') : ''; props.jsx = sylvanas.parseText(code); if (md) { // extract description & css style from md file const blocks: Record = {}; const lines = md.split('\n'); let blockName = ''; let cacheList: string[] = []; // Get block name const getBlockName = (text: string) => { if (text.startsWith('## ')) { return text.replace('## ', '').trim(); } if (text.startsWith('```css') || text.startsWith('