Viewers icon indicating copy to clipboard operation
Viewers copied to clipboard

Adding cornerstoneTools as workspace

Open MathisGuilhin opened this issue 7 years ago • 6 comments

Hello team,

I've been using and working on OHIF Viewer for 2 months and I was using yalc to make modifications on ohif-core, react-viewerbase and cornerstoneTools.

I saw that you made a lot of improvements on extension management and I'm now trying to sync my work with you new updates, but I'm having 2 problems :

  • cornerstoneTools is still used as dependencies, how could I add it as a workspace ?
  • I had added a tool, that was working with my version of the viewer. Now, when I try to use this tool with the new version (and my version of cornerstoneTools), I get cornerstoneTools.js:11846 Unable to find tool "Repulsor" for enabledElement. How are tools managed in the new version (I added it to the cornerstone toolBarModules, and i created a file for it in the tools folder in platform/core/src/measurements/tools). Have I missed another step?

Thanks for the help, and the great work you've done for managing extensions!

MathisGuilhin avatar Aug 27 '19 10:08 MathisGuilhin

I fixed the second problem in setupTools. Still, I don't know how to make of cornerstoneTools a workspace such as ohif-core, react-viewerbase, etc..

MathisGuilhin avatar Aug 27 '19 13:08 MathisGuilhin

👋 Hello @MathisGuilhin,

yalc has some issues with the monorepo setup because all dependencies are hoisted by yarn workspaces. This means that they sit in the root directory's node_modules folder. yalc, by default, will try to use the target project's node_modules folder.

I think, but haven't tried, to use Yarn's no hoist option for dependency's you would like to use with yalc. Ideally, you specify cornerstone-tools as a package that should not be hoisted, and then yalc behavior should continue to function as it has in the past.

You may also be able to npm link at the root level instead of at the package level, but I'm not positive.

If you intend to maintain a fork of cornerstone-tools indefinitely, you could update the lerna config to add a new project or project directory for it. Then, the dependency would resolve to your local copy, and likely build using it's source (with current defaults).

If you find a good solution that works well for you, please do report back. There are a lot of ways to approach this, and I think it's a question we'll see more and more frequently as people develop using the new setup.

dannyrb avatar Aug 27 '19 16:08 dannyrb

As a side note, please do read the comment I made in this related issue: https://github.com/OHIF/Viewers/issues/809#issuecomment-525358050

For your specific use case, you may very well not need to maintain a fork. Cornerstone and OHIF sponsored projects have been slowly working over the last couple of years to reduce the number of reasons that force users to maintain a fork. If you find yourself forking, let me know why and maybe there's something we can do about it.

dannyrb avatar Aug 27 '19 16:08 dannyrb

Hello, and thank you for this answer. I've been maintaining a fork of cornerstoneTools because I added some functionalities to the freehandMouseTool, and created a new tool as well. So if I get it right, I should not modify cornerstoneTools but use the extension mecanism to add new tools like with the segmentation-plugin? I may not have time to do this because Im near the end of my internship but this is something that can be considered for the future of the project I'm part of.

I may need some information about lerna config. Do I really need to change it? The idea is to have these 5 packages locally :

Capture

But I thought that it was something that I didn't need to specified because the config already says that every file in platform folder is a workspace, isn't it ?

Capture1

So I modified cornerstoneTools package.json to match others :

Capture2

But when I try to use it in other packages, it says 'module not found', even though I wrote it in dependencies

Capture3

Besides, when I modify a cornerstone-tools file, the project doesn't automatically compile.

Do you know what I have missed? Thank you again!

MathisGuilhin avatar Aug 28 '19 07:08 MathisGuilhin

Package management can be a pain -- I'll try to do my best to explain.

Best: Don't fork

In CornerstoneTools@3, every tool is a class that, at a minimum, fulfills the contract defined by the internal BaseTool class. This means you can create your tool outside of cornerstone-tools, and still add it as if it were one the tools that come bundled with the library.

import MyMagicToolClass from './MyMagicToolClass/index.js'
import cornerstoneTools from 'cornerstone-tools'

cornerstoneTools.addTool(MyMagicToolClass);

This change is new to cornerstone-tools as of version 3. We rewrote the entire library to make it possible to reduce the need to maintain a fork. It sounds like both of your tools would be great candidates for it.

If you took this approach, you would only need to modify where the Viewer project defines the available tools for react-cornerstone-viewport. One of the options is apiTool, which allows you to pass your own tool class.

No hoist and Yalc

At the repo root, specify that the cornerstone-tools package should not be hoisted. When you run yarn install, if configured correctly, the dependency should live at /platform/viewer/node_modules/cornerstone-tools. Now you should be able to leverage yalc as you normally would in the Viewer project.

Monorepo Package

This one is trickiest. If you change the package name, as you have done, you'll either need to update every single reference to the new package name, or add an alias in webpack so requests for cornerstone-tools resolve to @ohif/tools.

After the package has been added, and you're decently sure all references to it will resolve correctly, you'll need to run yarn install, so yarn workspaces can setup proper symlinks under the hood.

My suggestion to create a new directory was more for clarity. Platform and extensions have specific purposes. Something more explicit like forked-dependencies/* might be more fitting.

dannyrb avatar Aug 28 '19 11:08 dannyrb

Thank you for this well explained answer, I've managed to add it as a local forked-dependencies (I kept the name so I didn't have to change everyfile), it ended up working.

I think we will consider in the future to use the extension approach that looks better !

MathisGuilhin avatar Aug 28 '19 14:08 MathisGuilhin