UNPKG

vite-intlayer

Version:

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

116 lines 4.37 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var intlayerPrunePlugin_exports = {}; __export(intlayerPrunePlugin_exports, { IntlayerPrunePlugin: () => IntlayerPrunePlugin }); module.exports = __toCommonJS(intlayerPrunePlugin_exports); var import_babel = require("@intlayer/babel"); var import_config = require("@intlayer/config"); var import_fast_glob = __toESM(require("fast-glob")); var import_path = require("path"); const IntlayerPrunePlugin = (intlayerConfig) => { const { optimize, importMode, traversePattern } = intlayerConfig.build; const { dictionariesDir, dynamicDictionariesDir, mainDir, baseDir } = intlayerConfig.content; const filesListPattern = import_fast_glob.default.sync(traversePattern, { cwd: baseDir }).map((file) => (0, import_path.join)(baseDir, file)); return { name: "vite-intlayer-babel-transform", enforce: "post", // Run after other transformations as vue transform(code, id) { const dictionariesEntryPath = (0, import_path.join)(mainDir, "dictionaries.mjs"); const dynamicDictionariesEntryPath = (0, import_path.join)( mainDir, "dynamic_dictionaries.mjs" ); const filesList = [ ...filesListPattern, dictionariesEntryPath // should add dictionariesEntryPath to replace it by a empty object if import made dynamic ]; const filename = id.split("?", 1)[0]; if (!filesList.includes(filename)) return null; if (!optimize) return null; try { const babel = (0, import_config.ESMxCJSRequire)("@babel/core"); const result = babel.transformSync(code, { filename, plugins: [ [ import_babel.intlayerBabelPlugin, { dictionariesDir, dictionariesEntryPath, dynamicDictionariesDir, dynamicDictionariesEntryPath, importMode, filesList, replaceDictionaryEntry: false } ] ], parserOpts: { sourceType: "module", allowImportExportEverywhere: true, plugins: [ "typescript", "jsx", "decorators-legacy", "classProperties", "objectRestSpread", "asyncGenerators", "functionBind", "exportDefaultFrom", "exportNamespaceFrom", "dynamicImport", "nullishCoalescingOperator", "optionalChaining" ] } }); if (result?.code) { return { code: result.code, map: result.map }; } } catch (error) { console.warn("Failed to transform with Babel plugin:", error); } return null; } }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { IntlayerPrunePlugin }); //# sourceMappingURL=intlayerPrunePlugin.cjs.map