Skip to content

Commit

Permalink
Preserve original excludes and includes
Browse files Browse the repository at this point in the history
  • Loading branch information
jogold committed May 19, 2017
1 parent 74da19d commit b6d54cb
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 78 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ plugins:
- serverless-plugin-webpack
```
## Package exclude / include
The plugin will add `'**'` as an `exclude` at the service level and each bundled javascript file as an `include` at the function level. Original includes and excludes specified in your `serverless.yml` are preserved.

## Webpack configuration
The plugin will look for a `webpack.config.js` in the service root.

Expand Down
11 changes: 4 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs-extra');
const path = require('path');
const R = require('ramda');
const functions = require('./lib/functions');
const service = require('./lib/service');
const wpack = require('./lib/wpack');

// Folders
Expand Down Expand Up @@ -44,13 +44,10 @@ class ServerlessPluginWebpack {
this.serverless.config.servicePath = path.join(this.originalServicePath, webpackFolder);

// Package individually and exclude everything at the service level
this.serverless.service.package = {
individually: true,
exclude: ['**'],
};
this.serverless.service.package = service.setPackage(this.serverless.service.package);

// Include bundle at function level
this.serverless.service.functions = functions.setPackage(this.serverless.service.functions);
this.serverless.service.functions = service.setFnsPackage(this.serverless.service.functions);

// Run webpack
return wpack.run(
Expand All @@ -74,7 +71,7 @@ class ServerlessPluginWebpack {
return fs.copy(src, dest)
.then(() => {
if (type === 'service') {
this.serverless.service.functions = functions.setArtifacts(
this.serverless.service.functions = service.setFnsArtifacts(
dest,
this.serverless.service.functions
);
Expand Down
23 changes: 0 additions & 23 deletions src/lib/functions.js

This file was deleted.

28 changes: 28 additions & 0 deletions src/lib/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const path = require('path');
const R = require('ramda');

const appendPath = (p, e) => R.over(R.lensPath(p), R.append(e));

const setPackage = R.pipe(
R.over(R.lensProp('exclude'), R.append('**')),
R.assoc('individually', true)
);

const fnPath = R.compose(R.replace(/\.[^.]+$/, '.js'), R.prop('handler'));

const setFnsPackage = R.map(fn => appendPath(['package', 'include'], fnPath(fn))(fn));

const setFnsArtifacts = (serverlessPath, fns) => R.map(
R.over(
R.lensProp('artifact'),
artifact => path.join(serverlessPath, path.basename(artifact))
),
fns
);

module.exports = {
setPackage,
fnPath,
setFnsPackage,
setFnsArtifacts,
};
6 changes: 3 additions & 3 deletions src/lib/wpack.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path');
const R = require('ramda');
const webpack = require('webpack'); // eslint-disable-line import/no-unresolved
const functions = require('./functions');
const service = require('./service');

/**
* Sets webpack entry
Expand All @@ -13,8 +13,8 @@ const setEntry = (fn, servicePath) =>
R.assoc(
'entry',
R.objOf(
functions.fnPath(fn),
path.join(servicePath, functions.fnPath(fn))
service.fnPath(fn),
path.join(servicePath, service.fnPath(fn))
)
);

Expand Down
6 changes: 3 additions & 3 deletions test/fns.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ const originalFns = {
firstGet: {
handler: 'functions/first/get.handler',
package: {
include: ['*'],
include: ['node_modules/**'],
exclude: ['abc.js'],
},
},
secondGet: {
handler: 'functions/second/get.handler',
package: {
include: ['*'],
include: ['node_modules/**'],
exclude: ['abc.js'],
},
},
post: {
handler: 'functions/post.handler',
package: { exclude: ['*.js'] },
package: { exclude: ['*.sql'] },
},
};

Expand Down
42 changes: 0 additions & 42 deletions test/functions.test.js

This file was deleted.

82 changes: 82 additions & 0 deletions test/service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const path = require('path');
const service = require('../src/lib/service');
const fns = require('./fns.js');

test('service package when no package', () => {
const expectedPackage = {
individually: true,
exclude: ['**'],
};
expect(service.setPackage({})).toEqual(expectedPackage);
});

test('service package with existing package and no include/exclude', () => {
const existingPackage = {
individually: false,
};
const expectedPackage = {
individually: true,
exclude: ['**'],
};
expect(service.setPackage(existingPackage)).toEqual(expectedPackage);
});

test('service package with existing package and include/exclude', () => {
const existingPackage = {
individually: false,
include: ['node_modules/**'],
exclude: ['*.txt'],
};
const expectedPackage = {
individually: true,
include: ['node_modules/**'],
exclude: ['*.txt', '**'],
};
expect(service.setPackage(existingPackage)).toEqual(expectedPackage);
});

test('fnPath', () => {
expect(service.fnPath(fns.firstGet)).toBe('functions/first/get.js');
});

test('setFnsPackage', () => {
const modifiedFns = {
firstGet: {
handler: 'functions/first/get.handler',
package: {
include: ['node_modules/**', 'functions/first/get.js'],
exclude: ['abc.js'],
},
},
secondGet: {
handler: 'functions/second/get.handler',
package: {
include: ['node_modules/**', 'functions/second/get.js'],
exclude: ['abc.js'],
},
},
post: {
handler: 'functions/post.handler',
package: {
include: ['functions/post.js'],
exclude: ['*.sql'],
},
},
};
expect(service.setFnsPackage(fns)).toEqual(modifiedFns);
});

test('setArtifacts', () => {
const artifactFns = {
firstGet: { artifact: '/.serverless/.webpack/service-dev-firstGet.zip' },
secondGet: { artifact: '/.serverless/.webpack/service-dev-secondGet.zip' },
post: { artifact: '/.serverless/.webpack/service-dev-post.zip' },
};

const modifiedArtifactFns = {
firstGet: { artifact: path.join('/.serverless', 'service-dev-firstGet.zip') },
secondGet: { artifact: path.join('/.serverless', 'service-dev-secondGet.zip') },
post: { artifact: path.join('/.serverless', 'service-dev-post.zip') },
};
expect(service.setFnsArtifacts('/.serverless', artifactFns)).toEqual(modifiedArtifactFns);
});

0 comments on commit b6d54cb

Please sign in to comment.