最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angular - NX Monorepo - import of JSON file fails when using path alias - Stack Overflow

programmeradmin4浏览0评论

We have a NX Monorepo for a complex application. Recent change request from our client introduced couple of changes and one micro FE application was practically duplicated.

It consists of multiple libraries, imported using lazy-loading approach. Everything works fine, but I wanted to move the translation files from APP space to SHARED libraries space. And I'm getting an error when I try to import the translation file.

@NgModule({
  declarations: [],
  imports: [CommonModule, RouterModule.forChild(routes)],
  providers: [
    {
      provide: TRANSLOCO_SCOPE,
      useValue: {
        scope: 'etc',
        // Error: Module not found: Error: Can't resolve '@clientName/shared/assets/i18n' in ...
        // loader: InlineLoaderFactory((lang: string) => import(`@clientName/shared/assets/i18n/${lang}.json`)),
        
        // Relative path works, but I don't like it
        loader: InlineLoaderFactory((lang: string) => import(`../../../../../libs/shared/assets/i18n/${lang}.json`)),
        alias: 'etc'
      }
    }
  ]
})
export class RemoteEntryModule {}

// InlineLoaderFactor
import { Translation } from '@ngneat/transloco';

const SUPPORTED_LANG = ['en', 'de'] as const;

export function InlineLoaderFactory(loader: (lang: string) => Promise<any>) {
  return SUPPORTED_LANG.reduce((acc: Translation, lang: string) => {
    acc[lang] = () => loader(lang);
    return acc;
  }, {});
}

Is there a way to use path alias the way I imagined, or is my idea doomed from the start?

发布评论

评论列表(0)

  1. 暂无评论