vite-intlayer
Version:
A Vite plugin for seamless internationalization (i18n), providing locale detection, redirection, and environment-based configuration
34 lines (33 loc) • 1.02 kB
JavaScript
//#region src/intlayerVueAsyncPlugin.ts
const intlayerVueAsyncPlugin = (configuration, filesList) => {
const { importMode, optimize } = configuration.build;
return {
name: "vite-intlayer-simple-transform",
enforce: "pre",
apply: (_config, env) => {
const isBuild = env.command === "build";
return (optimize === void 0 && isBuild || optimize === true) && (importMode === "dynamic" || importMode === "live");
},
transform(code, id) {
if (!id.endsWith(".vue")) return null;
/**
* Transform file as
* .../HelloWorld.vue?vue&type=script&setup=true&lang.ts
* Into
* .../HelloWorld.vue
*
* Prevention for virtual file
*/
const filename = id.split("?", 1)[0];
if (!filesList.includes(filename)) return null;
if (!code.includes("useIntlayer")) return null;
return {
code: code.replace(/(\s+|=\s*)useIntlayer\s*\(/g, "$1await useIntlayer("),
map: null
};
}
};
};
//#endregion
export { intlayerVueAsyncPlugin };
//# sourceMappingURL=intlayerVueAsyncPlugin.mjs.map