Skip to content

Instantly share code, notes, and snippets.

@alexkli
Last active May 23, 2018 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexkli/1906091ed9229bf5e39b0d8557e6dbd6 to your computer and use it in GitHub Desktop.
Save alexkli/1906091ed9229bf5e39b0d8557e6dbd6 to your computer and use it in GitHub Desktop.
serverless-webpack automatic config and externals prototype https://github.com/serverless-heaven/serverless-webpack/issues/399
// prototype for https://github.com/serverless-heaven/serverless-webpack/issues/399
'use strict';
const fs = require('fs');
const webpackConfigFile = "webpack.config.js";
class OpenWhiskExtServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.hooks = {
'before:package:initialize': this.beforePackage.bind(this),
'after:package:finalize': this.afterPackage.bind(this),
};
}
beforePackage() {
if (this.isWebpackPluginInstalled()) {
if (fs.existsSync(webpackConfigFile)) {
this.log("Custom " + webpackConfigFile + " detected");
} else {
this.log("Automatic webpack configuration for openwhisk");
return this.writeWebpackConfig();
}
} else {
this.log("Plugin serverless-webpack is not installed, skipping serverless-openwhisk webpack feature");
}
return Promise.resolve();
}
afterPackage() {
if (this.doCleanup) {
var that = this;
return fs.unlink(webpackConfigFile, function(err) {
if (err) {
return console.error(err);
}
that.debugLog("Removed " + webpackConfigFile);
});
}
}
isWebpackPluginInstalled() {
// check for presence of "webpack" command in at least one plugin
return this.serverless.pluginManager.getPlugins().some( function(plugin) {
return null !== (((plugin || {}).commands || {}).webpack || null);
});
}
writeWebpackConfig() {
const that = this;
const mode = this.options.mode || "production";
var externals = '{}';
// TODO: check for different runtimes in `this.serverless.service.functions.*.runtime`
// and give out a warning then
console.log("Provider runtime", this.serverless.service.provider.runtime);
console.log("API function runtime", this.serverless.service.functions.api.runtime);
var runtime = this.serverless.service.provider.runtime;
if (runtime) {
try {
externals = JSON.stringify(require('./externals/' + runtime.replace(":", "_") + '.json'));
} catch (e) {
this.log("no externals found for runtime: " + runtime)
}
} else {
this.log("no provider.runtime configured, cannot exclude any preinstalled node dependencies");
}
const webpackConfig =
`
// auto-created by serverless-openwhisk plugin on demand
// this file should be automatically deleted after the build
// and not be checked in unless you want to have a custom
// webpack configuration
module.exports = {
mode: '${mode}',
target: 'node',
externals: ${externals}
}
`;
return fs.writeFile(webpackConfigFile, webpackConfig,
function(err) {
if (err) {
return console.error(err);
}
that.debugLog("Created " + webpackConfigFile);
that.doCleanup = true;
}
);
}
debugLog(msg) {
if (process.env.SLS_DEBUG) {
this.log(msg);
}
}
log(msg) {
this.serverless.cli.log("[serverless-openwhisk-extension] " + msg);
}
}
module.exports = OpenWhiskExtServerlessPlugin; 
// an example externals config for the openwhisk nodejs:6 container
// included in js above via require('./externals/' + runtime)
// standard ones would be distributed with the plugin
// but there would need to be a way to reference custom ones, not sure how that's best done yet
{
"cookie": "cookie",
"accepts": "accepts",
"http-errors": "http-errors",
"debug": "debug",
"content-disposition": "content-disposition",
"jsonpointer": "jsonpointer",
"forever-agent": "forever-agent",
"escape-html": "escape-html",
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment