Skip to content

Commit 0dc431a

Browse files
catalog: add barrel import example
1 parent fa102af commit 0dc431a

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

website/catalog/typescript/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ However, you can use the [`languageGlobs`](/reference/sgconfig.html#languageglob
1515
<!--@include: ./no-await-in-promise-all.md-->
1616
<!--@include: ./no-console-except-catch.md-->
1717
<!--@include: ./find-import-usage.md-->
18-
<!--@include: ./switch-from-should-to-expect.md-->
18+
<!--@include: ./switch-from-should-to-expect.md-->
19+
<!--@include: ./speed-up-barrel-import.md-->
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## Speed up Barrel Import <Badge type="tip" text="Has Fix" />
2+
3+
* [Playground Link](/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6ImphdmFzY3JpcHQiLCJxdWVyeSI6IiIsInJld3JpdGUiOiIiLCJjb25maWciOiJydWxlOlxuICBwYXR0ZXJuOiBpbXBvcnQgeyQkJElERU5UU30gZnJvbSAnLi9iYXJyZWwnXG5yZXdyaXRlcnM6XG4tIGlkOiByZXdyaXRlLWlkZW50aWZlclxuICBydWxlOlxuICAgIHBhdHRlcm46ICRJREVOVFxuICAgIGtpbmQ6IGlkZW50aWZpZXJcbiAgZml4OiBpbXBvcnQgJElERU5UIGZyb20gJy4vYmFycmVsLyRJREVOVCdcbnRyYW5zZm9ybTpcbiAgSU1QT1JUUzpcbiAgICByZXdyaXRlOlxuICAgICAgcmV3cml0ZXJzOiBbcmV3cml0ZS1pZGVudGlmZXJdXG4gICAgICBzb3VyY2U6ICQkJElERU5UU1xuICAgICAgam9pbkJ5OiBcIlxcblwiXG5maXg6ICRJTVBPUlRTIiwic291cmNlIjoiaW1wb3J0IHsgYSwgYiwgYyB9IGZyb20gJy4vYmFycmVsJzsifQ==)
4+
5+
### Description
6+
7+
A [barrel import](https://adrianfaciu.dev/posts/barrel-files/) is a way to consolidate the exports of multiple modules into a single convenient module that can be imported using a single import statement. For instance, `import {a, b, c} from './barrel'`.
8+
9+
It has [some](https://vercel.com/blog/how-we-optimized-package-imports-in-next-js) [benefits](https://marvinh.dev/blog/speeding-up-javascript-ecosystem-part-7/) to import each module directly from its own file without going through the barrel file.
10+
Such as reducing [bundle size](https://dev.to/tassiofront/barrel-files-and-why-you-should-stop-using-them-now-bc4), improving building time or avoiding [conflicting names](https://flaming.codes/posts/barrel-files-in-javascript/).
11+
12+
<!-- Use YAML in the example. Delete this section if use pattern. -->
13+
### YAML
14+
```yaml
15+
id: speed-up-barrel-import
16+
language: typescript
17+
# find the barrel import statement
18+
rule:
19+
pattern: import {$$$IDENTS} from './barrel'
20+
# rewrite imported identifiers to direct imports
21+
rewriters:
22+
- id: rewrite-identifer
23+
rule:
24+
pattern: $IDENT
25+
kind: identifier
26+
fix: import $IDENT from './barrel/$IDENT'
27+
# apply the rewriter to the import statement
28+
transform:
29+
IMPORTS:
30+
rewrite:
31+
rewriters: [rewrite-identifer]
32+
# $$$IDENTS contains imported identifiers
33+
source: $$$IDENTS
34+
# join the rewritten imports by newline
35+
joinBy: "\n"
36+
fix: $IMPORTS
37+
```
38+
39+
### Example
40+
41+
```ts {1}
42+
import {a, b, c} from './barrel'
43+
```
44+
45+
### Diff
46+
<!-- use // [!code --] and // [!code ++] to annotate diff -->
47+
```ts
48+
import {a, b, c} from './barrel' // [!code --]
49+
import a from './barrel/a' // [!code ++]
50+
import b from './barrel/b' // [!code ++]
51+
import c from './barrel/c' // [!code ++]
52+
```
53+
54+
55+
### Contributed by
56+
[Herrington Darkholme](https://x.com/hd_nvim)

0 commit comments

Comments
 (0)