feat: publish both ESM and CommonJS #63
Merged
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.
Currently, the library is distributed as a commonjs module, which at this point is starting to cause issues with frameworks that only support ES modules. To solve this, both a
commonjs
andesm
version is exported.Build output and
package.json
changesThis PR updates the published
package.json
to use anexports
field to define conditional exports dependent on the method of importing. Themain
field, and a newmodule
field, inpackage.json
are kept as a fallback. Thecommonjs
andesm
sources are published in separate directories, using separate Typescript configs, and using new scripts configured inpackage.json
. Running thenpm run build
script still runs all required steps to create a distributable package.As ESM packages need to explicitly set a
type
field in theirpackage.json
marking them as such, a helper script injects a minimal additonalpackage.json
into the twocjs
andesm
subdirectories. Otherwise, importing the esm version would fail as it would be considered acommonjs
module by default in many tools.Typescript Configuration Changes
The Typescript-exported sourcemaps are updated to inline the original module source so that source map and original source don't have to be included both in order to facilitate source mapping.
Supported ES syntax is updated to support the latest syntax for writing
react-ditto
by using theEXNext
configuration.debatable: The target ES version of the build output is updated from ES5 to ES6. Unless we have it at ES5 for a particular reason, I feel that ES6 support is wide spread enough at this point and some ES6 features (
WeakMap
) are required for Ditto to work anyway.Caveats
In ES modules,
import
statements need to include an unambiguous reference to a source. As the published package uses.js
files, the file ending of the TS sources needs to match that, even though the.js
files don't even exist yet. Unfortunately, tsc can't transform this during build (background).