|
| 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