@@ -73,10 +73,8 @@ def digest(resolver:)
7373 # and test to ensure the map caches are reset when javascript files are changed.
7474 def cache_sweeper ( watches : nil )
7575 if watches
76- @cache_sweeper =
77- Rails . application . config . file_watcher . new ( [ ] , Array ( watches ) . collect { |dir | [ dir . to_s , "js" ] } . to_h ) do
78- clear_cache
79- end
76+ watches = Array ( watches ) . collect { |dir | [ dir . to_s , accepted_extensions ] } . to_h
77+ @cache_sweeper = Rails . application . config . file_watcher . new ( [ ] , watches ) { clear_cache }
8078 else
8179 @cache_sweeper
8280 end
@@ -137,7 +135,7 @@ def expanded_packages_and_directories
137135 def expand_directories_into ( paths )
138136 @directories . values . each do |mapping |
139137 if ( absolute_path = absolute_root_of ( mapping . dir ) ) . exist?
140- find_javascript_files_in_tree ( absolute_path ) . each do |filename |
138+ find_accepted_files_in_tree ( absolute_path ) . each do |filename |
141139 module_filename = filename . relative_path_from ( absolute_path )
142140 module_name = module_name_from ( module_filename , mapping )
143141 module_path = module_path_from ( module_filename , mapping )
@@ -149,18 +147,26 @@ def expand_directories_into(paths)
149147 end
150148
151149 def module_name_from ( filename , mapping )
152- [ mapping . under , filename . to_s . remove ( filename . extname ) . remove ( /\/ ?index$/ ) . presence ] . compact . join ( "/" )
150+ [ mapping . under , remove_accepted_extensions ( filename ) . remove ( /\/ ?index$/ ) . presence ] . compact . join ( "/" )
153151 end
154152
155153 def module_path_from ( filename , mapping )
156- [ mapping . path || mapping . under , filename . to_s ] . compact . join ( "/" )
154+ [ mapping . path || mapping . under , " #{ remove_accepted_extensions ( filename ) } .js" ] . compact . join ( "/" )
157155 end
158156
159- def find_javascript_files_in_tree ( path )
160- Dir [ path . join ( "**/*.js{,m} " ) ] . collect { | file | Pathname . new ( file ) } . select ( &:file? )
157+ def find_accepted_files_in_tree ( path )
158+ Dir [ path . join ( "**/*.{ #{ accepted_extensions . join ( ',' ) } } " ) ] . map ( & Pathname . method ( :new ) ) . select ( &:file? )
161159 end
162160
163161 def absolute_root_of ( path )
164162 ( pathname = Pathname . new ( path ) ) . absolute? ? pathname : Rails . root . join ( path )
165163 end
164+
165+ def accepted_extensions
166+ Rails . application . config . importmap . accept
167+ end
168+
169+ def remove_accepted_extensions ( filename )
170+ filename . to_s . remove ( /\. (#{ accepted_extensions . map ( &Regexp . method ( :escape ) ) . join ( '|' ) } )\z / )
171+ end
166172end
0 commit comments