Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 modules in some cases such as selectively import, excluding
builtin package (aws-sdk) and handling webpack-incompatible modules. In this case,
you add all the modules, 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: ["module1", "module2"] // modules to be excluded from bundled file
}
```

```yaml
# serverless.yml
custom:
webpackIncludeModules:
- module1 # modules to be included in distribution
```

You can find an example setup in the [`examples`](./examples) folder.

## Usage

### Automatic bundling
Expand Down
5 changes: 5 additions & 0 deletions examples/include-external-npm-packages/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"key3": "value3",
"key2": "value2",
"key1": "value1"
}
8 changes: 8 additions & 0 deletions examples/include-external-npm-packages/handler.js
Original file line number Diff line number Diff line change
@@ -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 changes: 19 additions & 0 deletions examples/include-external-npm-packages/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"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 changes: 15 additions & 0 deletions examples/include-external-npm-packages/serverless.env.yml
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions examples/include-external-npm-packages/serverless.yml
Original file line number Diff line number Diff line change
@@ -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:
webpackIncludeModules: # modules to be included in distribution
- fbgraph

functions:
first:
handler: handler.hello
events:
- http:
method: GET
path: first
7 changes: 7 additions & 0 deletions examples/include-external-npm-packages/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var path = require('path');

module.exports = {
entry: './handler.js',
target: 'node',
externals: ["fbgraph", "aws-sdk"] // modules to be excluded from bundled file
};
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const compile = require('./lib/compile');
const cleanup = require('./lib/cleanup');
const run = require('./lib/run');
const serve = require('./lib/serve');
const packExternalModules = require('./lib/packExternalModules')

class ServerlessWebpack {
constructor(serverless, options) {
Expand All @@ -19,7 +20,8 @@ class ServerlessWebpack {
compile,
cleanup,
run,
serve
serve,
packExternalModules
);

this.commands = {
Expand All @@ -28,6 +30,7 @@ class ServerlessWebpack {
lifecycleEvents: [
'validate',
'compile',
'packExternalModules',
],
options: {
out: {
Expand Down Expand Up @@ -89,7 +92,8 @@ class ServerlessWebpack {
this.hooks = {
'before:deploy:createDeploymentArtifacts': () => BbPromise.bind(this)
.then(this.validate)
.then(this.compile),
.then(this.compile)
.then(this.packExternalModules),

'after:deploy:createDeploymentArtifacts': () => BbPromise.bind(this)
.then(this.cleanup),
Expand All @@ -100,6 +104,9 @@ class ServerlessWebpack {
'webpack:compile': () => BbPromise.bind(this)
.then(this.compile),

'webpack:packExternalModules': () => BbPromise.bind(this)
.then(this.packExternalModules),

'webpack:invoke:invoke': () => BbPromise.bind(this)
.then(this.validate)
.then(this.compile)
Expand Down
42 changes: 42 additions & 0 deletions lib/packExternalModules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

const BbPromise = require('bluebird');
const fs = require('fs');
const path = require('path');
const npm = require('npm-programmatic');

module.exports = {
packExternalModules() {

const includes = (
this.serverless.service.custom &&
this.serverless.service.custom.webpackIncludeModules
);

return BbPromise.resolve().then(() => {
if (!includes || includes.length === 0) {
return;
}

this.serverless.cli.log('Packing external modules: ' + includes.join(","));

const tmpPackageJson = path.join(this.serverless.config.servicePath, 'package.json');

// create a temp package.json in dist directory so that we can install the dependencies later.
fs.writeFileSync(tmpPackageJson, "{}");

return new BbPromise((resolve, reject) => {
npm.install(includes, {
cwd: this.serverless.config.servicePath,
save: false
}).then(() => {
fs.unlink(tmpPackageJson);
resolve()
}).catch(e => {
fs.unlink(tmpPackageJson);
reject(e);
})
})
})
},
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"body-parser": "^1.15.2",
"express": "^4.14.0",
"fs-extra": "^0.26.7",
"npm-programmatic": "0.0.5",
"webpack": "^1.13.1"
},
"devDependencies": {
Expand Down