As this has been bugging me for a while and I've now just solved it here is my lazy loading solution (using vue and typescript). Went from 4.5 - 7 mb to 2.6 mb. I lazy load and don't prefetch the languages.
vue.config.js
...
chainWebpack: config => {
// remove the prefetch plugin
// config.plugins.delete("prefetch");
// or:
// modify its options:
config.plugin("prefetch").tap(options => {
options[0].fileBlacklist = options[0].fileBlacklist || [];
options[0].fileBlacklist.push(/lang(.)+?\.js$/);
options[0].fileBlacklist.push(/lang(.)+?\.js.map$/);
return options;
});
},
...
update
if using vue cli pages this is a more dynamic prefetch selector
vue.config.js
...
chainWebpack: config => {
// remove the prefetch plugin
// config.plugins.delete("prefetch");
// or:
// modify its options:
if (config && config.plugins && config.plugins.store) {
const pluginKeys = Array.from(config.plugins.store.keys());
const prefetchKeys = pluginKeys.filter(x => x.indexOf("prefetch") != -1);
prefetchKeys.forEach(key => {
if (config.plugins.has(key)) {
config.plugin(key).tap(options => {
options[0].fileBlacklist = options[0].fileBlacklist || [];
options[0].fileBlacklist.push(/lang(.)+?\.js$/);
options[0].fileBlacklist.push(/lang(.)+?\.js.map$/);
return options;
});
}
}
}
},
...
i18n.ts
import Vue from "vue";
import VueI18n from "vue-i18n";
import { LanguageList } from "@/types/languages";
import messages from "@/locales/en_US.json";
Vue.use(VueI18n);
export const i18n = new VueI18n({
locale:
process.env.VUE_APP_I18N_LOCALE ||
"en_US",
silentTranslationWarn: true,
fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || "en_US",
messages: { en_US: messages }
});
const loadedLanguages = ["en_US"]; // our default language that is preloaded
export function setI18nLanguage(lang: string, load = true) {
if (load) {
/* eslint-disable @typescript-eslint/no-use-before-define */
loadLanguageAsync(lang).then(() => {
return lang;
});
} else {
i18n.locale = lang;
// axios.defaults.headers.common['Accept-Language'] = lang
(document.querySelector("html") as HTMLElement).setAttribute("lang", lang);
return lang;
}
}
export function loadLanguageAsync(lang: string) {
if (
i18n.locale !== lang ||
(!loadedLanguages.includes(lang) && LanguageList.find(l => l.code == lang))
) {
if (!loadedLanguages.includes(lang)) {
return import(
/* webpackChunkName: "lang-[request]" */ `@/locales/${lang}`
).then(msgs => {
debugger;
console.log(msgs.default);
i18n.setLocaleMessage(lang, msgs);
loadedLanguages.push(lang);
return setI18nLanguage(lang);
});
}
return Promise.resolve(setI18nLanguage(lang, false));
}
return Promise.resolve(setI18nLanguage(lang, false));
}
App.vue
<script>
import {setI18nLanguage } from "@/i18n";
...
beforeMount() {
setI18nLanguage(i18n.locale);
}
...
</script>
main.ts
...
import { i18n } from "@/i18n";
...
new Vue({
router,
store,
i18n,
render: h => h(App)
}).$mount("#app");
languages.ts
export interface LanguageI {
code: string;
language: string;
english: string;
}
export const LanguageList: LanguageI[] = [
{ code: "en_US", language: "English", english: "English" },
//...
];