Separate plugins for different environments


#1

I want to load different plugin per “stage”, I use Rich’s approach for this for custom variables and want to take the approach for plugins (http://www.goingserverless.com/blog/using-environment-variables-with-the-serverless-framework)

This does not load “offline”. Is there a means to see what the output of the serverless.yml parser is? Other ideas?

Thanks

In serverless.yml

plugins: ${file(./serverless_plugins.yml):${self:provider.stage}}

serverless_plugins.yml

default_plugins: &default_plugins

dev:
  <<: *default_plugins
  - serverless-offline

 stage:
  <<: *default_plugins

prod:
  <<: *default_plugins

#2

HI @pkubat,
I want do same thing as you wanted. Have you find a way to resolve your issue?


#3

@ztmdsbt Plugins are loaded before the serverless.yml is parsed, so you can’t have dynamic variables like this.

Can you expand on the use case for having stage-specific plugins?


#4

My scenario is that I would like to debug TypeScript with serverless-offline and without serverless-webpack (I was having trouble with the source mapping).

So being able to disable the webpack plugin during development, would streamline the workflow

So I guess I would not need this feature if I solved the webpack issue, but debugging without webpack is faster to build too.


#5

I see. That does make sense, and I can understand why you would want that. Unfortunately, it doesn’t really fit with how the Framework works given the plugin loading structure. Sorry about that – your best bet is just to comment out that plugin when needed.


#6

Thanks Alex, yeah, that is what I ended up doing. Also added documentation to the repo’s README about uncommenting and commenting those plugins.


#7

Why not always run the plugin, and then inside the plugin access the environment from the options object? You can then conditionally run the plugin logic.

If you don’t want to define for which environment it should run within the plugin, you could also do this within serverless.yml and then have the plugin lookup if it should run or not based on the given environment. Something like

custom:
  yourPlugin:
    prod:
      enabled: true

Indicating that it would only run its logic if the stage equals prod.