module-federation-examples icon indicating copy to clipboard operation
module-federation-examples copied to clipboard

unable to have a singleton service without barrel imports

Open nischaym-next opened this issue 1 year ago • 3 comments

I am looking to import from one the shared libs without the barrel import file (index.ts) so that my bundle files gets smaller. example: Before import { blahService } from '@shared-services' After import { blahService } from '@shared-services/blahservice'

my bundle is right now bloated given importing any of the services is importing all the services, without the barrel imports my bundle size reduce from 250kb to 50kb BUT the services are getting initated multiple times (twice) i.e blahService logs twice when i put a console.log in its constructor

my tsconfig.base.json "@shared-services/*": ["libs/shared-services/src/lib/*"], previously this was "@shared-services": ["libs/shared-services/src/index"], my module-federation.base.config.ts

  additionalShared: [
    {
      libraryName: '@shared-services',
      sharedConfig: {
        requiredVersion: '*',
        singleton: true,
        import: 'libs/shared-services/src/lib/*', --> previously this was libs/shared-services/src/index
      },
    },
  ],

so how to have a singleton without the barrel imports ?

nischaym-next avatar Dec 10 '24 18:12 nischaym-next

use a trailing slash.

"lodash/" instead of "lodash" for example.

ScriptedAlchemy avatar Feb 28 '25 00:02 ScriptedAlchemy

??

nischaym-next avatar Mar 05 '25 14:03 nischaym-next

Oh this looks like nx. I'm not sure how this works in nx. Will need to check

ScriptedAlchemy avatar Mar 15 '25 09:03 ScriptedAlchemy