cornerstone3D icon indicating copy to clipboard operation
cornerstone3D copied to clipboard

[Bug] Cornerstone3D tools does not build with vite

Open PintoGideon opened this issue 1 year ago • 11 comments

Describe the Bug

I am currently using cornerstone3D in a react app. I get this error when running an npm run build during build:

Error: Could not load /home/gideonp/ChRIS_ui/node_modules/@icr/polyseg-wasm/dist/ICRPolySeg.wasm (imported by node_modules/@icr/polyseg-wasm/dist/index.js): "ESM integration proposal for Wasm" is not supported currently. Use vite-plugin-wasm or other community plugins to handle this. Alternatively, you can use `.wasm?init` or `.wasm?url`. See https://vitejs.dev/guide/features.html#webassembly for more details.
    at Object.load (file:///home/gideonp/ChRIS_ui/node_modules/vite/dist/node/chunks/dep-9A4-l-43.js:62695:19)
    at file:///home/gideonp/ChRIS_ui/node_modules/rollup/dist/es/shared/node-entry.js:18692:40
    at async PluginDriver.hookFirstAndGetPlugin (file:///home/gideonp/ChRIS_ui/node_modules/rollup/dist/es/shared/node-entry.js:18592:28)
    at async file:///home/gideonp/ChRIS_ui/node_modules/rollup/dist/es/shared/node-entry.js:17762:33
    at async Queue.work (file:///home/gideonp/ChRIS_ui/node_modules/rollup/dist/es/shared/node-entry.js:18802:32)

I tried instaling vite-plugin-wasm with no luck. Anyone else seeing this error ?

Steps to Reproduce

  1. Install Vite and the cornerstone3D tools
  2. npm run build

The current behavior

N/A

The expected behavior

N/A

OS

Linux

Node version

Nodejs 20.7

Browser

Chrome

PintoGideon avatar Feb 09 '24 22:02 PintoGideon

I'm @PintoGideon's coworker.

For anyone else here looking for a workaround, see https://github.com/FNNDSC/ChRIS_ui/pull/1060

UMD seems to be working but ESM (the default) does not. So create an alias in vite.config.ts to prefer the UMD build of @cornerstonejs/tools:

import { defineConfig } from "vite";

export default defineConfig({
  resolve: {
    alias: {
      "@cornerstonejs/tools": "@cornerstonejs/tools/dist/umd/index.js"
    },
  },
});

jennydaman avatar Feb 11 '24 00:02 jennydaman

https://github.com/cornerstonejs/cornerstone3D/issues/1067#issuecomment-1973384485

jlopes90 avatar Mar 01 '24 15:03 jlopes90

seems like someone tried this and works for now

alias: { '@cornerstonejs/tools': '@cornerstonejs/tools/dist/umd/index.js' } }`

sedghi avatar Mar 08 '24 13:03 sedghi

The umd works, but the problem is compressed and it doesn't seem to have a source map.

image

jlopes90 avatar Mar 08 '24 14:03 jlopes90

I know :( we will fix it in next version since it will be a breaking change

sedghi avatar Mar 20 '24 17:03 sedghi

seems like someone tried this and works for now

alias: { '@cornerstonejs/tools': '@cornerstonejs/tools/dist/umd/index.js' } }`

The code can be bundled, but it cannot run properly.

fanhenghui avatar Mar 22 '24 06:03 fanhenghui

While the "alias" workaround fixes some issues the current version still cannot run on vitest for example and gives the following error:

Module node_modules/@cornerstonejs/core/src/index.ts:1 seems to be an ES Module but shipped in a CommonJS package. You might want to create an issue to the package "@cornerstonejs/core" asking them to ship the file in .mjs extension or add "type": "module" in their package.json.

Are there plans for a proper fix?

desprit avatar Apr 03 '24 08:04 desprit

"node_modules@cornerstonejs\tools\src\workers\polySegConverters.js" is not packed to umd module and polyseg worker is not worked

Qwd2017 avatar Apr 23 '24 05:04 Qwd2017

Since there are many other people complaining about web workers and Vite as well, I'm adding this here.

https://github.com/vitejs/vite/issues/8427

sedghi avatar Jun 17 '24 13:06 sedghi

I modified the registerPolySegWorker.js code: image can be build, But there are still errors when starting in Nginx image image

vincent1996 avatar Jun 19 '24 09:06 vincent1996

I'm working on cornerstone3D 2.x and have made progress in removing circular dependencies and publishing the types for the DICOM image loader. I'm trying to make it work with Vite, but there are numerous issues with Vite in dev mode, which are not directly related to Cornerstone. Multiple mentions of Vite problems with import.meta.url, web workers, and WebAssembly can be found on the Vite GitHub repository:

https://github.com/vitejs/vite/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+import.meta.url

I have started a template project for Vite, React, and cornerstone3D beta 2.x here. I appreciate your help in making it work.

https://github.com/cornerstonejs/vite-react-cornerstone3d

sedghi avatar Jun 21 '24 14:06 sedghi

seems like someone tried this and works for now

alias: { '@cornerstonejs/tools': '@cornerstonejs/tools/dist/umd/index.js' } }`

@sedghi There is no umd file in version 2 or above How to solve it? (Vue+Vite)

T-en1991 avatar Nov 29 '24 08:11 T-en1991

Look at the dedicated vite configs https://www.cornerstonejs.org/docs/getting-started/vue-angular-react-etc

sedghi avatar Nov 29 '24 13:11 sedghi