serverless-plugin-optimize icon indicating copy to clipboard operation
serverless-plugin-optimize copied to clipboard

Custom options can't be set with Serverless variables

Open sammort opened this issue 5 years ago • 0 comments

It's not possible to set custom option values for the plugin with Serverless Variables. For example, we are trying to disable minification of code at local stage deployment.

The reason seems to be because the /** Set custom options */ logic is run before any Serverless hooks. This means that the variables have not been interpolated.

this.custom.optimize.minify literally has the value specified in serverless.yml, for example

custom:
  optimize:
    minify: ${self:custom.enable_minify.${self:provider.stage}, 'true'}

this.custom.optimize.minify value is ${self:custom.enable_minify.${self:provider.stage}, 'true'}

I've fixed this by moving the custom options code in to the beforeCreateDeploymentArtifacts method, so that the options are set following the before:package:createDeploymentArtifacts hook. I'm not 100% confident that is a totally safe solution but it does the job.

Steps to reproduce

  1. In a serverless project, set serverless.yml
plugins:
  - serverless-plugin-optimize

custom:
  enable_minify:
    local: false
    production: true
  optimize:
    minify: ${self:custom.enable_minify.${self:provider.stage}, 'true'}
  1. Deploy with sls deploy -s local

Results You Received:

Code will be minified as part of optimized packaging steps.

Results You Expected:

Code is not minified as part of optimized packaging steps.

Environment Details:

Serverless: 2.1.1 serverless-plugin-optimize: 4.1.4-rc.1

sammort avatar Oct 15 '20 22:10 sammort