thegreenwebfoundation / co2.js

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

Reduce package size #121

Closed mishushakov closed 7 months ago

mishushakov commented 1 year ago

We're only using co2.js for calculating emissions, thus don't need the full 430 kB package Would be amazing if you could split the database and the calculator into separate sub-packages

fershad commented 1 year ago

This is an interesting point, something that we should consider looking into. The database is used by the greencheck hosting API in the library so isn't needed for emissions calculations. It looks like the database is used for testing (very quick assessment), so might be safe to exclude from the package.

There's a bunch of extra stuff in the data folder that also gets packaged at the moment which are probably surplus to requirements as well.

@mishushakov are you able to tree-shake out the stuff you don't need, or is that not an option in your use case?

mishushakov commented 1 year ago

I'm not using it for frontend, so that's not an option currently

fershad commented 1 year ago

Reduced package size a bit by removing debug dependency in #144. Will keep looking for opportunities to shave off some more bytes.

EvanHahn commented 1 year ago

It looks like some files are published in the npm package that don't need to be. I ran npm pack --dry-run and got 74 results:

74 files (click to expand) ``` 870B .all-contributorsrc 225B .esbuild.browser.js 941B .esbuild.esm.js 376B .esbuild.node.js 8B .eslintignore 183B .gitpod.yml 6.2kB CHANGELOG.md 12.0kB LICENSE 562B NOTICE 6.1kB README.md 3.6kB data/output/average-intensities.js 32.4kB data/output/average-intensities.json 3.4kB data/output/marginal-intensities-2021.js 104.6kB data/output/marginal-intensities-2021.json 1.9kB dist/cjs/1byte.js 3.4kB dist/cjs/1byte.js.map 9.0kB dist/cjs/co2.js 15.3kB dist/cjs/co2.js.map 1.0kB dist/cjs/constants/file-size.js 306B dist/cjs/constants/file-size.js.map 2.4kB dist/cjs/constants/index.js 1.8kB dist/cjs/constants/index.js.map 4.7kB dist/cjs/data/average-intensities.min.js 6.1kB dist/cjs/data/average-intensities.min.js.map 4.6kB dist/cjs/data/marginal-intensities-2021.min.js 6.5kB dist/cjs/data/marginal-intensities-2021.min.js.map 7.7kB dist/cjs/helpers/index.js 9.9kB dist/cjs/helpers/index.js.map 2.0kB dist/cjs/hosting-api.js 2.8kB dist/cjs/hosting-api.js.map 2.6kB dist/cjs/hosting-json.node.js 4.7kB dist/cjs/hosting-json.node.js.map 3.0kB dist/cjs/hosting-node.js 4.6kB dist/cjs/hosting-node.js.map 1.5kB dist/cjs/hosting.js 706B dist/cjs/hosting.js.map 1.8kB dist/cjs/index-node.js 514B dist/cjs/index-node.js.map 2.0kB dist/cjs/index.js 629B dist/cjs/index.js.map 27B dist/cjs/package.json 7.6kB dist/cjs/sustainable-web-design.js 14.6kB dist/cjs/sustainable-web-design.js.map 1.0kB dist/esm/1byte.js 7.5kB dist/esm/co2.js 117B dist/esm/constants/file-size.js 797B dist/esm/constants/index.js 3.7kB dist/esm/data/average-intensities.min.js 3.5kB dist/esm/data/marginal-intensities-2021.min.js 6.7kB dist/esm/helpers/index.js 1.1kB dist/esm/hosting-api.js 195B dist/esm/hosting.js 372B dist/esm/index.js 25B dist/esm/package.json 7.3kB dist/esm/sustainable-web-design.js 16.0kB dist/iife/index.js 57.6kB dist/iife/index.js.map 365B fixup 29B index.d.ts 2.3kB package.json 2.6kB src/1byte.js 10.9kB src/co2.js 70B src/constants/file-size.js 1.3kB src/constants/index.js 3.1kB src/data/average-intensities.min.js 3.0kB src/data/marginal-intensities-2021.min.js 6.8kB src/helpers/index.js 1.9kB src/hosting-api.js 3.2kB src/hosting-json.node.js 3.3kB src/hosting-node.js 431B src/hosting.js 270B src/index-node.js 336B src/index.js 10.8kB src/sustainable-web-design.js ```

Many of these are needed but I'm sure some can be removed.

You could add them to .npmignore. I'm happy to help but I don't know which files are necessary.

(Alternatively if you want to switch from npmignore's blocklist style to an allowlist style, you can use the files key in package.json.)

fershad commented 8 months ago

Looking at this again, there's probably a few changes we can make to reduce package size:

Alternately, we should:

Guesstimating a reduction of about 200kB if we do this.

fershad commented 7 months ago

@mishushakov I'm closing this issue since release v0.14.1 is now published (unpacked size now 194 kB on NPM).