Git Product home page Git Product logo

assets-loader's Introduction

assets-loader

Build Status

A play plugin that concatenates, minifies and gzips your JS or Css.

It will process the files depending on the configuration you provide

Supported Versions

  • Play 2.2./Scala 2.10. - version: > 0.11, branch: master (this branch no longer supports older play versions)
  • Play 2.1.*/Scala 2.9.1 - version: 0.10.x, branch: play-2.1.x

Running the examples

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

Description

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="/assets/javascripts/my-app/controllers-23423423.min.gz.js"/>

Api

/**
  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.

Deployer

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", ...)

Closure compiler options

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.

Installing

Add the Asset Loader as a dependency to your build:

  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
  )

Add a configuration for the Loader (either in the main conf or in a separate file that is included)

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
  }
}

Options

The following options apply to all configurations:

  • concatenate (true|false) - concatenate the files
  • minify (true|false) - minify the files
  • gzip (true|false) - gzip the files
  • addHints (true|false) - add hint information to the html - useful for debugging

Use the Asset Loader Assets Controller

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)

About Css concatenation

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.

Logging

If you want to see logs from asset loader - make sure you add a logger for 'assets-loader' to your log config.

Developing

git clone [email protected]:edeustace/assets-loader.git

plugin library

cd plugin
play test # unit tests
play it:test # integration tests
play clean compile publish-local etc....

example app

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.

Release Notes

See releases page from now on...

0.11.2

  • Always read files as utf-8
  • Fixed issues with prod mode + play 2.2.1
  • Support in dev mode for windows style paths

0.11.1

  • Added optional closure CompilerOptions as parameter to the loader

0.11.0

  • Update for Play 2.2.1

0.10.1

  • Enable assets loader to run when app is created using play dist
  • Created ability to specify separate js and/or css configs per mode

0.10-SNAPSHOT

  • Play 2.1.X support

0.9.4

  • Added the option to configure js/css specific settings
  • Added the config property 'deploy' to disable deployment for js/css specifically

0.9.3

  • Added ability for clients to use the assets processed by Assets Loader for deployment -- Added a trait Deployer that clients can implement and then instantiate Loader with this Implementation ! This is a breaking change - Loader is no longer an object.

0.9.2

  • First Version

assets-loader's People

Contributors

benburton avatar edeustace avatar mguillermin avatar rbokel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.