Public
Snippet $353 authored by Jérome Perrin

nodejs script to run webpack and upload the result through webdav

Edited
webpack-deploy.js
Raw
const webpack = require("webpack");
const config = require("./webpack.config");
const http = require("https");

const ArgumentParser = require("argparse").ArgumentParser;
const parser = new ArgumentParser({
  version: "0.0.1",
  addHelp: true,
  description: "Deploy webpack on webdav"
});

parser.addArgument(["-U", "--url"], {
  help: "WebDAV Server URL.",
  required: true
});
parser.addArgument(["-k", "--insecure"], {
  help: "Ignore SSL certificate validation.",
  action: "storeTrue"
});
parser.addArgument(["-u", "--username"], {
  help: "Username to authenticate on webdav server."
});
parser.addArgument(["-p", "--password"], {
  help: "Password to authenticate on webdav server."
});
parser.addArgument(["-w", "--watch"], {
  help: "Watch files for changs and rebuild incrementally.",
  action: "storeTrue"
});

const args = parser.parseArgs();

// see https://webpack.js.org/api/node/ for reference
const compiler = webpack(config);
compiler.outputFileSystem = require("webdav-fs")(
  args.url,
  args.username,
  args.password,
  args.insecure
    ? new http.Agent({
        rejectUnauthorized: false,
        checkServerIdentity: () => undefined
      })
    : undefined
);

// XXX We don't need to mkdir because the webdav client URL already points to the
// expected folder
compiler.outputFileSystem.mkdirp = function mkdirp(path, callback) {
  return callback();
};
// XXX we just put everyting in current folder
compiler.outputFileSystem.join = function join(base, relative) {
  return relative;
};

const compilerCallback = (err, stats) => {
  if (err) {
    console.error(err.stack || err);
    if (err.details) {
      console.error(err.details);
    }
    return;
  }

  const info = stats.toJson();
  if (stats.hasErrors()) {
    console.error(info.errors);
  }
  if (stats.hasWarnings()) {
    console.warn(info.warnings);
  }
  console.log(
    stats.toString({
      colors: true
    })
  );
};

if (args.watch) {
  console.log("Watching files for changes");
  compiler.watch({}, compilerCallback);
} else {
  compiler.run(compilerCallback);
}
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!