Skip to content

Commit

Permalink
fix: allow providing an empty public output dir (close #161) (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo authored Dec 24, 2021
1 parent cd3fd55 commit ef48c9b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
12 changes: 12 additions & 0 deletions test/dev_server_proxy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ def test_without_dev_server_running
assert_not_forwarded
end

def test_empty_public_output_dir
refresh_config(public_output_dir: '')
get_with_dev_server_running '/'
assert_not_forwarded

get_with_dev_server_running '/entrypoints/application.js'
assert_forwarded to: '/entrypoints/application.js'

get_with_dev_server_running '/entrypoints/sassy.scss.css'
assert_forwarded to: '/entrypoints/sassy.scss'
end

private

def get_with_dev_server_running(*args)
Expand Down
3 changes: 3 additions & 0 deletions test/test_app/app/frontend/entrypoints/sassy.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.colored {
color: red;
}
2 changes: 1 addition & 1 deletion vite-plugin-ruby/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function coerceConfigurationValues (config: ResolvedConfig, projectRoot: string,
// Add the asset host to enable usage of a CDN.
const assetHost = config.assetHost || ''
const assetHostWithProtocol = assetHost && !assetHost.startsWith('http') ? `//${assetHost}` : assetHost
const base = `${assetHostWithProtocol || config.base}/${config.publicOutputDir}/`
const base = slash(join(assetHostWithProtocol || config.base || '/', config.publicOutputDir, '/'))

const entrypoints = resolveEntrypointFiles(projectRoot, root, config)
return { ...config, root, outDir, base, entrypoints }
Expand Down
10 changes: 7 additions & 3 deletions vite_ruby/lib/vite_ruby/dev_server_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ def perform_request(env)

def rewrite_uri_for_vite(env)
uri = env.fetch('REQUEST_URI') { [env['PATH_INFO'], env['QUERY_STRING']].reject { |str| str.to_s.strip.empty? }.join('?') }
env['PATH_INFO'], env['QUERY_STRING'] = (env['REQUEST_URI'] = normalize_uri(uri)).split('?')
end

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.
env['PATH_INFO'], env['QUERY_STRING'] = (env['REQUEST_URI'] = uri).split('?')
end

def forward_to_vite_dev_server(env)
Expand All @@ -49,7 +53,7 @@ def forward_to_vite_dev_server(env)
end

def vite_should_handle?(env)
path = env['PATH_INFO']
path = normalize_uri(env['PATH_INFO'])
return true if path.start_with?(vite_asset_url_prefix) # Vite asset
return true if path.start_with?(VITE_DEPENDENCY_PREFIX) # Packages and imports
return true if file_in_vite_root?(path) # Fallback if Vite can serve the file
Expand All @@ -61,6 +65,6 @@ def file_in_vite_root?(path)
end

def vite_asset_url_prefix
@vite_asset_url_prefix ||= "/#{ config.public_output_dir }/"
@vite_asset_url_prefix ||= config.public_output_dir.empty? ? "\0" : "/#{ config.public_output_dir }/"
end
end

0 comments on commit ef48c9b

Please sign in to comment.