This is a simple default Nx to test refactoring.
Currently, refactoring is a pain, because the IDEs do not correctly update the import statements/index.ts files.
The screenshots below show what happens in IntelliJ IDEA 2020.1, but also VsCode is not able to handle this case.
Before refactoring we have this situation:
- a simple node-js app
- lib-a
- lib-b
The focus is on file-to-move.ts
in lib-a
:
- the file exports a constant
TO_MOVE
- the barrel file
index.ts
inlib-a
re-exports everything fromfile-to-move.ts
so that the constantTO_MOVE
is also available outside oflib-a
- the
main.ts
file of the node-app imports theTO_MOVE
constant using the path@nx-refactor/lib-a
which is specified intsconfig.tx
Now we move file-to-move.ts
from lib-a
to lib-b
.
Here is the actual refactoring in IntelliJ IDEA 2020.1:
Notes:
- the barrel file
index.ts
inlib-a
has been updated and now referencesfile-to-move.ts
via a relative deep import into `lib-b``. This import is forbidden by nx - libraries must not use deep imports in to other libraries.
Expected Refactoring
This is the expected refactoring tha should happen instead:
Notes:
- the export of
file-to-move.ts
must be removed from barrel fileindex.ts
inlib-a
, sincefile-to-move.ts
is not part of the library anymore - a new export for
file-to-move.ts
must be added to the barrel fileindex.ts
inlib-b
- all import statements - in this case only in
main.tx
must be updated to the new path:@nx-refactor/lib-b
Additional info
- after cloning the project run
yarn install
- to build the app run
nx build app
- to serve the app run
nx serve app
References: