Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman committed Jan 23, 2023
1 parent 47a53e5 commit 89e7fd1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 703 deletions.
7 changes: 0 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@
"@mdx-js/react": "^1.6.22"
},
"devDependencies": {
"@babel/cli": "^7.12.1",
"@babel/core": "^7.12.3",
"@babel/generator": "^7.12.11",
"@babel/parser": "^7.12.11",
"@babel/preset-env": "^7.12.1",
"@babel/preset-react": "^7.12.5",
"@babel/preset-typescript": "^7.13.0",
"@babel/template": "^7.14.5",
"@babel/traverse": "^7.12.11",
"@babel/types": "^7.14.8",
"@jest/types": "^27.0.6",
"@storybook/addon-actions": "next",
Expand All @@ -61,19 +58,15 @@
"babel-jest": "^27.0.6",
"babel-loader": "^8.1.0",
"concurrently": "^7.0.0",
"estree-to-babel": "^4.9.0",
"hast-util-to-estree": "^2.0.2",
"husky": ">=6",
"jest": "^27.0.6",
"jest-environment-jsdom": "^27.0.6",
"js-string-escape": "^1.0.1",
"lint-staged": ">=10",
"lodash": "^4.17.21",
"prettier": "^2.3.1",
"prompts": "^2.4.2",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"rimraf": "^3.0.2",
"storybook": "next",
"ts-dedent": "^2.2.0",
"ts-jest": "^27.0.4",
Expand Down
220 changes: 1 addition & 219 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,235 +1,17 @@
import mdx from '@mdx-js/mdx';
import generate from '@babel/generator';
import * as t from '@babel/types';
import cloneDeep from 'lodash/cloneDeep';
import toBabel from 'estree-to-babel';
import { toEstree } from 'hast-util-to-estree';
import { createCompiler, MdxOptions } from './sb-mdx-plugin';

// Keeping as much code as possible from the original compiler to avoid breaking changes
// import {
// genCanvasExports,
// genStoryExport,
// genMeta,
// CompilerOptions,
// Context,
// MetaExport,
// wrapperJs,
// stringifyMeta,
// } from './sb-mdx-plugin';
import type { CompileOptions, MdxCompileOptions, JSXOptions } from './types';
import { transformJSXAsync, transformJSXSync } from './jsx';
import { transformSync } from '@babel/core';

export type { CompileOptions, MdxCompileOptions, JSXOptions };

export const SEPARATOR = '/* ========= */';

// export { wrapperJs };

// const hasStoryChild = (node: any) => {
// return node.children?.length > 0 && node.children.find((c: any) => c.name === 'Story');
// };

// const generateMdxSource = (canvas: any) => {
// const babel = toBabel(cloneDeep(toEstree(canvas)));
// const { code } = generate(babel, {});
// return code.replace(/<\/?Canvas[^>]*>;?/g, '');
// };

// function extractExports(root: t.File, options: CompilerOptions) {
// const context: Context = {
// counter: 0,
// storyNameToKey: {},
// namedExports: {},
// };
// const storyExports = [];
// const includeStories = [];
// let metaExport: MetaExport | null = null;

// let contents: t.ExpressionStatement;
// root.program.body.forEach((child) => {
// if (t.isExpressionStatement(child) && t.isJSXFragment(child.expression)) {
// if (contents) throw new Error('duplicate contents');
// contents = child;
// } else if (
// t.isExportNamedDeclaration(child) &&
// t.isVariableDeclaration(child.declaration) &&
// child.declaration.declarations.length === 1
// ) {
// const declaration = child.declaration.declarations[0];
// if (t.isVariableDeclarator(declaration) && t.isIdentifier(declaration.id)) {
// const { name } = declaration.id;
// context.namedExports[name] = declaration.init;
// }
// }
// });
// if (contents) {
// const jsx = contents.expression as t.JSXFragment;
// jsx.children.forEach((child) => {
// if (t.isJSXElement(child)) {
// if (t.isJSXIdentifier(child.openingElement.name)) {
// const name = child.openingElement.name.name;
// let stories;
// if (['Canvas', 'Preview'].includes(name)) {
// stories = genCanvasExports(child, context);
// } else if (name === 'Story') {
// stories = genStoryExport(child, context);
// } else if (name === 'Meta') {
// const meta = genMeta(child, options);
// if (meta) {
// if (metaExport) {
// throw new Error('Meta can only be declared once');
// }
// metaExport = meta;
// }
// }
// if (stories) {
// Object.entries(stories).forEach(([key, story]) => {
// includeStories.push(key);
// storyExports.push(story);
// });
// }
// }
// } else if (t.isJSXExpressionContainer(child)) {
// // Skip string literals & other JSX expressions
// } else {
// throw new Error(`Unexpected JSX child: ${child.type}`);
// }
// });
// }

// if (metaExport) {
// if (!storyExports.length) {
// storyExports.push('export const __page = () => { throw new Error("Docs-only story"); };');
// storyExports.push('__page.parameters = { docsOnly: true };');
// includeStories.push('__page');
// }
// } else {
// metaExport = {};
// }
// metaExport.includeStories = JSON.stringify(includeStories);

// const fullJsx = [
// ...storyExports,
// `const componentMeta = ${stringifyMeta(metaExport)};`,
// wrapperJs,
// 'export default componentMeta;',
// ].join('\n\n');

// return fullJsx;
// }

// export const genBabel = (store: any, root: any) => {
// const estree = store.toEstree(root);
// // toBabel mutates root, so we need to clone it
// const clone = cloneDeep(estree);
// const babel = toBabel(clone);
// return babel;
// };

// export const plugin = (store: any) => (root: any) => {
// const babel = genBabel(store, root);
// store.exports = extractExports(babel, {});

// // insert mdxSource attributes for canvas elements
// root.children.forEach((node: any) => {
// if (node.type === 'mdxJsxFlowElement' && node.name === 'Canvas') {
// if (!hasStoryChild(node)) {
// node.attributes = [
// ...(node.attributes || []),
// { type: 'mdxJsxAttribute', name: 'mdxSource', value: generateMdxSource(node) },
// ];
// }
// }
// });

// return root;
// };

// export const postprocess = (code: string, extractedExports: string) => {
// const lines = code.toString().trim().split('\n');

// // /*@jsxRuntime automatic @jsxImportSource react*/
// const first = lines.shift();

// return [
// ...lines.filter((line) => !line.match(/^export default/)),
// SEPARATOR,
// extractedExports,
// ].join('\n');
// };

// // export const compile = async (
// // input: string,
// // { skipCsf = false, mdxCompileOptions = {}, jsxOptions = {} }: CompileOptions = {}
// // ) => {
// // const { options, context } = getCompilerOptionsAndContext(mdxCompileOptions, skipCsf);

// // if (skipCsf) {
// // const mdxResult = await mdxCompile(input, options);

// // return mdxResult.toString();
// // }

// // const mdxResult = await mdxCompile(input, options);

// // return transformJSXAsync(postprocess(mdxResult.toString(), context.exports), jsxOptions);
// // };

// // export const compileSync = (
// // input: string,
// // { skipCsf = false, mdxCompileOptions = {}, jsxOptions = {} }: CompileOptions = {}
// // ) => {
// // const { options, context } = getCompilerOptionsAndContext(mdxCompileOptions, skipCsf);

// // if (skipCsf) {
// // const mdxResult = mdxCompileSync(input, options);

// // return mdxResult.toString();
// // }

// // const mdxResult = mdxCompileSync(input, options);

// // return transformJSXSync(postprocess(mdxResult.toString(), context.exports), jsxOptions);
// // };

export const compile = async (code: string, options?: MdxOptions) => {
const result = await mdx(code, { compilers: options?.skipCsf ? [] : [createCompiler(options)] });
return transformJSXAsync(result, options?.jsxOptions);
};

export const compileSync = (code: string, options?: MdxOptions) => {
const result = mdx.sync(code, { compilers: options?.skipCsf ? [] : [createCompiler(options)] });
return transformSync(result);
return transformJSXSync(result, options?.jsxOptions);
};

// function getCompilerOptionsAndContext(
// mdxCompileOptions: MdxCompileOptions,
// skipCsf: boolean = false
// ): { options: MdxCompileOptions; context: any } {
// if (skipCsf) {
// return {
// options: {
// providerImportSource: '@mdx-js/react',
// rehypePlugins: [],
// ...mdxCompileOptions,
// },
// context: {},
// };
// }

// const context = { exports: '', toEstree };

// return {
// options: {
// providerImportSource: '@mdx-js/react',
// rehypePlugins: [[plugin, context]],
// ...mdxCompileOptions,

// // preserve the JSX, we'll deal with it using babel
// jsx: true,
// },
// context,
// };
// }
Loading

0 comments on commit 89e7fd1

Please sign in to comment.