zooniverse / community-catalog

Zooniverse-powered website for exploring community-tagged images. Experimental as of Nov 2022.
https://community-catalog.zooniverse.org
0 stars 0 forks source link

Change to ESM (ECMAScript module) #165

Open shaunanoordin opened 1 year ago

shaunanoordin commented 1 year ago

Project Improvement / Code Standardisation

Suggestion as of f797e52

We should change this project to an ESM format. This can be done by adding { "type": "module" } to package.json (docs), which tells Node.js to treat our project as an ECMAScript module, instead of the default CommonJS module.

// package.json
{
  "type": "module",
  ...
}

Q: is it essential? A: nope!

Q: why do this? A: code standardisation.

Q: what's the catch? A: it kinda breaks the current builds.

Things To Do

Step 1: Add { "type": "module" } to package.json

Step 2: jest.config.js

Step 3: webpack.dev.js and webpack.prod.js

Status

Low priority for the moment, but if we can solve those gotchas, it'd be a nice improvement to have to the code. Thanks to Jim for pointing out why running Jest requires module.exports = ... in the config instead of export ..., in PR 163's discussions

eatyourgreens commented 1 year ago

Where you're currently doing something like:

import { subjects } from '@zooniverse/panoptes-js'

you're importing the entire client object, then destructuring client.subjects. That means that you're loading in dependencies that you don't need, like jose.

You should be able to get around this by importing just the object that you need. Something like this:

import subjects from '@zooniverse/panoptes-js/resources/subjects'
eatyourgreens commented 11 months ago

Node's documentation on interoperability with CommonJS, including __dirname and require.resolve.

https://nodejs.org/api/esm.html#differences-between-es-modules-and-commonjs

Also worth noting that you can rename the webpack config to .cjs if you need to use require.

eatyourgreens commented 11 months ago

If you're building a new package from scratch, then Rollup might be a better choice than Webpack, for bundling an ES module.