UNPKG

6.54 kBMarkdownView Raw
1# ![Barrelsby Logo](https://github.com/bencoveney/barrelsby/blob/master/img/logo.png?raw=true)
2
3Automatically create TypeScript barrels for your entire code base.
4
5[![npm version](https://badge.fury.io/js/barrelsby.svg)](https://badge.fury.io/js/barrelsby)
6[![Node.js CI](https://github.com/bencoveney/barrelsby/actions/workflows/node.js.yml/badge.svg?branch=master)](https://github.com/bencoveney/barrelsby/actions/workflows/node.js.yml)
7[![CodeQL](https://github.com/bencoveney/barrelsby/actions/workflows/codeql-analysis.yml/badge.svg?branch=master)](https://github.com/bencoveney/barrelsby/actions/workflows/codeql-analysis.yml)
8
9## About Barrels
10
11Barrels are files that rollup exports from several modules into a single convenient module
12typically named `index.ts`. They tend to help simplify large blocks of import statements at the top
13of files and help to group up related functionality.
14
15A barrel file looks like this:
16
17```TypeScript
18export * from "./DropDown";
19export * from "./TextBox";
20export * from "./CheckBox";
21export * from "./DateTimePicker";
22export * from "./Slider";
23```
24
25It can help you go from messy imports like this:
26
27```TypeScript
28import {DropDown} from "./src/controls/DropDown";
29import {TextBox} from "./src/controls/TextBox";
30import {CheckBox} from "./src/controls/CheckBox";
31import {DateTimePicker} from "./src/controls/DateTimePicker";
32import {Slider} from "./src/controls/Slider";
33```
34
35...to something tidier like this:
36
37```TypeScript
38import {DropDown, TextBox, CheckBox, DateTimePicker, Slider} from "./src/controls";
39```
40
41...or even this:
42
43```TypeScript
44import * as Controls from "./src/controls/index";
45```
46
47### More Reading
48
49* [TattooCoder Blog](http://tattoocoder.com/angular2-barrels/)
50
51### Barrelsby Articles
52
53* [Rupesh Tiwari Blog](http://rupeshtiwari.com/create-barrel/)
54* [Medium Article](https://medium.com/@klauskpm/do-a-barrel-export-aa5b79b76b05)
55
56### Alternatives
57
58* [Barrelbot](https://github.com/sw-yx/barrelbot)
59* [creeate-index](https://github.com/gajus/create-index)
60
61## Usage
62
63To install Barrelsby:
64
65```
66npm install --save-dev barrelsby
67```
68
69To run barrelsby first add a script to the `package.json` file:
70
71```json
72{
73 "scripts": {
74 "generate-barrels": "barrelsby --delete"
75 }
76}
77```
78
79You can now generate barrels:
80
81```
82npm run generate-barrels
83```
84
85## Configuration Options
86
87Barrelsby accepts a number of options to help refine how your barrels are created. These options
88can be configured from the command line or using a configuration file.
89
90### `-c [path]` or `--config [path]`
91
92Specifies the location of the barrelsby configuration file. This file must be a `.json` file. You
93can include any of the configuration options using their long name.
94
95### `-d [path...]` or `--directory [path...]`
96
97Specifies a list of root directories where barrels will be created from. Uses the current directory
98by default.
99
100**Note**: This is backwards compatible with previous versions. Existing configuration files will be
101parsed correctly. The following two json files will behave identically.
102
103```json lines
104{
105 "directory": "./src"
106}
107
108{
109 "directory": ["./src"]
110}
111```
112
113### `-D` or `--delete`
114
115Deletes any existing barrels encountered by barrelsby. Disabled by default.
116
117### `-e [regex...]` or `--exclude [regex...]`
118
119Excludes any files whose paths match any of the specified regular expressions.
120
121### `-E` or `--exportDefault`
122
123Also export the default export of the file. Currently works only with the `flat` mode.
124
125```TypeScript
126export * from "./barrel";
127export { default as barrel } from "./barrel";
128```
129
130### `-F` or `--fullPathname`
131
132exportDefault with full pathname to create distinct name. Currently works only with the `flat` mode and exportDefault flag.
133
134```TypeScript
135export * from "./example/of/the/path";
136export { default as exampleOfThePath } from "./example/of/the/path";
137```
138
139### `-h` or `--help`
140
141Displays help information on the command line arguments that barrelsby accepts.
142
143### `-i [regex...]` or `--include [regex...]`
144
145Only include files whose paths match any of the specified regular expressions.
146
147### `-l [mode]` or `--location [mode]`
148
149The mode that barrelsby should use to determine where which directories to create barrels in.
150Defaulted to *top*.
151
152- `top` only creates a barrel in the target directory.
153- `below` creates a barrel in every directory just below the target directory.
154- `all` creates a barrel in every directory below (and including) the target directory.
155- `replace` only creates barrels in directories where one already existed.
156- `branch` creates a barrel in every directory that contains other directories.
157
158### `-L` or `--local`
159
160Enable this to prevent barrels including modules that exist in the same directory, rather
161than recursively searching child directories.
162
163### `-n [name]` or `--name [name]`
164
165Specifies the name to use for creating new barrels (and identifying old ones). `.ts` wil be
166appended if not included in the name. Barrels names will be defaulted to `index.ts`.
167
168### `-s [mode]` or `--structure [mode]`
169
170The structure that barrelsby should create inside the barrels. Defaulted to *flat*.
171
172#### `flat`
173
174Exports modules without any nesting.
175
176```TypeScript
177export * from "./barrel";
178export * from "./index";
179export * from "./directory2/script";
180export * from "./directory2/directory4/deeplyNested";
181export * from "./directory3/program";
182```
183
184#### `filesystem`
185
186Exports modules as a nested structure that matches the file system directories.
187
188```TypeScript
189import * as barrelts from "./barrel";
190import * as directory2directory4deeplyNestedts from "./directory2/directory4/deeplyNested";
191import * as directory2scriptts from "./directory2/script";
192import * as directory3programts from "./directory3/program";
193import * as indexts from "./index";
194export {barrelts as barrel};
195export const directory2 = {
196 directory4: {
197 deeplyNested: directory2directory4deeplyNestedts,
198 },
199 script: directory2scriptts,
200};
201export const directory3 = {
202 program: directory3programts,
203};
204export {indexts as index};
205```
206
207### `-q` or `--singleQuotes`
208
209Use 'single quotes' in the generated barrel files instead of the default "double quotes".
210
211### `-S` or `--noSemicolon`
212
213Omit semicolons from the end of lines in the generated barrel files.
214
215### `-H` or `--noHeader`
216
217Omit adding a header comment to the top of the barrel file.
218
219### `-v` or `--version`
220
221Display the barrelsby version number.
222
223### `-V` or `--verbose`
224
225Display additional debug information.
226
227## Requirements
228
229Requires node v6.0.0 or greater for ES6 syntax.
230
231## Contributing
232
233See CONTRIBUTING.md