Speed up Barrel Import Has Fix
Description
A barrel import 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'
.
It has some benefits to import each module directly from its own file without going through the barrel file. Such as reducing bundle size, improving building time or avoiding conflicting names.
YAML
yaml
id: speed-up-barrel-import
language: typescript
# find the barrel import statement
rule:
pattern: import {$$$IDENTS} from './barrel'
# rewrite imported identifiers to direct imports
rewriters:
- id: rewrite-identifer
rule:
pattern: $IDENT
kind: identifier
fix: import $IDENT from './barrel/$IDENT'
# apply the rewriter to the import statement
transform:
IMPORTS:
rewrite:
rewriters: [rewrite-identifer]
# $$$IDENTS contains imported identifiers
source: $$$IDENTS
# join the rewritten imports by newline
joinBy: "\n"
fix: $IMPORTS
Example
ts
import {a, b, c} from './barrel'
Diff
ts
import {a, b, c} from './barrel'
import a from './barrel/a'
import b from './barrel/b'
import c from './barrel/c'