Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base path URL is not configurable #151

Closed
Mcdostone opened this issue Dec 8, 2021 · 2 comments
Closed

base path URL is not configurable #151

Mcdostone opened this issue Dec 8, 2021 · 2 comments

Comments

@Mcdostone
Copy link
Contributor

Is your feature request related to a problem? Please describe.
I have a RoR app running on https://example.com/my-app
When I compile the assets for production, URLs used in my CSS are incorrect: expected /my-app/vite/assets/foo.png, got /vite/assets/foo.png. this issue is normal since my vite configuration is incomplete.

According to the vite documentation, the public base path is the solution. Let's tweak the configuration:

import { defineConfig } from "vite";
import RubyPlugin from "vite-plugin-ruby";

export default defineConfig({
  plugins: [RubyPlugin()],
  base: '/my-app/vite'
});
RAILS_ENV=production bundle exec vite build --force --debug | grep 'my-app'
# no matching lines

It looks likeRubyPlugin() overwrites my custom configuration. Let's find another way.

I read through the vite-ruby doc and found the assetHost property:

{
  "all": {
    "sourceCodeDir": "app/assets",
    "watchAdditionalPaths": [],
    "assetHost": "/my-app"
  }
}
RAILS_ENV=production bundle exec vite build --force --debug | grep 'my-app'
base: '///my-app/vite/',
assetHost: '/my-app',
base: '///my-app/vite/',
BASE_URL: '///my-app/vite/',

We're getting close to what I would expect. However, the two leading slashes annoy me. After some research in your code the slashes are added in config.ts:103

Describe the solution you'd like

  • Be able to change the base property
    Or
  • remove the 2 leading slashes. Developers still have the choice to add them manually if they want to.
{
 "all": {
   "sourceCodeDir": "app/assets",
   "watchAdditionalPaths": [],
   "assetHost": "/my-app",                                           // or 
   "assetHost": "//my-app",      ,                                   // or
   "assetHost": "https://example.com/my-app"    
 }
}
@ElMassimo
Copy link
Owner

ElMassimo commented Dec 8, 2021

Hi Yann!

assetHost is intended for an external CDN host.

Hosting a Rails app in a nested path is unusual, but I think it makes sense to add a separate base setting in config/vite.json to support this use case.

{
  "all": {
     "watchAdditionalPaths": [],
  },
  "production": {
    "base": "my-app"
  }
}

Let me know if you would be willing to rework #152 to add the base setting instead of modifying the assetHost behavior.

const base = `${assetHostWithProtocol || config.base}/${config.publicOutputDir}/`

It would also make sense to allow configuring this setting using environment variables, like for other settings (VITE_RUBY_BASE=my_app). All it takes to implement that, is adding a default value for base:

  "base": "",

@ElMassimo
Copy link
Owner

ElMassimo commented Dec 9, 2021

Released vite-plugin-ruby@3.0.4 and vite_ruby@3.0.3 which add support for the base setting.

Run bin/vite upgrade to bump both dependencies.

Thanks! 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants