GithubHelp home page GithubHelp logo

sbt-closure's Introduction

NOTE: This project is no longer maintained. You might want to look at sbt-web which has a closure complier.

sbt-closure

Simple Build Tool plugin for compiling JavaScript files from multiple sources using Google's Closure Compiler.

Settings

Installation

If you have not already added the sbt community plugin resolver to your plugin definition file, add this

resolvers += Resolver.url("sbt-plugin-releases",
  new URL("http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-releases/"))(
    Resolver.ivyStylePatterns)

Then add this (see ls.implicit.ly for current version)

addSbtPlugin("org.scala-sbt" % "sbt-closure" % "0.1.0")

Then in your build definition, add

seq(closureSettings:_*)

This will append sbt-closure's settings for the Compile and Test configurations.

To add them to other configurations, use the provided closureSettingsIn(config) method.

seq(closureSettingsIn(SomeOtherConfig):_*)

Usage

The plugin scans your src/main/javascript directory and looks for files of extension .jsm. These files should contain ordered lists of JavaScript source locations. For example:

# You can specify remote files using URLs...
http://code.jquery.com/jquery-1.5.1.js

# ...and local files using regular paths
#    (relative to the location of the manifest):
lib/foo.js
bar.js
../bar.js

# Blank lines and bash-style comments are ignored.

The plugin compiles this in two phases: first, it downloads and caches any remote scripts. Second, it feeds all of the specified scripts into the Closure compiler. The compiler outputs a file with the same name but with a .js extension under path/to/resource_managed/main/js

For example, if your manifest file is at src/main/javascript/foo.jsm in the source tree, the final path would be resource_managed/main/js/foo.js in the target tree.

If, on compilation, the plugin finds remote scripts already cached on your filesystem, it won't try to download them again. Running sbt clean will delete the cache.

Tasks

The plugin is tied to the compile task and will run whenver compile is run. You can also run closure to run it on its own. clean(for closure) will delete the generated files.

Customization

If you're using xsbt-web-plugin, add the output files to the webapp with:

// add managed resources to the webapp
(webappResources in Compile) <+= (resourceManaged in Compile)

Changing the directory that is scanned, use:

(sourceDirectory in (Compile, ClosureKeys.closure)) <<= (sourceDirectory in Compile)(_ / "path" / "to" / "jsmfiles")

Changing target js destination:

To change the default location of compiled js files, add the following to your build definition

(resourceManaged in (Compile, ClosureKeys.closure)) <<= (resourceManaged in Compile)(_ / "your_preference" / "js")

File versioning

The plugin has a setting for a file suffix that is appended to the output file name before the file extension. This allows you to update the version whenever you make changes to your Javascript files. Useful when you are caching your js files in production. To use, add the following to your build.sbt:

(ClosureKeys.suffix in (Compile, ClosureKeys.closure)) := "4"

Then if you have manifest file src/main/javascript/script.jsm it will be output as resource_managed/src/main/js/script-4.js

This is only half of the puzzle, though. In order to know what that suffix is in your code, you can use the sbt-buildinfo plugin. Add the plugin to your project, then add the following to your build.sbt:

seq(buildInfoSettings: _*)

buildInfoPackage := "mypackage"

buildInfoKeys := Seq[Scoped](ClosureKeys.suffix in (Compile, ClosureKeys.closure))

sourceGenerators in Compile <+= buildInfo

This will generate a Scala file with your suffix in src_managed/main/BuildInfo.scala and you can access it in your code like this:

mypackage.BuildInfo.closure_suffix

In my Lift project I have the following snippet:

package mypackage
package snippet

import net.liftweb._
import util.Helpers._

object JavaScript {
  def render = "* [src]" #> "/js/script-%s.js".format(BuildInfo.closure_suffix)
}

Which is called in my template like:

<script lift="JavaScript"></script>

Acknowledgements

This plugin is a sbt 0.11.2 port of sbt-closure

It was modeled after and heavily influenced by less-sbt

sbt-closure's People

Contributors

eltimn avatar s11001001 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

sbt-closure's Issues

Suitable for Play?

Hi there Tim,

What would it take to make this plugin something suitable for Play? We're thinking of factoring out the JS Compiler of Play for 2.3. The JS Compiler is essentially a wrapper for Google's Closure compiler.

Thanks.
Christopher

sbt-closure for sbt 0.13

Hi.

I wanted to ask, if sbt-closure will be published for sbt 0.13 any time soon?
It's a great plugin and I'm currently using it with sbt 0.12, but I really would like to update to 0.13 soon.

Cheers!

Is there anyway to compress all resource files by using one jsm file?

Hi,

On my project, I want to use this plugin to compress all js/css files under the public folder and of course, there are subdirectory which all js.css files there. I followed the readme instruction to change the source directory, and noticed that I have to define one jsm.file for each js/css file If I don't want to combine output source file into one. And it also seems like the output source file has to be the same directory where the jsm at. Are there any configuration I can use to make it more organized?

Jin

Support additional js directories

In order to make this work well for applications written with CoffeeScript, I would like to make this plugin support having additional js source directories. This will allow the sbt-closure plugin to pickup js files produced from compiling CoffeeScript, while keeping the jsm files housed in the src directory.

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.