9
9
A Serverless v1.x plugin to build your lambda functions with [ Webpack] [ link-webpack ] .
10
10
11
11
This plugin is for you if you want to use the latest Javascript version with [ Babel] [ link-babel ] ;
12
- use custom [ resource loaders] [ link-webpack-loaders ] ;
13
- try your lambda functions locally and much more!
12
+ use custom [ resource loaders] [ link-webpack-loaders ] , optimize your packaged functions individually
13
+ and much more!
14
14
15
- > ** BREAKING CHANGE IN v2** : ` webpack ` must now be installed alongside ` serverless-webpack ` as a peer dependency. This allows more control over which version of Webpack to run.
15
+ ## Highlights
16
+
17
+ * Configuration possibilities range from zero-config to fully customizable
18
+ * Support of ` serverless package ` , ` serverless deploy ` and ` serverless deploy function `
19
+ * Support of ` serverless invoke local ` and ` serverless invoke local --watch `
20
+ * Integrates with [ ` serverless-offline ` ] [ link-serverless-offline ] to simulate local API Gateway endpoints
21
+ * When enabled in your service configuration, functions are packaged and compiled
22
+ individually, resulting in smaller Lambda packages that contain only the code and
23
+ dependencies needed to run the function. This allows the plugin to fully utilize
24
+ WebPack's [ Tree-Shaking] [ link-webpack-tree ] optimization.
25
+
26
+ ## New in V3
27
+
28
+ * Support for individual packaging and optimization
29
+ * Integrate with ` serverless invoke local ` (including watch mode)
30
+ * Stabilized package handling
31
+ * Improved compatibility with other plugins
32
+ * Updated examples
33
+
34
+ For the complete release notes see the end of this document.
16
35
17
36
## Install
18
37
@@ -55,6 +74,8 @@ module.exports = {
55
74
56
75
serverless-webpack exposes a lib object, that can be used in your webpack.config.js
57
76
to make the configuration easier and to build fully dynamic configurations.
77
+ This is the preferred way to configure webpack - the plugin will take care of
78
+ as much of the configuration (and subsequent changes in your services) as it can.
58
79
59
80
# ### Automatic entry resolution
60
81
@@ -154,6 +175,7 @@ custom:
154
175
webpackIncludeModules : true # enable auto-packing of external modules
155
176
` ` `
156
177
178
+
157
179
All modules stated in ` externals` will be excluded from bundled files. If an excluded module
158
180
is stated as `dependencies` in `package.json`, it will be packed into the Serverless
159
181
artifact under the `node_modules` directory.
@@ -171,6 +193,43 @@ custom:
171
193
172
194
You can find an example setups in the [`examples`][link-examples] folder.
173
195
196
+ # ### Service level packaging
197
+
198
+ If you do not enable individual packaging in your service (serverless.yml), the
199
+ plugin creates one ZIP file for all functions (the service package) that includes
200
+ all node modules used in the service. This is the fastest packaging, but not the
201
+ optimal one, as node modules are always packaged, that are not needed by some
202
+ functions.
203
+
204
+ # ### Optimization / Individual packaging per function
205
+
206
+ A better way to do the packaging, is to enable individual packaging in your
207
+ service :
208
+
209
+ ` ` ` yaml
210
+ # serverless.yml
211
+ ...
212
+ package:
213
+ individually: true
214
+ ...
215
+ ` ` `
216
+
217
+ This will switch the plugin to per function packaging which makes use of the multi-compiler
218
+ feature of Webpack. That means, that Webpack compiles **and optimizes** each
219
+ function individually, removing unnecessary imports and reducing code sizes
220
+ significantly. Tree-Shaking only makes sense with this approach.
221
+
222
+ Now the needed external node modules are also detected by Webpack per function
223
+ and the plugin only packages the needed ones into the function artifacts. As a
224
+ result, the deployed artifacts are smaller, depending on the functions and
225
+ cold-start times (to install the functions into the cloud at runtime) are reduced
226
+ too.
227
+
228
+ The individual packaging should be combined with the _automatic entry resolution_ (see above).
229
+
230
+ The individual packaging needs more time at the packaging phase, but you'll
231
+ get that paid back twice at runtime.
232
+
174
233
# # Usage
175
234
176
235
# ## Automatic bundling
@@ -181,6 +240,43 @@ The normal Serverless deploy procedure will automatically bundle with Webpack:
181
240
- Install Serverless Webpack as above
182
241
- Deploy with `serverless deploy`
183
242
243
+ # ## Run a function locally
244
+
245
+ The plugin fully integrates with `serverless invoke local`. To run your bundled functions
246
+ locally you can :
247
+
248
+ ` ` ` bash
249
+ $ serverless invoke local --function <function-name>
250
+ ` ` `
251
+
252
+ All options that are supported by invoke local can be used as usual :
253
+
254
+ - ` --function` or `-f` (required) is the name of the function to run
255
+ - ` --path` or `-p` (optional) is a JSON file path used as the function input event
256
+ - ` --data` or `-d` (optional) inline JSON data used as the function input event
257
+
258
+ > :exclamation: The old `webpack invoke` command has been disabled.
259
+
260
+ # ## Run a function locally on source changes
261
+
262
+ Or to run a function every time the source files change use the `--watch` option
263
+ together with `serverless invoke local` :
264
+
265
+ ` ` ` bash
266
+ $ serverless invoke local --function <function-name> --path event.json --watch
267
+ ` ` `
268
+
269
+ Everytime the sources are changed, the function will be executed again with the
270
+ changed sources. The command will watch until the process is terminated.
271
+
272
+ All options that are supported by invoke local can be used as usual :
273
+
274
+ - ` --function` or `-f` (required) is the name of the function to run
275
+ - ` --path` or `-p` (optional) is a JSON file path used as the function input event
276
+ - ` --data` or `-d` (optional) inline JSON data used as the function input event
277
+
278
+ > :exclamation: The old `webpack watch` command has been disabled.
279
+
184
280
# ## Usage with serverless-offline
185
281
186
282
The plugin integrates very well with [serverless-offline][link-serverless-offline] to
@@ -226,32 +322,6 @@ Run `serverless offline start`.
226
322
You can reduce the clutter generated by `serverless-offline` with `--dontPrintOutput` and
227
323
disable timeouts with `--noTimeout`.
228
324
229
- # ## Run a function locally
230
-
231
- To run your bundled functions locally you can :
232
-
233
- ` ` ` bash
234
- $ serverless webpack invoke --function <function-name>
235
- ` ` `
236
-
237
- Options are :
238
-
239
- - ` --function` or `-f` (required) is the name of the function to run
240
- - ` --path` or `-p` (optional) is a JSON file path used as the function input event
241
-
242
- # ## Run a function locally on source changes
243
-
244
- Or to run a function every time the source files change use `watch` :
245
-
246
- ` ` ` bash
247
- $ serverless webpack watch --function <function-name> --path event.json
248
- ` ` `
249
-
250
- Options are :
251
-
252
- - ` --function` or `-f` (required) is the name of the function to run
253
- - ` --path` or `-p` (optional) is a JSON file path used as the function input event
254
-
255
325
# ## Bundle with webpack
256
326
257
327
To just bundle and see the output result use :
@@ -266,57 +336,41 @@ Options are:
266
336
267
337
# ## Simulate API Gateway locally
268
338
269
- _The integrated simulation functionality will be removed in version 3 in favor of
270
- using `serverless-offline` (see [#135][link-135]) which already does the job
271
- perfectly and fully integrates with `serverless-webpack`.
272
- Please switch to `serverless-offline` if you do not use it already._
273
-
274
- To start a local server that will act like API Gateway, use the following command.
275
- Your code will be reloaded upon change so that every request to your local server
276
- will serve the latest code.
277
-
278
- ` ` ` bash
279
- $ serverless webpack serve
280
- ` ` `
281
-
282
- Options are :
283
-
284
- - ` --port` or `-p` (optional) The local server port. Defaults to `8000`
285
-
286
- The `serve` command will automatically look for the local `serverless.yml` and serve
287
- all the `http` events. For example this configuration will generate a GET endpoint :
288
-
289
- ` ` ` yaml
290
- functions:
291
- hello:
292
- handler: handler.hello
293
- events:
294
- - http:
295
- method: get
296
- path: hello
297
- ` ` `
339
+ :exclamation : The serve command has been removed. See above how to achieve the
340
+ same functionality with the [`serverless-offline`][link-serverless-offline] plugin.
298
341
299
342
# # Example with Babel
300
343
301
344
In the [`examples`][link-examples] folder there is a Serverless project using this
302
345
plugin with Babel. To try it, from inside the example folder :
303
346
304
347
- ` npm install` to install dependencies
305
- - ` serverless webpack run -f hello` to run the example function
348
+ - ` serverless invoke local -f hello` to run the example function
306
349
307
350
# # Provider Support
308
351
309
352
Plugin commands are supported by the following providers. ⁇ indicates that command has not been tested with that provider.
310
353
311
- | | AWS Lambda | Apache OpenWhisk | Azure Functions | Google Cloud Functions |
312
- |----------------|------------|------------------|-----------------|------------------------|
313
- | webpack | ✔︎ | ✔︎ | ⁇ | ⁇ |
314
- | webpack invoke | ✔︎ | ✘ | ⁇ | ⁇ |
315
- | webpack watch | ✔︎ | ✔︎ | ⁇ | ⁇ |
316
- | webpack serve | ✔︎ | ✘ | ⁇ | ⁇ |
354
+ | | AWS Lambda | Apache OpenWhisk | Azure Functions | Google Cloud Functions |
355
+ |-----------------------|------------|------------------|-----------------|------------------------|
356
+ | webpack | ✔︎ | ✔︎ | ⁇ | ⁇ |
357
+ | invoke local | ✔︎ | ⁇ | ⁇ | ⁇ |
358
+ | invoke local --watch | ✔︎ | ⁇ | ⁇ | ⁇ |
317
359
318
360
# # Release Notes
319
361
362
+ * 3.0.0
363
+ * Integrate with `serverless invoke local` [#151][link-151]
364
+ * Support watch mode with `serverless invoke local --watch`
365
+ * Stabilized and improved the bundling of node modules [#116][link-116], [#117][link-117]
366
+ * Improved interoperability with Serverless and 3rd party plugins [#173][link-173]
367
+ * Support individual packaging of the functions in a service [#120][link-120]
368
+ * Allow setting stdio max buffers for NPM operations [#185][link-185]
369
+ * Support bundling of node modules via node-externals whitelist [#186][link-186]
370
+ * Removed the `webpack serve` command in favor of [`serverless-offline`][link-serverless-offline] [#152][link-152]
371
+ * Updated examples [#179][link-179]
372
+ * Added missing unit tests to improve code stability
373
+
320
374
* 2.2.0
321
375
* Allow full dynamic configurations [#158][link-158]
322
376
* Fix a bug that prevented the entries lib export to work with TypeScript [#165][link-165]
@@ -350,6 +404,7 @@ Plugin commands are supported by the following providers. ⁇ indicates that com
350
404
[link-babel] : https://babeljs.io/
351
405
[link-webpack-loaders] : https://webpack.github.io/docs/loaders.html
352
406
[link-webpack-libtarget] : https://webpack.github.io/docs/configuration.html#output-librarytarget
407
+ [link-webpack-tree] : https://webpack.js.org/guides/tree-shaking/
353
408
[link-webpack-externals] : https://webpack.github.io/docs/configuration.html#externals
354
409
[link-examples] : ./examples
355
410
[link-serverless-offline] : https://www.npmjs.com/package/serverless-offline
@@ -376,3 +431,13 @@ Plugin commands are supported by the following providers. ⁇ indicates that com
376
431
377
432
[link-158] : https://github.com/elastic-coders/serverless-webpack/issues/158
378
433
[link-165] : https://github.com/elastic-coders/serverless-webpack/issues/165
434
+
435
+ [link-116] : https://github.com/elastic-coders/serverless-webpack/issues/116
436
+ [link-117] : https://github.com/elastic-coders/serverless-webpack/issues/117
437
+ [link-120] : https://github.com/elastic-coders/serverless-webpack/issues/120
438
+ [link-151] : https://github.com/elastic-coders/serverless-webpack/issues/151
439
+ [link-152] : https://github.com/elastic-coders/serverless-webpack/issues/152
440
+ [link-173] : https://github.com/elastic-coders/serverless-webpack/issues/173
441
+ [link-179] : https://github.com/elastic-coders/serverless-webpack/pull/179
442
+ [link-185] : https://github.com/elastic-coders/serverless-webpack/pull/185
443
+ [link-186] : https://github.com/elastic-coders/serverless-webpack/pull/186
0 commit comments