thegreenwebfoundation / co2.js

An npm module for accessing the green web API, and estimating the carbon emissions from using digital services
Other
389 stars 49 forks source link

Add v0.10.0 hybrid module generation for node, es modules and browsers #91

Closed mrchrisadams closed 2 years ago

mrchrisadams commented 2 years ago

This PR adds hybrid module generation so we have specific builds.

Changes in detail

Runtime specific esbuilds commands

We now have three invocations of esbuild, which generate:

hybrid module entry points

For this to be possible, we need different code entry points

For nodejs

Some software consuming co2.js uses it in server side nodejs projects like sitespeed.io. which use commonjs variants. To support this, the esbuild.nodejs runs a build to generates a CommonJS compatible build and places it in ./dist/cjs/index-node.js.

For modern ES module capable clients

Most of other software likely would consume co2.js as ES modules (referred to as esm). The entry point for esm code is in dist/esm/index.js. This is likely the more common approach going forward.

These build is what is consumed by Skypack in this codepen and Observable in this demo notebook

For use in browsers when you don't want to faff around

Finally, for folks who do not want to rely on third party hosted services or fiddly build steps, we have a single file version presented as an immediately invoked function express.

This can be run locally with the npm run serve command if you check out this repo, as a demo