Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
require() deployed nodejs function instead of executing inline (#789)
Browse files Browse the repository at this point in the history
* require() deployed nodejs function instead of executing inline

* add instructions and sample configs for webpack users to runtimes.md

* Update NodeJS images
  • Loading branch information
ianserlin authored and andresmgot committed Sep 18, 2018
1 parent 64acdd4 commit 2581ccb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docker/runtime/nodejs/kubeless.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const context = {
'memory-limit': process.env.FUNC_MEMORY_LIMIT
};

const script = new vm.Script(fs.readFileSync(modPath) + '\nrequire(\'kubeless\')(module.exports);\n', {
const script = new vm.Script('\nrequire(\'kubeless\')(require(\''+ modPath +'\'));\n', {
filename: modPath,
displayErrors: true,
});
Expand Down
61 changes: 61 additions & 0 deletions docs/runtimes.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,67 @@ $ kubeless function deploy myFunction --runtime nodejs6 \
--from-file test.js
```

**For Webpack Users**

Your webpacked functions will be `require()`-d in so your bundle should work out of the box. However, if your bundle size is approaching 1mb you should take advantage of Kubeless' ability to install dependencies for you instead of bundling them all into your payload.

You will need to customize your webpack config to suit your own project, but below is an sample config of how to achieve this in Webpack 4.x:

_webpack.config.js_
```js
const path = require("path");
const nodeExternals = require("webpack-node-externals");
const CopyWebpackPlugin = require("copy-webpack-plugin");

module.exports = {
entry: {
handlers: "./handlers.js"
},
node: {
__filename: true,
__dirname: true
},
target: "node",
// do not include dependencies in the bundle
externals: [nodeExternals()],
devtool: "source-map",
module: {
rules: [
{
test: /\.js$/,
use: "babel-loader",
// do not transpile the depedencies
exclude: /node_modules/
}
]
},
plugins: [
// do include the project's `package.json` in the bundle
new CopyWebpackPlugin([
{
from: path.join(__dirname, "path", "to", "your", "package.json"),
to: "package.json"
}
])
]
};
```

Additionally, in your babel config, you can specify the transpile target to be the version of node you're using for your runtime. This is an example for Babel 7.x:

```js
module.exports = {
plugins: [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-transform-runtime"
],
// note the target node version here for nodejs8
presets: [["@babel/preset-env", { targets: { node: "8.10" } }]]
};
```

#### Server implementation

For the Node.js runtime we start an [Express](http://expressjs.com) server and we include the routes for serving the health check and exposing the monitoring metrics. Apart from that we enable [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) requests and [Morgan](https://github.com/expressjs/morgan) for handling the logging in the server. Monitoring is supported if the function is synchronous or if it uses promises.
Expand Down
4 changes: 2 additions & 2 deletions kubeless-non-rbac.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ local runtime_images ='[
{
"name": "node6",
"version": "6",
"runtimeImage": "kubeless/nodejs@sha256:f2a338c62d010687137c0880d1b68bea926f71a7111251a4622db8ae8c036898",
"runtimeImage": "kubeless/nodejs@sha256:556ff930c7a609d1ad90322d41c8b562cb42313898486fed9674fb2647e4b42f",
"initImage": "node:6.10"
},
{
"name": "node8",
"version": "8",
"runtimeImage": "kubeless/nodejs@sha256:3b5180a9e0bdce043f0f455758561cf4ad62406fcc80140c2393a2c3a1ff88ac",
"runtimeImage": "kubeless/nodejs@sha256:5c9c5e36f9845f2cf8e9e0d55993796d82e34a2b8c0f8a508c9d3c04b2041076",
"initImage": "node:8"
}
],
Expand Down

0 comments on commit 2581ccb

Please sign in to comment.