Skip to content

Instantly share code, notes, and snippets.

@fredfortier
Last active September 2, 2015 18:17
Show Gist options
  • Save fredfortier/7cb24fd1fb58271758c7 to your computer and use it in GitHub Desktop.
Save fredfortier/7cb24fd1fb58271758c7 to your computer and use it in GitHub Desktop.
Test sass.js with Rhino

This should help you test sass.js using Rhino.

Some notes:

  • I have included a sample pom.xml in case you would like to use Maven. The important part is that I am using Rhino 1.7 (latest version).
  • The TestRhino#main method initializes a Rhino context which loads the main.js module using CommonJs
  • Create a "modules" directory anywhere in the application classpath. If using Maven to create the project, "src/main/resources" will be in the classpath by convention.
  • Add main.js directly in the "modules" directory
  • Clone sass.js in the "modules" directory. Basically, you will be able to require modules relative to the "modules" directory. In my main.js example, I copied "sass.js/dist" to "modules/sass".
java.lang.System.out.println('requiring sass.sync.js...');
var Sass = require('sass/sass.sync.js');
java.lang.System.out.println('instantiating Sass...');
var sass = new Sass();
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sass.js</groupId>
<artifactId>sass-compiler</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sass-compiler</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.mozilla</groupId>
<artifactId>rhino</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies>
</project>
Exception in thread "main" java.lang.RuntimeException: could not run script
at com.enchoice.kwcn.TestRhino.main(TestRhino.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: org.mozilla.javascript.EcmaError: ReferenceError: "print" is not defined. (file:/Users/fredfortier/Code/kwcn-post-13/sass-compiler/target/classes/modules/sass/sass.sync.js#76)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3951)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3929)
at org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:4019)
at org.mozilla.javascript.ScriptRuntime.nameOrFunction(ScriptRuntime.java:1917)
at org.mozilla.javascript.ScriptRuntime.name(ScriptRuntime.java:1856)
at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1558)
at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:815)
at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:123)
at org.mozilla.javascript.commonjs.module.Require.executeModuleScript(Require.java:342)
at org.mozilla.javascript.commonjs.module.Require.getExportedModuleInterface(Require.java:290)
at org.mozilla.javascript.commonjs.module.Require.call(Require.java:219)
at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1479)
at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:815)
at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:109)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3282)
at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:120)
at org.mozilla.javascript.commonjs.module.Require.executeModuleScript(Require.java:342)
at org.mozilla.javascript.commonjs.module.Require.getExportedModuleInterface(Require.java:290)
at org.mozilla.javascript.commonjs.module.Require.requireMain(Require.java:137)
at com.enchoice.kwcn.TestRhino.main(TestRhino.java:28)
... 5 more
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.commonjs.module.Require;
import org.mozilla.javascript.commonjs.module.provider.StrongCachingModuleScriptProvider;
import org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider;
import java.net.URI;
import java.util.Collections;
public class TestRhino {
public static void main(String[] args) {
try {
URI modulesUri = Thread.currentThread().getContextClassLoader().getResource("modules/").toURI();
System.out.println("the modules uri: " + modulesUri.getPath());
final Context cx = Context.enter();
try {
cx.setOptimizationLevel(-1);
Scriptable scope = cx.initStandardObjects();
Require require = new Require(cx, scope,
new StrongCachingModuleScriptProvider(
new UrlModuleSourceProvider(
Collections.singleton(modulesUri), null)),
null, null, false);
require.requireMain(cx, "main");
} finally {
Context.exit();
}
} catch (Exception e) {
throw new RuntimeException("could not run script", e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment