@carbonorm/barrelsby

中文文档
1.0.0发布于 2024/03/06MITCommonJS近 30 天下载 3426

Automatic TypeScript barrels for your entire code base

原始使用文档
文档目录24 节
  1. About Barrels
  2. More Reading
  3. Barrelsby Articles
  4. Alternatives
  5. Usage
  6. Configuration Options
  7. -c [path] or --config [path]
  8. -d [path...] or --directory [path...]
  9. -D or --delete
  10. -e [regex...] or --exclude [regex...]
  11. -E or --exportDefault
  12. -h or --help
  13. -i [regex...] or --include [regex...]
  14. -l [mode] or --location [mode]
  15. -L or --local
  16. -n [name] or --name [name]
  17. -s [mode] or --structure [mode]
  18. -q or --singleQuotes
  19. -S or --noSemicolon
  20. -H or --noHeader
  21. -v or --version
  22. -V or --verbose
  23. Requirements
  24. Contributing

Barrelsby Logo

Automatically create TypeScript barrels for your entire code base.

npm version Node.js CI CodeQL

About Barrels

Barrels are files that rollup exports from several modules into a single convenient module typically named index.ts. They tend to help simplify large blocks of import statements at the top of files and help to group up related functionality.

A barrel file looks like this:

export * from "./DropDown";
export * from "./TextBox";
export * from "./CheckBox";
export * from "./DateTimePicker";
export * from "./Slider";

It can help you go from messy imports like this:

import {DropDown} from "./src/controls/DropDown";
import {TextBox} from "./src/controls/TextBox";
import {CheckBox} from "./src/controls/CheckBox";
import {DateTimePicker} from "./src/controls/DateTimePicker";
import {Slider} from "./src/controls/Slider";

...to something tidier like this:

import {DropDown, TextBox, CheckBox, DateTimePicker, Slider} from "./src/controls";

...or even this:

import * as Controls from "./src/controls/index";

More Reading

Barrelsby Articles

Alternatives

Usage

To install Barrelsby:

npm install --save-dev barrelsby

To run barrelsby first add a script to the package.json file:

{
  "scripts": {
    "generate-barrels": "barrelsby --delete"
  }
}

You can now generate barrels:

npm run generate-barrels

Configuration Options

Barrelsby accepts a number of options to help refine how your barrels are created. These options can be configured from the command line or using a configuration file.

-c [path] or --config [path]

Specifies the location of the barrelsby configuration file. This file must be a .json file. You can include any of the configuration options using their long name.

-d [path...] or --directory [path...]

Specifies a list of root directories where barrels will be created from. Uses the current directory by default.

Note: This is backwards compatible with previous versions. Existing configuration files will be parsed correctly. The following two json files will behave identically.

{
  "directory": "./src"
}

{
  "directory": ["./src"]
}

-D or --delete

Deletes any existing barrels encountered by barrelsby. Disabled by default.

-e [regex...] or --exclude [regex...]

Excludes any files whose paths match any of the specified regular expressions.

-E or --exportDefault

Also export the default export of the file. Currently works only with the flat mode.

export * from "./barrel";
export * from "./barrel";
export { default as barrel } from "./barrel";

-h or --help

Displays help information on the command line arguments that barrelsby accepts.

-i [regex...] or --include [regex...]

Only include files whose paths match any of the specified regular expressions.

-l [mode] or --location [mode]

The mode that barrelsby should use to determine where which directories to create barrels in. Defaulted to top.

  • top only creates a barrel in the target directory.
  • below creates a barrel in every directory just below the target directory.
  • all creates a barrel in every directory below (and including) the target directory.
  • replace only creates barrels in directories where one already existed.
  • branch creates a barrel in every directory that contains other directories.

-L or --local

Enable this to prevent barrels including modules that exist in the same directory, rather than recursively searching child directories.

-n [name] or --name [name]

Specifies the name to use for creating new barrels (and identifying old ones). .ts wil be appended if not included in the name. Barrels names will be defaulted to index.ts.

-s [mode] or --structure [mode]

The structure that barrelsby should create inside the barrels. Defaulted to flat.

flat

Exports modules without any nesting.

export * from "./barrel";
export * from "./index";
export * from "./directory2/script";
export * from "./directory2/directory4/deeplyNested";
export * from "./directory3/program";

filesystem

Exports modules as a nested structure that matches the file system directories.

import * as barrelts from "./barrel";
import * as directory2directory4deeplyNestedts from "./directory2/directory4/deeplyNested";
import * as directory2scriptts from "./directory2/script";
import * as directory3programts from "./directory3/program";
import * as indexts from "./index";
export {barrelts as barrel};
export const directory2 = {
  directory4: {
    deeplyNested: directory2directory4deeplyNestedts,
  },
  script: directory2scriptts,
};
export const directory3 = {
  program: directory3programts,
};
export {indexts as index};

-q or --singleQuotes

Use 'single quotes' in the generated barrel files instead of the default "double quotes".

-S or --noSemicolon

Omit semicolons from the end of lines in the generated barrel files.

-H or --noHeader

Omit adding a header comment to the top of the barrel file.

-v or --version

Display the barrelsby version number.

-V or --verbose

Display additional debug information.

Requirements

Requires node v6.0.0 or greater for ES6 syntax.

Contributing

See CONTRIBUTING.md

版本与发布记录

@carbonorm/barrelsby 共发布 2 个版本,首次发布于 2024/03/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2024/03/062 年前
0.0.32024/03/062 年前

关于 @carbonorm/barrelsby 的常见问题

@carbonorm/barrelsby 是什么?

Automatic TypeScript barrels for your entire code base

如何安装 @carbonorm/barrelsby?

在项目目录运行 npm install @carbonorm/barrelsby,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 barrelsby,可以用 npx barrelsby 直接运行。

@carbonorm/barrelsby 支持 TypeScript 吗?

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

@carbonorm/barrelsby 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@carbonorm/barrelsby 需要什么版本的 Node.js?

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

@carbonorm/barrelsby 可以商用吗?

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

@carbonorm/barrelsby 的下载量有多少?

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

@carbonorm/barrelsby 还在维护吗?

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