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

Commit f3afe65

Browse files
committed
Include all built-in plugin modules in webpack bundle.
1 parent 08e18ff commit f3afe65

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# UNRELEASED
2+
- [FIXED] Include all built-in plugin modules in webpack bundle.
3+
14
# 4.2.0 (2019-08-27)
25
- [NEW] Added option to set new IAM API key.
36
- [FIXED] Allow plugins to be loaded from outside the 'plugins/' directory.

lib/client.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,7 @@ class CloudantClient {
100100
}
101101

102102
var pluginName = Object.keys(plugin)[0];
103-
104-
try {
105-
Plugin = require(self._buildPluginPath(pluginName));
106-
} catch (e) {
107-
throw new Error(`Failed to load plugin - ${e.message}`);
108-
}
103+
Plugin = self._importPlugin(pluginName);
109104

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

122-
try {
123-
Plugin = require(self._buildPluginPath(plugin));
124-
} catch (e) {
125-
throw new Error(`Failed to load plugin - ${e.message}`);
126-
}
127-
117+
Plugin = self._importPlugin(plugin);
128118
cfg = {};
129119
break;
130120

@@ -163,6 +153,30 @@ class CloudantClient {
163153
return path.join(process.cwd(), name);
164154
}
165155

156+
_importPlugin(pluginName) {
157+
switch (pluginName) {
158+
// Note: All built-in plugins are individually listed here to ensure they
159+
// are included in a webpack bundle.
160+
case 'cookieauth':
161+
return require('../plugins/cookieauth');
162+
case 'iamauth':
163+
return require('../plugins/iamauth');
164+
case 'retry':
165+
return require('../plugins/retry');
166+
default:
167+
// Warning: Custom plugins will not be included in a webpack bundle
168+
// by default because the exact module is not known on compile
169+
// time.
170+
try {
171+
// Use template literal to suppress 'dependency is an expression'
172+
// webpack compilation warning.
173+
return require(`${this._buildPluginPath(pluginName)}`);
174+
} catch (e) {
175+
throw new Error(`Failed to load plugin - ${e.message}`);
176+
}
177+
}
178+
}
179+
166180
_initClient(client) {
167181
if (typeof client !== 'undefined') {
168182
debug('Using custom client.');

0 commit comments

Comments
 (0)