Skip to content

Commit 85560af

Browse files
authored
Merge pull request #175 from elastic-coders/v3.0.0
V3.0.0
2 parents cf51110 + c838a31 commit 85560af

32 files changed

+2744
-1804
lines changed

.eslintrc

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
# Based on the AirBnb JavaScript styleguides with our own twist
3+
env:
4+
es6: true
5+
node: true
6+
mocha: true
7+
extends:
8+
- eslint:recommended
9+
- plugin:lodash/recommended
10+
- plugin:promise/recommended
11+
- plugin:import/errors
12+
- plugin:import/warnings
13+
parser: babel-eslint
14+
parserOptions:
15+
sourceType: module
16+
ecmaFeatures:
17+
classes: true
18+
experimentalObjectRestSpread: true
19+
plugins:
20+
- promise
21+
- lodash
22+
- import
23+
rules:
24+
indent: [ error, 2, {
25+
MemberExpression: off
26+
} ]
27+
linebreak-style: [ error, unix ]
28+
semi: [ error, always ]
29+
quotes: [ error, single, { avoidEscape: true } ]
30+
no-mixed-spaces-and-tabs: error
31+
space-before-blocks: error
32+
arrow-spacing: error
33+
key-spacing: [ error, { afterColon: true, mode: minimum } ]
34+
brace-style: [ error, '1tbs' ]
35+
comma-spacing: [ error, { before: false, after: true } ]
36+
comma-style: [ error, last, { exceptions: { VariableDeclaration: true } } ]
37+
array-bracket-spacing: [ error, always, { singleValue: false } ]
38+
computed-property-spacing: [ error, never ]
39+
object-curly-spacing: [ error, always ]
40+
prefer-const: error
41+
no-var: error
42+
promise/no-nesting: off
43+
import/first: error
44+
import/newline-after-import: error
45+
import/no-named-as-default: off
46+
import/no-extraneous-dependencies: [ error, { devDependencies: true } ]
47+
lodash/import-scope: off
48+
lodash/preferred-alias: off
49+
lodash/prop-shorthand: off
50+
lodash/prefer-lodash-method: [ error, { ignoreObjects: [ BbPromise, path ] } ]

README.md

+130-65
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,29 @@
99
A Serverless v1.x plugin to build your lambda functions with [Webpack][link-webpack].
1010

1111
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!
1414

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.
1635

1736
## Install
1837

@@ -55,6 +74,8 @@ module.exports = {
5574

5675
serverless-webpack exposes a lib object, that can be used in your webpack.config.js
5776
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.
5879

5980
#### Automatic entry resolution
6081

@@ -154,6 +175,7 @@ custom:
154175
webpackIncludeModules: true # enable auto-packing of external modules
155176
```
156177
178+
157179
All modules stated in `externals` will be excluded from bundled files. If an excluded module
158180
is stated as `dependencies` in `package.json`, it will be packed into the Serverless
159181
artifact under the `node_modules` directory.
@@ -171,6 +193,43 @@ custom:
171193

172194
You can find an example setups in the [`examples`][link-examples] folder.
173195

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+
174233
## Usage
175234

176235
### Automatic bundling
@@ -181,6 +240,43 @@ The normal Serverless deploy procedure will automatically bundle with Webpack:
181240
- Install Serverless Webpack as above
182241
- Deploy with `serverless deploy`
183242

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+
184280
### Usage with serverless-offline
185281

186282
The plugin integrates very well with [serverless-offline][link-serverless-offline] to
@@ -226,32 +322,6 @@ Run `serverless offline start`.
226322
You can reduce the clutter generated by `serverless-offline` with `--dontPrintOutput` and
227323
disable timeouts with `--noTimeout`.
228324

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-
255325
### Bundle with webpack
256326

257327
To just bundle and see the output result use:
@@ -266,57 +336,41 @@ Options are:
266336

267337
### Simulate API Gateway locally
268338

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.
298341

299342
## Example with Babel
300343

301344
In the [`examples`][link-examples] folder there is a Serverless project using this
302345
plugin with Babel. To try it, from inside the example folder:
303346

304347
- `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
306349

307350
## Provider Support
308351

309352
Plugin commands are supported by the following providers. ⁇ indicates that command has not been tested with that provider.
310353

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 | ✔︎ | ⁇ | ⁇ | ⁇ |
317359

318360
## Release Notes
319361

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+
320374
* 2.2.0
321375
* Allow full dynamic configurations [#158][link-158]
322376
* 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
350404
[link-babel]: https://babeljs.io/
351405
[link-webpack-loaders]: https://webpack.github.io/docs/loaders.html
352406
[link-webpack-libtarget]: https://webpack.github.io/docs/configuration.html#output-librarytarget
407+
[link-webpack-tree]: https://webpack.js.org/guides/tree-shaking/
353408
[link-webpack-externals]: https://webpack.github.io/docs/configuration.html#externals
354409
[link-examples]: ./examples
355410
[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
376431

377432
[link-158]: https://github.com/elastic-coders/serverless-webpack/issues/158
378433
[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

Comments
 (0)