I selected GraalVM as it looked like a great way to execute npm modules from Java code. In my use-case I'm wanting to use something like PurgeCSS to reduce CSS file sizes in my Spring application. A controller would call PurgeCSS that would in turn cache a CSS file and then later serve the cached version.
What design pattern can I use to execute a JavaScript-based CSS reducing/purging utility from Java code?
It seems that there are two flavours of JavaScript that GraalVM Polyglot / Truffle support. Node flavour (accessible from the command-line only), and ECMAScript flavour (accessible programatically only). And it seems that the two cannot interact with one another. Please correct me.
This is what I tried:
Using GraalVM's npm install purgecss, which worked great, then the following code which didn't work.
@Component
public class JavaScript {
String js = "var Purgecss = require('purgecss')\n" +
"const purgeCss = new Purgecss({\n" +
" content: ['**/*.html'],\n" +
" css: ['**/*.css']\n" +
"})\n" +
"const purgecssResult = purgecss.purge()";
public void testJavaScript() {
try (Context context = Context.create()) {
Value function = context.eval("js", this.js);
assert function.canExecute();
String response = function.execute().asString();
System.out.println(response);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Seems that require is a node-specific thing so that doesn't work. Unfortunately the whole of purgeCSS is filled with require's so I assume that puts the whole library out.
- Would I have to find a single file pure
.mjsCSS library (ESM version)? - Or is there a way to specify node favour JavaScript, enabling
require? - Or can I replace all the
require's withload's?
Any help or direction on how to make GraalVM's JavaScript functionality work in this use-case is greatly appreciated.
PS: I'm running Spring via Gradle. Was previously looking at hooking Spring up to Gulp via the Gradle Wrapper and then executing CSS tasks via Gulp. I'm hosting on Heroku and see they do allow multiple buildpacks (Java and Node)