A play plugin that concatenates, minifies and gzips your JS or Css.
It will process the files depending on the configuration you provide
master
(this branch no longer supports older play versions)play-2.1.x
The example is configured as a Play 2.2.1 application by default.
To see the logs in dev mode run:
play -Dlogger.file=conf/logging/prod.xml
To see the logs in prod mode run:
./target/universal/stage/bin/example-221 -Dlogger.file=conf/logging/prod.xml
Say you have the following folder structure:
/public
/javascripts
/my-app
singleFile.js
/controllers
app.js
/subfolder
/helper.js
A configuration like so:
dev {
concatenate: true
minify: true
gzip: true
}
You call the loader:
val loader = new com.ee.assets.Loader()
loader.scripts("name")("javascripts/my-app/controllers", "javascript/my-app/singleFile.js")
The loader will concatenate singleFile.js, app.js and helper.js into one file, minify it then gzip it and place it in your target folder and return a script tag so your html will look like this:
<script type="text/javascript" src="https://github.com/edeustace/assets-loader/raw/master/assets/javascripts/my-app/controllers-23423423.min.gz.js"/>
/**
Loader(
deployer:Option[Deployer] = None,
mode : Mode.Mode,
config : Configuration,
closureCompilerOptions : Option[CompilerOptions] = None,
info : AssetsInfo = AssetsInfo("assets", "public")
)
*/
val loader = new com.ee.assets.Loader(None, Play.current.mode, Play.current.configuration)
loader.scripts("name")("path_to_scripts_folder_or_file", ...)
loader.css("name")("path_to_css_folder_or_file", ...)
Note: The paths argument supports either directories or individual files. If a directory it'll recursively pick the js/css files for you.
When instantiating the loader you can optionally pass in an implementation of the Deployer trait. This looks like this:
trait Deployer {
def deploy(filename: String, lastModified: Long, contents: => InputStream, info : ContentInfo): Either[String,String]
}
case class ContentInfo(contentType:String, contentEncoding:Option[String] = None)
This allows you to for example deploy your assets to Amazon S3, then return the deployed path to the Loader which will the return that script path.
val loader = new com.ee.assets.Loader(Some(S3Deployer))
loader.scripts("name")("path_to_scripts_folder_or_file", ...)
loader.css("name")("path_to_css_folder_or_file", ...)
You can pass in your own closure compiler options when you are instantiating the Loader instance. If you pass in nothing it'll use the default settings.
val assetsLoader = "com.ee" %% "assets-loader" % "0.12.3"
// snapshot version
//val assetsLoader = "com.ee" %% "assets-loader" % "0.12.4-SNAPSHOT"
val assetsLoaderReleases = "ed eustace" at "http://edeustace.com/repository/releases"
val assetsLoaderSnapshots = "ed eustace" at "http://edeustace.com/repository/snapshots"
PlayProject(...).settings(
libraryDependencies += assetsLoader,
resolvers += assetLoaderReleases
)
assetsLoader: {
# if within dev/test/prod there is a js/css node - these settings will be used specifically for those files.
dev : {
js : {
concatenate:true
minify:false
gzip:false
deploy: true
addHints: true|false (default: false)
}
css : {
concatenate:true
minify:false
gzip:false
}
}
# if no js/css node defined - settings apply to both
test : {
concatenate: true
minify: false
}
prod : {
concatenate: true
minify: true
gzip: true
}
}
The following options apply to all configurations:
The default Assets controller in Play doesn't work with the loader because it only does a look up on the classLoader, the provided controller also looks up using the file system.
GET /assets/*file com.ee.assets.controllers.Assets.at(path="/public", file)
Css concatenation doesn't account for paths to other resources within css files, so paths may break if the source css file and the concatenated css are in different folders. We are looking into whats the best way to solve this.
If you want to see logs from asset loader - make sure you add a logger for 'assets-loader' to your log config.
git clone git@github.com:edeustace/assets-loader.git
cd plugin
play test # unit tests
play it:test # integration tests
play clean compile publish-local etc....
You need to publish-local in the plugin to update your local repo. Then in the example play app run play update
to pull in the latest jar. To run the example run play run
.
play dist