Skip to content

Commit

Permalink
Keep original functions paths (#13)
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
dapriett authored and jogold committed May 17, 2017
1 parent 291e44e commit ed1e526
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 51 deletions.
8 changes: 3 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@ class ServerlessPluginWebpack {
// Package individually and exclude everything at the service level
this.serverless.service.package = {
individually: true,
exclude: ['*'],
exclude: ['**'],
};

// Include bundle and update handler at function level
this.serverless.service.functions = functions.setPackageAndHandler(
this.serverless.service.functions
);
// Include bundle at function level
this.serverless.service.functions = functions.setPackage(this.serverless.service.functions);

// Run webpack
return wpack.run(
Expand Down
24 changes: 4 additions & 20 deletions src/lib/functions.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
const path = require('path');
const R = require('ramda');
const list = require('./list');

const handlerProp = R.prop('handler');

const handlerPath = R.replace(/\.[^.]+$/, '.js');
const handlerFile = R.compose(path.basename, handlerPath);
const fnPath = R.compose(handlerPath, handlerProp);
const fnFilename = R.compose(handlerFile, handlerProp);

const setPackage = fn =>
R.assoc(
'package',
R.objOf(
'include',
R.compose(list, fnFilename)(fn)
),
fn
);
const fnPath = R.compose(handlerPath, R.prop('handler'));

const setHandler = R.over(R.lensProp('handler'), path.basename);
const fnInclude = fn => R.objOf('include', [fnPath(fn)]);

const setPackageAndHandler = R.map(R.compose(setHandler, setPackage));
const setPackage = R.map(fn => R.assoc('package', fnInclude(fn), fn));

const setArtifacts = (serverlessPath, fns) => R.map(
R.over(
Expand All @@ -33,7 +18,6 @@ const setArtifacts = (serverlessPath, fns) => R.map(

module.exports = {
fnPath,
fnFilename,
setPackageAndHandler,
setPackage,
setArtifacts,
};
3 changes: 0 additions & 3 deletions src/lib/list.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib/wpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const setEntry = (fn, servicePath) =>
R.assoc(
'entry',
R.objOf(
functions.fnFilename(fn),
functions.fnPath(fn),
path.join(servicePath, functions.fnPath(fn))
)
);
Expand Down
9 changes: 8 additions & 1 deletion test/fns.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
const originalFns = {
get: {
firstGet: {
handler: 'functions/first/get.handler',
package: {
include: ['*'],
exclude: ['abc.js'],
},
},
secondGet: {
handler: 'functions/second/get.handler',
package: {
include: ['*'],
exclude: ['abc.js'],
},
},
post: {
handler: 'functions/post.handler',
package: { exclude: ['*.js'] },
Expand Down
30 changes: 16 additions & 14 deletions test/functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,38 @@ const functions = require('../src/lib/functions');
const fns = require('./fns.js');

const modifiedFns = {
get: {
handler: 'get.handler',
package: { include: ['get.js'] },
firstGet: {
handler: 'functions/first/get.handler',
package: { include: ['functions/first/get.js'] },
},
secondGet: {
handler: 'functions/second/get.handler',
package: { include: ['functions/second/get.js'] },
},
post: {
handler: 'post.handler',
package: { include: ['post.js'] },
handler: 'functions/post.handler',
package: { include: ['functions/post.js'] },
},
};

const artifactFns = {
get: { artifact: '/.serverless/.webpack/service-dev-get.zip' },
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 = {
get: { artifact: path.join('/.serverless', 'service-dev-get.zip') },
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') },
};

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

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

test('setPackageAndHandler', () => {
expect(functions.setPackageAndHandler(fns)).toEqual(modifiedFns);
test('setPackage', () => {
expect(functions.setPackage(fns)).toEqual(modifiedFns);
});

test('setArtifacts', () => {
Expand Down
5 changes: 0 additions & 5 deletions test/list.test.js

This file was deleted.

15 changes: 13 additions & 2 deletions test/wpack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const folder = '.webpack';
const configs = [
{
entry: {
'get.js': path.join(servicePath, 'functions/first/get.js'),
'functions/first/get.js': path.join(servicePath, 'functions/first/get.js'),
},
output: {
libraryTarget: 'commonjs2',
Expand All @@ -29,7 +29,18 @@ const configs = [
},
{
entry: {
'post.js': path.join(servicePath, 'functions/post.js'),
'functions/second/get.js': path.join(servicePath, 'functions/second/get.js'),
},
output: {
libraryTarget: 'commonjs2',
path: path.join(servicePath, folder),
filename: '[name]',
},
externals: [/aws-sdk/],
},
{
entry: {
'functions/post.js': path.join(servicePath, 'functions/post.js'),
},
output: {
libraryTarget: 'commonjs2',
Expand Down

0 comments on commit ed1e526

Please sign in to comment.