cornerstonejs / cornerstone3D

Cornerstone is a set of JavaScript libraries that can be used to build web-based medical imaging applications. It provides a framework to build radiology applications such as the OHIF Viewer.
https://cornerstonejs.org
MIT License
563 stars 288 forks source link

Uncaught ReferenceError: Cannot access 'Iw' before initialization #579

Open MYWpro opened 1 year ago

MYWpro commented 1 year ago

Hello, when I used Cornerstone3d in vite and deployed it on the server, the page was blank and an error was reported. Why is this? I have no problem running locally. image image

biu8bo commented 1 year ago

I am using Vite and have had no problems in the development environment. However, after compiling and bundling the code, I encountered the same error in the same location as you did. When I switched to Webpack, the problem seemed to disappear. Moreover, this issue only occurs in 3D.

biu8bo commented 1 year ago

TMD I suspect both of them have issues - there may be some problems with the framework, and also some issues with Vite's bundling process.

MYWpro commented 1 year ago

I am using Vite and have had no problems in the development environment. However, after compiling and bundling the code, I encountered the same error in the same location as you did. When I switched to Webpack, the problem seemed to disappear. Moreover, this issue only occurs in 3D.

@2738644273 If using Webpack, can it run normally after deployment?

biu8bo commented 1 year ago

I am using Vite and have had no problems in the development environment. However, after compiling and bundling the code, I encountered the same error in the same location as you did. When I switched to Webpack, the problem seemed to disappear. Moreover, this issue only occurs in 3D.

@2738644273 If using Webpack, can it run normally after deployment?

yes ,you can try

shunia commented 1 year ago

If you're using vitejs, you have to import core and tools as seperated imports:

import * as Tools from '@cornerstonejs/tools';

to:

import { init as ToolInit, ToolGroupManager, addTool, PanTool, ZoomTool, WindowLevelTool } from '@cornerstonejs/tools';

It's kinda of a race condition when bundling.

MYWpro commented 1 year ago

If you're using vitejs, you have to import core and tools as seperated imports:

import * as Tools from '@cornerstonejs/tools';

to:

import { init as ToolInit, ToolGroupManager, addTool, PanTool, ZoomTool, WindowLevelTool } from '@cornerstonejs/tools';

It's kinda of a race condition when bundling.

I tried your method, but it still doesn't seem to work @shunia image image

shunia commented 1 year ago

@MYWpro try reduce your code base to minimize the impact from any other codes and iterate over to see where the problem exists.

abustany commented 10 months ago

I believe this is caused by circular dependencies, rollup doesn't support them when bundling. See #742 for more details, there's no proper fix I know of at this stage.

abustany commented 10 months ago

I needed a workaround before circular dependencies (hopefully) get fixed in the 2.0 release. Here's the stuff that I tried without success:

what worked for me in the end was to tell Rollup to build the UMD build of cornerstone3D/tools (core somehow bundles fine) for production builds. Here's the relevant vite.config.ts snippet:

  resolve: {
    alias: [
      ...(process.env.NODE_ENV === "production"
        ? [
            {
              // Rollup can't bundle @cornerstonejs/tools because of circular dependencies.
              //
              // See https://github.com/cornerstonejs/cornerstone3D/issues/742
              find: "@cornerstonejs/tools",
              replacement: "./node_modules/@cornerstonejs/tools/dist/umd/index.js",
            },
          ]
        : []),
    ]
  }

I suppose this is not optimal for tree shaking, but at this stage I'd rather add a few KBs to my bundle and get something working :shrug:

JeffreyWW commented 7 months ago

the same error with the code:

import {annotation, drawing, utilities} from '@cornerstonejs/tools'; const { drawHandles: drawHandlesSvg, } = drawing;

const {getAnnotations} = annotation.state;

const {triggerAnnotationRenderForViewportIds} = utilities; const {getCanvasEllipseCorners} = utilities.math.ellipse;

I fix it when I change it to:

import {annotation, drawing, utilities} from '@cornerstonejs/tools';

const drawHandlesSvg = drawing.drawHandles const getAnnotations = annotation.state.getAnnotations;

const triggerAnnotationRenderForViewportIds = utilities.triggerAnnotationRenderForViewportIds; const getCanvasEllipseCorners = utilities.math.ellipse.getCanvasEllipseCorners;

so if you are using Vite,don't use the {} to get the variable from the framework