I am attempting to use the memory adapter for PouchDB. I want to bundle my application (along with dependencies like Pouch and this adapter) using Rollup. In order to reduce this to a minimally reproducible issue imagine this is my application I want to bundle:
import PouchDB from 'pouchdb-browser'
import MemoryAdapterPlugin from 'pouchdb-adapter-memory'
PouchDB.plugin(MemoryAdapterPlugin)
Since I am using a node module I’m obviously going to need the rollup-node-resolve
Rollup plugin. Also many of PouchDB’s dependent modules are in CJS format so I’m going to also need the rollup-node-commonjs
Rollup plugin. Finally PouchDB makes use of some built-in Node modules (such as events
) and the memory adapter even more (buffer
, etc). I’m not sure these are used at runtime (it may be just in PouchDB’s code because it can work in Node or the browser) but to prevent a bundling error I’m going to also include the rollup-plugin-node-polyfills
plugin but direct the resolve plugin to prefer built-ins to the browser target if they are needed and available.
With all that in place here is my rollup config:
import commonjs from '@rollup/plugin-commonjs'
import resolve from '@rollup/plugin-node-resolve'
import nodePolyfills from 'rollup-plugin-node-polyfills'
export default {
input: 'index.js',
output: {
format: 'iife',
name: 'app',
file: 'bundle.js'
},
plugins: [
nodePolyfills(),
resolve({
preferBuiltins: true,
browser: true
}),
commonjs({
dynamicRequireTargets: [
'node_modules/memdown/*.js',
],
}),
]
}
The dynamicRequireTargets
option is added because without it I get some sort of circular dependency warning:
(!) Circular dependencies
node_modules/memdown/immediate-browser.js -> node_modules/memdown/immediate-browser.js
node_modules/memdown/immediate-browser.js -> /home/me/Data/Development/rollup-test/node_modules/memdown/immediate-browser.js?commonjs-proxy -> node_modules/memdown/immediate-browser.js
But even with that correction in place I still end up with the following error causing a failure to build the bundle:
[!] Error: 'default' is not exported by node_modules/ltgt/index.js, imported by node_modules/sublevel-pouchdb/lib/index.es.js
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
node_modules/sublevel-pouchdb/lib/index.es.js (1:7)
1: import ltgt from 'ltgt';
^
2: import events from 'events';
3: import Codec from 'level-codec';
Error: 'default' is not exported by node_modules/ltgt/index.js, imported by node_modules/sublevel-pouchdb/lib/index.es.js
Reading this error it indicates that sublevel-pouch (and internal package for PouchDB) which is in ES format (as indicated by the .es.js
) is importing the ltgt library with:
import ltgt from 'ltgt';
ltgt is in CJS format but that should be ok as the commonjs Rollup plugin should transform it to be in ES format. I think this specifically relates to the defaultIsModuleExports
option which should cause the default
property of the generated ES code to be equal to module.exports
. This all seems right and therefore there should be a default.
I’m not sure if the problem is my Rollup config, my understanding of CJS and ES format or it’s a bug in PouchDB. Any direction is appreciated!