Answer the question
In order to leave comments, you need to log in
Why doesn't this code create lazy-loading chunks?
There is the following code:
// Тут беру все файлы из папки components
const files = require.context('./components', true, /\.vue$/i);
files.keys().map(key => {
let componentName = key.split('/').pop().split('.')[0];
// пытаюсь создать динамическую загрузку для этих компонентов
Vue.component(componentName, () => import(
/* webpackChunkName: "base-component-" */
`./components/${componentName}.vue`
))
});
module.exports = {
output: {
chunkFilename: `dist/js/chunks/[name].js`,
},
};
Answer the question
In order to leave comments, you need to log in
I 've heard that require.context can take some fourth parameter called mode, whose default value is NOT 'lazy' at all.
Well, you have no reason for webpack to create a chunk - the code sticks together.
As I understand it, we need conditions or events that separate the code
const components = [
'cargovoyage.detail',
'cargovoyage.list',
];
document.addEventListener('DOMContentLoaded', () => {
components.forEach((component) => {
const componentClass = component.replace(/\./g, '-');
if (document.querySelector(`.${componentClass}`)) {
import(/* webpackChunkName: "[request]" */ `components/${component}`);
}
});
import(/* webpackChunkName: "vessel-form" */ './js/vessel/form');
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question