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

Refactor researcher app to use ECMAScript modules (ESM) #496

Merged
merged 17 commits into from Mar 15, 2024
Merged

Refactor researcher app to use ECMAScript modules (ESM) #496

merged 17 commits into from Mar 15, 2024

Conversation

barbarah
Copy link
Contributor

@barbarah barbarah commented Feb 27, 2024

This pull request details the transition of the research app and dataset browser from CommonJS to ECMAScript Modules (ESM). The motivation behind this move is to align our projects with modern JavaScript standards, improve module interoperability, and prepare our codebase for future scalability and performance enhancements.

Next.js 13+ supports ESM simply by adding type: module to your package.json and renaming next.config.js → next.config.mjs.

Step 1: Type module in package.json

Node.js allows JavaScript to be interpreted as an ES module via the package.json "type" field with a value of "module". Most config files are in CommonJS, so they need to be updated. If possible, I used .ts for config files. For others, alternative formats were chosen, as detailed below.

Step 2: Updating the typescript config

For Next.js, the recommended settings are module: esnext and moduleResolution: bundler:

module: esnext instructs TypeScript to output ESM syntax, similar to nodenext, but it's a more general setting that isn't tailored exclusively to Node.js. It's suitable for code running in various environments, including browsers and bundlers (like Webpack, which Next.js uses internally).

moduleResolution: bundler tells TypeScript that module resolution will be handled by a module bundler, which can apply additional transformations or optimizations to the modules beyond what Node.js's native ESM resolution does.

These changes needed some minor updates:

  1. src/i18n.ts gave errors; I updated it based on the newest example code of next-intl
  2. package iso-639-1-dir showed some typescript errors, I replaced it with the package it was forked from (and now updated) iso-639-1.

Copy link

vercel bot commented Feb 27, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
colonial-collections-dataset-browser ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 15, 2024 8:54am
colonial-collections-researcher ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 15, 2024 8:54am

@barbarah
Copy link
Contributor Author

@sdevalk This is the first part of the ESM conversion. I have only updated the researcher app, but now I know what needs to be done, I can easily change the rest.

I have NOT changed the module setting in typescript to nodenext. This gave a lot of errors and needs a lot more refactoring. I'm not sure if this pull request makes sense without this typescript setting. Changing this will take some time.

Copy link
Contributor

@sdevalk sdevalk left a comment

Choose a reason for hiding this comment

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

Nice work! I've added two questions.

packages/tailwind-config/index.ts Outdated Show resolved Hide resolved
packages/ui/package.json Outdated Show resolved Hide resolved
@barbarah
Copy link
Contributor Author

Renaming the config packages did give a problem. Cypress gave a weird error after renaming the package tsconfig to @colonial-collections/ts-config. This disappeared after using a relative path for the typescript extends. I think this has to do with the ts-node Cypress uses; there are multiple ts-node issues with extends. Looking at the issues, I believe ts-node works better with a relative path. I see we already use relative paths for our base config: https://github.com/colonial-heritage/colonial-collections/blob/main/packages/tsconfig/base.json.

@barbarah barbarah requested a review from sdevalk March 15, 2024 09:30
Copy link
Contributor

@sdevalk sdevalk left a comment

Choose a reason for hiding this comment

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

Nice!

@barbarah barbarah merged commit f3b8520 into main Mar 15, 2024
6 checks passed
@barbarah barbarah deleted the esm branch March 15, 2024 12:50
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.

None yet

2 participants