jshttp / mime-db

Media Type Database
MIT License
1.12k stars 260 forks source link

(Experimental) Add ESM support #300

Open marvin-j97 opened 1 year ago

marvin-j97 commented 1 year ago

This PR adds ESM support by adding an index.mjs file and the appropiate package.json field (module), I also added exports. The import uses the experimental JSON module syntax (https://nodejs.org/api/esm.html#json-modules), which requires Node 16.14+.

I also added a Github workflow and a new test file that uses Node's test runner. FWIW, the tests merged at some point in the future.


An alternative would be to use createRequire, but it does not seem well supported by bundlers: https://github.com/evanw/esbuild/issues/1828 https://github.com/rollup/rollup/issues/4274


Another alternative would be to just drop CommonJS altogether, and only provide a ESM export, dropping Node 0 - 16.13 support, and bumping one major version to mime-db@2.0.0. After all, everything up to Node 14 has reached EOL, even Node 16 is getting close. That way, the engines field in the package.json could also be adjusted appropiately.

Maybe some years down the line...


PS: It's 2023, I think it's OK to drop some old Node versions and io.js from the CI :) It could be massively simplified by just using a node-version matrix entry and the setup-node GH action.