cornerstone3D icon indicating copy to clipboard operation
cornerstone3D copied to clipboard

fix: package exports

Open Blackman99 opened this issue 1 year ago • 28 comments
trafficstars

Context

The current main field in package.json would cause app fail to import @cornerstone/xxx packages. For example the current main field in @cornerstonejs/core package.json is src/index.ts, this would import the package as source code. But after bundled, it should not be imported as source code in other's projects. It would case error: can not resolve src/index.ts in @cornerstonejs/core . It could happen in framework like NextJS or any project that doesn't recognize @cornerstonejs/xxx package as source code. As metioned in #882, but it is for beta 2 version This PR is created for those using version 1

Changes & Results

Add exports field in package.json in these packages to distinguish cjs and mjs usage

  • @cornerstonejs/core
  • @cornerstonejs/tools
  • @cornerstonejs/nifti-volume-loader
  • @cornerstonejs/streaming-image-volume-loader

Testing

None

Checklist

PR

  • [x] My Pull Request title is descriptive, accurate and follows the semantic-release format and guidelines.

Code

  • [x] My code has been well-documented (function documentation, inline comments, etc.)

  • [x] I have run the yarn build:update-api to update the API documentation, and have committed the changes to this PR. (Read more here https://www.cornerstonejs.org/docs/contribute/update-api)

Public Documentation Updates

  • [x] The documentation page has been updated as necessary for any public API additions or removals.

Tested Environment

  • [x] "OS: Ubuntu 23.10
  • [x] "Node version: 20.10.0 & 18.19.0
  • [x] "Browser: Chrome 120.0.6099.201

Blackman99 avatar Dec 19 '23 13:12 Blackman99

Deploy Preview for cornerstone-3d-docs ready!

Name Link
Latest commit a651873aacc3cb964c8eb78d67ef500a9791f126
Latest deploy log https://app.netlify.com/sites/cornerstone-3d-docs/deploys/65b224210c896200085d40ab
Deploy Preview https://deploy-preview-960--cornerstone-3d-docs.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Dec 19 '23 13:12 netlify[bot]

For some reason this is failing the build You can try it cd packages/dicomImageLoader then yarn build

it gives this error

CleanShot 2024-01-08 at 09 18 50

Looks like the .webpack/webpack-bundle.js forgot to make @cornerstonejs/core package as external. I've added it in this PR. But it should has nothing to do with the package.json changes. It should also be existing in the original repo.

Blackman99 avatar Jan 10 '24 12:01 Blackman99

Thanks for the update, one more thing. Can you please enhance the description of the PR on when it actually fails to import when you mentioned, which framework?

The current main field in package.json would cause app fail to import @cornerstone/xxx packages

sedghi avatar Jan 10 '24 13:01 sedghi

Thanks for the update, one more thing. Can you please enhance the description of the PR on when it actually fails to import when you mentioned, which framework?

The current main field in package.json would cause app fail to import @cornerstone/xxx packages

My bad for not clearly describing it. Added in the description of this PR.

Blackman99 avatar Jan 11 '24 01:01 Blackman99

I know we merged in 2.x, since that was beta and less restrictions, but for 1.x which is considered stable I would like to get reproducible testing steps. What I'm looking for are steps (repo, any thing that I can clone, try) and shows the error and then try your fix and make sure it is fixed. You mentioned NextJS, do you have a playground in NextJS that you can share that would fail on the main branch but works when we apply this PR fix?

sedghi avatar Jan 11 '24 03:01 sedghi

I know we merged in 2.x, since that was beta and less restrictions, but for 1.x which is considered stable I would like to get reproducible testing steps. What I'm looking for are steps (repo, any thing that I can clone, try) and shows the error and then try your fix and make sure it is fixed. You mentioned NextJS, do you have a playground in NextJS that you can share that would fail on the main branch but works when we apply this PR fix?

I can see your concern. Here's a reproduction link with stackbliz: https://stackblitz.com/edit/stackblitz-starters-q3ogjh?file=app%2Fpage.tsx Simply run this demo online will get this error: image

Blackman99 avatar Jan 11 '24 05:01 Blackman99

Thanks for the test. I downloaded the project on stackBlitz to local, confirmed the bug, now I applied your patch/fix here, but still does not work.

CleanShot 2024-01-11 at 08 20 03

Have you tried your fix in the stackBlitz you sent?

sedghi avatar Jan 11 '24 13:01 sedghi

Thanks for the test. I downloaded the project on stackBlitz to local, confirmed the bug, now I applied your patch/fix here, but still does not work.

CleanShot 2024-01-11 at 08 20 03

Have you tried your fix in the stackBlitz you sent?

If you want to fix this in the stackbliz. You can only apply the package.json changes to the local installed packages in node_modules

Blackman99 avatar Jan 12 '24 01:01 Blackman99

@Blackman99 These were my steps

  1. Downloaded your stackblitz to local
  2. yarn/npm install (don't remember which)
  3. Saw the original error you posted
  4. Went into the local node_modules in the project (downloaded)
  5. Applied the fixes
  6. Saw new error I posted above

Am i missing something?

sedghi avatar Jan 12 '24 02:01 sedghi

@Blackman99 These were my steps

  1. Downloaded your stackblitz to local
  2. yarn/npm install (don't remember which)
  3. Saw the original error you posted
  4. Went into the local node_modules in the project (downloaded)
  5. Applied the fixes
  6. Saw new error I posted above

Am i missing something?

After the changes applied. You may also need to clear the next cache by deleting the .next directory. Then restart the project

Blackman99 avatar Jan 12 '24 03:01 Blackman99

I tried deleting the .next and doesn't work still show me the new error. Are you certain it works for you after you apply that patch?

sedghi avatar Jan 12 '24 03:01 sedghi

I tried deleting the .next and doesn't work still show me the new error. Are you certain it works for you after you apply that patch?

Sorry, I forgot a change which is to remove main field in package.json

Blackman99 avatar Jan 12 '24 03:01 Blackman99

But removing main field would cause development become complex. I understand that the current main field point to src is to provide convience for developing. So that the packages can reference each other during development.

But after bunduled. The main field should no longer be needed. And will cause the error. Using exports filed would be common practice.

Blackman99 avatar Jan 12 '24 03:01 Blackman99

Still no luck

CleanShot 2024-01-11 at 22 18 23

I did remove .next first before yarn dev

sedghi avatar Jan 12 '24 03:01 sedghi

That's strange. I'm using yarn by the way

Blackman99 avatar Jan 12 '24 03:01 Blackman99

I've noticed that in your screenshot. The package.json in the @cornerstonejs/streaming-image-volume-loader is not shown. Did you also apply the changes?

image

Blackman99 avatar Jan 12 '24 03:01 Blackman99

Yeah applied there, didn't notice not including it in the screenshot

sedghi avatar Jan 12 '24 03:01 sedghi

CleanShot 2024-01-11 at 22 32 01

sedghi avatar Jan 12 '24 03:01 sedghi

I don't quite know the reason. My changes basically are the same. image And my /page compiling is passed as shown in the terminal.

Blackman99 avatar Jan 12 '24 06:01 Blackman99

I did a fresh download from stackblits and tried the steps, still the same new error. What is your node version? mine is 18.17.0

CleanShot 2024-01-12 at 08 22 24

sedghi avatar Jan 12 '24 13:01 sedghi

can you zip the whole folder with node_modules and everything and send it to me, just to see if that works, then we take a diff

sedghi avatar Jan 12 '24 13:01 sedghi

I did a fresh download from stackblits and tried the steps, still the same new error. What is your node version? mine is 18.17.0

CleanShot 2024-01-12 at 08 22 24

I've tried on node 20.10.0 and 18.19.0. And my OS is Ubuntu 23.10.

Blackman99 avatar Jan 14 '24 07:01 Blackman99

i think, it's very necessary for us to support MWA. for example react、angular、Vue .etc framework. resolve and pull it, it seems beneficial to development. thank you~

baokeyu123 avatar Jan 17 '24 02:01 baokeyu123

@baokeyu123 Have you tried the stack blitz locally and tried the fix here and can confirm it resolves it?

sedghi avatar Jan 19 '24 13:01 sedghi

I've tried on Windows10. It works as expected. How can I give the node_modules.zip file? @sedghi

Blackman99 avatar Jan 22 '24 02:01 Blackman99

maybe dropbox or something? zip it

sedghi avatar Jan 24 '24 14:01 sedghi

Try this: https://www.dropbox.com/scl/fi/49pnfgguek83m7210j801/node_modules.zip?rlkey=nkvy8o6dmp2lym0otqt60b2hq&dl=0

Blackman99 avatar Jan 26 '24 07:01 Blackman99

@IbrahimCSAE can you please check this for me? Please read through the conversation and reproducible steps and see if the fix works

sedghi avatar Feb 15 '24 14:02 sedghi

I did it in beta version, can you check it @Blackman99 ?

sedghi avatar Jun 21 '24 16:06 sedghi