nx icon indicating copy to clipboard operation
nx copied to clipboard

TSLint: imports of lazy loaded libraries are forbidden (nx-enforce-module-boudries)

Open tommyc38 opened this issue 8 years ago • 10 comments

I have an app called home that has a lazy-loaded module, lazy. lazy imports a lib, shared, to use ui components from. Everything works but I get this error in Webstorm on my import statement within lazy.module.ts (e.g. import { SharedModule } from '@workspace/shared).

Is this expected? If I have a ui lib I would expect to be able to share that code with lazy-loaded modules. Also, shared lib is NOT a lazy-loaded lib anywhere in my routing configurations so I don't quite understand what is going on.

If I change the import statement to import { Shared Module } from '../../../../../libs/shared/src', the error goes away. I am confused.

tommyc38 avatar Aug 12 '18 03:08 tommyc38

I looked at the source code for this rule and it's as follows:

     // if we import a library using loadChildre, we should not import it using es6 imports 
      if (this.onlyLoadChildren(sourceProject.name, targetProject.name, [])) {
        this.addFailureAt(
          node.getStart(),
          node.getWidth(),
          'imports of lazy-loaded libraries are forbidden'
        );
        return;
      }

tommyc38 avatar Aug 12 '18 03:08 tommyc38

If you have done everything as you describe, then you shouldn't have problem. Could you please write the content of tsconfig.app.json file of the home app?

stefanoslig avatar Aug 13 '18 17:08 stefanoslig

@stefanoslig - see below. I read the documentation and tried a few things which is why its commented out. I uncommitted it but my IDE is still giving me an error.

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/out-tsc/apps/home",
    "module": "es2015",
    "types": []
  },
  "exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ],
  "include": [
    "**/*.ts",
//    "../../../libs/shared/src/lib/index.ts"
  ]
}

tommyc38 avatar Aug 13 '18 17:08 tommyc38

To be clear, my code works fine regardless of the lint error. I am just wondering why I am getting it in the first place.

tommyc38 avatar Aug 13 '18 17:08 tommyc38

Yes, as I expected you have included shared module which is not a lazy module into the tsconfig.app.json. The lazy module as you said is the "lazy" module. So your tsconfig.app.json should be something like this .... .... "include": [ "**/*.ts", "../../../libs/lazy/src/lib/index.ts" ]

stefanoslig avatar Aug 13 '18 18:08 stefanoslig

@stefanoslig lazy module is NOT in libs. It's in the home app folder (e.g. apps/home/src/app/lazy/lazy.module.ts)

tommyc38 avatar Aug 13 '18 18:08 tommyc38

The guide appears to only covers lazy loading a module defined in a library. I have the use case of lazy loading a module defined in the app. This lazy loaded app module exclusively uses a library so I expect that library will be bundled and loaded with that lazy loaded app module. Does this use case need to be similarly instrumented in tsconfig.app.json and tslint.json?

fricker avatar Aug 17 '18 01:08 fricker

@fricker this is exactly what I am wondering as well. There is no documentation for this use case and I'd like to know if my IDE lint error is a false positive or if this use case isn't acceptable in Nx (which would seem odd to me).

tommyc38 avatar Aug 17 '18 15:08 tommyc38

This is already fixed in the master branch, please try with the latest version of Nx. If the problem persists, please open a new issue.

bcabanes avatar Nov 01 '19 14:11 bcabanes

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

github-actions[bot] avatar Mar 25 '23 09:03 github-actions[bot]