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

How to use?

Open joel1st opened this issue 8 years ago • 5 comments

Does this need to be npm installed inside each function of the project? Do we need to add the custom object to s-function.json shown in the readme for each function in the project? Does this work when deploying using sls dash deploy or does it require a particular command? Are there any global dependencies that need to be installed?

Thanks,

Joel.

joel1st avatar Jan 11 '16 02:01 joel1st

hey joel - i can answer a little bit of this (below) as to how we got it working but i also have a question for @ac360 on dependencies -> why do I have major issues with certain npm modules not being included in the resulting minified file? For example, I've got a dependency on an npm module facebook-node-sdk... the optimizer plugin is successfully minifying and packaging up the code but I get errors on lambda as if the dependency was not added to the minified file - what am i doing wrong??

 "errorMessage": "Cannot find module '/private/var/folders/_3/z575_nzs6d7_vh7vgf7fhp3r0000gn/T/facebook@1452621875866/modules/registration/node_modules/facebook-node-sdk/lib/basefacebook.js'",

@joel1st as to your questions, to get this running what you'll need is the following: 1.) create a directory at your project root called plugins 2.) move into that directory, git clone this repo 3.) move into serverless-optimizer-plugin directory and npm install 4.) now in root s-project.json, include a plugins section at the end:

"plugins": [
    {
      "path" : "./plugins/serverless-optimizer-plugin"
    }

5.)in your s-function.json you're trying to deploy you'll need to add the config that is listed in the readme of this repo, something like this:

        "custom": {
          "optimize": {
            "browserify": {
              "babelify": true,
              "exclude":[
                "aws-sdk"
              ]
            },
            "minify": true
          }
        }
      }

srg-clearlaunch avatar Jan 12 '16 17:01 srg-clearlaunch

fwiw i think i found out the problem with my facebook-node-sdk module above... looks like there is something funky with the module itself - and i needed to follow these instructions: http://stackoverflow.com/questions/32707970/how-do-you-bundle-aws-sdk-js-into-a-jaws-optimized-package

however, this kind of snowballs... especially if you have a good number of dependencies - for example, now my next issue appears to be something similar - cannot find module ./aes

any tips on how to deal with what appears to be troublesome modules that break browserify - or even any tips on how to discover this locally before deploy?

srg-clearlaunch avatar Jan 12 '16 19:01 srg-clearlaunch

fwiw to anyone else who reads this, here's what i settled on -

"custom": {
        "excludePatterns": [],
        "envVars": [],
        "optimize": {
          "browserify": {
            "babelify": true,
            "exclude" : [
              "aws-sdk",
              "facebook-node-sdk",
              "googleapis",
              "google-auth-library",
              "simple-twitter"
            ]
          },
          "includePaths":[
            "modules/registration/node_modules/"
          ],
          "minify": true
        }
      },

what happens above is that i'm telling browserify to exclude the specific dependencies for the module and then including the entire node_modules directory - note the path there on includePaths is relative from the back/ folder. this gives a good bit of space savings but not ideal, i'd definitely like to understand more about why browserify fails and how i can help along the modules which are messy - browserify shims perhaps?

srg-clearlaunch avatar Jan 12 '16 20:01 srg-clearlaunch

Thanks, @srg-clearlaunch , that was a good tip. I cost me some time to get this working for me with 0.1.0. As I'm using mysql I had to do something like that:

  "custom": {
    "optimize": {
      "exclude": [
        "aws-sdk",
        "mysql"
      ]
    },
    "includePaths": [
      "config",
      "node_modules/mysql"
    ]
  }

Note that a bug (?) in 0.1.0 requires includePaths to be directly underneath the custom property instead of inside of optimize where it belongs: https://github.com/serverless/serverless-optimizer-plugin/pull/8

arabold avatar Jan 20 '16 23:01 arabold

See my comment https://github.com/serverless/serverless-optimizer-plugin/issues/49#issuecomment-236313291

skilledDeveloper avatar Jul 29 '16 23:07 skilledDeveloper