Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
mdynnl committed May 12, 2024
0 parents commit baf9ada
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
bun.lockb
18 changes: 18 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type Predicate =
| (string | RegExp)[]
| RegExp
| string
| ((id: string) => boolean);

export interface Options {
/**
* @default /[.]mdx?$/
*/
extensions?: Predicate;
}

export interface State extends babel.PluginPass {
opts: Options;
}

export default function babelPluginSolidMdx(): import('@babel/core').PluginObj<State>;
85 changes: 85 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* @typedef {import('./index.js').Predicate} Predicate
* @typedef {import('@babel/core').types} types
* @typedef {import('@babel/core').types.MemberExpression} MemberExpression
* @typedef {import('@babel/core').types.JSXMemberExpression} JSXMemberExpression
* @typedef {import('@babel/core').types.JSXIdentifier} JSXIdentifier
*/

/**
* @type {import('./index.js').default}
*/
export default function babelPluginSolidMdx(ctx) {
const t = /** @type {types} */ (ctx.types);

return {
visitor: {
Program(path, state) {
const extensions = /** @type {Predicate[]} */ (
'extensions' in state.opts ? state.opts.extensions : /[.]mdx?$/
);

if (!state.filename || !predicate(state.filename, extensions)) {
return;
}

path.traverse({
JSXOpeningElement(path) {
const { node } = path;
if (t.isJSXMemberExpression(node.name)) {
const component = t.jsxAttribute(
t.jsxIdentifier('component'),
t.jsxExpressionContainer(toMemberExpression(node.name)),
);

const attributes = [component, ...node.attributes];

const dynamic = t.jsxOpeningElement(
t.jsxIdentifier('Dynamic'),
attributes,
);

path.replaceWith(dynamic);
}
},
});
},
},
};

/**
* @param {JSXMemberExpression} expr
* @returns {MemberExpression}
*/
function toMemberExpression(expr) {
return t.memberExpression(
expr.object.type === 'JSXIdentifier'
? toIdentifier(expr.object)
: toMemberExpression(expr.object),
toIdentifier(expr.property),
);
}

/**
* @param {JSXIdentifier} expr
*/
function toIdentifier(expr) {
return t.identifier(expr.name);
}
}

/**
* @param {string} filename
* @param {Predicate} pred
*/
function predicate(filename, pred) {
if (typeof pred === 'function') {
return pred(filename);
} else if (Array.isArray(pred)) {
return pred.some((pred) => predicate(filename, pred));
} else if (pred instanceof RegExp) {
return pred.test(filename);
} else {
return filename.endsWith(String(pred));
}
}
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "babel-plugin-solid-mdx",
"version": "0.0.1",
"description": "babel plugin to transform mdx to solid",
"license": "MIT",
"author": "Nyi Nyi Lwin <[email protected]>",
"repository": "mdynnl/babel-plugin-solid-mdx",
"bugs": "https://github.com/mdynnl/babel-plugin-solid-mdx/issues",
"sideEffects": false,
"type": "module",
"exports": "./index.js",
"files": [
"index.d.ts",
"index.js"
]
}
88 changes: 88 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# babel-plugin-solid-mdx

[![NPM](https://img.shields.io/npm/v/babel-plugin-solid-mdx.svg)](https://www.npmjs.com/package/babel-plugin-solid-mdx)

A simple babel plugin to transform mdx to solid

## Install

```bash
npm install --save-dev babel-plugin-solid-mdx
```

```bash
yarn add -D babel-plugin-solid-mdx
```

```bash
pnpm add -D babel-plugin-solid-mdx
```

```bash
bun add -d babel-plugin-solid-mdx
```

## Usage

```ts
import { defineConfig } from 'vite';
import solidPlugin from 'vite-plugin-solid';
import mdx from '@mdx-js/rollup';
import solidMdx from 'babel-plugin-solid-mdx';

export default defineConfig({
plugins: [
// use @mdx-js/rollup before solidPlugin
{
enforce: 'pre',
...mdx({
jsx: true,
jsxImportSource: 'solid-js',
}),
},
solidPlugin({
// configure solid to compile `.mdx` files
extensions: ['.mdx'],
babel: {
// use the plugin to transform the mdx output
plugins: [solidMdx],
},
}),
],
});
```

## Frontmatter Support

```js
import { defineConfig } from 'vite';
import solidPlugin from 'vite-plugin-solid';
import mdx from '@mdx-js/rollup';
import solidMdx from 'babel-plugin-solid-mdx';

import remarkFrontmatter from 'remark-frontmatter';
import remarkMdxFrontmatter from 'remark-mdx-frontmatter';

export default defineConfig({
plugins: [
{
enforce: 'pre',
...mdx({
jsx: true,
jsxImportSource: 'solid-js',
remarkPlugins: [
//
remarkFrontmatter,
remarkMdxFrontmatter,
],
}),
},
solidPlugin({
extensions: ['.mdx'],
babel: {
plugins: [solidMdx],
},
}),
],
});
```

0 comments on commit baf9ada

Please sign in to comment.