vite-intlayer
Version:
A Vite plugin for seamless internationalization (i18n), providing locale detection, redirection, and environment-based configuration
1 lines • 2.82 kB
Source Map (JSON)
{"version":3,"file":"intlayerVueAsyncPlugin.mjs","names":[],"sources":["../../src/intlayerVueAsyncPlugin.ts"],"sourcesContent":["import type { IntlayerConfig } from '@intlayer/types';\nimport type { PluginOption } from 'vite';\n\nexport const intlayerVueAsyncPlugin = (\n configuration: IntlayerConfig,\n filesList: string[]\n): PluginOption => {\n const { importMode, optimize } = configuration.build;\n\n return {\n /**\n * On vue, we pre-insert the 'await' to the useIntlayer call\n * It will trigger the transformation of the async call by the vue compiler\n *\n * Then the second plugin will make the second transformation to replace the useIntlayer call by the useDictionaryDynamic call\n */\n name: 'vite-intlayer-simple-transform',\n enforce: 'pre', // Run before Vue so Vue sees the 'await'\n apply: (_config, env) => {\n // Only apply babel plugin if optimize is enabled\n\n const isBuild = env.command === 'build';\n const isEnabled =\n (optimize === undefined && isBuild) || optimize === true;\n const isAsync = importMode === 'dynamic' || importMode === 'live';\n\n return isEnabled && isAsync;\n },\n\n transform(code, id) {\n // Only process .vue files\n // The await injection is only needed for Vue to trigger async component compilation\n if (!id.endsWith('.vue')) return null;\n\n /**\n * Transform file as\n * .../HelloWorld.vue?vue&type=script&setup=true&lang.ts\n * Into\n * .../HelloWorld.vue\n *\n * Prevention for virtual file\n */\n const filename = id.split('?', 1)[0];\n\n if (!filesList.includes(filename)) return null;\n\n // Check if the file actually uses the composable to avoid unnecessary work\n if (!code.includes('useIntlayer')) return null;\n\n // B. Add 'await' to the function call\n // Matches: useIntlayer(args) -> await useIntlayer(args)\n // Note: Since we aliased the import above, 'useIntlayer' now refers to 'useDictionaryAsync'\n const transformedCode = code.replace(\n /(\\s+|=\\s*)useIntlayer\\s*\\(/g,\n '$1await useIntlayer('\n );\n\n return {\n code: transformedCode,\n map: null, // Simple string replace doesn't strictly need a sourcemap for this case\n };\n },\n };\n};\n"],"mappings":";AAGA,MAAa,0BACX,eACA,cACiB;CACjB,MAAM,EAAE,YAAY,aAAa,cAAc;AAE/C,QAAO;EAOL,MAAM;EACN,SAAS;EACT,QAAQ,SAAS,QAAQ;GAGvB,MAAM,UAAU,IAAI,YAAY;AAKhC,WAHG,aAAa,UAAa,WAAY,aAAa,UACtC,eAAe,aAAa,eAAe;;EAK7D,UAAU,MAAM,IAAI;AAGlB,OAAI,CAAC,GAAG,SAAS,OAAO,CAAE,QAAO;;;;;;;;;GAUjC,MAAM,WAAW,GAAG,MAAM,KAAK,EAAE,CAAC;AAElC,OAAI,CAAC,UAAU,SAAS,SAAS,CAAE,QAAO;AAG1C,OAAI,CAAC,KAAK,SAAS,cAAc,CAAE,QAAO;AAU1C,UAAO;IACL,MANsB,KAAK,QAC3B,+BACA,uBACD;IAIC,KAAK;IACN;;EAEJ"}