pyrsmk / toast

A modern JS/CSS asset loader, written in TypeScript.
MIT License
118 stars 13 forks source link

Allow loading stylesheets with cache-busting suffixes #6

Closed rudylattae closed 10 years ago

rudylattae commented 10 years ago

The current snippet of code that figures out if you are trying to load a css or javascript resource is very strict.

https://github.com/pyrsmk/toast/blob/master/src/toast.js#L56

Since it expects the resource name to absolutely end in ".css" it is impossible to use other naming conventions e.g. "cache buster" like http://www.example.com/static/styles/some-facy-stuff.css?v=20131201

Kindly consider supporting such formats as they are used quite a bit.

My suggestion is that instead of using the regex \.css$ you could use \.css[^\.]*$. It's an example of a more relaxed regex I saw used in another loader (I forget which one so I can't properly attribute source here).

The test then becomes:

// CSS
if(/\.css[^\.]*$/.test(resource)){
    // Create LINK element
    // ...
} 
// JS
else{
    // Create SCRIPT element
    // ...
}
pyrsmk commented 10 years ago

I didn't think of that, thanks! For the regexp, I think it should be better to match if a ? is there otherwise of matching anything else than a .. I'll work on that quickly ;)

pyrsmk commented 10 years ago

I forget how that code was written. We don't care, your solution is great :p

pyrsmk commented 10 years ago

Fixed ;)