# @blocz/mdx-plugin-detect-imports

Latest version **0.3.0** (published 2023-04-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install @blocz/mdx-plugin-detect-imports
pnpm add @blocz/mdx-plugin-detect-imports
yarn add @blocz/mdx-plugin-detect-imports
bun add @blocz/mdx-plugin-detect-imports
```

## Health

**Score 45/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2023-04-20 |
| First published | 2021-01-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 8.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ayc0 <ayc0.benj@gmail.com> |
| Maintainers | ayc0 |
| Keywords | mdx, markdown, jsx, remark, mdxast, plugin |

## Links

- npm: https://www.npmjs.com/package/@blocz/mdx-plugin-detect-imports
- Repository: git@github.com:bloczjs/mdx.git
- npm.io page: https://npm.io/package/@blocz/mdx-plugin-detect-imports

## Dependencies (3)

- [acorn](https://npm.io/package/acorn.md) ^8.0.0
- [estree-util-visit](https://npm.io/package/estree-util-visit.md) ^1.0.0
- [unist-util-select](https://npm.io/package/unist-util-select.md) ^4.0.0

## Alternatives

- [@mdxeditor/editor](https://npm.io/package/@mdxeditor/editor.md) — 962.4K weekly downloads
- [mmdb-lib](https://npm.io/package/mmdb-lib.md) — 680.9K weekly downloads
- [playcanvas](https://npm.io/package/playcanvas.md) — 36.2K weekly downloads
- [@glw907/cairn-cms](https://npm.io/package/@glw907/cairn-cms.md) — 967 weekly downloads
- [markdown-to-confluence](https://npm.io/package/markdown-to-confluence.md) — 103 weekly downloads

## Recent versions

- 0.3.0 (latest) — 2023-04-20
- 0.2.1 — 2023-04-20
- 0.2.0 — 2023-03-20
- 0.2.0-rc.9 — 2023-03-20
- 0.2.0-rc.8 — 2023-01-11
- 0.2.0-rc.7 — 2023-01-10
- 0.2.0-rc.6 — 2023-01-10
- 0.2.0-rc.5 — 2023-01-04
- 0.2.0-rc.4 — 2022-07-23
- 0.2.0-rc.3 — 2022-07-22
- 0.2.0-rc.2 — 2022-07-13
- 0.2.0-rc.1 — 2022-07-11
- 0.2.0-rc — 2022-07-10
- 0.2.0-beta.4 — 2022-07-10
- 0.2.0-beta.3 — 2022-02-09
- … 9 more at https://npm.io/package/@blocz/mdx-plugin-detect-imports/versions

## README

# `@blocz/mdx-plugin-detect-imports`

MDX plugin to detect the list of every imports done in a MDX file.

## MDX 2

Since the v0.2.0, it's based on MDX v2. It you want to use it with MDX v1, you can look at the [v0.1.0](https://github.com/bloczjs/mdx/tree/v0.1.0).<br/>
If you’re looking to upgrade to the v0.2.0, [the list of breaking changes is listed here](https://github.com/bloczjs/mdx/releases/tag/v0.2.0).

## What does it do?

If your MDX file look like this:

```mdx
import { Tabs, Button } from "@blocz/elements";

## Hello MDX

1. First item
2. Second item

<Button variant="blue" label="Label" />
```

This plugin will transform this file into:

```mdx
import { Button, Tabs as Tabulations } from "@blocz/elements";

## Hello MDX

1. First item
2. Second item

<Button variant="blue" label="Label" />

export const importStatements = [
    {
        module: "@blocz/elements",
        imports: [
            {
                kind: "named",
                imported: "Button",
                local: "Button",
                value: this_is_the_value_of_the_variable_Button
            },
            {
                kind: "named",
                imported: "Tabs",
                local: "Tabulations",
                value: this_is_the_value_of_the_variable_Tabs
            }
        ]
    }

]
```

## How to use

`yarn add -D @blocz/mdx-plugin-detect-imports`

### Webpack

```js
const detectImportsPlugin = require("@blocz/mdx-plugin-detect-imports");

module.exports = {
    // ...
    module: {
        rules: [
            // ...
            {
                test: /.mdx?$/,
                loader: "@mdx-js/loader",
                options: {
                    remarkPlugins: [detectImportsPlugin],
                    // Or if you want to specify a custom name for the exported variable:
                    remarkPlugins: [
                        [detectImportsPlugin, { exportName: "otherName" }],
                    ],
                },
            },
        ],
    },
};
```

And finally:

`import MyAwesomeComponent, { importStatements } from './my-awesome-component.mdx';`
(or `import MyAwesomeComponent, { otherName } from './my-awesome-component.mdx';` if you specified a custom name.)

### With MDX

```js
import compile from "@mdx-js/mdx";
import detectImportsPlugin from "@blocz/mdx-plugin-detect-imports";

const vFile = await compile(mdxText, {
    remarkPlugins: [detectImportsPlugin],
});

// Or if you want to specify a custom name for the exported variable:

const vFile = await compile(mdxText, {
    remarkPlugins: [[detectImportsPlugin, { exportName: "otherName" }]],
});
```

## type `ImportStatement`

If you need typings, we provide the following type:

```typescript
interface ImportStatement {
    module: string;
    imports: Array<
        | {
              kind: "named";
              imported: string;
              local: string;
              value: any;
          }
        | {
              kind: "namespace" | "default";
              local: string;
              value: any;
          }
    >;
}
```

And then you can do:

```typescript
// mdx.d.ts
declare module "*.mdx" {
    const MDXComponent: (props: any) => JSX.Element;
    // you can replace `importStatements` with your own variable name
    export const importStatements: ImportStatement[];
    export default MDXComponent;
}
```

---
_Source: https://npm.io/package/@blocz/mdx-plugin-detect-imports · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
