Giter Club home page Giter Club logo

asset-minifier's Introduction

asset-minifier

Continuous Integration status

A Clojure library to minify HTML, CSS and Js resources, for a Leiningen plugin see lein-asset-minifier.

The latest version requires ClojureScript 1.9+. If you're on an older version of ClojureScript, then use version 0.2.0 instead.

Usage

Clojars Project

The minifier provides three functions called minify-html, minify-css and minify-js, all functions accept a source path followed by the output target and an optional parameter map. The source can be a filename, a directory, or a sequence of directories and or filenames.

(ns my.ns
  (:require [asset-minifier.core :refer [minify-html minify-css minfy-js minify]]))

The minify-html function allows specifying config for htmlcompressor. Please refer too clj-html-compressor.

; minify html files from folder /html into /minified
(minify-html "html" "minified")

The minify-css function allows specifying :linebreak to force line breaks after a certain number of chracters in the minified CSS.

;minify the site.css into site.min.css
(minify-css "site.css" "site.min.css")

;minify the site.css file and any files found under dev/resources/css
(minify-css ["site.css" "dev/resources/css"] "site.min.css")

(minify-css "site.css" "site.min.css" {:linebreak 80})

;minify all css resources into site.min.css
(minify-css "dev/resources/css" "resources/public/css/site.min.css")

The function returns a map containing :original-size, :compressed-size, and :gzipped-size keys indicating the size of the assets before and after compression.

The minify-js function allows specifying the level of optimizations, which can be :none, :simple, :whitespace or :advanced and defaults to simple optimizations.

The :externs key can be used to specify the externs file to be used with the advanced optimisations to prevent munging of external functions.

The default input language is ecmascript6 with an ecmascript5 output. This can be changed with setting the :language-out key to :ecmascript3 for example. The es6 output is not supported by the closure-compiler currently included.

;minify site.js into site.min.js
(minify-js "site.js" "site.min.js")

;minify files found under dev/resources/js and dev/resources/vendor into site.min.js
(minify-js ["dev/resources/js" "dev/resources/vendor"] "site.min.js")

;minify site.js into site.min.js with advanced optimizations and jquery externs
(minify-js "site.js" "site.min.js" {:optimization :advanced
                                    :externs ["jquery.min.js"]
                                    :language-out :ecmascript3})

;minify all Js resources into site.min.js
(minify-js "dev/resources/js" "resources/public/js/site.min.js")

The function returns a map containing :original-size, :compressed-size, and :gzipped-size keys indicating the size of the assets before and after compression. In addition the map may contain :warnings and :errors keys to indicate any warnings or errors that were issued during compilation.

Minify

The minify function can be used intead of functions describet below. It tales vector based config as argument. See example:

(minify [[:html {:source "dev/resource/html" :target "dev/minified/html"}]
         [:css {:source "dev/resources/css" :target "dev/minified/css/styles.min.css"}]
         [:js {:source ["dev/res/js1", "dev/res/js2"] :target "dev/minified/js/script.min.js"}]])

License

Copyright © 2017 Yogthos

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

asset-minifier's People

Contributors

alehatsman avatar blak3mill3r avatar mavbozo avatar nkabrich avatar rangeoshun avatar rredpoppy avatar stephanos avatar yogthos avatar

Stargazers

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

asset-minifier's Issues

Charset exceptions

I am getting this exception when using minify-js:
java.lang.String cannot be cast to java.nio.charset.Charset

on a side note: why use such an old closure compiler?

No such file or directory

I'm using lein-asset-minifier and ran into a problem:

minifying assets...
java.io.FileNotFoundException: resources/public/js/libs.js (No such file or directory)
 at java.io.FileOutputStream.open (FileOutputStream.java:-2)
    java.io.FileOutputStream.<init> (FileOutputStream.java:221)
    clojure.java.io/fn (io.clj:230)
    clojure.java.io$fn__8641$G__8604__8648.invoke (io.clj:69)
    clojure.java.io/fn (io.clj:263)
    clojure.java.io$fn__8641$G__8604__8648.invoke (io.clj:69)
    clojure.java.io/fn (io.clj:166)
    clojure.java.io$fn__8654$G__8608__8661.invoke (io.clj:69)
    clojure.java.io$writer.doInvoke (io.clj:119)
    clojure.lang.RestFn.invoke (RestFn.java:410)
    asset_minifier.core$minify_js.doInvoke (core.clj:112)
    clojure.lang.RestFn.invoke (RestFn.java:442)
    asset_minifier.core$minify$iter__1060__1064$fn__1065.invoke (core.clj:127)
    clojure.lang.LazySeq.sval (LazySeq.java:40)
    clojure.lang.LazySeq.seq (LazySeq.java:49)
    clojure.lang.RT.seq (RT.java:484)
    clojure.core$seq.invoke (core.clj:133)
    clojure.core.protocols$seq_reduce.invoke (protocols.clj:30)
    clojure.core.protocols/fn (protocols.clj:54)
    clojure.core.protocols$fn__6031$G__6026__6044.invoke (protocols.clj:13)
    clojure.core$reduce.invoke (core.clj:6289)
    clojure.core$into.invoke (core.clj:6341)
    asset_minifier.core$minify.doInvoke (core.clj:124)
    clojure.lang.RestFn.invoke (RestFn.java:423)
    leiningen.minify_assets$minify.invoke (minify_assets.clj:42)
    leiningen.minify_assets$minify_assets.doInvoke (minify_assets.clj:99)
    clojure.lang.RestFn.invoke (RestFn.java:423)
    clojure.lang.Var.invoke (Var.java:383)
    clojure.lang.AFn.applyToHelper (AFn.java:156)
    clojure.lang.Var.applyTo (Var.java:700)
    clojure.core$apply.invoke (core.clj:626)
    leiningen.core.main$partial_task$fn__6071.doInvoke (main.clj:253)
    clojure.lang.RestFn.applyTo (RestFn.java:139)
    clojure.lang.AFunction$1.doInvoke (AFunction.java:29)
    clojure.lang.RestFn.applyTo (RestFn.java:137)
    clojure.core$apply.invoke (core.clj:626)
    leiningen.core.main$apply_task.invoke (main.clj:303)
    leiningen.core.main$resolve_and_apply.invoke (main.clj:309)
    leiningen.do$do.doInvoke (do.clj:40)
    clojure.lang.RestFn.invoke (RestFn.java:516)
    clojure.lang.Var.invoke (Var.java:409)
    clojure.lang.AFn.applyToHelper (AFn.java:178)
    clojure.lang.Var.applyTo (Var.java:700)
    clojure.core$apply.invoke (core.clj:626)
    leiningen.core.main$partial_task$fn__6071.doInvoke (main.clj:253)
    clojure.lang.RestFn.invoke (RestFn.java:410)
    clojure.lang.AFn.applyToHelper (AFn.java:154)
    clojure.lang.RestFn.applyTo (RestFn.java:132)
    clojure.lang.AFunction$1.doInvoke (AFunction.java:29)
    clojure.lang.RestFn.applyTo (RestFn.java:137)
    clojure.core$apply.invoke (core.clj:626)
    leiningen.core.main$apply_task.invoke (main.clj:303)
    leiningen.core.main$resolve_and_apply.invoke (main.clj:309)
    leiningen.core.main$_main$fn__6136.invoke (main.clj:377)
    leiningen.core.main$_main.doInvoke (main.clj:366)
    clojure.lang.RestFn.invoke (RestFn.java:408)
    clojure.lang.Var.invoke (Var.java:379)
    clojure.lang.AFn.applyToHelper (AFn.java:154)
    clojure.lang.Var.applyTo (Var.java:700)
    clojure.core$apply.invoke (core.clj:624)
    clojure.main$main_opt.invoke (main.clj:315)
    clojure.main$main.doInvoke (main.clj:420)
    clojure.lang.RestFn.invoke (RestFn.java:436)
    clojure.lang.Var.invoke (Var.java:388)
    clojure.lang.AFn.applyToHelper (AFn.java:160)
    clojure.lang.Var.applyTo (Var.java:700)
    clojure.main.main (main.java:37)

The js directory does not exist inside resources/public (because lein clean removed it). Could you automatically create the parent directories with (io/make-parents dest-file)?

setting language-in option

there is a minify task in boot-cljsjs that uses minify-js. Being able to set the closure compiler option for "language-in" to ECMASCRIPT5 will help us prevent "trailing comma" errors when minifying some cljsjs-packages that are auto-built.

I can create the PR; just let me know if you have thoughts on it. thanks

setting language-out option

I need to transpile a package from ES6 to ES5 in cljsjs/packages, which uses asset-minifier.core/minify-js to do minifications. Currently this is not possible as LANGUAGE_OUT arg cannot be set, and hence this error:

ES6 is only supported for transpilation to a lower ECMAScript version. Set --language_out to ES3, ES5, or ES5_STRICT.

Any ideas or thought, or shall I just create a PR? I'd gladly do that BTW.

Cheers!

Multiple targets option

There could be a way of create multiples targets with the same source file, so i will not have a big file with all js, and i only need to import in my html the file that i want. It will only minify the file, not group all files in one.

Something like this example:

:minify-assets [[:js {
    :source ["source/"] 
    :target "target/",
    :pattern "*.min.js"}]]

from:

source
├── login.js
└── home.js

to

target
├── login.min.js
└── home.min.js

What you think of it?

Directory structure being flattened

Right now if source is a directory with subdirectories, the subdirectory structure will not be preserved in target. Would it make sense to preserve any subdirectory structure in the target directory? The current behavior runs the risk of files in subdirectories overwriting identically-named files in a parent directory (in the target directory).

Otherwise, it would be nice to be able to disallow this recursive behavior, giving the user the chance to handle subdirectories with multiple calls to minify.

I am actually having this problem with the lein-asset-minifier project, but I'm guessing this is the appropriate place for the issue. If you you think either of these should be implemented but don't have the time to do it yourself, I'd be happy to help.

Add HTML minification

In addition to CSS and JS, HTML can also be minified to reduce the size of assets that need to be sent over the wire.

There is an existent library for the JVM that does that well: htmlcompressor. Example Clojure use of this can be found here.

Would you accept a PR for this? Any additional thoughts?

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.