@intlayer/vue-compiler 7.3.2-canary.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +275 -0
- package/dist/cjs/VueIntlayerCompiler.cjs +245 -0
- package/dist/cjs/VueIntlayerCompiler.cjs.map +1 -0
- package/dist/cjs/_virtual/rolldown_runtime.cjs +29 -0
- package/dist/cjs/index.cjs +12 -0
- package/dist/cjs/vue-intlayer-extract.cjs +174 -0
- package/dist/cjs/vue-intlayer-extract.cjs.map +1 -0
- package/dist/esm/VueIntlayerCompiler.mjs +241 -0
- package/dist/esm/VueIntlayerCompiler.mjs.map +1 -0
- package/dist/esm/index.mjs +4 -0
- package/dist/esm/vue-intlayer-extract.mjs +168 -0
- package/dist/esm/vue-intlayer-extract.mjs.map +1 -0
- package/dist/types/VueIntlayerCompiler.d.ts +101 -0
- package/dist/types/VueIntlayerCompiler.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/vue-intlayer-extract.d.ts +99 -0
- package/dist/types/vue-intlayer-extract.d.ts.map +1 -0
- package/package.json +123 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vue-intlayer-extract.mjs","names":["parseVue: (code: string) => VueParseResult","MagicString: new (code: string) => MagicStringType","extractedContent: ExtractedContent"],"sources":["../../src/vue-intlayer-extract.ts"],"sourcesContent":["import { basename, dirname, extname } from 'node:path';\n\n/* ────────────────────────────────────────── constants ───────────────────── */\n\n/**\n * Attributes that should be extracted for localization\n */\nexport const ATTRIBUTES_TO_EXTRACT = [\n 'title',\n 'placeholder',\n 'alt',\n 'aria-label',\n 'label',\n];\n\n/* ────────────────────────────────────────── types ───────────────────────── */\n\nexport type ExtractedContent = Record<string, string>;\n\n/**\n * Extracted content result from a file transformation\n */\nexport type ExtractResult = {\n /** Dictionary key derived from the file path */\n dictionaryKey: string;\n /** File path that was processed */\n filePath: string;\n /** Extracted content key-value pairs */\n content: ExtractedContent;\n /** Default locale used */\n locale: string;\n};\n\n/**\n * Options for extraction plugins\n */\nexport type ExtractPluginOptions = {\n /**\n * The default locale for the extracted content\n * @default 'en'\n */\n defaultLocale?: string;\n /**\n * The package to import useIntlayer from\n * @default 'vue-intlayer'\n */\n packageName?: string;\n /**\n * Files list to traverse. If provided, only files in this list will be processed.\n */\n filesList?: string[];\n /**\n * Custom function to determine if a string should be extracted\n */\n shouldExtract?: (text: string) => boolean;\n /**\n * Callback function called when content is extracted from a file.\n * This allows the compiler to capture the extracted content and write it to files.\n * The dictionary will be updated: new keys added, unused keys removed.\n */\n onExtract?: (result: ExtractResult) => void;\n};\n\n/* ────────────────────────────────────────── helpers ─────────────────────── */\n\n/**\n * Default function to determine if a string should be extracted\n */\nexport const defaultShouldExtract = (text: string): boolean => {\n const trimmed = text.trim();\n if (!trimmed) return false;\n // Must contain at least one space (likely a sentence/phrase)\n if (!trimmed.includes(' ')) return false;\n // Must start with a capital letter\n if (!/^[A-Z]/.test(trimmed)) return false;\n // Filter out template logic identifiers\n if (trimmed.startsWith('{') || trimmed.startsWith('v-')) return false;\n return true;\n};\n\n/**\n * Generate a unique key from text\n */\nexport const generateKey = (\n text: string,\n existingKeys: Set<string>\n): string => {\n const maxWords = 5;\n let key = text\n .replace(/\\s+/g, ' ')\n .replace(/_+/g, ' ')\n .replace(/-+/g, ' ')\n .replace(/[^a-zA-Z0-9 ]/g, '')\n .trim()\n .split(' ')\n .filter(Boolean)\n .slice(0, maxWords)\n .map((word, index) =>\n index === 0\n ? word.toLowerCase()\n : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()\n )\n .join('');\n\n if (!key) key = 'content';\n if (existingKeys.has(key)) {\n let i = 1;\n while (existingKeys.has(`${key}${i}`)) i++;\n key = `${key}${i}`;\n }\n return key;\n};\n\n/**\n * Extract dictionary key from file path\n */\nexport const extractDictionaryKeyFromPath = (filePath: string): string => {\n const ext = extname(filePath);\n let baseName = basename(filePath, ext);\n\n if (baseName === 'index') {\n baseName = basename(dirname(filePath));\n }\n\n // Convert to kebab-case\n const key = baseName\n .replace(/([a-z])([A-Z])/g, '$1-$2')\n .replace(/[\\s_]+/g, '-')\n .toLowerCase();\n\n return `comp-${key}`;\n};\n\n/**\n * Check if a file should be processed based on filesList\n */\nexport const shouldProcessFile = (\n filename: string | undefined,\n filesList?: string[]\n): boolean => {\n if (!filename) return false;\n if (!filesList || filesList.length === 0) return true;\n\n // Normalize paths for comparison (handle potential path separator issues)\n const normalizedFilename = filename.replace(/\\\\/g, '/');\n return filesList.some((f) => {\n const normalizedF = f.replace(/\\\\/g, '/');\n return normalizedF === normalizedFilename;\n });\n};\n\n/* ────────────────────────────────────────── Vue types ───────────────────── */\n\ntype VueParseResult = {\n descriptor: {\n template?: {\n ast: VueAstNode;\n loc: { start: { offset: number }; end: { offset: number } };\n };\n script?: {\n content: string;\n loc: { start: { offset: number }; end: { offset: number } };\n };\n scriptSetup?: {\n content: string;\n loc: { start: { offset: number }; end: { offset: number } };\n };\n };\n};\n\ntype VueAstNode = {\n type: number;\n content?: string;\n children?: VueAstNode[];\n props?: VueAstProp[];\n loc: { start: { offset: number }; end: { offset: number } };\n};\n\ntype VueAstProp = {\n type: number;\n name: string;\n value?: { content: string };\n loc: { start: { offset: number }; end: { offset: number } };\n};\n\n// Vue AST NodeTypes\nconst NODE_TYPES = {\n TEXT: 2,\n ELEMENT: 1,\n ATTRIBUTE: 6,\n};\n\n// MagicString type for dynamic import\ntype MagicStringType = {\n overwrite: (start: number, end: number, content: string) => void;\n appendLeft: (index: number, content: string) => void;\n prepend: (content: string) => void;\n toString: () => string;\n generateMap: (options: {\n source: string;\n includeContent: boolean;\n }) => unknown;\n};\n\n/* ────────────────────────────────────────── plugin ──────────────────────── */\n\n/**\n * Vue extraction plugin that extracts content and transforms Vue SFC to use useIntlayer.\n *\n * This plugin:\n * 1. Scans Vue SFC files for extractable text (template text, attributes)\n * 2. Auto-injects useIntlayer import and composable call\n * 3. Reports extracted content via onExtract callback (for the compiler to write dictionaries)\n * 4. Replaces extractable strings with content references\n *\n * ## Input\n * ```vue\n * <template>\n * <div>Hello World</div>\n * </template>\n * ```\n *\n * ## Output\n * ```vue\n * <script setup>\n * import { useIntlayer } from 'vue-intlayer';\n * const content = useIntlayer('hello-world');\n * </script>\n * <template>\n * <div>{{ content.helloWorld }}</div>\n * </template>\n * ```\n */\nexport const intlayerVueExtract = async (\n code: string,\n filename: string,\n options: ExtractPluginOptions = {}\n): Promise<{ code: string; map?: unknown; extracted: boolean } | null> => {\n const {\n defaultLocale = 'en',\n packageName = 'vue-intlayer',\n filesList,\n shouldExtract = defaultShouldExtract,\n onExtract,\n } = options;\n\n // Check if file should be processed\n if (!shouldProcessFile(filename, filesList)) {\n return null;\n }\n\n // Skip non-Vue files\n if (!filename.endsWith('.vue')) {\n return null;\n }\n\n // Dynamic imports for dependencies (peer dependencies)\n let parseVue: (code: string) => VueParseResult;\n let MagicString: new (code: string) => MagicStringType;\n\n try {\n const vueSfc = await import('@vue/compiler-sfc');\n // Type assertion needed because Vue's SFCParseResult uses `null` for optional properties\n // while our VueParseResult uses `undefined` (optional). This is safe since we check\n // for truthy values before accessing template/script properties.\n parseVue = vueSfc.parse as unknown as (code: string) => VueParseResult;\n } catch {\n console.warn(\n 'Vue extraction: @vue/compiler-sfc not found. Install it to enable Vue content extraction.'\n );\n return null;\n }\n\n try {\n const magicStringModule = await import('magic-string');\n MagicString = magicStringModule.default;\n } catch {\n console.warn(\n 'Vue extraction: magic-string not found. Install it to enable Vue content extraction.'\n );\n return null;\n }\n\n const sfc = parseVue(code);\n const magic = new MagicString(code);\n\n const extractedContent: ExtractedContent = {};\n const existingKeys = new Set<string>();\n const dictionaryKey = extractDictionaryKeyFromPath(filename);\n\n // Walk the template AST\n if (sfc.descriptor.template) {\n const walkVueAst = (node: VueAstNode) => {\n if (node.type === NODE_TYPES.TEXT) {\n // Text node\n const text = node.content ?? '';\n if (shouldExtract(text)) {\n const key = generateKey(text, existingKeys);\n existingKeys.add(key);\n extractedContent[key] = text.replace(/\\s+/g, ' ').trim();\n magic.overwrite(\n node.loc.start.offset,\n node.loc.end.offset,\n `{{ content.${key} }}`\n );\n }\n } else if (node.type === NODE_TYPES.ELEMENT) {\n // Element node - check attributes\n node.props?.forEach((prop) => {\n if (\n prop.type === NODE_TYPES.ATTRIBUTE &&\n ATTRIBUTES_TO_EXTRACT.includes(prop.name) &&\n prop.value\n ) {\n const text = prop.value.content;\n if (shouldExtract(text)) {\n const key = generateKey(text, existingKeys);\n existingKeys.add(key);\n extractedContent[key] = text.trim();\n magic.overwrite(\n prop.loc.start.offset,\n prop.loc.end.offset,\n `:${prop.name}=\"content.${key}.value\"`\n );\n }\n }\n });\n }\n\n // Recurse into children\n if (node.children) {\n node.children.forEach(walkVueAst);\n }\n };\n\n walkVueAst(sfc.descriptor.template.ast);\n }\n\n // If nothing was extracted, return null\n if (Object.keys(extractedContent).length === 0) {\n return null;\n }\n\n // Get script content for checking existing imports/declarations\n const scriptContent =\n sfc.descriptor.scriptSetup?.content ?? sfc.descriptor.script?.content ?? '';\n\n // Check if useIntlayer is already imported\n const hasUseIntlayerImport =\n /import\\s*{[^}]*useIntlayer[^}]*}\\s*from\\s*['\"][^'\"]+['\"]/.test(\n scriptContent\n ) || /import\\s+useIntlayer\\s+from\\s*['\"][^'\"]+['\"]/.test(scriptContent);\n\n // Check if content variable is already declared with useIntlayer\n const hasContentDeclaration = /const\\s+content\\s*=\\s*useIntlayer\\s*\\(/.test(\n scriptContent\n );\n\n // Skip injection if already using useIntlayer\n if (hasUseIntlayerImport && hasContentDeclaration) {\n return null;\n }\n\n // Prepare injection statements (only what's missing)\n const importStmt = hasUseIntlayerImport\n ? ''\n : `import { useIntlayer } from '${packageName}';`;\n const contentDecl = hasContentDeclaration\n ? ''\n : `const content = useIntlayer('${dictionaryKey}');`;\n\n // Build injection string\n const injectionParts = [importStmt, contentDecl].filter(Boolean);\n if (injectionParts.length === 0) {\n return null;\n }\n const injection = `\\n${injectionParts.join('\\n')}\\n`;\n\n if (sfc.descriptor.scriptSetup) {\n // Insert at the beginning of script setup content\n magic.appendLeft(sfc.descriptor.scriptSetup.loc.start.offset, injection);\n } else if (sfc.descriptor.script) {\n // Insert at the beginning of script content\n magic.appendLeft(sfc.descriptor.script.loc.start.offset, injection);\n } else {\n // No script block, create one\n magic.prepend(`<script setup>\\n${importStmt}\\n${contentDecl}\\n</script>\\n`);\n }\n\n // Call the onExtract callback with extracted content\n if (onExtract) {\n const result: ExtractResult = {\n dictionaryKey,\n filePath: filename,\n content: { ...extractedContent },\n locale: defaultLocale,\n };\n onExtract(result);\n }\n\n return {\n code: magic.toString(),\n map: magic.generateMap({ source: filename, includeContent: true }),\n extracted: true,\n };\n};\n"],"mappings":";;;;;;AAOA,MAAa,wBAAwB;CACnC;CACA;CACA;CACA;CACA;CACD;;;;AAuDD,MAAa,wBAAwB,SAA0B;CAC7D,MAAM,UAAU,KAAK,MAAM;AAC3B,KAAI,CAAC,QAAS,QAAO;AAErB,KAAI,CAAC,QAAQ,SAAS,IAAI,CAAE,QAAO;AAEnC,KAAI,CAAC,SAAS,KAAK,QAAQ,CAAE,QAAO;AAEpC,KAAI,QAAQ,WAAW,IAAI,IAAI,QAAQ,WAAW,KAAK,CAAE,QAAO;AAChE,QAAO;;;;;AAMT,MAAa,eACX,MACA,iBACW;CAEX,IAAI,MAAM,KACP,QAAQ,QAAQ,IAAI,CACpB,QAAQ,OAAO,IAAI,CACnB,QAAQ,OAAO,IAAI,CACnB,QAAQ,kBAAkB,GAAG,CAC7B,MAAM,CACN,MAAM,IAAI,CACV,OAAO,QAAQ,CACf,MAAM,GATQ,EASI,CAClB,KAAK,MAAM,UACV,UAAU,IACN,KAAK,aAAa,GAClB,KAAK,OAAO,EAAE,CAAC,aAAa,GAAG,KAAK,MAAM,EAAE,CAAC,aAAa,CAC/D,CACA,KAAK,GAAG;AAEX,KAAI,CAAC,IAAK,OAAM;AAChB,KAAI,aAAa,IAAI,IAAI,EAAE;EACzB,I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CAAC,YAAY,YAAY,CAAC,OAAO,QAAQ;AAChE,KAAI,eAAe,WAAW,EAC5B,QAAO;CAET,MAAM,YAAY,KAAK,eAAe,KAAK,KAAK,CAAC;AAEjD,KAAI,IAAI,WAAW,YAEjB,OAAM,WAAW,IAAI,WAAW,YAAY,IAAI,MAAM,QAAQ,UAAU;UAC/D,IAAI,WAAW,OAExB,OAAM,WAAW,IAAI,WAAW,OAAO,IAAI,MAAM,QAAQ,UAAU;KAGnE,OAAM,QAAQ,mBAAmB,WAAW,IAAI,YAAY,gBAAe;AAI7E,KAAI,UAOF,WAN8B;EAC5B;EACA,UAAU;EACV,SAAS,EAAE,GAAG,kBAAkB;EAChC,QAAQ;EACT,CACgB;AAGnB,QAAO;EACL,MAAM,MAAM,UAAU;EACtB,KAAK,MAAM,YAAY;GAAE,QAAQ;GAAU,gBAAgB;GAAM,CAAC;EAClE,WAAW;EACZ"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { GetConfigurationOptions } from "@intlayer/config";
|
|
2
|
+
import { CompilerConfig } from "@intlayer/types";
|
|
3
|
+
|
|
4
|
+
//#region src/VueIntlayerCompiler.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Mode of the compiler
|
|
8
|
+
*/
|
|
9
|
+
type CompilerMode = 'dev' | 'build';
|
|
10
|
+
/**
|
|
11
|
+
* Context for hot update handling
|
|
12
|
+
*/
|
|
13
|
+
type HotUpdateContext = {
|
|
14
|
+
file: string;
|
|
15
|
+
server: {
|
|
16
|
+
ws: {
|
|
17
|
+
send: (message: {
|
|
18
|
+
type: string;
|
|
19
|
+
}) => void;
|
|
20
|
+
};
|
|
21
|
+
moduleGraph: {
|
|
22
|
+
getModulesByFile: (file: string) => Set<unknown> | null | undefined;
|
|
23
|
+
invalidateModule: (module: unknown, seen: Set<unknown>, timestamp: number, isHmr: boolean) => void;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
timestamp: number;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Transform result from the compiler
|
|
30
|
+
*/
|
|
31
|
+
type TransformResult = {
|
|
32
|
+
code?: string;
|
|
33
|
+
map?: unknown;
|
|
34
|
+
} | null;
|
|
35
|
+
/**
|
|
36
|
+
* Options for initializing the Vue compiler
|
|
37
|
+
*/
|
|
38
|
+
type VueIntlayerCompilerOptions = {
|
|
39
|
+
/**
|
|
40
|
+
* Configuration options for getting the intlayer configuration
|
|
41
|
+
*/
|
|
42
|
+
configOptions?: GetConfigurationOptions;
|
|
43
|
+
/**
|
|
44
|
+
* Custom compiler configuration to override defaults
|
|
45
|
+
*/
|
|
46
|
+
compilerConfig?: Partial<CompilerConfig>;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Vite plugin object returned by the compiler
|
|
50
|
+
*/
|
|
51
|
+
type VueIntlayerVitePlugin = {
|
|
52
|
+
name: string;
|
|
53
|
+
enforce: 'pre' | 'post';
|
|
54
|
+
configResolved: (config: {
|
|
55
|
+
env?: {
|
|
56
|
+
DEV?: boolean;
|
|
57
|
+
};
|
|
58
|
+
root: string;
|
|
59
|
+
}) => Promise<void>;
|
|
60
|
+
buildStart: () => Promise<void>;
|
|
61
|
+
configureServer: () => Promise<void>;
|
|
62
|
+
handleHotUpdate: (ctx: HotUpdateContext) => Promise<unknown[] | undefined>;
|
|
63
|
+
transform: {
|
|
64
|
+
order: 'pre' | 'post';
|
|
65
|
+
handler: (code: string, id: string, options?: {
|
|
66
|
+
ssr?: boolean;
|
|
67
|
+
}) => Promise<TransformResult>;
|
|
68
|
+
};
|
|
69
|
+
apply: (config: unknown, env: {
|
|
70
|
+
command: string;
|
|
71
|
+
}) => boolean;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Create a VueIntlayerCompiler - A Vite-compatible compiler plugin for Vue with Intlayer
|
|
75
|
+
*
|
|
76
|
+
* Handles Vue Single File Components (SFC) with special handling for:
|
|
77
|
+
* - Script blocks in .vue files
|
|
78
|
+
* - TypeScript in Vue files
|
|
79
|
+
* - Vue-specific transformations
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* // vite.config.ts
|
|
84
|
+
* import { defineConfig } from 'vite';
|
|
85
|
+
* import vue from '@vitejs/plugin-vue';
|
|
86
|
+
* import { vueIntlayerCompiler } from '@intlayer/vue-compiler';
|
|
87
|
+
*
|
|
88
|
+
* export default defineConfig({
|
|
89
|
+
* plugins: [vue(), vueIntlayerCompiler()],
|
|
90
|
+
* });
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
declare const createVueIntlayerCompiler: (options?: VueIntlayerCompilerOptions) => VueIntlayerVitePlugin;
|
|
94
|
+
/**
|
|
95
|
+
* Factory function for creating a Vite plugin
|
|
96
|
+
*/
|
|
97
|
+
declare const vueIntlayerCompiler: (options?: VueIntlayerCompilerOptions) => VueIntlayerVitePlugin;
|
|
98
|
+
declare const VueIntlayerCompiler: (options?: VueIntlayerCompilerOptions) => VueIntlayerVitePlugin;
|
|
99
|
+
//#endregion
|
|
100
|
+
export { CompilerMode, HotUpdateContext, TransformResult, VueIntlayerCompiler, VueIntlayerCompilerOptions, VueIntlayerVitePlugin, createVueIntlayerCompiler, vueIntlayerCompiler };
|
|
101
|
+
//# sourceMappingURL=VueIntlayerCompiler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VueIntlayerCompiler.d.ts","names":[],"sources":["../../src/VueIntlayerCompiler.ts"],"sourcesContent":[],"mappings":";;;;;;;AAeA;AAKY,KALA,YAAA,GAKgB,KAAA,GAKc,OAG5B;AAYd;AAQA;;AAS2B,KArCf,gBAAA,GAqCe;EAAR,IAAA,EAAA,MAAA;EAAO,MAAA,EAAA;IAMd,EAAA,EAAA;MAMJ,IAAA,EAAA,CAAA,OAAA,EAAA;QACY,IAAA,EAAA,MAAA;MACK,CAAA,EAAA,GAAA,IAAA;IACA,CAAA;IAAqB,WAAA,EAAA;MAO7B,gBAAA,EAAA,CAAA,IAAA,EAAA,MAAA,EAAA,GAtDyB,GAsDzB,CAAA,OAAA,CAAA,GAAA,IAAA,GAAA,SAAA;MAAR,gBAAA,EAAA,CAAA,MAAA,EAAA,OAAA,EAAA,IAAA,EAnDK,GAmDL,CAAA,OAAA,CAAA,EAAA,SAAA,EAAA,MAAA,EAAA,KAAA,EAAA,OAAA,EAAA,GAAA,IAAA;IAAO,CAAA;EAyBH,CAAA;EAgVA,SAAA,EAAA,MAAA;AAOb,CAAA;;;;KAvZY,eAAA;;;;;;;KAQA,0BAAA;;;;kBAIM;;;;mBAKC,QAAQ;;;;;KAMf,qBAAA;;;;;;;;QAMJ;oBACY;yBACK;yBACA,qBAAqB;;;;;UAOrC,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;cAyBJ,sCACD,+BACT;;;;cA8UU,gCACD,+BACT;cAKU,gCAtVD,+BACT"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { CompilerMode, HotUpdateContext, TransformResult, VueIntlayerCompiler, VueIntlayerCompilerOptions, VueIntlayerVitePlugin, createVueIntlayerCompiler, vueIntlayerCompiler } from "./VueIntlayerCompiler.js";
|
|
2
|
+
import { ATTRIBUTES_TO_EXTRACT, ExtractPluginOptions, ExtractResult, ExtractedContent, defaultShouldExtract, extractDictionaryKeyFromPath, generateKey, intlayerVueExtract, shouldProcessFile } from "./vue-intlayer-extract.js";
|
|
3
|
+
export { ATTRIBUTES_TO_EXTRACT, type CompilerMode, type ExtractPluginOptions, type ExtractResult, type ExtractedContent, type HotUpdateContext, type TransformResult, VueIntlayerCompiler, type VueIntlayerCompilerOptions, type VueIntlayerVitePlugin, createVueIntlayerCompiler, defaultShouldExtract, extractDictionaryKeyFromPath, generateKey, intlayerVueExtract, shouldProcessFile, vueIntlayerCompiler };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
//#region src/vue-intlayer-extract.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Attributes that should be extracted for localization
|
|
4
|
+
*/
|
|
5
|
+
declare const ATTRIBUTES_TO_EXTRACT: string[];
|
|
6
|
+
type ExtractedContent = Record<string, string>;
|
|
7
|
+
/**
|
|
8
|
+
* Extracted content result from a file transformation
|
|
9
|
+
*/
|
|
10
|
+
type ExtractResult = {
|
|
11
|
+
/** Dictionary key derived from the file path */
|
|
12
|
+
dictionaryKey: string;
|
|
13
|
+
/** File path that was processed */
|
|
14
|
+
filePath: string;
|
|
15
|
+
/** Extracted content key-value pairs */
|
|
16
|
+
content: ExtractedContent;
|
|
17
|
+
/** Default locale used */
|
|
18
|
+
locale: string;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Options for extraction plugins
|
|
22
|
+
*/
|
|
23
|
+
type ExtractPluginOptions = {
|
|
24
|
+
/**
|
|
25
|
+
* The default locale for the extracted content
|
|
26
|
+
* @default 'en'
|
|
27
|
+
*/
|
|
28
|
+
defaultLocale?: string;
|
|
29
|
+
/**
|
|
30
|
+
* The package to import useIntlayer from
|
|
31
|
+
* @default 'vue-intlayer'
|
|
32
|
+
*/
|
|
33
|
+
packageName?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Files list to traverse. If provided, only files in this list will be processed.
|
|
36
|
+
*/
|
|
37
|
+
filesList?: string[];
|
|
38
|
+
/**
|
|
39
|
+
* Custom function to determine if a string should be extracted
|
|
40
|
+
*/
|
|
41
|
+
shouldExtract?: (text: string) => boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Callback function called when content is extracted from a file.
|
|
44
|
+
* This allows the compiler to capture the extracted content and write it to files.
|
|
45
|
+
* The dictionary will be updated: new keys added, unused keys removed.
|
|
46
|
+
*/
|
|
47
|
+
onExtract?: (result: ExtractResult) => void;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Default function to determine if a string should be extracted
|
|
51
|
+
*/
|
|
52
|
+
declare const defaultShouldExtract: (text: string) => boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Generate a unique key from text
|
|
55
|
+
*/
|
|
56
|
+
declare const generateKey: (text: string, existingKeys: Set<string>) => string;
|
|
57
|
+
/**
|
|
58
|
+
* Extract dictionary key from file path
|
|
59
|
+
*/
|
|
60
|
+
declare const extractDictionaryKeyFromPath: (filePath: string) => string;
|
|
61
|
+
/**
|
|
62
|
+
* Check if a file should be processed based on filesList
|
|
63
|
+
*/
|
|
64
|
+
declare const shouldProcessFile: (filename: string | undefined, filesList?: string[]) => boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Vue extraction plugin that extracts content and transforms Vue SFC to use useIntlayer.
|
|
67
|
+
*
|
|
68
|
+
* This plugin:
|
|
69
|
+
* 1. Scans Vue SFC files for extractable text (template text, attributes)
|
|
70
|
+
* 2. Auto-injects useIntlayer import and composable call
|
|
71
|
+
* 3. Reports extracted content via onExtract callback (for the compiler to write dictionaries)
|
|
72
|
+
* 4. Replaces extractable strings with content references
|
|
73
|
+
*
|
|
74
|
+
* ## Input
|
|
75
|
+
* ```vue
|
|
76
|
+
* <template>
|
|
77
|
+
* <div>Hello World</div>
|
|
78
|
+
* </template>
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* ## Output
|
|
82
|
+
* ```vue
|
|
83
|
+
* <script setup>
|
|
84
|
+
* import { useIntlayer } from 'vue-intlayer';
|
|
85
|
+
* const content = useIntlayer('hello-world');
|
|
86
|
+
* </script>
|
|
87
|
+
* <template>
|
|
88
|
+
* <div>{{ content.helloWorld }}</div>
|
|
89
|
+
* </template>
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
declare const intlayerVueExtract: (code: string, filename: string, options?: ExtractPluginOptions) => Promise<{
|
|
93
|
+
code: string;
|
|
94
|
+
map?: unknown;
|
|
95
|
+
extracted: boolean;
|
|
96
|
+
} | null>;
|
|
97
|
+
//#endregion
|
|
98
|
+
export { ATTRIBUTES_TO_EXTRACT, ExtractPluginOptions, ExtractResult, ExtractedContent, defaultShouldExtract, extractDictionaryKeyFromPath, generateKey, intlayerVueExtract, shouldProcessFile };
|
|
99
|
+
//# sourceMappingURL=vue-intlayer-extract.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vue-intlayer-extract.d.ts","names":[],"sources":["../../src/vue-intlayer-extract.ts"],"sourcesContent":[],"mappings":";;AAOA;AAUA;AAKY,cAfC,qBAqBF,EAAA,MAAgB,EAAA;AAQf,KAnBA,gBAAA,GAAmB,MAmBC,CAAA,MAwBT,EAAA,MAAA,CAAa;AAQpC;AAeA;AAiCA;AAoBa,KAlHD,aAAA,GA+HX;EAoFY;;;;;WA7MF;;;;;;;KAQC,oBAAA;;;;;;;;;;;;;;;;;;;;;;;;uBAwBW;;;;;cAQV;;;;cAeA,0CAEG;;;;cA+BH;;;;cAoBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAiGA,+DAGF,yBACR"}
|
package/package.json
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@intlayer/vue-compiler",
|
|
3
|
+
"version": "7.3.2-canary.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Vite-compatible compiler plugin for Vue with Intlayer, providing HMR support, file transformation, and optimized dictionary loading for Vue applications.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"intlayer",
|
|
8
|
+
"vue",
|
|
9
|
+
"vite",
|
|
10
|
+
"compiler",
|
|
11
|
+
"i18n",
|
|
12
|
+
"internationalization",
|
|
13
|
+
"hmr",
|
|
14
|
+
"typescript"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://intlayer.org",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/aymericzip/intlayer/issues"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/aymericzip/intlayer.git"
|
|
23
|
+
},
|
|
24
|
+
"license": "Apache-2.0",
|
|
25
|
+
"author": {
|
|
26
|
+
"name": "Aymeric PINEAU",
|
|
27
|
+
"url": "https://github.com/aymericzip"
|
|
28
|
+
},
|
|
29
|
+
"contributors": [
|
|
30
|
+
{
|
|
31
|
+
"name": "Aymeric Pineau",
|
|
32
|
+
"email": "ay.pineau@gmail.com",
|
|
33
|
+
"url": "https://github.com/aymericzip"
|
|
34
|
+
}
|
|
35
|
+
],
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"exports": {
|
|
38
|
+
".": {
|
|
39
|
+
"types": "./dist/types/index.d.ts",
|
|
40
|
+
"require": "./dist/cjs/index.cjs",
|
|
41
|
+
"import": "./dist/esm/index.mjs"
|
|
42
|
+
},
|
|
43
|
+
"./package.json": "./package.json"
|
|
44
|
+
},
|
|
45
|
+
"main": "dist/cjs/index.cjs",
|
|
46
|
+
"module": "dist/esm/index.mjs",
|
|
47
|
+
"types": "dist/types/index.d.ts",
|
|
48
|
+
"typesVersions": {
|
|
49
|
+
"*": {
|
|
50
|
+
"package.json": [
|
|
51
|
+
"./package.json"
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"bin": {
|
|
56
|
+
"intlayer": "./dist/cjs/index.cjs"
|
|
57
|
+
},
|
|
58
|
+
"files": [
|
|
59
|
+
"./dist",
|
|
60
|
+
"./package.json"
|
|
61
|
+
],
|
|
62
|
+
"scripts": {
|
|
63
|
+
"_prepublish": "cp -f ../../README.md ./README.md",
|
|
64
|
+
"build": "tsdown --config tsdown.config.ts",
|
|
65
|
+
"build:ci": "tsdown --config tsdown.config.ts",
|
|
66
|
+
"clean": "rimraf ./dist .turbo",
|
|
67
|
+
"dev": "tsdown --config tsdown.config.ts --watch",
|
|
68
|
+
"format": "biome format . --check",
|
|
69
|
+
"format:fix": "biome format --write .",
|
|
70
|
+
"lint": "biome lint .",
|
|
71
|
+
"lint:fix": "biome lint --write .",
|
|
72
|
+
"prepublish": "echo prepublish temporally disabled to avoid rewrite readme",
|
|
73
|
+
"publish": "bun publish || true",
|
|
74
|
+
"publish:canary": "bun publish --access public --tag canary || true",
|
|
75
|
+
"publish:latest": "bun publish --access public --tag latest || true",
|
|
76
|
+
"serve": "webpack serve --config ./webpack.config.ts",
|
|
77
|
+
"test": "vitest run",
|
|
78
|
+
"test:watch": "vitest",
|
|
79
|
+
"transpile": "webpack --config ./webpack.config.ts",
|
|
80
|
+
"typecheck": "tsc --noEmit --project tsconfig.types.json",
|
|
81
|
+
"watch": "webpack --config ./webpack.config.ts --watch"
|
|
82
|
+
},
|
|
83
|
+
"dependencies": {
|
|
84
|
+
"@babel/core": "7.28.4",
|
|
85
|
+
"@intlayer/babel": "7.3.2-canary.0",
|
|
86
|
+
"@intlayer/chokidar": "7.3.2-canary.0",
|
|
87
|
+
"@intlayer/cli": "7.3.2-canary.0",
|
|
88
|
+
"@intlayer/config": "7.3.2-canary.0",
|
|
89
|
+
"@intlayer/dictionaries-entry": "7.3.2-canary.0",
|
|
90
|
+
"@intlayer/types": "7.3.2-canary.0",
|
|
91
|
+
"fast-glob": "3.3.3",
|
|
92
|
+
"magic-string": "^0.30.17"
|
|
93
|
+
},
|
|
94
|
+
"devDependencies": {
|
|
95
|
+
"@types/node": "24.10.1",
|
|
96
|
+
"@utils/ts-config": "1.0.4",
|
|
97
|
+
"@utils/ts-config-types": "1.0.4",
|
|
98
|
+
"@utils/tsdown-config": "1.0.4",
|
|
99
|
+
"rimraf": "6.1.2",
|
|
100
|
+
"tsdown": "0.16.6",
|
|
101
|
+
"typescript": "5.9.3",
|
|
102
|
+
"vitest": "4.0.13"
|
|
103
|
+
},
|
|
104
|
+
"peerDependencies": {
|
|
105
|
+
"@babel/core": ">=6.0.0",
|
|
106
|
+
"@vue/compiler-sfc": ">=3.0.0",
|
|
107
|
+
"vite": ">=4.0.0"
|
|
108
|
+
},
|
|
109
|
+
"peerDependenciesMeta": {
|
|
110
|
+
"@babel/core": {
|
|
111
|
+
"optional": true
|
|
112
|
+
},
|
|
113
|
+
"@vue/compiler-sfc": {
|
|
114
|
+
"optional": true
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
"engines": {
|
|
118
|
+
"node": ">=14.18"
|
|
119
|
+
},
|
|
120
|
+
"bug": {
|
|
121
|
+
"url": "https://github.com/aymericzip/intlayer/issues"
|
|
122
|
+
}
|
|
123
|
+
}
|