@@ -73,10 +73,8 @@ def digest(resolver:)
73
73
# and test to ensure the map caches are reset when javascript files are changed.
74
74
def cache_sweeper ( watches : nil )
75
75
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 }
80
78
else
81
79
@cache_sweeper
82
80
end
@@ -137,7 +135,7 @@ def expanded_packages_and_directories
137
135
def expand_directories_into ( paths )
138
136
@directories . values . each do |mapping |
139
137
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 |
141
139
module_filename = filename . relative_path_from ( absolute_path )
142
140
module_name = module_name_from ( module_filename , mapping )
143
141
module_path = module_path_from ( module_filename , mapping )
@@ -149,18 +147,26 @@ def expand_directories_into(paths)
149
147
end
150
148
151
149
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 ( "/" )
153
151
end
154
152
155
153
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 ( "/" )
157
155
end
158
156
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? )
161
159
end
162
160
163
161
def absolute_root_of ( path )
164
162
( pathname = Pathname . new ( path ) ) . absolute? ? pathname : Rails . root . join ( path )
165
163
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
166
172
end
0 commit comments