Skip to content
This repository

Fixed assets precompile regex #2876

Merged
merged 1 commit into from over 2 years ago

5 participants

Alex Yakoubian Guillermo Iguaran Santiago Pastorino Richard Hulse Joshua Peek
Alex Yakoubian

The original regex was matching things like jquery.min.js and putting a lot of junk (like jquery-#{digest}.min.js) in public/assets/ that shouldn't be there. The new regex matches everything except files ending with .js or .css (as was intended).

Guillermo Iguaran

@alex3 can you provide a test case for this?

Santiago Pastorino
Owner

/cc @josh

Richard Hulse

This pull request is related to bug #1913

The orignal regex appears to want to include files that are not .js or .css files, but as you say is too broad.

Alex Yakoubian

@guilleiguaran I just wrote and pushed a test case for this. The commits are squashed into one.

Richard Hulse

Can I suggest that you add sanity tests for images and the other file types (including with multiple dots) that this is supposed to capture?

Alex Yakoubian

@rhulse I think the other tests test for images. Maybe an image named something like happy.face.png?

Also, should I push a new commit or squash it into one again?

Richard Hulse

I would expect so.

I suggested the extra tests because I spent some time (unsuccessfully) trying to fix that regex! I wonder if it's worth covering off underscores and dashes too. If this breaks there will be a lot of head-scratching and gnashing of teeth. :-)

Santiago Pastorino
Owner

@alex3 +1 to rhulse suggestion, please add those tests and I will merge the pull request

Richard Hulse

suggestions:

  • happy-face.png
  • happy_face.png
  • happy.happy_face.png
  • happy_happy.face.png
  • happy.happy.face.png

And NOT match:

  • .happy.face
  • .anything-with-a-dot-start

:-) !

Alex Yakoubian

@rhulse @spastorino All that in one test or a couple of them?

Santiago Pastorino
Owner

@alex3 as you find it better, perhaps you can iterate over a bunch of valid / invalid file names checking if they have been precompiled or not

Richard Hulse

Would it be a appropriate to extract the creation of this array into its own method, which can then include more comprehensive documentation on the intent? It may avoid future issues if there is refactoring.

Alex Yakoubian

@spastorino I just pushed a new (squashed) commit with the updated regex and a pretty comprehensive test. I've included all of @rhulse's suggestions in the test.

@rhulse That would probably be better but I think that should be a separate pull request.

railties/lib/rails/application/configuration.rb
... ...
@@ -37,7 +37,7 @@ module Rails
37 37
         @assets = ActiveSupport::OrderedOptions.new
38 38
         @assets.enabled         = false
39 39
         @assets.paths           = []
40  
-        @assets.precompile      = [ /\w+\.(?!js|css).+/, /application.(css|js)$/ ]
  40
+        @assets.precompile      = [ /^[^.].*(\.(?!js$|css$)[^.]+)+\.*$|^[^.](?:(?!\.).)*\.*$/, /application.(css|js)$/ ]
5
Joshua Peek Collaborator
josh added a note

Gnarly!

Good luck maintaing that regexp, rails core :)

Santiago Pastorino Owner

lol, good luck to you too since you're part of the core :P

Richard Hulse
rhulse added a note

Yeah, use a regex and now you have two problems. ;-)

Perhaps I should split that regexp into two, and put each regexp on its own line with a comment explaining what it does?

Richard Hulse
rhulse added a note

I think putting this in a method on its own would 'minimize' the issue. If maintenance is a concern it could be more verbose in compiling the array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Joshua Peek
Collaborator

Protip: This logic is now apart of rails itself. You can hack it.

https://github.com/rails/rails/blob/master/actionpack/lib/sprockets/assets.rake#L27-31

Maybe allow precompile to accept a Proc or something so you can do File.extname(path) != '.js'.

Alex Yakoubian

@josh See most recent commits.

Alex Yakoubian

@guilleiguaran Thanks, just pushed a new commit with your suggestion.

railties/lib/rails/application/configuration.rb
... ...
@@ -37,7 +37,8 @@ module Rails
37 37
         @assets = ActiveSupport::OrderedOptions.new
38 38
         @assets.enabled         = false
39 39
         @assets.paths           = []
40  
-        @assets.precompile      = [ /\w+\.(?!js|css).+/, /application.(css|js)$/ ]
  40
+        @assets.precompile      = [ Proc.new{ |path| !File.extname(path).in?(['.js', '.css']) && path[0] != '.' },
6
Joshua Peek Collaborator
josh added a note

Curious why != '.' is needed?

To ignore dot files like .gitkeep or something

Joshua Peek Collaborator
josh added a note

Dot files should already be ignored by sprockets.

Oh, didn't know that... should I remove the path[0] != '.'?

Guillermo Iguaran Owner

Yes, please :)

All right, I'll have to change the test too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Alex Yakoubian

Let me know if this is ready for merge and you want me to squash the commits.

Santiago Pastorino
Owner

I think this is ready to go, please rebase and squash the commits

Santiago Pastorino
Owner

@alex3 the pull request cannot be merged, can you rebase it on top of the current master?

Santiago Pastorino spastorino merged commit be24be6 into from
Santiago Pastorino spastorino closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 08, 2011
Alex Yakoubian Fixed assets precompile regex, now accepts Procs 901c02d
This page is out of date. Refresh to see the latest.
2  actionpack/lib/sprockets/assets.rake
@@ -26,6 +26,8 @@ namespace :assets do
26 26
         env.each_logical_path do |logical_path|
27 27
           if path.is_a?(Regexp)
28 28
             next unless path.match(logical_path)
  29
+          elsif path.is_a?(Proc)
  30
+            next unless path.call(logical_path)
29 31
           else
30 32
             next unless File.fnmatch(path.to_s, logical_path)
31 33
           end
3  railties/lib/rails/application/configuration.rb
@@ -37,7 +37,8 @@ def initialize(*)
37 37
         @assets = ActiveSupport::OrderedOptions.new
38 38
         @assets.enabled         = false
39 39
         @assets.paths           = []
40  
-        @assets.precompile      = [ /\w+\.(?!js|css).+/, /application.(css|js)$/ ]
  40
+        @assets.precompile      = [ Proc.new{ |path| !File.extname(path).in?(['.js', '.css']) },
  41
+                                    /application.(css|js)$/ ]
41 42
         @assets.prefix          = "/assets"
42 43
         @assets.version         = ''
43 44
         @assets.debug           = false
28  railties/test/application/assets_test.rb
@@ -64,6 +64,34 @@ def app
64 64
       end
65 65
     end
66 66
 
  67
+    test "precompile application.js and application.css and all other files not ending with .js or .css by default" do
  68
+      app_file "app/assets/javascripts/application.js", "alert();"
  69
+      app_file "app/assets/stylesheets/application.css", "body{}"
  70
+      app_file "app/assets/javascripts/something.min.js", "alert();"
  71
+      app_file "app/assets/stylesheets/something.min.css", "body{}"
  72
+
  73
+      images_should_compile = ["a.png", "happyface.png", "happy_face.png", "happy.face.png",
  74
+                               "happy-face.png", "happy.happy_face.png", "happy_happy.face.png", 
  75
+                               "happy.happy.face.png", "happy", "happy.face", "-happyface",
  76
+                               "-happy.png", "-happy.face.png", "_happyface", "_happy.face.png",
  77
+                               "_happy.png"]
  78
+      images_should_compile.each do |filename|
  79
+        app_file "app/assets/images/#{filename}", "happy"
  80
+      end
  81
+
  82
+      capture(:stdout) do
  83
+        Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
  84
+      end
  85
+
  86
+      images_should_compile.each do |filename|
  87
+        assert File.exists?("#{app_path}/public/assets/#{filename}")
  88
+      end
  89
+      assert File.exists?("#{app_path}/public/assets/application.js")
  90
+      assert File.exists?("#{app_path}/public/assets/application.css")
  91
+      assert !File.exists?("#{app_path}/public/assets/something.min.js")
  92
+      assert !File.exists?("#{app_path}/public/assets/something.min.css")
  93
+    end
  94
+
67 95
     test "asset pipeline should use a Sprockets::Index when config.assets.digest is true" do
68 96
       add_to_config "config.assets.digest = true"
69 97
       add_to_config "config.action_controller.perform_caching = false"
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.