Skip to content

craigburke/javascript-core-asset-pipeline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JavaScript Core Asset Pipeline

This provides the JavaScriptProcessor base class for building Asset Pipeline processors that run JavaScript with caching, thread safety, and a nice DSL.

Note
The JavaScript is currently run using the Rhino engine. Nashorn support might be added at a later date.

Getting started

dependencies {
    compile 'com.craigburke.assets:javascript-core:0.2.4'
}

Example

import com.craigburke.asset.JavaScriptProcessor
import com.craigburke.asset.JavaScriptEngine

import groovy.transform.Synchronized

class ExampleProcessor extends JavaScriptProcessor {

    ExampleProcessor(AssetCompiler precompiler) {
        super(precompiler)
    }

    static JavaScriptEngine exampleEngine

    @Syncronized
    JavaScriptEngine getEngine() { // <1>
        exampleEngine = exampleEngine ?: new JavaScriptEngine('transform.js') // <2>
        exampleEngine
    }

    String process(String input, AssetFile file) {
        javaScript {
            var1 = input // <3>
            eval 'transformFunction(var1);' // <4>
        }
    }
}
  1. The getEngine method must be implemented, here we’re just returning a static JavaScriptEngine

  2. The JavaScriptEngine constructor can optionally load a file that’s contained on your classpath. Here the file transform.js is loaded

  3. This adds the variable var1 to your JavaScript context with the value of input.

  4. The eval method allows you to execute a JavaScript function (or arbitrary JavaScript). The result is returned as a String

About

Core project for JavaScript Asset Pipeline processors

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published