Skip to content
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

Tests: use ES modules #23315

Merged
merged 7 commits into from
Jan 24, 2022
Merged

Tests: use ES modules #23315

merged 7 commits into from
Jan 24, 2022

Conversation

marcofugaro
Copy link
Contributor

@marcofugaro marcofugaro commented Jan 24, 2022

Related issue: Fixes #23304

Description

While we're at it, let's update the tests to use es modules introduced in Node 16.
No need to use rollup in tests anymore.

Following the Node.js docs, the new package.json looks like this:

"type": "module",
"main": "./build/three.js",
"module": "./build/three.module.js",
"exports": {
  ".": {
    "import": "./build/three.module.js",
    "require": "./build/three.js"
  },
  "./examples/jsm/*": "./examples/jsm/*.js",
  "./src/*": "./src/*.js"
},

Note that I had to include the src folder in the exports field as well, since the tests are importing from the src folder.

I also added the .js extension to imports since in esmodules (both web and node), the extension has to be explicit.

You can test this PR by:

  1. make sure you have Node 16 or greater
  2. npm i --prefix test
  3. npm run test-unit
  4. npm run test-unit-examples

To test the browser version

  1. npm i --prefix test
  2. npm start
  3. Open http://localhost:8080/test/unit/UnitTests.html

Note that the web tests work only in Chrome/Edge because of import maps. The import maps polyfill doesn't work with Qunit because it's syncronous.

/cc @takahirox

@mrdoob
Copy link
Owner

mrdoob commented Jan 24, 2022

Nice!

Do we still need the files section?

three.js/package.json

Lines 12 to 23 in be9d017

"files": [
"build/three.js",
"build/three.min.js",
"build/three.module.js",
"examples/js",
"examples/jsm",
"examples/fonts",
"LICENSE",
"package.json",
"README.md",
"src"
],

@mrdoob mrdoob added this to the r137 milestone Jan 24, 2022
@marcofugaro
Copy link
Contributor Author

Do we still need the files section?

Yes 🙂 that field contains the files that are downloaded when a user installs three from npm.
The exports field indicates the files directly available for import.

"import": "./build/three.module.js",
"require": "./build/three.js"
},
"./examples/jsm/*": "./examples/jsm/*.js",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if we renamed this? Say...

"./addons/*": "./examples/jsm/*.js",

Would people be able to this?

import { OrbitControls } from 'three/addons/controls/OrbitControls.js'

And, would the old version stil work?

import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'

Copy link
Contributor Author

@marcofugaro marcofugaro Jan 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently yes, you can do that, from the docs:

And, would the old version stil work?

Only if you have both of the import aliases in the exports field:

"./addons/*": "./examples/jsm/*.js",
"./examples/jsm/*": "./examples/jsm/*.js",

Otherwise node (or any bundler) throws an error if you're trying to import something that is not in the export fields.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent! That makes thing easy.

What do you think about the name addons? Do you think @drcmda will like it? 🤓

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hahaha I don't mind the name!

I even made a three-addons package back when examples/jsm wasn't a thing 😆

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll keep it in mind 🙏

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrdoob i do like it 😁

@mrdoob mrdoob merged commit 1c685e6 into mrdoob:dev Jan 24, 2022
@mrdoob
Copy link
Owner

mrdoob commented Jan 24, 2022

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Some unit tests on web fails because of undefined three
3 participants