Last active
December 12, 2015 03:59
-
-
Save jbwyme/4711280 to your computer and use it in GitHub Desktop.
A Java class to execute the requirejs r.js optimizer with Rhino embedded (vs having to do this from the command line)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.mozilla.javascript.Context; | |
import org.mozilla.javascript.Scriptable; | |
import org.mozilla.javascript.tools.shell.Global; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.InputStreamReader; | |
class RequireJSOptimizer { | |
public void optimize(String pathToRJs, String pathToBuildConfigJs) { | |
try { | |
System.out.println("Running r.js optimizer"); | |
// setup Rhino to call r.js | |
Context cx = Context.enter(); | |
cx.setOptimizationLevel(9); | |
Global global = new Global(); | |
global.init(cx); | |
Scriptable scope = cx.initStandardObjects(global); | |
// prepare arguments | |
StringBuffer defineArguments = new StringBuffer("arguments= ["); | |
defineArguments.append("'-o', "); | |
defineArguments.append("'"+pathToBuildConfigJs+"'"); | |
defineArguments.append("]"); | |
cx.evaluateString(scope, defineArguments.toString(), null, 1, null); | |
// load r.js | |
File rjs = new File(pathToRJs); | |
// execute r.js with specified args | |
cx.evaluateReader(scope, new InputStreamReader(new FileInputStream(rjs)), rjs.getName(), 1, null); | |
System.out.println("Ran r.js optimizer"); | |
} catch (Exception e) { | |
System.out.println("Failed to run r.js optimizer"); | |
e.printStackTrace(System.out); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment