emulator icon indicating copy to clipboard operation
emulator copied to clipboard

Add serverless-webpack support

Open vincentsolutions opened this issue 7 years ago • 0 comments

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:

  1. Have 2 versions of your yml files (i.e. serverless.yml & serverless-webpack.yml)
  2. Update the serverless-webpack.yml handlers to load the functions from dist/{functionName}/{functionName}.{handlerName}
  3. Run serverless webpack --out dist from the service.
  4. Swap the yml files name (so that the serverless.yml is now the one loading from the dist directory)
  5. Run sls run as usual, but this time it will load the functions from the precompiled webpack dist 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!

vincentsolutions avatar Oct 29 '17 14:10 vincentsolutions