Code Monkey home page Code Monkey logo

openwhisk-webpack's Introduction

Bundling OpenWhisk Actions with Webpack

This is an example OpenWhisk action that is meant to be bundled with Webpack. When creating an action on OpenWhisk with JavaScript, it requires that there only be one JavaScript file and that that file has a main function.

OpenWhisk also runs on Node v0.12.9 which does not support any ES6 syntax.

To get around this, and to use arbitrary npm packages when creating our actions, we bundle them with Webpack.

For more information about the motivation behind this example repo, read this blog post.

Methodology

When running in OpenWhisk, the runtime expects there to be an accessible main function. To satisfy this requirement, we set global.main to our main function in index.js. This allows the action to run properly in OpenWhisk despite all of the closures Webpack creates.

What this Example Does

This uses code over multiple files, es6 syntax, and the bluebird promise library. This should cover all the bases for different Webpack use cases. Our OpenWhisk action will take a GitHub username, and then request language data for each repo that user has.

Setup

Make sure you've read through the OpenWhisk getting started docs, then install the dependencies via:

> npm install

Create the Action Bundle

As defined in our webpack.config.js, this will make a minified bundle at dist/bundle.js. To create it we can run the pre-configured script:

> npm run build

or invoke webpack via the command line:

> npm install -g webpack
> webpack --config webpack.config.js --progress --colors

Create an Action on OpenWhisk

From this point on, creating the action is the same as if we had a single js file. All we need to do is point to our bundle:

> wsk action create github-language dist/bundle.js
ok: created action github-language

Invoke the Action

> wsk action invoke --blocking --result github-language --param user 'kauffecup'
{
  "languageArr": [
    {
      "JavaScript": 51019
    },
    {
      "JavaScript": 3083
    },
    {
      "CSS": 1010,
      "HTML": 2666,
      "JavaScript": 8957,
      "Shell": 628
    },
    {
      "JavaScript": 16386
    },
    {
      "JavaScript": 22489
    }
  ]
}

openwhisk-webpack's People

Contributors

kauffecup avatar

Watchers

James Cloos avatar James Taylor 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.