@@ -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