Skip to content

Commit

Permalink
fix: avoid removing double extension for non-preprocessor assets (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo authored Nov 13, 2022
1 parent 37d21d6 commit 2024f62
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ jobs:
"3.1",
]
gemfile: [
"gemfiles/Gemfile-rails.6.0.x",
"gemfiles/Gemfile-rails.6.1.x",
"gemfiles/Gemfile-rails.7.0.x"
"Gemfile-rails.6.0.x",
"Gemfile-rails.6.1.x",
"Gemfile-rails.7.0.x"
]
experimental: [false]
include:
- ruby: "3.1"
os: ubuntu-latest
gemfile: gemfiles/Gemfile-rails-edge
gemfile: Gemfile-rails-edge
experimental: true

env: # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}

steps:
- uses: actions/checkout@v3

Expand Down
10 changes: 10 additions & 0 deletions test/dev_server_proxy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ def test_scss_with_extra_css
assert_forwarded to: '/vite-production/entrypoints/sassy.scss'
end

def test_stylus_with_extra_css
get_with_dev_server_running '/vite-production/entrypoints/sassy.stylus.css'
assert_forwarded to: '/vite-production/entrypoints/sassy.stylus'
end

def test_min_css
get_with_dev_server_running '/vite-production/colored.min.css'
assert_forwarded to: '/vite-production/colored.min.css'
Expand All @@ -90,6 +95,11 @@ def test_module_css
assert_forwarded to: '/vite-production/colored.module.css'
end

def test_random_extension_css
get_with_dev_server_running '/vite-production/colored.bubble.css'
assert_forwarded to: '/vite-production/colored.bubble.css'
end

def test_without_dev_server_running
get '/vite-production/application.js'
assert_not_forwarded
Expand Down
6 changes: 5 additions & 1 deletion test/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ def test_vite_stylesheet_tag
with_dev_server_running {
assert_similar link(href: '/vite-dev/entrypoints/app.css'), vite_stylesheet_tag('app')
assert_equal vite_stylesheet_tag('app'), vite_stylesheet_tag('app.css')
assert_similar link(href: '/vite-dev/entrypoints/sassy.scss.css'), vite_stylesheet_tag('sassy.scss')
if Rails::VERSION::MAJOR >= 7
assert_similar link(href: '/vite-dev/entrypoints/sassy.scss'), vite_stylesheet_tag('sassy.scss')
else
assert_similar link(href: '/vite-dev/entrypoints/sassy.scss.css'), vite_stylesheet_tag('sassy.scss')
end
}
end

Expand Down
7 changes: 7 additions & 0 deletions vite_rails/lib/vite_rails/tag_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ def vite_javascript_tag(*names,
entries = vite_manifest.resolve_entries(*names, type: asset_type)
tags = javascript_include_tag(*entries.fetch(:scripts), crossorigin: crossorigin, type: type, extname: false, **options)
tags << vite_preload_tag(*entries.fetch(:imports), crossorigin: crossorigin, **options) unless skip_preload_tags

options[:extname] = false if Rails::VERSION::MAJOR >= 7

tags << stylesheet_link_tag(*entries.fetch(:stylesheets), media: media, **options) unless skip_style_tags

tags
end

Expand All @@ -54,6 +58,9 @@ def vite_typescript_tag(*names, **options)
# Public: Renders a <link> tag for the specified Vite entrypoints.
def vite_stylesheet_tag(*names, **options)
style_paths = names.map { |name| vite_asset_path(name, type: :stylesheet) }

options[:extname] = false if Rails::VERSION::MAJOR >= 7

stylesheet_link_tag(*style_paths, **options)
end

Expand Down
2 changes: 1 addition & 1 deletion vite_ruby/lib/vite_ruby/dev_server_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def normalize_uri(uri)
uri
.sub(HOST_WITH_PORT_REGEX, '/') # Hanami adds the host and port.
.sub('.ts.js', '.ts') # Hanami's javascript helper always adds the extension.
.sub(/(\.(?!min|module)\w+)\.css$/, '\1') # Rails' stylesheet_link_tag always adds the extension.
.sub(/\.(sass|scss|styl|stylus|less|pcss|postcss)\.css$/, '.\1') # Rails' stylesheet_link_tag always adds the extension.
end

def forward_to_vite_dev_server(env)
Expand Down

0 comments on commit 2024f62

Please sign in to comment.