Browse Source

added examples and readme for packing external packages feature

master
Tony Yuen 8 years ago
parent
commit
cf4fee44d8
  1. 24
      README.md
  2. 5
      examples/include-external-npm-packages/event.json
  3. 8
      examples/include-external-npm-packages/handler.js
  4. 19
      examples/include-external-npm-packages/package.json
  5. 15
      examples/include-external-npm-packages/serverless.env.yml
  6. 21
      examples/include-external-npm-packages/serverless.yml
  7. 7
      examples/include-external-npm-packages/webpack.config.js

24
README.md

@ -29,6 +29,30 @@ custom:
Note that, if the `output` configuration is not set, it will automatically be
generated to write bundles in the `.webpack` directory.
By default, the plugin will try to bundle all dependencies. However, you don't
want to include all packages in some cases such as selectively import, excluding
builtin package (aws-sdk) and handling webpack-incompatible packages. In this case,
you add all the packages, you want to exclude from bundled files, into `externals` field
of your `webpack.config.json` and add those, you want to include in final distribution,
into `serverless.yml`:
```json
// webpack.config.json
{
externals: ["package1", "package2"] // packages to be excluded from bundled file
}
```
```yaml
# serverless.yml
custom:
includePackages:
- package1 # packages to be included in distribution
```
You can find an example setup in the [`examples`](./examples) folder.
## Usage
### Automatic bundling

5
examples/include-external-npm-packages/event.json

@ -0,0 +1,5 @@
{
"key3": "value3",
"key2": "value2",
"key1": "value1"
}

8
examples/include-external-npm-packages/handler.js

@ -0,0 +1,8 @@
'use strict';
var AWS = require('aws-sdk');
var fbgraph = require('fbgraph');
module.exports.hello = function (event, context, cb) {
cb(null, { message: 'hello fb & aws', event });
}

19
examples/include-external-npm-packages/package.json

@ -0,0 +1,19 @@
{
"name": "serverless-include-external-npm-package",
"version": "1.0.0",
"description": "Serverless webpack example",
"main": "handler.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Tony Yuen <colsy2@gmail.com>",
"license": "MIT",
"devDependencies": {
"serverless-webpack": "^1.0.0-beta.2",
"webpack": "^1.13.1"
},
"dependencies": {
"aws-sdk": "^2.5.3",
"fbgraph": "^1.3.0"
}
}

15
examples/include-external-npm-packages/serverless.env.yml

@ -0,0 +1,15 @@
# This is the Serverless Environment File
#
# It contains listing of your stages and their regions
# It also manages serverless variables at 3 levels:
# - common variables: variables that apply to all stages/regions
# - stage variables: variables that apply to a specific stage
# - region variables: variables that apply to a specific region
vars:
stages:
dev:
vars:
regions:
us-east-1:
vars:

21
examples/include-external-npm-packages/serverless.yml

@ -0,0 +1,21 @@
service: serverless-webpack-multiple-entries-example
# Add the serverless-webpack plugin
plugins:
- serverless-webpack
provider:
name: aws
runtime: nodejs4.3
custom:
includePackages: # packages to be included in distribution
- fbgraph
functions:
first:
handler: handler.hello
events:
- http:
method: GET
path: first

7
examples/include-external-npm-packages/webpack.config.js

@ -0,0 +1,7 @@
var path = require('path');
module.exports = {
entry: './handler.js',
target: 'node',
externals: ["fbgraph", "aws-sdk"] // packages to be excluded from bundled file
};
Loading…
Cancel
Save