Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It is common in many npm projects that the "main" field of the package.json points to the CJS modules. Currently we let the main field point to the UMD bundle. In theory this shouldn't be a problem as UMD supports CJS (as well as AMD and the browser). But it's a bit more complicated:
The UMD is built by Webpack; and when Webpack resolves the import paths of the dependencies it will by default use the ES modules of the libraries (if they provide them). Actually it shouldn't make a difference whether Webpack uses CJS or ES modules from a library. But in some libraries the code of ES and CJS differs slightly to work around plattform differences (e.g. Node vs. browser). That is, the UMD bundle may not work in in CJS environments out of the box even if a library provides alternative CJS code that will work.
Here is an example of an issue with the Mirador UMD bundle and Jest
Somewhere in my app:
And the test:
Currently without the fix of this PR Jest will throw an error saying:
The problem here is that Jest is a CJS environment and runs the Mirador UMD bundle that was refered to by pkg.main. The bundle contains code from UUID that uses the browser crypto API which is not supported by jest/jsdom. I think this is not intended by UUID because it also provides code that will work under Jest (see uuid/dist/rng.js).
We can fix that by pointing pkg.main field to the CJS modules of Mirador. That way a CJS environment can decide by itself what kind of modules to import by analyzing the package.json of the libraries.
Do you see any trouble with that? Do you know any tool that expects a UMD bundle at pkg.main rather than a CJS module? Could that change break any integration?