@cloudcannon/remark-auto-import

中文文档
1.0.1发布于 2023/05/16MITES Module近 30 天下载 4562

Remark plugin for automatically importing components

原始使用文档
文档目录3 节
  1. Options
  2. Astro
  3. Next.js

remark-auto-import

A remark plugin for automatically importing components in MDX.

Usage

This plugin searches directories for component files and then automatically generates default imports for them during your MDX build. This means that your content editors can freely and and remove components in MDX without worrying about keeping imports up to date.

To use the plugin provide it to your MDX engine's remarkPlugins array and provide an options object containg a list of directories to search and a glob pattern of files to import.

Options

type Options = {
  directories: Array<string>;
  patterns: Array<string>;
  name?: (path: string): string;

  additionals: Array<{
    importPath: string;
    defaultImport: string;
    namedImports: Array<{ name: string; alias?: string }>;
  }>;
};

directories: An array of directory names to search for files to import.

patterns: An array of fast-glob patterns for which files to import.

name: An optional function which takes the absolute path of a file to be imported and returns the identifier for that files default import. By default the file name is used, e.g /path/to/Component.jsx is imported as Component, and the generated import would be import Component from "/path/to/Component.jsx".

additional: An array of objects that specify additional fixed imports:

  • importPath: The path to the file to be imported. This will be inserted as-is so it's recommend to use package names or absolute paths.

  • defaultImport: The identifier that will be used for the default import.

  • namedImports: An array of objects specifying named imports:

    • name: The idenifier of the item to import
    • alias: An optional identifier to alias the import to with as.

So, an additional import with the structure { importPath: "package", defaultImport: "default", namedImports: [{name: "named", alias: 'alias'}]} would be imported as import default, {named as alias} from "package".

This can also be used for imports with only side effects, such as CSS files, by not specifying any default or named imports, i.e { importPath: "/path/to/styles.css" }.

Configuration Examples

Astro

//astro.config.mjs

import { defineConfig } from "astro/config";

import mdx from "@astrojs/mdx";
import remarkAutoImport from "@cloudcannon/remark-auto-import";

// https://astro.build/config
export default defineConfig({
  integrations: [mdx()],
  markdown: {
    remarkPlugins: [
      [
        remarkAutoImport,
        {
          directories: ["src/components"],
          patterns: ["*.*"],
        },
      ],
    ],
    extendDefaultPlugins: true,
  },
});

Next.js

//next.config.mjs

import remarkFrontmatter from "remark-frontmatter";
import remarkAutoImport from "@cloudcannon/remark-auto-import";
import nextMDX from "@next/mdx";

const withMDX = nextMDX({
  extension: /\.mdx?$/,
  options: {
    remarkPlugins: [
      remarkFrontmatter,
      [
        remarkAutoImport,
        {
          directories: ["components"],
          patterns: ["*.js"],
        },
      ],
    ],
  },
});

const nextConfig = {
  // Configure pageExtensions to include md and mdx
  pageExtensions: ["ts", "tsx", "js", "jsx", "md", "mdx"],
  // Optionally, add any other Next.js config below
  reactStrictMode: true,
  images: { unoptimized: true },
};

export default withMDX(nextConfig);

版本与发布记录

@cloudcannon/remark-auto-import 共发布 2 个版本,首次发布于 2023/03/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.1latest2023/05/163 年前
1.0.02023/03/243 年前

关于 @cloudcannon/remark-auto-import 的常见问题

@cloudcannon/remark-auto-import 是什么?

Remark plugin for automatically importing components

如何安装 @cloudcannon/remark-auto-import?

在项目目录运行 npm install @cloudcannon/remark-auto-import,或使用 pnpm add / yarn add / bun add。

@cloudcannon/remark-auto-import 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cloudcannon__remark-auto-import 是否存在。

@cloudcannon/remark-auto-import 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cloudcannon/remark-auto-import 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14.16。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cloudcannon/remark-auto-import 可以商用吗?

@cloudcannon/remark-auto-import 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cloudcannon/remark-auto-import 的下载量有多少?

根据 npm 官方下载统计,@cloudcannon/remark-auto-import 在 2026/10/10 – 2026/11/08 这 30 天内被下载 4,562 次,日均约 152 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cloudcannon/remark-auto-import 还在维护吗?

最新版本 1.0.1 发布于 2023/05/16(3 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/03/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。