magnars / optimus

A Ring middleware for frontend performance optimization.
364 stars 23 forks source link

Regex paths are not checked in dependencies #29

Open domkm opened 9 years ago

domkm commented 9 years ago

This means that you must specify the exact path when referencing assets in jar dependencies, which makes using things like WebJars awkward.

magnars commented 9 years ago

File paths and Jar paths are treated the same by the asset loader. So whatever weird is going on here, I don't think it's the regex matching on jars. See https://github.com/magnars/optimus/blob/master/src/optimus/assets/creation.clj#L84-L94

Could you investigate more?

domkm commented 9 years ago

Sure. Let's use com.facebook/react as an example.

This jar adds "react/react.js" to the classpath.

(clojure.java.io/resource "react/react.js")
;=> #<URL jar:file:/Users/DomKM/.m2/repository/com/facebook/react/0.11.1/react-0.11.1.jar!/react/react.js>

We can load this through Optimus if we specify the exact name.

(optimus.assets/load-bundle "react" "main.js" ["/react.js"])
;=> ({:bundle "main.js", :last-modified 1406325048000,...

However, Optimus cannot find the file if a regex is used.

(optimus.assets/load-bundle "react" "main.js" [#".+\.js$"])
;=> java.lang.Exception: No files matched regex .*js

This is because optimus.assets.creation/realize-regex-paths matches against file paths in the filtered directories from optimus.class-path/class-path-elements, which removes any that contain "/.m2/".

magnars commented 9 years ago

Ah, yes, there's even this big comment on it.

there are major performance improvements to be gained by not traversing the entirety of the class path when running locally and picking up files from the class path for every request. Since you're not serving files from a .m2 folder in production, this is hopefully a safe bet.

I guess the bet failed in your case. However, doing a regexp scan on all the files on the class path is very slow.

There has been some work on fixing this here: #20 - but seems like it was abandoned. Maybe you would be interested in taking a look?

domkm commented 9 years ago

I ended up using webjars-locator since it circumvented this issue for WebJars assets.

Perhaps adding a note in the exception here about only scanning project files might help users avoid confusion as to why their assets can't be found.