ajmath / serverless-offline-scheduler

MIT License
96 stars 40 forks source link

function replace all previous output of webpack #34

Open kalitamih opened 5 years ago

kalitamih commented 5 years ago

Good day! If function invokes, it replaces all output of webpack. In this case, I can't use anything from serverless offline. Could somebody provide me information, how I can configure webpack to solve this issue?

melvinmaron05 commented 5 years ago

Ran to this issue my workaround was to override the plugins and use --no-build on invoke

plugins/offline-scheduler.js

const Scheduler = require("serverless-offline-scheduler/lib/scheduler");

Scheduler.prototype._executeFunction = function (fName, fInput) {
  return childProcess.execSync(
    \`serverless invoke local --function ${fName} --no-build --data ${JSON.stringify(fInput)}\`,
    {cwd: "./", stdio: "inherit" });
}

class OfflineScheduler {
  constructor(serverless) {
    this.serverless = serverless;

    this.scheduler = new Scheduler(serverless);

    this.commands = {
      schedule: {
        usage: "Run scheduled lambdas locally",
        lifecycleEvents: [
          "run"
        ]
      }
    };
    this.hooks = {
      "before:offline:start:init": () => this.scheduler.run()
    };
  }
}

module.exports = OfflineScheduler;

And use this instead on serverless.yml


plugins:
  - serverless-webpack
  - ./plugins/offline-scheduler
tcastelli commented 4 years ago

I'm having trouble with this too, I tried the previous workaround, but it complains about the folder for the function not existing

Error: ENOENT: no such file or directory, chdir '/home/t/serverless/services/XX' -> '/home/t/serverless/services/XX/.webpack/completeEvents'

If I look at my .webpack folder i can see that the code build with serverless offline start is different and keeps the structure /.webpack/service/nameOfFunction/. is it possible to change that somewhere so both generated structures match?

kingsloi commented 4 years ago

Ah, I spent about 5 hours trying to figure this out, working on one function successfully but then when I went to try my other function, it would ENOENT no such file or directory. I haven't tried the workaround, honestly, I'd just rather invoke my function manually until the package is compatible with webpack. Unless there's another fork/repo we can use instead?

weasnerb commented 3 years ago

The following was modified from @melvinmaron05 because it was no longer working for me.

Make sure the plugin (serverless-offline-scheduler) is installed (mine was installed with --save-dev)

Updated Code:

/**
 * Following code was taken from melvinmaron05 and modified
 * To Fix: https://github.com/ajmath/serverless-offline-scheduler/issues/34
 * From Comment: https://github.com/ajmath/serverless-offline-scheduler/issues/34#issuecomment-544941382
 */
const Scheduler = require("serverless-offline-scheduler/lib/scheduler");
const {execSync} = require('child_process');

Scheduler.prototype._executeFunction = function (fName, fInput) {
  const args = ["npx", "serverless", "invoke", "local", "--function", fName, "--no-build"]
  if (fInput) {
    args.push("--data", JSON.stringify(JSON.stringify(fInput))); // Stringify 2x to escape " when putting json as string in console command.
  }
  for (const { name, value } of this._getSlsInvokeOptions()) {
    args.push(`--${name}`, value);
  }
  return execSync(args.join(' '), { cwd: "./", stdio: "inherit" });
}

class OfflineScheduler {
  constructor(serverless) {
    this.serverless = serverless;

    this.scheduler = new Scheduler(serverless);

    this.commands = {
      schedule: {
        usage: "Run scheduled lambdas locally",
        lifecycleEvents: [
          "run"
        ]
      }
    };
    this.hooks = {
      "before:offline:start:init": () => this.scheduler.run()
    };
  }
}

module.exports = OfflineScheduler;

As before, add the custom plugin file to the plugins list in serverless.yml

plugins:
  - serverless-webpack
  - ./plugins/offline-scheduler