Skip to content

Commit b5dc61f

Browse files
authoredMar 8, 2018
Merge pull request #338 from serverless-heaven/v5
V5
2 parents 5a54583 + d7c5f8a commit b5dc61f

13 files changed

+1396
-596
lines changed
 

‎README.md

+56-18
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ and much more!
2424
individually, resulting in smaller Lambda packages that contain only the code and
2525
dependencies needed to run the function. This allows the plugin to fully utilize
2626
WebPack's [Tree-Shaking][link-webpack-tree] optimization.
27+
* Webpack version 3 and 4 support
2728

2829
## Recent improvements and important changes
2930

30-
* Restrict webpack peer dependency version to `< 4`. Compatibility with webpack 4 will be
31-
added with the next major release (5.0.0).
32-
* Support for [serverless-step-functions-offline][link-step-functions-offline]
31+
* Webpack 2 support has been dropped in favor of Webpack 4
32+
* Cleaned up configuration. You should now use a `custom.webpack` object to configure everything relevant for the plugin. The old configuration still works but will be removed in the next major release. For details see below.
33+
* This 5.0.0 prerelease is based on the current 4.4.0
3334

3435
For the complete release notes see the end of this document.
3536

@@ -48,12 +49,27 @@ plugins:
4849
4950
## Configure
5051
51-
By default the plugin will look for a `webpack.config.js` in the service directory.
52-
Alternatively, you can specify a different file or configuration in `serverless.yml`:
52+
The configuration of the plugin is done by defining a `custom: webpack` object in your `serverless.yml` with your specific configuration. All settings are optional and will be set to reasonable defaults if missing.
53+
54+
See the sections below for detailed descriptions of the settings. The defaults are:
55+
56+
```yaml
57+
custom:
58+
webpack:
59+
webpackConfig: 'webpack.config.js' # Name of webpack configuration file
60+
includeModules: false # Node modules configuration for packaging
61+
packager: 'npm' # Reserved for future use. Any other values will not work right now.
62+
packExternalModulesMaxBuffer: 200 * 1024 # Size of stdio buffers for spawned child processes
63+
```
64+
65+
### Webpack configuration file
66+
67+
By default the plugin will look for a `webpack.config.js` in the service directory. Alternatively, you can specify a different file or configuration in `serverless.yml`.
5368

5469
```yaml
5570
custom:
56-
webpack: ./folder/my-webpack.config.js
71+
webpack:
72+
webpackConfig: ./folder/my-webpack.config.js
5773
```
5874

5975
A base Webpack configuration might look like this:
@@ -126,6 +142,24 @@ as that will modify the running framework and leads to unpredictable behavior!
126142
If you have cool use cases with the full customization, we might add your solution
127143
to the plugin examples as showcase.
128144

145+
#### Invocation state
146+
147+
`lib.webpack` contains state variables that can be used to configure the build
148+
dynamically on a specific plugin state.
149+
150+
##### isLocal
151+
152+
`lib.webpack.isLocal` is a boolean property that is set to true, if any known
153+
mechanism is used in the current Serverless invocation that runs code locally.
154+
155+
This allows to set properties in the webpack configuration differently depending
156+
if the lambda code is run on the local machine or deployed.
157+
158+
A sample is to set the compile mode with Webpack 4:
159+
```
160+
mode: slsw.lib.webpack.isLocal ? "development" : "production"
161+
```
162+
129163
### Output
130164

131165
Note that, if the `output` configuration is not set, it will automatically be
@@ -174,7 +208,7 @@ builtin package (ie: `aws-sdk`) and handling webpack-incompatible modules.
174208

175209
In this case you might add external modules in
176210
[Webpack's `externals` configuration][link-webpack-externals].
177-
Those modules can be included in the Serverless bundle with the `webpackIncludeModules`
211+
Those modules can be included in the Serverless bundle with the `custom: webpack: includeModules`
178212
option in `serverless.yml`:
179213

180214
```js
@@ -191,7 +225,8 @@ module.exports = {
191225
```yaml
192226
# serverless.yml
193227
custom:
194-
webpackIncludeModules: true # enable auto-packing of external modules
228+
webpack:
229+
includeModules: true # enable auto-packing of external modules
195230
```
196231

197232

@@ -205,8 +240,9 @@ use a different package file, set `packagePath` to your custom `package.json`:
205240
```yaml
206241
# serverless.yml
207242
custom:
208-
webpackIncludeModules:
209-
packagePath: '../package.json' # relative path to custom package.json file.
243+
webpack:
244+
includeModules:
245+
packagePath: '../package.json' # relative path to custom package.json file.
210246
```
211247
> Note that only relative path is supported at the moment.
212248

@@ -228,10 +264,11 @@ your service's production dependencies in `package.json`.
228264
```yaml
229265
# serverless.yml
230266
custom:
231-
webpackIncludeModules:
232-
forceInclude:
233-
- module1
234-
- module2
267+
webpack:
268+
includeModules:
269+
forceInclude:
270+
- module1
271+
- module2
235272
```
236273

237274
#### Forced exclusion
@@ -244,10 +281,11 @@ Just add them to the `forceExclude` array property and they will not be packaged
244281
```yaml
245282
# serverless.yml
246283
custom:
247-
webpackIncludeModules:
248-
forceExclude:
249-
- module1
250-
- module2
284+
webpack:
285+
includeModules:
286+
forceExclude:
287+
- module1
288+
- module2
251289
```
252290

253291
If you specify a module in both arrays, `forceInclude` and `forceExclude`, the

‎index.js

+20-6
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');
@@ -28,8 +27,11 @@ class ServerlessWebpack {
2827
this.options = options;
2928

3029
if (
31-
_.has(this.serverless, 'service.custom.webpack') &&
32-
_.endsWith(this.serverless.service.custom.webpack, '.ts')
30+
(_.has(this.serverless, 'service.custom.webpack') &&
31+
_.isString(this.serverless.service.custom.webpack) &&
32+
_.endsWith(this.serverless.service.custom.webpack, '.ts')) ||
33+
(_.has(this.serverless, 'service.custom.webpack.webpackConfig') &&
34+
_.endsWith(this.serverless.service.custom.webpack.webpackConfig, '.ts'))
3335
) {
3436
require('ts-node/register');
3537
}
@@ -108,7 +110,10 @@ class ServerlessWebpack {
108110
.then(() => this.serverless.pluginManager.spawn('webpack:package')),
109111

110112
'before:invoke:local:invoke': () => BbPromise.bind(this)
111-
.then(() => this.serverless.pluginManager.spawn('webpack:validate'))
113+
.then(() => {
114+
lib.webpack.isLocal = true;
115+
return this.serverless.pluginManager.spawn('webpack:validate');
116+
})
112117
.then(() => this.serverless.pluginManager.spawn('webpack:compile'))
113118
.then(this.prepareLocalInvoke),
114119

@@ -158,16 +163,25 @@ class ServerlessWebpack {
158163
.then(this.packageModules),
159164

160165
'before:offline:start': () => BbPromise.bind(this)
166+
.tap(() => {
167+
lib.webpack.isLocal = true;
168+
})
161169
.then(this.prepareOfflineInvoke)
162170
.then(this.wpwatch),
163171

164172
'before:offline:start:init': () => BbPromise.bind(this)
173+
.tap(() => {
174+
lib.webpack.isLocal = true;
175+
})
165176
.then(this.prepareOfflineInvoke)
166177
.then(this.wpwatch),
167178

168179
'before:step-functions-offline:start': () => BbPromise.bind(this)
169-
.then(this.prepareStepOfflineInvoke)
170-
.then(this.compile)
180+
.tap(() => {
181+
lib.webpack.isLocal = true;
182+
})
183+
.then(this.prepareStepOfflineInvoke)
184+
.then(this.compile)
171185
};
172186
}
173187
}

0 commit comments

Comments
 (0)