csantanapr commented on a change in pull request #2958: initial support for 
nodejs8
URL: 
https://github.com/apache/incubator-openwhisk/pull/2958#discussion_r151793962
 
 

 ##########
 File path: docs/actions.md
 ##########
 @@ -493,6 +494,90 @@ To create an OpenWhisk action from this package:
 
 Finally, note that while most `npm` packages install JavaScript sources on 
`npm install`, some also install and compile binary artifacts. The archive file 
upload currently does not support binary dependencies but rather only 
JavaScript dependencies. Action invocations may fail if the archive includes 
binary dependencies.
 
+### Package an action as a single bundle
+
+It is convenient to only include the minimal code into a single `.js` file 
that includes dependencies. This approach allows for faster deployments, and in 
some circumstances where packaging the action as a zip might be too large 
because it includes unnecessary files.
+
+You can use a JavaScript module bundler such as 
[webpack](https://webpack.js.org/concepts/). When webpack processes your code, 
it recursively builds a dependency graph that includes every module that your 
action needs.
+
+Here is a quick example using webpack:
+
+Taking the previous example `package.json` add `webpack` as a development 
depency and add some npm script commands.
+```json
+{
+  "name": "my-action",
+  "main": "dist/bundle.js",
+  "scripts": {
+    "build": "webpack --config webpack.config.js",
+    "deploy": "wsk action update my-action dist/bundle.js --kind nodejs:8"
+  },
+  "dependencies": {
+    "left-pad": "1.1.3"
+  },
+  "devDependencies": {
+    "webpack": "^3.8.1"
+  }
+}
+```
+
+Create the webpack configuration file `webpack.config.js`.
+```javascript
+var path = require('path');
+module.exports = {
+  entry: './index.js',
+  output: {
+    path: path.resolve(__dirname, 'dist'),
+    filename: 'bundle.js'
+  },
+  target: 'node'
+};
+```
+
+Set the variable `global.main` to the main function of the action.
+From the previous example:
+```javascript
+function myAction(args) {
+    const leftPad = require("left-pad")
+    const lines = args.lines || [];
+    return { padded: lines.map(l => leftPad(l, 30, ".")) }
+}
+global.main = myAction;
+```
+
+If your function name is `main`, use this syntax instead:
+```javascript
+global.main = main;
+```
+
+To build and deploy an OpenWhisk Action using `npm` and `webpack`:
+
+1. First, install dependencies locally:
+
+  ```
+  npm install
+  ```
+
+2. Build the webpack bundle:
+
+  ```
+  npm run build
+  ```
+
+  The file `dist/bundle.js` is created, and is used to deploy as the Action 
source code.
+
+3. Create the Action using the `npm` script or the CLI.
+  Using `npm` script:
+  ```
+  npm run deploy
+  ```
+  {: pre}
+  Using the CLI:
+  ```
+  bx wsk action update my-action dist/bundle.js
 
 Review comment:
   sorry about that will fix

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to