It would be nice to have a config option to set the folder to serve the lambdas from.
I want to use webpack watch in order to create an output folder and use serverless-offline to serve from that folder.
I cannot use serverless-webpack since it is outdated (the pull request that makes it possible to use these two plugins is not merged yet)
I'd like this too.
I've put together a solution in my fork at dsole/serverless-offline/153-serve-from-subfolder.
I've called the option --location or -l for short. I'm undecided whether that's a good name though - something like --path / --prefix / --root might be better but you already have -P and -p @dherault what do you think ?
The solution for now is to append the --location value to the servicePath. I haven't tested it extensively but it seems to work ok.
A quick example of how it might be used.
This is my scenario but I'm sure there are others. I'm coding in Typescript with the source .ts files in /src/ and the generated JS files being written to /dist/ . I'm using webpack to bundle the code; it reads from /dist/ and writes the bundles to /.webpack/
\Code\my-app\
\serverless.yml
\dist\service.js
\src\service.ts
\.webpack\service.js
My serverless.yml file contains
functions:
service:
handler: service.handler
events:
- http:
method: get
path: service
I dont want to run webpack all the time, Im using it mainly to get the files small before they go to Lambda. So to use this setup with serverless-offline I will be running the JS files from the /dist/ folder.
sls offline --location dist
But then as a pre-deployment test I might run the bundled files to make sure they're ok.
sls offline --location .webpack
FYI the reason I'm not using sls webpack serve is that it doesn't handle the API gateway behaviour accurately such as processing the result.statusCode and result.headers properties in the HTTP response. It just seems to dump the whole result from the function as the response. I also prefer plugins to stick to their main focus rather than trying to do too many things.
Thank you @utkuturunc for that!
Can you PR it please ? (And also edit README.md and put your name in package.json)
Can we also specify the filename, not just the folder? This is for complete flexibility with serverless-webpack
@rvaidya I'm not sure how it would work if the services in the serverless config were in different files.
For example with a config like this
functions:
serviceOne:
handler: serviceOne.handler
...
serviceTwo:
handler: serviceTwo.handler
...
The serverless framework interprets the 'handler' value as [filename].[function].
And therefore there needs to be two files in the folder, serviceOne.js and serviceTwo.js. If a filename was passed as an argument to the offline plugin as you proposed, how would it distinguish between the files ?
Perhaps if a file was passed as the argument it would assume that file is a webpack bundle; it could load the webpack bundle and then it would read the serviceOne and serviceTwo modules from in there in order to find their handler functions.
@dsole I agree, given that it is possible to have multiple files I think it is best to leave this setting as a directory.
@rvaidya is there a specific use case with serverless-webpack that would require being able to specify a file?
@dsole @mhodgson I wasn't aware that serverless can just pick up multiple files like that so your argument makes sense, you can disregard my request.
Most helpful comment
I'd like this too.
I've put together a solution in my fork at dsole/serverless-offline/153-serve-from-subfolder.
I've called the option
--locationor-lfor short. I'm undecided whether that's a good name though - something like--path / --prefix / --rootmight be better but you already have-Pand-p@dherault what do you think ?The solution for now is to append the
--locationvalue to the servicePath. I haven't tested it extensively but it seems to work ok.