From ed1e526940aacbf8f94c23c796452a5af9387e74 Mon Sep 17 00:00:00 2001 From: Dan Prietti Date: Wed, 17 May 2017 06:51:50 -0400 Subject: [PATCH] Keep original functions paths (#13) Fixes #12 --- src/index.js | 8 +++----- src/lib/functions.js | 24 ++++-------------------- src/lib/list.js | 3 --- src/lib/wpack.js | 2 +- test/fns.js | 9 ++++++++- test/functions.test.js | 30 ++++++++++++++++-------------- test/list.test.js | 5 ----- test/wpack.test.js | 15 +++++++++++++-- 8 files changed, 45 insertions(+), 51 deletions(-) delete mode 100644 src/lib/list.js delete mode 100644 test/list.test.js diff --git a/src/index.js b/src/index.js index a17420f..f7acec4 100644 --- a/src/index.js +++ b/src/index.js @@ -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( diff --git a/src/lib/functions.js b/src/lib/functions.js index 7cfeb95..c7fd85a 100644 --- a/src/lib/functions.js +++ b/src/lib/functions.js @@ -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( @@ -33,7 +18,6 @@ const setArtifacts = (serverlessPath, fns) => R.map( module.exports = { fnPath, - fnFilename, - setPackageAndHandler, + setPackage, setArtifacts, }; diff --git a/src/lib/list.js b/src/lib/list.js deleted file mode 100644 index d0ec965..0000000 --- a/src/lib/list.js +++ /dev/null @@ -1,3 +0,0 @@ -const R = require('ramda'); - -module.exports = R.unapply(R.identity); diff --git a/src/lib/wpack.js b/src/lib/wpack.js index bcfe8b6..2fe2d35 100644 --- a/src/lib/wpack.js +++ b/src/lib/wpack.js @@ -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)) ) ); diff --git a/test/fns.js b/test/fns.js index 0720287..d0639ae 100644 --- a/test/fns.js +++ b/test/fns.js @@ -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'] }, diff --git a/test/functions.test.js b/test/functions.test.js index 70511e2..e8ade19 100644 --- a/test/functions.test.js +++ b/test/functions.test.js @@ -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', () => { diff --git a/test/list.test.js b/test/list.test.js deleted file mode 100644 index ec936fd..0000000 --- a/test/list.test.js +++ /dev/null @@ -1,5 +0,0 @@ -const list = require('../src/lib/list'); - -test('list', () => { - expect(list('test')).toEqual(['test']); -}); diff --git a/test/wpack.test.js b/test/wpack.test.js index f7d6fdb..ea78aac 100644 --- a/test/wpack.test.js +++ b/test/wpack.test.js @@ -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', @@ -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',