You’re dealing with a classic SPA bundling issue—by default, if you import everything into one main Vue entry point, the bundle grows quickly. To solve this, you can separate your Vue applications by section (multiple entry points), and/or leverage code splitting via dynamic imports and lazy-loaded routes. Here’s how to approach both:
1. Multiple Entry Points (Multiple Vue Apps)
Create separate .js files for each section. For example, app-admin.js, app-user.js, and app-dashboard.js, each bootstrapping its own Vue instance. This is ideal if your sections are truly independent.
Structure Example:
resources/
js/
admin-app.js
user-app.js
dashboard-app.js
admin-app.js
import { createApp } from 'vue';
import AdminComponent from './components/AdminComponent.vue';
import UIStuff from 'your-ui-library/admin';
const app = createApp(AdminComponent);
app.use(UIStuff);
app.mount('#admin-app');
user-app.js
import { createApp } from 'vue';
import UserComponent from './components/UserComponent.vue';
import UIStuff from 'your-ui-library/user';
const app = createApp(UserComponent);
app.use(UIStuff);
app.mount('#user-app');
In your HTML:
<!-- Only load what's needed for each section -->
<div id="admin-app"></div>
<script src="/js/admin-app.js"></script>
<div id="user-app"></div>
<script src="/js/user-app.js"></script>
In Laravel Mix or Vite:
// Laravel Mix
mix.js('resources/js/admin-app.js', 'public/js')
.js('resources/js/user-app.js', 'public/js');
or
// vite.config.js (Vite)
export default {
build: {
rollupOptions: {
input: {
admin: 'resources/js/admin-app.js',
user: 'resources/js/user-app.js',
}
}
}
}
2. Code Splitting with Dynamic Imports (Recommended for Router-based SPAs)
If you use Vue Router, lazy-load each section with dynamic imports. This way, only the JavaScript for the visited route is downloaded.
router.js
const routes = [
{
path: '/admin',
component: () => import('./views/Admin.vue'), // Lazy!
},
{
path: '/user',
component: () => import('./views/User.vue'), // Lazy!
},
];
If you import UI library components only inside those views, users will only download what’s needed for that section.
Summary
- Create a separate Vue entry (
.js) file for each main section. Include only the components/UI libraries needed for that area. - Use build tools (Mix, Vite, Webpack) to generate individual JS files.
- On each page, only load the relevant JS bundle.
You’ll get much smaller assets, tailored to each area of your site, and no one will download unnecessary code. Let me know if you need more detailed setup for your build tool!