gkjohnson / three-mesh-bvh

A BVH implementation to speed up raycasting and enable spatial queries against three.js meshes.
https://gkjohnson.github.io/three-mesh-bvh/example/bundle/raycast.html
MIT License
2.38k stars 247 forks source link

Import error with NextJS #659

Closed benjaminbours closed 2 months ago

benjaminbours commented 2 months ago

Describe the bug

I started a project with the version 0.6.8. Everything was working as expected. When I update to 0.7.0 or any superior version, I got this compilation error (I am using nextjs 14.1.0):

 ⨯ ../node_modules/three-mesh-bvh/src/index.js
Attempted import error: 'bvh_struct_definitions' is not exported from './gpu/BVHShaderGLSL.js' (imported as 'BVHShaderGLSL').

Which is not true, I checked the files in my node_modules/three-mesh-bvh folder and it's of course exported.

To Reproduce

Steps to reproduce the behavior:

  1. Access a page compiled with nextjs 14.1.0 containing a client side component using three-mesh-bvh version at least 0.7.0
  2. See error describe earlier.

Code

// code goes here

Live example

I have not live example yet but could deploy one if it's required. Don't know if it's relevant in that case.

Expected behavior

I would like the lib to load correctly, as it was the case in 0.6.8 while having the update from the last releases.

Screenshots

Not relevant, I already printed the error.

Platform:

Additional info

@gkjohnson first, thank you for the great job on this lib! I am currently working on this personal project where your lib is completely key to make it possible => https://dev.compositethegame.com/en | https://github.com/benjaminbours/Composite/tree/level-builder-core-back (If you are not in europe, you will experience high latency) so without your work, it would be much more complicated for me, thank you.

My usage of this lib is probably unconventional, as I am even using it on the backend with NodeJS (there, no import problem with 0.7.0 though) because I have an authoritative server responsible to calculate the physic.

I assume you probably did something between 0.6.8 and 0.7.0 to influence the way the lib is loaded or packaged, do you remember any valuable information to solve this problem?

Thanks in advance,

Benjamin

benjaminbours commented 2 months ago

Ok I managed to find a hotfix to unlock my situation, in package.json, I did this change

"module": "build/index.module.js",

I don't know why your package.json is targetting src/index.js but you might have your reason.

Let me know if you agree with this fix and I can make a PR if you don't have time for this

gkjohnson commented 2 months ago

Nothing dramatic changed in 0.7.0 - just some file refactoring. Is this something you can reproduce in a new, minimal project? Looking at the code there doesn't seem to be anything wrong and I haven't heard of issues with other bundlers and I don't use nextjs myself.

Let me know if you agree with this fix and I can make a PR if you don't have time for this

I'd prefer not to make this change without understanding why. Importing the original src should be better for some bundlers when it comes to treeshaking and dead code elimination. The index.module.js file is only produced because people have asked for a standalone file to download.

If this can reproduced in a minimal case and it's not looking like the project code is structured incorrectly this may be something worth reporting to nextjs.

em3ai commented 1 month ago

"export * as BVHShaderGLSL from './gpu/BVHShaderGLSL.js", it is a syntex error, i think