I am interested in dynamically importing a module only when it is needed.
To achieve this, I have created a small mixin:
import {extend} from "vee-validate";
export const rules = {
methods: {
addRule (name) {
let requiredRule = null;
let emailRule = null;
switch (name) {
case 'required' :
if (!requiredRule) {
requiredRule = require("vee-validate/dist/rules/required");
extend ('required', {
...requiredRule,
message: 'This field is required'
});
}
break;
case 'email' :
if (!emailRule) {
emailRule = require("vee-validate/dist/rules/email");
extend ('email', {
...emailRule,
message: 'Invalid email address'
});
}
break;
}
}
}
};
My question now is: How can I ensure that the email
and required
modules are imported only when they are required? For example, if only the required
rule is added, there is no need to import the email
rule.