-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Allow prefixing the generated path #2254
Conversation
This will allow working with vite with a backend. It's not possible to inject this with the config since the full URL is transformed by resolveBaseUrl() ```js { base: 'http://localhost:3000/assets/', } ``` With this fix we can use ``` VITE_BASE=http://localhost:3000 yarn run dev ``` And the generated paths will contain the full path (so vite can be used with any server) and fix #2196
While this fix would work, I think the fact that this feature is meant to ease the integration with back-ends should make us consider to use a configuration option instead, so plugins can easily update it. Alternatively, a hook like proposed in #1675. |
@innocenzi The PR you linked is for build time right ? Cause we need this for the devServer too, I could add a new configuration option but I have no idea how to name it :D |
Yeah it's for the build-time URL. But that was just an idea, it's probably overkill. It makes sense for the build options to be highly flexible, but for the development URLs, I think a configuration option is the best bet. I was going to suggest something like |
Why do we need a separate
and
|
@jonaskuske Well, since the |
@SirDavidLudwig you could just use the environment variable in your Vite Config¹ and pass that as I think having two separate base options that are defined in different places and then stitched together is quite confusing 😅 ¹ note that you currently need to call |
@jonaskuske Ah, can't believe I didn't think of that; that's actually a much better solution! (my brain ist still stuck in the realm of json configs I think maybe idk :p) |
I thought the way it works currently was intended, but if I'm wrong, this can be a better fix indeed.
Usually I would disagree since it'd kind of kill the point of using an environment variable, but a lot of Vite's configuration is environment-specific (like I think a configuration like this would be ideal: export default defineConfig({
base: process.env.ASSET_URL
}) Otherwise, an additional top-level option, or an environment variable that would not be part of the config at all. EDIT: Ah, I responded too late. Woops |
@jonaskuske Do you know what is this for? Is it useful to strip external URLs' hosts in development mode? I think just that would be enough to add support like you suggested. |
The solution in this PR is definitely not enough to fix this issue as other plugins will still potentially suffer (for example, this doesn't work with Vue files). After some experimentation, I've managed to come up with a different solution that should be plugin-independent, and so far has worked with Vue and CSS. While I'm sure my solution can be improved upon, I thought I'd explain my process to see if it would at least give a better idea at what needs to really be changed to make this work. This fix requires two small modifications across two different functions.
if (!isExternalUrl(base)) {
// ensure leading slash
if (!base.startsWith('/')) {
logger.warn(
chalk.yellow.bold(`(!) "base" option should start with a slash.`)
)
base = '/' + base
}
}
if (isExternalUrl(hmrBase)) {
port += "/";
} else {
if (options.path) {
hmrBase = path__default.posix.join(hmrBase, options.path);
}
if (hmrBase !== '/') {
port = path__default.posix.normalize(`${port}${hmrBase}`);
}
} This solution yields only a single minor issue that I can see which is the displayed local and network addresses the server is running on as it doubles up the URL since it's merging the server's URL with the external URL base. EDIT: |
@SirDavidLudwig I couldn't access the files directly because of an: So I commented-out these lines: Getting uglier and uglier :( |
I don't think it's a good pattern to have vite replying solely on a magical env but not config. Closing this PR for now, and feel free to bring up other solutions in another thread. Thanks. |
This will allow working with vite with any backend.
It's not possible to inject a full path with the config since the full URL is transformed by
resolveBaseUrl()
So every path generated by
fileToDevUrl()
will be/assets/...
instead ofhttp://localhost:3000/assets/
.With this fix we can use
And the generated paths will contain the full path (so vite can be used with any server) and fix #2196