emulator
emulator copied to clipboard
Add serverless-webpack support
Currently, the emulator doesn't seem to be able to work with the serverless-webpack
plugin when using sls run
.
Even though everything is setup in my serverless.yml
file, it doesn't seem to compile it with webpack before loading the functions.
For the time being, I found the following workaround:
- Have 2 versions of your
yml
files (i.e.serverless.yml
&serverless-webpack.yml
) - Update the
serverless-webpack.yml
handlers to load the functions fromdist/{functionName}/{functionName}.{handlerName}
- Run
serverless webpack --out dist
from the service. - Swap the
yml
files name (so that theserverless.yml
is now the one loading from thedist
directory) - Run
sls run
as usual, but this time it will load the functions from the precompiled webpackdist
directory.
I created some helper scripts in my package.json
to make this process a little easier:
"scripts": {
"change-config": "mv serverless.yml serverless-temp.yml && mv serverless-webpack.yml serverless.yml && mv serverless-temp.yml serverless-webpack.yml",
"sls-webpack-run": "npm run-script change-config && serverless webpack --out dist && npm run-script change-config && sls run"
}
Please note that if you want to use those scripts, the serverless.yml
file must be the one loading the handlers from the dist
directory prior to running npm run-script sls-webpack-run
, since it will switch back the files before compiling with webpack and it switches them back when it's done. This is to ensure that every time we run sls-run
, we load the functions from the dist
directory, even if we didn't re-compile it with webpack beforehand.
But it'd be great if there was a way to use the serverless-webpack
plugin with sls-run
out-of-the-box!