Skip to content
This repository has been archived by the owner on Mar 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #390 from cloudant/389-fix-for-webpack
Browse files Browse the repository at this point in the history
Include all built-in plugin modules in webpack bundle.
  • Loading branch information
smithsz authored Aug 29, 2019
2 parents 08e18ff + f3afe65 commit 23970a4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# UNRELEASED
- [FIXED] Include all built-in plugin modules in webpack bundle.

# 4.2.0 (2019-08-27)
- [NEW] Added option to set new IAM API key.
- [FIXED] Allow plugins to be loaded from outside the 'plugins/' directory.
Expand Down
38 changes: 26 additions & 12 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,7 @@ class CloudantClient {
}

var pluginName = Object.keys(plugin)[0];

try {
Plugin = require(self._buildPluginPath(pluginName));
} catch (e) {
throw new Error(`Failed to load plugin - ${e.message}`);
}
Plugin = self._importPlugin(pluginName);

cfg = plugin[pluginName];
if (typeof cfg !== 'object' || Array.isArray(cfg)) {
Expand All @@ -119,12 +114,7 @@ class CloudantClient {
return; // noop
}

try {
Plugin = require(self._buildPluginPath(plugin));
} catch (e) {
throw new Error(`Failed to load plugin - ${e.message}`);
}

Plugin = self._importPlugin(plugin);
cfg = {};
break;

Expand Down Expand Up @@ -163,6 +153,30 @@ class CloudantClient {
return path.join(process.cwd(), name);
}

_importPlugin(pluginName) {
switch (pluginName) {
// Note: All built-in plugins are individually listed here to ensure they
// are included in a webpack bundle.
case 'cookieauth':
return require('../plugins/cookieauth');
case 'iamauth':
return require('../plugins/iamauth');
case 'retry':
return require('../plugins/retry');
default:
// Warning: Custom plugins will not be included in a webpack bundle
// by default because the exact module is not known on compile
// time.
try {
// Use template literal to suppress 'dependency is an expression'
// webpack compilation warning.
return require(`${this._buildPluginPath(pluginName)}`);
} catch (e) {
throw new Error(`Failed to load plugin - ${e.message}`);
}
}
}

_initClient(client) {
if (typeof client !== 'undefined') {
debug('Using custom client.');
Expand Down

0 comments on commit 23970a4

Please sign in to comment.