storybook/code/addons/docs/jest-transform-mdx.js
2022-10-18 00:27:55 +08:00

23 lines
689 B
JavaScript

const path = require('path');
const { ScriptTransformer } = require('@jest/transform');
const { dedent } = require('ts-dedent');
const { compileAsync } = require('@storybook/mdx2-csf');
module.exports = {
async processAsync(src, filename, config, { instrument }) {
const code = await compileAsync(src, { skipCsf: false });
const result = dedent`
/* @jsx mdx */
import React from 'react'
import { mdx } from '@mdx-js/react'
${code}
`;
const extension = path.extname(filename);
const jsFileName = `${filename.slice(0, -extension.length)}.js`;
return new ScriptTransformer(config).transformSource(jsFileName, result, instrument);
},
};