UNPKG

vite-intlayer

Version:

A Vite plugin for seamless internationalization (i18n), providing locale detection, redirection, and environment-based configuration

90 lines (88 loc) 2.67 kB
import { intlayerPrune } from "./intlayerPrunePlugin.mjs"; import { resolve } from "node:path"; import { prepareIntlayer, watch } from "@intlayer/chokidar"; import { getAlias, getConfiguration } from "@intlayer/config"; //#region src/intlayerPlugin.ts /** * @deprecated Rename to intlayer instead * * A Vite plugin that integrates Intlayer configuration into the build process * * ```ts * // Example usage of the plugin in a Vite configuration * export default defineConfig({ * plugins: [ intlayer() ], * }); * ``` * */ const intlayerPlugin = (configOptions) => { const intlayerConfig = getConfiguration(configOptions); const alias = getAlias({ configuration: intlayerConfig, formatter: (value) => resolve(value) }); const aliasPackages = Object.keys(alias); const plugins = [{ name: "vite-intlayer-plugin", config: async (config, env) => { const { mode } = intlayerConfig.build; const isDevCommand = env.command === "serve" && env.mode === "development"; const isBuildCommand = env.command === "build"; if (isDevCommand || isBuildCommand || mode === "auto") await prepareIntlayer(intlayerConfig, { clean: isBuildCommand, cacheTimeoutMs: isBuildCommand ? 1e3 * 30 : 1e3 * 60 * 60 }); config.resolve = { ...config.resolve, alias: { ...config.resolve?.alias, ...alias } }; config.optimizeDeps = { ...config.optimizeDeps, exclude: [...config.optimizeDeps?.exclude ?? [], ...aliasPackages] }; if (config.ssr?.noExternal !== true) { const currentNoExternal = Array.isArray(config.ssr?.noExternal) ? config.ssr.noExternal : config.ssr?.noExternal ? [config.ssr.noExternal] : []; config.ssr = { ...config.ssr, noExternal: [...currentNoExternal, /(^@intlayer\/|intlayer$)/] }; } return config; }, configureServer: async (_server) => { if (intlayerConfig.content.watch) watch({ configuration: intlayerConfig }); } }]; plugins.push(intlayerPrune(intlayerConfig)); return plugins; }; /** * A Vite plugin that integrates Intlayer configuration into the build process * * ```ts * // Example usage of the plugin in a Vite configuration * export default defineConfig({ * plugins: [ intlayer() ], * }); * ``` */ const intlayer = intlayerPlugin; /** * @deprecated Rename to intlayer instead * * A Vite plugin that integrates Intlayer configuration into the build process * * ```ts * // Example usage of the plugin in a Vite configuration * export default defineConfig({ * plugins: [ intlayer() ], * }); * ``` */ const intLayerPlugin = intlayerPlugin; //#endregion export { intLayerPlugin, intlayer, intlayerPlugin }; //# sourceMappingURL=intlayerPlugin.mjs.map