FogelAI / babel-plugin-transform-barrels

A Babel plugin that transforms indirect imports through a barrel file (index.js) into direct imports.
11 stars 1 forks source link

Plugin not working for Feature Sliced design approach #1

Open 7iomka opened 8 months ago

7iomka commented 8 months ago

Hi. I tried to use your solution for this case:

I have entity, lets call it compare

.
└── entities /
    └── compare/
        ├── ui/
        │   ├── compare-header/
        │   │   ├── compare-header.component.tsx
        │   │   └── index.ts 
        │   ├── compare-content/
        │   │   ├── compare-content.component.tsx
        │   │   └── index.ts
        │   └── index.ts
        ├── compare.component.tsx
        ├── compare.lib.ts
        └── index.ts

root index file exports public api of this entity:

export {
  CompareContent,
  CompareHeader,
  type CompareContentProps,
  type CompareProductCardProps,
} from './ui';

export { $$compare } from './compare.model';

or in some cases when entity have multiple components or utils to export:

export * from './ui';
export { $$compare } from './compare.model';

So, each page of my next.js application include BaseLayout component, which need to import just $$compare model to use event from it inside useEffect.

import { $$compare } from '@/entities/compare'

If I import like this, I have all component from ui inside initial chunk for all the pages that uses BaseLayout

image

Only when I replaced all imports to direct imports - I see expected result

import { $$compare } from '@/entities/compare/compare.model'

Any suggestion why your plugin can't do this manual work for me? I have a huge code base. and I don't know how long it would take me to manually redo it. Furthermore. I don't want to do it because I need to know what the private api (structure) of the module is. That is, it breaks the whole understanding of public api, when the outside world knows nothing about the internal structure of the module. But I suppose it can be neglected considering that otherwise I get just hundreds of extra kilobytes in the bundle of each page.

Many thanks.

FogelAI commented 7 months ago

Hi @7iomka

I have published a new version of the plugin (v1.0.8) that should address the issue. Please update your version accordingly. Please note that I have also updated the readme file, so be sure to modify your webpack config file as follows: "plugins": ["transform-barrels", { webpackConfigFilename: __filename, ...(typeof module.exports === "function" && { args: arguments })}]

Please try it and let me know if it resolves your issue.

Thanks

sibelius commented 7 months ago

why do you need the webpackConfigFilename?

FogelAI commented 7 months ago

Hi @sibelius

Because it needs to read aliases from the Webpack config file.

anilanar commented 4 months ago

Wouldn't it be better if you just take an alias map object instead of taking a path to webpack config? People can just reuse their alias map.

FogelAI commented 4 months ago

Hi @anilanar Because it is an easier configuration, you just use the same configuration for every project as it appears in the readme.md and it should work. Is there a reason that you prefer the alias option in the plugin configuration instead of a path to the webpack config?