ionic-framework: PouchDB not working with RC0 due to Rollup+CommonJS

See:

I took a stab at solving this, but ultimately failed due to another module besides PouchDB that Rollup can’t handle:

[06:21:04]  Error: Module /Users/nolan/workspace/cutePuppyPics/node_modules/rxjs/Subject.js does not export Subject (imported by /Users/nolan/workspace/cutePuppyPics/node_modules/@angular/core/src/facade/async.js)
    at Module.trace (/Users/nolan/workspace/cutePuppyPics/node_modules/rollup/dist/rollup.js:7677:29)
    at ModuleScope.findDeclaration (/Users/nolan/workspace/cutePuppyPics/node_modules/rollup/dist/rollup.js:7300:22)
    at Identifier.bind (/Users/nolan/workspace/cutePuppyPics/node_modules/rollup/dist/rollup.js:6489:29)
    at /Users/nolan/workspace/cutePuppyPics/node_modules/rollup/dist/rollup.js:5151:50
    at CallExpression.eachChild (/Users/nolan/workspace/cutePuppyPics/node_modules/rollup/dist/rollup.js:5165:19)
    at CallExpression.bind (/Users/nolan/workspace/cutePuppyPics/node_modules/rollup/dist/rollup.js:5151:7)
    at CallExpression.bind (/Users/nolan/workspace/cutePuppyPics/node_modules/rollup/dist/rollup.js:5842:23)
    at /Users/nolan/workspace/cutePuppyPics/node_modules/rollup/dist/rollup.js:5151:50
    at ParenthesizedExpression.eachChild (/Users/nolan/workspace/cutePuppyPics/node_modules/rollup/dist/rollup.js:5168:5)
    at ParenthesizedExpression.bind (/Users/nolan/workspace/cutePuppyPics/node_modules/rollup/dist/rollup.js:5151:7)

There probably exists some combination of Rollup plugins that can work for all npm packages that contain a mix of CommonJS and ES6 modules, but I’m not sure what it is; keep in mind that a lot of what Browserify/Webpack do by default, you have to do with plugins in Rollup-land. (E.g. I’m fairly sure you would need -json, -commonjs, -node-resolve, and -globals at the very least. FWIW this is why, for PouchDB’s own build, we opted to only use Rollup to take ES6 source and configure it to highly-optimized CommonJS, and we only used ES6 source for packages that we ourselves controlled. At that point, we assume our consumers are using Browserify or Webpack.)

On the other hand, maybe I’m just dense and @rich-harris can point us in the right direction. Or maybe PouchDB is misusing jsnext:main, and we shouldn’t have it point to a module that includes any CommonJS dependencies. I have no idea, honestly. I just know that I tried to solve this problem using only Rollup plugins and was unable.

My sample project in case anyone wants to play with this: I just did ionic create cutePuppyPics --v2 as in the guide and then added pouchdb-browser: https://github.com/nolanlawson/cutePuppyPics-with-pouchdb-browser

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 48 (5 by maintainers)

Most upvoted comments

Hello all! We have made some great progress and now have a working example of PouchDB with Ionic 2 RC.0. We are still throwing around ideas and reviewing this, but it looks like the key is to use namedImports in our rollup.config. I will share more once we have gone over all this. Thanks!

The circular dependency issue, and the fact that @calvinmetcalf was not able to find a solution via Rollup’s plugin system, leads me to believe that this is best solved by either 1) using Webpack/Browserify instead of Rollup, or 2) using Rollup in a very limited way (to produce a small CJS bundle), and then following up with Webpack/Browserify. I would recommend making the change to ionic-app-scripts’ bundle.ts.

I just released PouchDB 6.0.7. Our solution was to drop the js-extend dependency because Rollup can’t seem to handle it.

It’s not an ideal solution, but PouchDB now works out-of-the-box with Ionic 2. You can get the fix by merely doing npm update.

I’ll close this issue because the title itself is no longer applicable. The core problem still exists (e.g. if any Ionic users actually legitimately do want to use js-extend or similar modules with tricky export patterns) but I’ll leave it up to Ionic/Rollup if they’d like to fix the root problem.

Another possibility would be for the ionic-app-scripts to include a small whitelist of modules that are known to play nice with Rollup, and then use Browserify/Webpack for the rest.

are there any workarounds?

@audan2009 It worked for me. You have to update to @ionic/app-scripts because v0.0.27 doesn’t set up the process.env.IONIC_ENV correctly. With the latest version you have to remove ngTemplate from your config.

Here’s what worked for me:

npm install --save pouchdb
npm install --save pouchdb-quick-search
npm install --save-dev rollup-plugin-node-builtins
npm install --save-dev rollup-plugin-node-globals
npm install --save-dev rollup-plugin-json
npm install js-extend
npm install @types/pouchdb

I use pouchdb-quick-search but there are no typings for it, so I manually created:

node_modules/@types/pouchdb-quick-search/index.d.ts:

declare module 'pouchdb-quick-search' {
  var PouchQuickSearch: any;
  export = PouchQuickSearch;
}

I edited node_modules/@ionic/app-scripts/config/rollup.config.js with this: https://gist.github.com/shaggy8871/7cd2d8222ae67b2bf6d8d141f1d9c07c

I edited node_modules/rollup-plugin-node-builtins/dist/rollup-plugin-node-builtins.es6.js and node_modules/rollup-plugin-node-builtins/dist/rollup-plugin-node-builtins.cjs.js (didn’t know which is being used) by commenting out this line:

//libs.set('crypto', require.resolve('crypto-browserify'));

I also changed my require(‘pouchdb’) and require(‘pouchdb-quick-search’) calls to:

import PouchDB from 'pouchdb';
import PouchDB_Plugin_QuickSearch from 'pouchdb-quick-search';
PouchDB.plugin(PouchDB_Plugin_QuickSearch);

Some of these steps may not be necessary, but they worked for me 😃 Hope it helps someone.

@matheusgarcez’s approach didn’t work for me 😞