Barrel File

Angular: The Full Gamut Edition

Charlie Greenman
April 28, 2021
2 min read

What is a Barrel File?

A barrel File is a way to roll up exports from several modules into a single convenience module.

The barrel itself is a module file that re-exports selected exports of other modules.

Barrel File In Practice

In the previous chapter we discussed doing something like the following:

import {module} from '@ill/color-picker';

We are able to do this, because the nrwl nx layer on top Angular CLI, will generate an index.tx file. This contains all the imports. Anything that is within the component, that should be exposed outside the lib, should be put in the index.ts - aka, the barrel file.

export { IllColorPickerModule } from './src/ill-color-picker.module';

Enforcing Barrel File With Tslint

In addition, Nrwl nx has a tslint add on called nx-enforce-module-bounderies.

// tslint.json"nx-enforce-module-boundaries": [true,{"allow": [],"depConstraints": [{"sourceTag": "*","onlyDependOnLibsWithTags": ["*"]}]}],

Adding true as a parameter will make the tslint complain whenever we are not using the barrel import when accessing a lib file.

Subscribe to the Razroo Angular Newsletter!

Razroo takes pride in it's Angular newsletter, and we really pour heart and soul into it. Pass along your e-mail to recieve it in the mail. Our commitment, is to keep you up to date with the latest in Angular, so you don't have to.

More articles similar to this

footer

Razroo is committed towards contributing to open source. Take the pledge towards open source by tweeting, #itaketherazroopledge to @_Razroo on twitter. One of our associates will get back to you and set you up with an open source project to work on.