Repository: serverless-heaven/serverless-webpack (js)

Issue:
Webpack configuration not loading on compile
<!--
1. Please check if an issue already exists so there are no duplicates
2. Fill out the whole template so we have a good overview on the issue
3. Do not remove any section of the template. If something is not applicable leave it empty but leave it in the Issue
4. Please follow the template, otherwise we'll have to ask you to update it
-->

# This is a (Bug Report)

## Description

When deploying with serverless deploy I get the following error:

TypeError: Cannot read property 'stats' of undefined
      at ServerlessWebpack.compile (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/serverless-webpack/lib/compile.js:118:48)
      at ServerlessWebpack.tryCatcher (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/promise.js:547:31)
      at Promise._settlePromise (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/promise.js:604:18)
      at Promise._settlePromiseCtx (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/promise.js:641:10)
      at _drainQueueStep (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/async.js:97:12)
      at _drainQueue (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/async.js:86:9)
      at Async._drainQueues (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/async.js:102:5)
      at Immediate.Async.drainQueues [as _onImmediate] (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/async.js:15:14)
      at runCallback (timers.js:705:18)
      at tryOnImmediate (timers.js:676:5)
      at processImmediate (timers.js:658:5)
      at process.topLevelDomainCallback (domain.js:126:23)

Doing some debuging I found that compile.js is not getting the configuration from the serverless.yml, as I tried to print "this.webpackConfig" and it is empty.

module.exports = {
  compile() {
    this.serverless.cli.log('Bundling with Webpack...: '+this.webpackConfig);

    const configs = ensureArray(this.webpackConfig);
    const logStats = getStatsLogger(configs[0].stats, this.serverless.cli.consoleLog);

    if (!this.configuration) {
      return BbPromise.reject('Missing plugin configuration');
    }
    const concurrency = this.configuration.concurrency;

    return webpackConcurrentCompile(configs, logStats, concurrency).then(stats => {
      this.compileStats = { stats };
      return BbPromise.resolve();
    });
  }
};

For bug reports:
* What went wrong?
compile.js should be getting the config file from serverless.yml

* What did you expect should have happened?
That the compilation was successful

* What was the config you used?
Default config, using custom.webpack

* What stacktrace or error message from your provider did you see?

TypeError: Cannot read property 'stats' of undefined
      at ServerlessWebpack.compile (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/serverless-webpack/lib/compile.js:118:48)
      at ServerlessWebpack.tryCatcher (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/promise.js:547:31)
      at Promise._settlePromise (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/promise.js:604:18)
      at Promise._settlePromiseCtx (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/promise.js:641:10)
      at _drainQueueStep (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/async.js:97:12)
      at _drainQueue (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/async.js:86:9)
      at Async._drainQueues (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/async.js:102:5)
      at Immediate.Async.drainQueues [as _onImmediate] (/home/ubuntu/environment/co.com.ucloud.aws.lambda.sample/node_modules/bluebird/js/release/async.js:15:14)
      at runCallback (timers.js:705:18)
      at tryOnImmediate (timers.js:676:5)
      at processImmediate (timers.js:658:5)
      at process.topLevelDomainCallback (domain.js:126:23)

Similar or dependent issue(s):
* #12345

## Additional Data

* ***Serverless-Webpack Version you're using***: 5.5.1
* ***Webpack version you're using***: 
* ***Serverless Framework Version you're using***: 2.51
* ***Operating System***: Linux
* ***Stack Trace (if available)***:


Pull request description:
Avoid a JS error when the Webpack config is not found
In case the Webpack config can't be found, throw an error instead of the useless JS error:

```
TypeError: Cannot read property 'stats' of undefined
```

Fix #907

Patch:
diff --git a/lib/compile.js b/lib/compile.js
index 3c0bccd..cf847ac 100644
--- a/lib/compile.js
+++ b/lib/compile.js
@@ -120,6 +120,10 @@ module.exports = {
     this.serverless.cli.log('Bundling with Webpack...');
 
     const configs = ensureArray(this.webpackConfig);
+    if (configs[0] === undefined) {
+      return BbPromise.reject('Unable to find Webpack configuration');
+    }
+
     const logStats = getStatsLogger(configs[0].stats, this.serverless.cli.consoleLog);
 
     if (!this.configuration) {
diff --git a/lib/utils.js b/lib/utils.js
index 146c083..0975c62 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -118,7 +118,7 @@ function getAllNodeFunctions() {
   return _.filter(functions, funcName => {
     const func = this.serverless.service.getFunction(funcName);
 
-    // if `uri` is provided or simple remote image path, it means the 
+    // if `uri` is provided or simple remote image path, it means the
     // image isn't built by Serverless so we shouldn't take care of it
     if ((func.image && func.image.uri) || (func.image && typeof func.image == 'string')) {
       return false;


Test patch:
diff --git a/tests/compile.test.js b/tests/compile.test.js
index 41409d3..186c56c 100644
--- a/tests/compile.test.js
+++ b/tests/compile.test.js
@@ -74,6 +74,10 @@ describe('compile', () => {
   });
 
   it('should fail if configuration is missing', () => {
+    return expect(module.compile()).to.be.rejectedWith('Unable to find Webpack configuration');
+  });
+
+  it('should fail if plugin configuration is missing', () => {
     const testWebpackConfig = 'testconfig';
     module.webpackConfig = testWebpackConfig;
     module.configuration = undefined;