Looks like dynamic imports will finally work with Mix:
https://laravel-news.com/using-dynamic-imports-with-laravel-mix
I have followed this post exactly (including installing @babel/plugin-syntax-dynamic-import with npm install --save-dev, a step not mentioned in the post).
In my .babelrc I have:
{
"plugins": [
"@babel/plugin-syntax-dynamic-import"
]
}
In my vue-router file I try to dynamically import a component:
const Login = () => import(/* webpackChunkName: "LoginComponent" */ '../views/Login.vue');
But always get an error:
ERROR in ./resources/assets/js/router/index.js 7:9
Module parse failed: Unexpected token (7:9)
File was processed with these loaders:
* ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
|
| var Login = function Login() {
> return import(
| /* webpackChunkName: "LoginComponent" */
| '../views/Login.vue');
@ ./resources/assets/js/app.js 26:0-30 146:13-19 208:10-16 218:18-24 220:0-6
@ multi ./resources/assets/js/app.js ./resources/assets/sass/app.scss
I've also tried importing directly inside the routes, but get the same error:
{
path: '/login',
name: 'login',
component: () => import(/* webpackChunkName: "login" */ '../views/Login.vue'),
},
Any ideas?
Thanks :)
EDIT:
I've resolved the initial problem- seems to have been due to my npm dependencies. Just need to make sure laravel-mix is requiring latest versions of webpack and acorn. I found help in this thread: https://github.com/webpack/webpack/issues/8656#issuecomment-456010969
However, there's still an issue with laravel-mix not outputting CSS when using extract(), so I still cannot go ahead with dynamic imports in my project:
https://github.com/JeffreyWay/laravel-mix/issues/1856