Currently, idb offers itself in two flavours: as a CommonJS module through the browser field of package.json, and as an ES6 module through the module field.
As noted in webpack/webpack#4674, webpack prefers browser to module. Hence webpack will use the CommonJS module.
This is sub-optimal: idb has a perfectly good ES6 module, but webpack's not using it, leading to users not being able to write import { openDb } from "idb" in their .mjs files without extra configuration work (which itself may be of dubious correctness).
Fortunately it seems like there's a workaround, as noted in the webpack issue: browser can be a map that selectively overrides some modules, rather than being used in all cases. In this instance, it seems like we want to specifically override the main module (i.e., lib/node.js).
So something like this could work (which I haven't tested):
Currently,
idb
offers itself in two flavours: as a CommonJS module through thebrowser
field ofpackage.json
, and as an ES6 module through themodule
field.As noted in webpack/webpack#4674, webpack prefers
browser
tomodule
. Hence webpack will use the CommonJS module.However. when importing a CommonJS module from an
.mjs
file, you can only use a default import, not named imports.This is sub-optimal:
idb
has a perfectly good ES6 module, but webpack's not using it, leading to users not being able to writeimport { openDb } from "idb"
in their.mjs
files without extra configuration work (which itself may be of dubious correctness).Fortunately it seems like there's a workaround, as noted in the webpack issue:
browser
can be a map that selectively overrides some modules, rather than being used in all cases. In this instance, it seems like we want to specifically override themain
module (i.e.,lib/node.js
).So something like this could work (which I haven't tested):