(#6347) - fix build process, put pouchdb-find dist/ in pouchdb pkg
diff --git a/bin/build-module.js b/bin/build-module.js
index cd17332..d53171b 100755
--- a/bin/build-module.js
+++ b/bin/build-module.js
@@ -23,12 +23,6 @@
 var rimraf = denodeify(require('rimraf'));
 var builtInModules = require('builtin-modules');
 var fs = require('fs');
-var buildUtils = require('./build-utils');
-var addPath = buildUtils.addPath;
-var doUglify = buildUtils.doUglify;
-var doBrowserify = buildUtils.doBrowserify;
-var writeFile = buildUtils.writeFile;
-var camelCase = require('change-case').camel;
 var all = Promise.all.bind(Promise);
 
 // special case - pouchdb-for-coverage is heavily optimized because it's
@@ -41,10 +35,6 @@
 // packages that only have a browser version
 var BROWSER_ONLY_PACKAGES =
   ['pouchdb-browser'];
-// packages that need to publish a dist/ folder
-var PACKAGES_WITH_DIST_FOLDER =
-  ['pouchdb-find'];
-
 
 function buildModule(filepath) {
   var pkg = require(path.resolve(filepath, 'package.json'));
@@ -106,24 +96,6 @@
         }));
       });
     }));
-  }).then(function () {
-    if (PACKAGES_WITH_DIST_FOLDER.indexOf(pkg.name) === -1) {
-      return;
-    }
-    return rimraf(path.resolve(filepath, 'dist')).then(function () {
-      return mkdirp(path.resolve(filepath, 'dist'));
-    }).then(function () {
-      var thePath = path.resolve(filepath, 'lib/index-browser');
-      return doBrowserify(pkg.name, thePath, {
-        standalone: camelCase(pkg.name)
-      });
-    }).then(function (code) {
-      var scriptName = 'dist/pouchdb.' + pkg.name;
-      return all([
-        writeFile(addPath(pkg.name, scriptName + '.js'), code),
-        doUglify(pkg.name, code, '', scriptName + '.min.js')
-      ]);
-    });
   });
 }
 if (require.main === module) {
diff --git a/bin/build-pouchdb.js b/bin/build-pouchdb.js
index b3bdb02..480d6d0 100644
--- a/bin/build-pouchdb.js
+++ b/bin/build-pouchdb.js
@@ -32,7 +32,7 @@
 var external = Object.keys(require('../package.json').dependencies)
   .concat(builtInModules);
 
-var plugins = ['fruitdown', 'localstorage', 'memory'];
+var plugins = ['fruitdown', 'localstorage', 'memory', 'find'];
 
 var currentYear = new Date().getFullYear();
 
@@ -70,6 +70,15 @@
   '\n// PouchDB may be freely distributed under the Apache license, ' +
   'version 2.0.' +
   '\n// For all details and documentation:' +
+  '\n// http://pouchdb.com\n',
+
+  'find': '// pouchdb-find plugin ' + version +
+  '\n// Based on Mango: https://github.com/cloudant/mango' +
+  '\n// ' +
+  '\n// (c) 2012-' + currentYear + ' Dale Harvey and the PouchDB team' +
+  '\n// PouchDB may be freely distributed under the Apache license, ' +
+  'version 2.0.' +
+  '\n// For all details and documentation:' +
   '\n// http://pouchdb.com\n'
 };
 
@@ -151,7 +160,11 @@
 }
 
 function buildPouchDBNext() {
-  return doBrowserify('pouchdb', 'src/next.js', {standalone: 'PouchDB'}).then(function (code) {
+  return doRollup('src/next.js', true, {
+    cjs: 'lib/next.js'
+  }).then(function () {
+    return doBrowserify('pouchdb', 'lib/next.js', {standalone: 'PouchDB'});
+  }).then(function (code) {
     return writeFile('packages/node_modules/pouchdb/dist/pouchdb-next.js', code);
   });
 }
diff --git a/packages/node_modules/pouchdb-find/src/index.js b/packages/node_modules/pouchdb-find/src/index.js
index 393de6d..0bedd51 100644
--- a/packages/node_modules/pouchdb-find/src/index.js
+++ b/packages/node_modules/pouchdb-find/src/index.js
@@ -46,9 +46,4 @@
   deleteIndex(this, indexDef, callback);
 });
 
-export default plugin;
-
-/* istanbul ignore next */
-if (typeof window !== 'undefined' && window.PouchDB) {
-  window.PouchDB.plugin(plugin);
-}
+export default plugin;
\ No newline at end of file
diff --git a/packages/node_modules/pouchdb/src/next.js b/packages/node_modules/pouchdb/src/next.js
index fa797be..d149555 100644
--- a/packages/node_modules/pouchdb/src/next.js
+++ b/packages/node_modules/pouchdb/src/next.js
@@ -1,4 +1,9 @@
-module.exports = require('pouchdb-core')
-  .plugin(require('pouchdb-adapter-indexeddb'))
-  .plugin(require('pouchdb-adapter-http'))
-  .plugin(require('pouchdb-replication'));
\ No newline at end of file
+import PouchDB from 'pouchdb-core';
+import idbPouch from 'pouchdb-adapter-indexeddb';
+import httpPouch from 'pouchdb-adapter-http';
+import replication from 'pouchdb-replication';
+
+export default PouchDB
+  .plugin(idbPouch)
+  .plugin(httpPouch)
+  .plugin(replication);
\ No newline at end of file
diff --git a/packages/node_modules/pouchdb/src/plugins/find.js b/packages/node_modules/pouchdb/src/plugins/find.js
new file mode 100644
index 0000000..15c8bb2
--- /dev/null
+++ b/packages/node_modules/pouchdb/src/plugins/find.js
@@ -0,0 +1,14 @@
+/* global PouchDB */
+
+// this code only runs in the browser, as its own dist/ script
+
+import FindPlugin from 'pouchdb-find';
+import { guardedConsole } from 'pouchdb-utils';
+
+if (typeof PouchDB === 'undefined') {
+  guardedConsole('error', 'pouchdb-find plugin error: ' +
+    'Cannot find global "PouchDB" object! ' +
+    'Did you remember to include pouchdb.js?');
+} else {
+  PouchDB.plugin(FindPlugin);
+}
\ No newline at end of file
diff --git a/packages/node_modules/pouchdb/src/plugins/fruitdown.js b/packages/node_modules/pouchdb/src/plugins/fruitdown.js
index dded2f2..5250cd7 100644
--- a/packages/node_modules/pouchdb/src/plugins/fruitdown.js
+++ b/packages/node_modules/pouchdb/src/plugins/fruitdown.js
@@ -1,13 +1,14 @@
 /* global PouchDB */
 
+// this code only runs in the browser, as its own dist/ script
+
 import FruitdownPouchPlugin from 'pouchdb-adapter-fruitdown';
 import { guardedConsole } from 'pouchdb-utils';
 
-var PDB = (typeof PouchDB !== 'undefined') ? PouchDB : require('pouchdb');
-if (!PDB) {
+if (typeof PouchDB === 'undefined') {
   guardedConsole('error', 'fruitdown adapter plugin error: ' +
     'Cannot find global "PouchDB" object! ' +
     'Did you remember to include pouchdb.js?');
 } else {
-  FruitdownPouchPlugin(PDB);
-}
+  PouchDB.plugin(FruitdownPouchPlugin);
+}
\ No newline at end of file
diff --git a/packages/node_modules/pouchdb/src/plugins/localstorage.js b/packages/node_modules/pouchdb/src/plugins/localstorage.js
index f76ab8f..6af5e4f 100644
--- a/packages/node_modules/pouchdb/src/plugins/localstorage.js
+++ b/packages/node_modules/pouchdb/src/plugins/localstorage.js
@@ -1,13 +1,14 @@
 /* global PouchDB */
 
+// this code only runs in the browser, as its own dist/ script
+
 import LocalStoragePouchPlugin from 'pouchdb-adapter-localstorage';
 import { guardedConsole } from 'pouchdb-utils';
 
-var PDB = (typeof PouchDB !== 'undefined') ? PouchDB : require('pouchdb');
-if (!PDB) {
+if (typeof PouchDB === 'undefined') {
   guardedConsole('error', 'localstorage adapter plugin error: ' +
     'Cannot find global "PouchDB" object! ' +
     'Did you remember to include pouchdb.js?');
 } else {
-  LocalStoragePouchPlugin(PDB);
-}
+  PouchDB.plugin(LocalStoragePouchPlugin);
+}
\ No newline at end of file
diff --git a/packages/node_modules/pouchdb/src/plugins/memory.js b/packages/node_modules/pouchdb/src/plugins/memory.js
index 9ed2bc6..794e417 100644
--- a/packages/node_modules/pouchdb/src/plugins/memory.js
+++ b/packages/node_modules/pouchdb/src/plugins/memory.js
@@ -1,13 +1,14 @@
 /* global PouchDB */
 
+// this code only runs in the browser, as its own dist/ script
+
 import MemoryPouchPlugin from 'pouchdb-adapter-memory';
 import { guardedConsole } from 'pouchdb-utils';
 
-var PDB = (typeof PouchDB !== 'undefined') ? PouchDB : require('pouchdb');
-if (!PDB) {
+if (typeof PouchDB === 'undefined') {
   guardedConsole('error', 'memory adapter plugin error: ' +
     'Cannot find global "PouchDB" object! ' +
     'Did you remember to include pouchdb.js?');
 } else {
-  MemoryPouchPlugin(PDB);
-}
+  PouchDB.plugin(MemoryPouchPlugin);
+}
\ No newline at end of file
diff --git a/tests/integration/webrunner.js b/tests/integration/webrunner.js
index 6a96d17..bf02fde 100644
--- a/tests/integration/webrunner.js
+++ b/tests/integration/webrunner.js
@@ -24,8 +24,9 @@
   }
   if (plugins) {
     plugins[1].split(',').forEach(function (plugin) {
+      plugin = plugin.replace(/^pouchdb-/, '');
       scriptsToLoad.push(
-        '../../packages/node_modules/' + plugin + '/dist/pouchdb.' + plugin + '.js');
+        '../../packages/node_modules/pouchdb/dist/pouchdb.' + plugin + '.js');
     });
   }