Skip to content

Commit

Permalink
Merge pull request #334 from serverless-heaven/expose-local-state
Browse files Browse the repository at this point in the history
Make plugin state available in lib object
  • Loading branch information
HyperBrain authored Mar 6, 2018
2 parents 773c134 + fb396fa commit 2e8fbb2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ as that will modify the running framework and leads to unpredictable behavior!
If you have cool use cases with the full customization, we might add your solution
to the plugin examples as showcase.

#### Invocation state

`lib.webpack` contains state variables that can be used to configure the build
dynamically on a specific plugin state.

##### isLocal

`lib.webpack.isLocal` is a boolean property that is set to true, if any known
mechanism is used in the current Serverless invocation that runs code locally.

This allows to set properties in the webpack configuration differently depending
if the lambda code is run on the local machine or deployed.

A sample is to set the compile mode with Webpack 4:
```
mode: slsw.lib.webpack.isLocal ? "development" : "production"
```

### Output

Note that, if the `output` configuration is not set, it will automatically be
Expand Down
19 changes: 15 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const BbPromise = require('bluebird');
const _ = require('lodash');
const path = require('path');

const validate = require('./lib/validate');
const compile = require('./lib/compile');
Expand Down Expand Up @@ -108,7 +107,10 @@ class ServerlessWebpack {
.then(() => this.serverless.pluginManager.spawn('webpack:package')),

'before:invoke:local:invoke': () => BbPromise.bind(this)
.then(() => this.serverless.pluginManager.spawn('webpack:validate'))
.then(() => {
lib.webpack.isLocal = true;
return this.serverless.pluginManager.spawn('webpack:validate');
})
.then(() => this.serverless.pluginManager.spawn('webpack:compile'))
.then(this.prepareLocalInvoke),

Expand Down Expand Up @@ -158,16 +160,25 @@ class ServerlessWebpack {
.then(this.packageModules),

'before:offline:start': () => BbPromise.bind(this)
.tap(() => {
lib.webpack.isLocal = true;
})
.then(this.prepareOfflineInvoke)
.then(this.wpwatch),

'before:offline:start:init': () => BbPromise.bind(this)
.tap(() => {
lib.webpack.isLocal = true;
})
.then(this.prepareOfflineInvoke)
.then(this.wpwatch),

'before:step-functions-offline:start': () => BbPromise.bind(this)
.then(this.prepareStepOfflineInvoke)
.then(this.compile)
.tap(() => {
lib.webpack.isLocal = true;
})
.then(this.prepareStepOfflineInvoke)
.then(this.compile)
};
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';

module.exports = {
entries: {}
entries: {},
webpack: {
isLocal: false
}
};
21 changes: 21 additions & 0 deletions tests/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,5 +712,26 @@ describe('validate', () => {
}).to.throw(new RegExp(`^Function "${testFunction}" doesn't exist`));
});
});

describe('webpack', () => {
it('should default isLocal to false', () => {
const testOutPath = 'test';
const testConfig = {
entry: 'test',
context: 'testcontext',
output: {
path: testOutPath,
},
};
module.serverless.service.custom.webpack = testConfig;
return expect(module.validate()).to.be.fulfilled
.then(() => {
const lib = require('../lib/index');
expect(lib.webpack.isLocal).to.be.false;
return null;
});
});
});

});
});

0 comments on commit 2e8fbb2

Please sign in to comment.