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

make gallery ESM compatible #1351

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft

make gallery ESM compatible #1351

wants to merge 13 commits into from

Conversation

dragonofdeath
Copy link
Contributor

@dragonofdeath dragonofdeath commented Feb 3, 2025

This PR fixes node esm consumption of pro-gallery library which is required to run in modern front-end framework. The fixing esm consists of:

  1. Adding explicit file extensions on every import( import ... from './file' -> import ... from './file.js') (Mandatory file extensions)[https://nodejs.org/api/esm.html#mandatory-file-extensions]
    This could be done in too ways: changing the source or changing the build output. But as pro-gallery uses tsc directly
    there is no possibility to modify build output.

  2. Dropping directory imports (import ... from './utils' -> import ... from '/utils/index.js')

  3. Adding 'exports' field in package json to help node find cjs & esm builds (doc)[https://nodejs.org/api/packages.html#exports]. The 'main' and 'module' fields are recognized only by bundlers, node ignores them. Using exports field in root package json is only way to have esm & cjs builds in one package

  4. Adding package.json with 'type: module' in esm dist folder. Node has to know the file module type before executing it and there are too posibilities:
    a. having explicit file extensions cjs & mjs.
    b. if the file ending is js it will decide by looking into nearest package.json and check its type field. If the type is equal to module it will interpret all js endings in the directory as mjs and will fallback to cjs in any other case (doc)[https://nodejs.org/api/esm.html#enabling]

Few other things had to be done:

  1. Its very complicated to make ts-node work with esm (its used in unit tests mainly), thus I've replaced it with tsx library. And tsx can't be loaded with mock in node-16, so the bump to node-18 had to be done.

  2. validator build was creating mixed umd/cjs/es6 module which node doesn't support. Dropped all the transpilation with borwserify, because the library itself is provided non-minified, this should not make problems for consumers.

  3. added ts-jest-resolver for jest specs so it would resolve files in typescript as typescript iteself does (allowing to import with 'js' extension even though the actual files in source directory have ts extension.

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.

1 participant