-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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 to specify resolve.alias or virtual module based on entry #16685
Comments
I guess you can configure it like: import { defineConfig } from 'vite'
export default defineConfig({
resolve: {
'@/router': IS_ADMIN ? 'router_admin' : 'router_user',
'@/navbar': IS_ADMIN ? 'navbar_admin' : 'navbar_user',
},
build: {
rollupOptions: {
input: IS_ADMIN ? 'admin' : 'user',
},
},
}) |
I have a similar setup (the IS_ADMIN is on the whole resolve object):
I just asked for a leaner way because if handled by vite it could split my bundle in three automatically without thinking about it: common (main + router + footer), bundle_admin (router_admin + navbar_admin) and bundle_user (router_user + navbar_user) |
Ah, I missed that sentence.
Do you mean you want to share chunks between admin and user? That won't be possible with your suggested solution. graph TD;
entry_admin --> page_admin.html
page_admin.html --> main.jsx
main.jsx --> router_admin.jsx
router_admin.jsx --> layout.jsx
layout.jsx --> navbar_admin.jsx
layout.jsx --> footer.jsx
entry_user --> page_user.html
page_user.html --> main.jsx
main.jsx --> router_user.jsx
router_user.jsx --> layout.jsx
layout.jsx --> navbar_user.jsx
<!-- add in page_admin.html -->
<script>window.type = 'admin'</script> <!-- add in page_user.html -->
<script>window.type = 'admin'</script> // main.jsx
const loadRouter = () => {
switch (window.type) {
case 'user': return import('./router_admin.jsx')
case 'admin': return import('./router_admin.jsx')
}
throw new Error('Unknown type')
}
;(async () => {
const router = await loadRouter()
// do something
})() |
(your did copy paste typo) Thanks for the example it can improve my situations. So I have to write this code snippet for each selection ? so for my graph it will result in 5 chunks ? Maybe I could do a super
I just thought it was optimal to have three chunks. If there exist a solution with two chunk I can take this. If I have two entry point rollup is called only one time with multiple input ? because if called twice we could just change the given file when launching it. |
Yes, unless you could make a plugin that transforms your code. Or as you said, it'll work if you organize the dependency graph using dependency injection or something. graph TD;
entry_admin --> page_admin.html
page_admin.html --> main.jsx
main.jsx -.-> admin.jsx
admin.jsx --> router_admin.jsx
admin.jsx --> navbar_admin.jsx
main.jsx --> layout.jsx
layout.jsx --> footer.jsx
entry_user --> page_user.html
page_user.html --> main.jsx
main.jsx -.-> user.jsx
user.jsx --> router_user.jsx
user.jsx --> navbar_user.jsx
main.jsx --> layout.jsx
layout.jsx --> footer.jsx
(dot arrows are dynamic imports)
Rollup will be called once with multiple inputs. If you want to call twice, you'll need to call Vite twice (e.g. |
Ok so I might first open an issue in upstream rollup (and esbuild ?) before it can be done in vite. Native build tool provide this functionality by delaying object selection to link and optimize at link time. https://dune.readthedocs.io/en/stable/variants.html |
A single rollup build creates a single module graph. What you are trying to build has two module graphs (one for user, one for admin). So you'll need to build twice. IIUC you want to create a single module graph from two module graphs. That's not supported by rollup and requires changes on rollup side. I'll close this issue for now. |
Description
I have:
In this setup it create many file but only two are really differents: navbar and router. I saw alias or virtual module could help me have:
I replace resolve.alias based on an env var (but it cause to have two build command).
Suggested solution
Allow to put in entry section
resolve.alias
and other options that can be customized depending on entry.Alternative
Pass top-level entry/dependencies path in custom resolver so we can implement this function user land.
create an overlay mechanism like nix: https://nixos.org/manual/nixpkgs/stable/#sec-overlays-alternatives
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: