-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Examples: Convert jsm files to use bare three import before npm publish #21654
Conversation
I like it! :) But let's increase the difficulty level; there are also imports from other three.js/examples/jsm/loaders/KTX2Loader.js Lines 24 to 34 in e07eca4
|
This comment has been minimized.
This comment has been minimized.
Actually those shouldn't be an issue. The purpose of this is to fix the potential for a bundler to resolve "three" to a different file than "build/three.module.js". The other imports do not run this risk and generally importing using relative file paths in the examples is not a problem. |
Ah good point! One other edge case, suppose the user imports both KTX2Loader and BasisTextureLoader: import { KTX2Loader } from 'three/examples/jsm/loaders/KTX2Loader.js';
import { BasisTextureLoader } from 'three/examples/jsm/loaders/BasisTextureLoader.js'; Because KTX2Loader depends on BasisTexture loader, now the bundler is being asked to resolve both of these paths:
But can't see what else that could possibly resolve to, so this sounds safe to me, but wanted to call it out. |
Those two paths should be fine. The core of the issue is the discrepancy between the "main" and "module" fields in package.json. The two paths you mention will unambiguously point to the same file. |
Sounds good. I'd like to test this with an |
Works for https://gltf-viewer.donmccurdy.com/! (built with Parcel 1.12) I guess the remaining question would be whether we need to deal with the |
Um, on what quirks are you referring to? |
See #17482 (comment). This change doesn't directly break anything, but does change which CDNs these files support (Skypack yes, Unpkg no). Files not packaged as normal ES Modules (e.g. |
Hmm, what will happen with those? 🤔 |
See brief discussion here #17482 (comment). The I've been doing a bit of work with emscripten recently and from looking at the options it doesn't afford the ability to convert those requires to use the asynchronous "import()" (might be worth making an issue for), which might work better with skypack depending on how they're used. I just did a quick emscripten test with one of my other projects and it does look like setting the ENVIRONMENT option to use "web,worker" will result in no require statements in the final result which I think is what would work in this case. I'm not sure if there's a reason draco_decoder would need to be built with node.js in mind or why the "web,worker" version of the build wouldn't work in node in the first place (I don't think a file system would be used in it?) but if there isn't one then we could make a request to the draco maintainers to include that emcc option. You can see the current skypack-transformed state of draco_decoder here (notice the two generated imports at the top). I'm not sure if there are other files in /libs that might have this issue. All of the other vanilla jsm files maintained by the project should work as expected. |
The Draco team publishes npm packages ( |
@donmccurdy Maybe that's another thing the |
I don't think we necessarily think we need the node version. The browser version would need to be be rebuilt by the draco team to target something like the "web" and "worker" environments to avoid including the require statements. Normally when bundling packages like this you'll setup something like wepback to have a fallback for "fs" or "path" so everything will build without error knowing the package will correctly detect if it's running in node or not. Skypack is instead trying to import the packages immediately (because browsers have no synchronous approach to dynamic imports) and not giving the file a chance to determine whether it's running in node or not in the first place. Just looking through the files in the
Having said that none of this will break existing projects that are installing three.js via npm and it will fix projects that are having the common.js vs module duplicate import issue. My opinion is that we shouldn't fret too much about these libraries unless they are easily fixable. |
I'll work on a proposal to the Draco team, I have some other suggestions about their NPM package anyway. But I do agree with @gkjohnson, I don't particularly want the /libs dependencies to be burdened with Node.js support. Node.js interoperability with ES modules is really messy, but these users can easily bring in the official Draco NPM package. That package does not work on the web at this time. If the Draco team isn't interested in publishing a build with ES Modules, it shouldn't be difficult to compile it ourselves with custom flags. |
It is a bit messy. But really this is only a problem with Skypack -- there aren't any other great options to do what skypack is trying to do because there's no synchronous browser-native equivalent of |
I think it does - see https://emscripten.org/docs/tools_reference/emcc.html#emcc-o-target. Could rename the |
Heh you'd think that would work 😁. From my testing previously setting the output to use the Generally I'm pretty impressed with the flexibility and number of options that emscripten has -- it just doesn't seem to be able to do this yet. |
Not sure I understand what the consensus is here... |
Ok, I don't think we need to block this PR on the questions about |
Alright. Lets do this! 💪 |
Thanks! |
I think we can close #17482 now? |
I think so! |
…before publish (mrdoob#21654)" This reverts commit 53e3b99.
This change is breaking change for me. I could not import modules from three.js with vanilla JS anymore. For example:
Now it is broken, because OrbitControls.js couldn't resolve import |
You should not ever write |
'three/examples/jsm/controls/OrbitControls.js' is not valid link in vanilla JS. |
It is definitely a valid link in Node.js. |
UPDATE: I found kind a vanilla JS solution by using |
|
Interesting, was not aware of that limitation. But having them in the same repo was confusing for contributors... Maybe someone can create a new repo for them and run the original script every release? |
Related issue: #17482
Description
Adds a "prepublishOnly" npm script that will convert the jsm examples to import from "three" rather than "three.module.js" before publish. The changes will not be committed but will but will be published to NPM.
After publish there will be modified files in git but we can add a postpublish script of something like "git checkout ./examples/jsm/" if desired.
cc @donmccurdy @Mugen87