Skip to content

Commit fb396fa

Browse files
author
Frank Schmid
committed
Small code beautify
Added webpack state variable paragraph to readme Added unit test for isLocal default value Add new webpack lib object that contains states relevant for the config.
1 parent 773c134 commit fb396fa

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,24 @@ as that will modify the running framework and leads to unpredictable behavior!
126126
If you have cool use cases with the full customization, we might add your solution
127127
to the plugin examples as showcase.
128128

129+
#### Invocation state
130+
131+
`lib.webpack` contains state variables that can be used to configure the build
132+
dynamically on a specific plugin state.
133+
134+
##### isLocal
135+
136+
`lib.webpack.isLocal` is a boolean property that is set to true, if any known
137+
mechanism is used in the current Serverless invocation that runs code locally.
138+
139+
This allows to set properties in the webpack configuration differently depending
140+
if the lambda code is run on the local machine or deployed.
141+
142+
A sample is to set the compile mode with Webpack 4:
143+
```
144+
mode: slsw.lib.webpack.isLocal ? "development" : "production"
145+
```
146+
129147
### Output
130148

131149
Note that, if the `output` configuration is not set, it will automatically be

index.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const BbPromise = require('bluebird');
44
const _ = require('lodash');
5-
const path = require('path');
65

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

110109
'before:invoke:local:invoke': () => BbPromise.bind(this)
111-
.then(() => this.serverless.pluginManager.spawn('webpack:validate'))
110+
.then(() => {
111+
lib.webpack.isLocal = true;
112+
return this.serverless.pluginManager.spawn('webpack:validate');
113+
})
112114
.then(() => this.serverless.pluginManager.spawn('webpack:compile'))
113115
.then(this.prepareLocalInvoke),
114116

@@ -158,16 +160,25 @@ class ServerlessWebpack {
158160
.then(this.packageModules),
159161

160162
'before:offline:start': () => BbPromise.bind(this)
163+
.tap(() => {
164+
lib.webpack.isLocal = true;
165+
})
161166
.then(this.prepareOfflineInvoke)
162167
.then(this.wpwatch),
163168

164169
'before:offline:start:init': () => BbPromise.bind(this)
170+
.tap(() => {
171+
lib.webpack.isLocal = true;
172+
})
165173
.then(this.prepareOfflineInvoke)
166174
.then(this.wpwatch),
167175

168176
'before:step-functions-offline:start': () => BbPromise.bind(this)
169-
.then(this.prepareStepOfflineInvoke)
170-
.then(this.compile)
177+
.tap(() => {
178+
lib.webpack.isLocal = true;
179+
})
180+
.then(this.prepareStepOfflineInvoke)
181+
.then(this.compile)
171182
};
172183
}
173184
}

lib/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

33
module.exports = {
4-
entries: {}
4+
entries: {},
5+
webpack: {
6+
isLocal: false
7+
}
58
};

tests/validate.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,5 +712,26 @@ describe('validate', () => {
712712
}).to.throw(new RegExp(`^Function "${testFunction}" doesn't exist`));
713713
});
714714
});
715+
716+
describe('webpack', () => {
717+
it('should default isLocal to false', () => {
718+
const testOutPath = 'test';
719+
const testConfig = {
720+
entry: 'test',
721+
context: 'testcontext',
722+
output: {
723+
path: testOutPath,
724+
},
725+
};
726+
module.serverless.service.custom.webpack = testConfig;
727+
return expect(module.validate()).to.be.fulfilled
728+
.then(() => {
729+
const lib = require('../lib/index');
730+
expect(lib.webpack.isLocal).to.be.false;
731+
return null;
732+
});
733+
});
734+
});
735+
715736
});
716737
});

0 commit comments

Comments
 (0)