We'll take the stylus plugin as an exemple.
To make our plugin to work with node, we are going to use Christopher's js-transpiler. It allows to develop a shell script that will work independtly on both node and the jvm. You can call a shell script from the cli like this:
node my-shell.js file.extension '{"flag":true}'
You can use this to debug your processor until it works!
You will find in attached, stylus-shell.js
which will be our example. There are 4 parts in the file:
Nothing hard here. You can use npm (package.json
) to define dependencies if you want. Note that sbt-web
works natively with package.json
, but cannot ensure that your code will work on Rhino, and node_modules/
files won't be accessible from other projects (such as the ones who will have the currently developed plugin). So a better practice is to use npm only the early stages of the developement workflow, then use webjars.
This is the code the logic of the build. We use when.js promises here to manage the callbacks. What you have to do is to define a function that will take in argument an input and an output file locations, and that returns a when promise.
The processor uses node apis such as mkdirp
or fs.readFile
, those apis are readable from Rhino, so you don't have to worry about this.
One line:
jst.process({processor: processor, inExt: ".styl", outExt: (args.options.compress? ".min.css" : ".css")}, args);
You send the processor and some informations about the extentions to watch and to output. The last parametter is the arguments sent from sbt or the cli, basically the files locations.
The biggest advantage of using sbt-web, especially with Playframework, is to get nice error reporting (in the browser or the IDE). In order to do that, you need to transform errors from your processor to this json object structure:
{
message: "ERROR MESSAGE",
severity: "error",
lineNumber: parseInt(lineNumber),
characterOffset: 0,
lineContent: "x + z...",
source: input
}
TODO