GithubHelp home page GithubHelp logo

bjfletcher / serverless-plugin-browserify Goto Github PK

View Code? Open in Web Editor NEW

This project forked from doapp-ryanp/serverless-plugin-browserify

0.0 2.0 0.0 26 KB

Speed up your node based lambda's

License: MIT License

JavaScript 100.00%

serverless-plugin-browserify's Introduction

Serverless Browserify Plugin

serverless

A Serverless v1.0 plugin that uses Browserify to bundle your NodeJS Lambda functions.

Why? Lambda's with smaller code start and run faster. Lambda also has an account wide deployment package size limit.

aws-sdk-js now officially supports browserify. Read more about why this kicks ass on my blog.

With the example package.json and javascript code below, the default packaging for NodeJs lambdas in Serverless produces a zip file that is 11.3 MB, because it blindly includes all of node_modules in the zip.

This plugin with 2 lines of configuration produces a zip file that is 400KB!

...
  "dependencies": {
    "aws-sdk": "^2.6.12",
    "moment": "^2.15.2",
    "request": "^2.75.0",
    "rxjs": "^5.0.0-rc.1"
  },
...
const Rx      = require('rxjs/Rx');
const request = require('request');
...

Install

From your serverless project run:

npm install serverless-plugin-browserify --save-dev

Add the plugin to your serverless.yml file and set package.individually to true:

plugins:
  - serverless-plugin-browserify
package:
  individually: true

package.individually is required because it makes configuration more straight forward, and if you are not packaging individually size is not a concern of yours in the 1st place.

Configure

For most use cases you should NOT need to do any configuration. If you are a code ninja, read on.

The base config for browserify is read from the custom.browserify section of serverless.yml. All browserify options are supported (most are auto configured by this plugin). This plugin adds one special option disable which if true will bypass this plugin.

The base config can be over-ridden on a function by function basis. Again custom.browserify is not required and should not even need to be defined in most cases.

custom:
  browserify:
    #any option defined in https://github.com/substack/node-browserify#browserifyfiles--opts

functions:
    usersGet:
      name: ${self:provider.stage}-${self:service}-pageGet
      description: get user
      handler: users/handler.hello      
      browserify:
        noParse:
          - ./someBig.json  #browserify can't optimize json, will take long time to parse for nothing      

Note: package.include is ignored when using this plugin. This is a foreign concept in Browserify, every use case I've ever run across can be handled by leveraging browserify options.

Usage

When this plugin is enabled, and package.individually is true, running serverless deploy and serverless deploy -f <funcName> will automatically browserify your node lambda code.

If you want to see output of bundled file or zip simply set SLS_DEBUG. Ex (using Fish Shell): env SLS_DEBUG=true sls deploy function -v -f usersGet

Also check out the examples directory

Bundle only

Run serverless browserify -f <functionName>. You can optionally dictate where the bundling output dir is by using the -o flag. Ex: sls browserify -o /tmp/test -f pageUpdate.

FAQ

  • Should I use Webpack instead of this plugin? I prefer Browserify over webpack because I have found it supports more modules, optimizes better, and requires less configuration.
  • Why is UglifyJS not built-in? No ES6 support. Issue been open since 2014.
  • My code is not bundling correctly The bundled code is always stored in a tmp dir on your computer. Set SLS_DEBUG=true then re-run your command to output the directory. Fish Shell ex: env SLS_DEBUG=true sls browserify

serverless-plugin-browserify's People

Contributors

doapp-ryanp avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.