@intlayer/svelte-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.
@@ -0,0 +1,198 @@
1
+ import { basename, dirname, extname } from "node:path";
2
+
3
+ //#region src/svelte-intlayer-extract.ts
4
+ /**
5
+ * Attributes that should be extracted for localization
6
+ */
7
+ const ATTRIBUTES_TO_EXTRACT = [
8
+ "title",
9
+ "placeholder",
10
+ "alt",
11
+ "aria-label",
12
+ "label"
13
+ ];
14
+ /**
15
+ * Default function to determine if a string should be extracted
16
+ */
17
+ const defaultShouldExtract = (text) => {
18
+ const trimmed = text.trim();
19
+ if (!trimmed) return false;
20
+ if (!trimmed.includes(" ")) return false;
21
+ if (!/^[A-Z]/.test(trimmed)) return false;
22
+ if (trimmed.startsWith("{") || trimmed.startsWith("v-")) return false;
23
+ return true;
24
+ };
25
+ /**
26
+ * Generate a unique key from text
27
+ */
28
+ const generateKey = (text, existingKeys) => {
29
+ let key = text.replace(/\s+/g, " ").replace(/_+/g, " ").replace(/-+/g, " ").replace(/[^a-zA-Z0-9 ]/g, "").trim().split(" ").filter(Boolean).slice(0, 5).map((word, index) => index === 0 ? word.toLowerCase() : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join("");
30
+ if (!key) key = "content";
31
+ if (existingKeys.has(key)) {
32
+ let i = 1;
33
+ while (existingKeys.has(`${key}${i}`)) i++;
34
+ key = `${key}${i}`;
35
+ }
36
+ return key;
37
+ };
38
+ /**
39
+ * Extract dictionary key from file path
40
+ */
41
+ const extractDictionaryKeyFromPath = (filePath) => {
42
+ let baseName = basename(filePath, extname(filePath));
43
+ if (baseName === "index") baseName = basename(dirname(filePath));
44
+ return `comp-${baseName.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase()}`;
45
+ };
46
+ /**
47
+ * Check if a file should be processed based on filesList
48
+ */
49
+ const shouldProcessFile = (filename, filesList) => {
50
+ if (!filename) return false;
51
+ if (!filesList || filesList.length === 0) return true;
52
+ const normalizedFilename = filename.replace(/\\/g, "/");
53
+ return filesList.some((f) => {
54
+ return f.replace(/\\/g, "/") === normalizedFilename;
55
+ });
56
+ };
57
+ /**
58
+ * Svelte extraction plugin that extracts content and transforms Svelte components to use useIntlayer.
59
+ *
60
+ * This plugin:
61
+ * 1. Scans Svelte files for extractable text (template text, attributes)
62
+ * 2. Auto-injects useIntlayer import and store binding
63
+ * 3. Reports extracted content via onExtract callback (for the compiler to write dictionaries)
64
+ * 4. Replaces extractable strings with content references using Svelte's reactive `$` prefix
65
+ *
66
+ * ## Input
67
+ * ```svelte
68
+ * <h1>Hello World</h1>
69
+ * <p>Welcome to our app</p>
70
+ * ```
71
+ *
72
+ * ## Output
73
+ * ```svelte
74
+ * <script>
75
+ * import { useIntlayer } from 'svelte-intlayer';
76
+ * const content = useIntlayer('hello-world');
77
+ * <\/script>
78
+ * <h1>{$content.helloWorld}</h1>
79
+ * <p>{$content.welcomeToOurApp}</p>
80
+ * ```
81
+ *
82
+ * Note: Svelte uses reactive stores with `$` prefix for automatic subscription.
83
+ * The `useIntlayer` composable returns a Svelte store that can be accessed reactively.
84
+ */
85
+ const intlayerSvelteExtract = async (code, filename, options = {}) => {
86
+ const { defaultLocale = "en", packageName = "svelte-intlayer", filesList, shouldExtract = defaultShouldExtract, onExtract } = options;
87
+ if (!shouldProcessFile(filename, filesList)) return null;
88
+ if (!filename.endsWith(".svelte")) return null;
89
+ let MagicString;
90
+ try {
91
+ MagicString = (await import("magic-string")).default;
92
+ } catch {
93
+ console.warn("Svelte extraction: magic-string not found. Install it to enable Svelte content extraction.");
94
+ return null;
95
+ }
96
+ const magic = new MagicString(code);
97
+ const extractedContent = {};
98
+ const existingKeys = /* @__PURE__ */ new Set();
99
+ const dictionaryKey = extractDictionaryKeyFromPath(filename);
100
+ const replacements = [];
101
+ const scriptBlockRegex = /<script[^>]*>[\s\S]*?<\/script>/gi;
102
+ const styleBlockRegex = /<style[^>]*>[\s\S]*?<\/style>/gi;
103
+ const skipRanges = [];
104
+ const scriptMatches = code.matchAll(scriptBlockRegex);
105
+ for (const match of scriptMatches) if (match.index !== void 0) skipRanges.push({
106
+ start: match.index,
107
+ end: match.index + match[0].length
108
+ });
109
+ const styleMatches = code.matchAll(styleBlockRegex);
110
+ for (const match of styleMatches) if (match.index !== void 0) skipRanges.push({
111
+ start: match.index,
112
+ end: match.index + match[0].length
113
+ });
114
+ skipRanges.sort((a, b) => a.start - b.start);
115
+ const isInSkipRange = (pos) => {
116
+ return skipRanges.some((range) => pos >= range.start && pos < range.end);
117
+ };
118
+ const textMatches = code.matchAll(/>([^<]+)</g);
119
+ for (const match of textMatches) {
120
+ if (match.index === void 0) continue;
121
+ const textStart = match.index + 1;
122
+ const text = match[1];
123
+ const textEnd = textStart + text.length;
124
+ if (isInSkipRange(textStart)) continue;
125
+ if (shouldExtract(text)) {
126
+ const key = generateKey(text, existingKeys);
127
+ existingKeys.add(key);
128
+ const normalizedValue = text.replace(/\s+/g, " ").trim();
129
+ replacements.push({
130
+ start: textStart,
131
+ end: textEnd,
132
+ replacement: `{$content.${key}}`,
133
+ key,
134
+ value: normalizedValue
135
+ });
136
+ }
137
+ }
138
+ for (const attrName of ATTRIBUTES_TO_EXTRACT) {
139
+ const attrRegex = new RegExp(`(${attrName})=["']([^"']+)["']`, "gi");
140
+ const attrMatches = code.matchAll(attrRegex);
141
+ for (const match of attrMatches) {
142
+ if (match.index === void 0) continue;
143
+ const attrStart = match.index;
144
+ const attrEnd = attrStart + match[0].length;
145
+ const text = match[2];
146
+ if (isInSkipRange(attrStart)) continue;
147
+ if (shouldExtract(text)) {
148
+ const key = generateKey(text, existingKeys);
149
+ existingKeys.add(key);
150
+ replacements.push({
151
+ start: attrStart,
152
+ end: attrEnd,
153
+ replacement: `${attrName}={$content.${key}.value}`,
154
+ key,
155
+ value: text.trim()
156
+ });
157
+ }
158
+ }
159
+ }
160
+ replacements.sort((a, b) => b.start - a.start);
161
+ for (const { start, end, replacement, key, value } of replacements) {
162
+ magic.overwrite(start, end, replacement);
163
+ extractedContent[key] = value;
164
+ }
165
+ if (Object.keys(extractedContent).length === 0) return null;
166
+ const scriptMatch = /<script[^>]*>([\s\S]*?)<\/script>/.exec(code);
167
+ const scriptContent = scriptMatch ? scriptMatch[1] : "";
168
+ const hasUseIntlayerImport = /import\s*{[^}]*useIntlayer[^}]*}\s*from\s*['"][^'"]+['"]/.test(scriptContent) || /import\s+useIntlayer\s+from\s*['"][^'"]+['"]/.test(scriptContent);
169
+ const hasContentDeclaration = /const\s+content\s*=\s*useIntlayer\s*\(/.test(scriptContent);
170
+ if (hasUseIntlayerImport && hasContentDeclaration) return null;
171
+ const importStmt = hasUseIntlayerImport ? "" : `import { useIntlayer } from '${packageName}';`;
172
+ const contentDecl = hasContentDeclaration ? "" : `const content = useIntlayer('${dictionaryKey}');`;
173
+ const injectionParts = [importStmt, contentDecl].filter(Boolean);
174
+ if (injectionParts.length === 0) return null;
175
+ const injection = `\n ${injectionParts.join("\n ")}\n`;
176
+ if (scriptMatch) {
177
+ const scriptContentStart = scriptMatch.index + scriptMatch[0].indexOf(">") + 1;
178
+ magic.appendLeft(scriptContentStart, injection);
179
+ } else magic.prepend(`<script>\n ${importStmt}\n ${contentDecl}\n<\/script>\n\n`);
180
+ if (onExtract) onExtract({
181
+ dictionaryKey,
182
+ filePath: filename,
183
+ content: { ...extractedContent },
184
+ locale: defaultLocale
185
+ });
186
+ return {
187
+ code: magic.toString(),
188
+ map: magic.generateMap({
189
+ source: filename,
190
+ includeContent: true
191
+ }),
192
+ extracted: true
193
+ };
194
+ };
195
+
196
+ //#endregion
197
+ export { ATTRIBUTES_TO_EXTRACT, defaultShouldExtract, extractDictionaryKeyFromPath, generateKey, intlayerSvelteExtract, shouldProcessFile };
198
+ //# sourceMappingURL=svelte-intlayer-extract.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"svelte-intlayer-extract.mjs","names":["MagicString: new (code: string) => MagicStringType","extractedContent: ExtractedContent","replacements: Replacement[]","skipRanges: Array<{ start: number; end: number }>"],"sources":["../../src/svelte-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 'svelte-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/* ────────────────────────────────────────── MagicString type ────────────── */\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 * Svelte extraction plugin that extracts content and transforms Svelte components to use useIntlayer.\n *\n * This plugin:\n * 1. Scans Svelte files for extractable text (template text, attributes)\n * 2. Auto-injects useIntlayer import and store binding\n * 3. Reports extracted content via onExtract callback (for the compiler to write dictionaries)\n * 4. Replaces extractable strings with content references using Svelte's reactive `$` prefix\n *\n * ## Input\n * ```svelte\n * <h1>Hello World</h1>\n * <p>Welcome to our app</p>\n * ```\n *\n * ## Output\n * ```svelte\n * <script>\n * import { useIntlayer } from 'svelte-intlayer';\n * const content = useIntlayer('hello-world');\n * </script>\n * <h1>{$content.helloWorld}</h1>\n * <p>{$content.welcomeToOurApp}</p>\n * ```\n *\n * Note: Svelte uses reactive stores with `$` prefix for automatic subscription.\n * The `useIntlayer` composable returns a Svelte store that can be accessed reactively.\n */\nexport const intlayerSvelteExtract = 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 = 'svelte-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-Svelte files\n if (!filename.endsWith('.svelte')) {\n return null;\n }\n\n // Dynamic import for MagicString\n let MagicString: new (code: string) => MagicStringType;\n\n try {\n const magicStringModule = await import('magic-string');\n MagicString = magicStringModule.default;\n } catch {\n console.warn(\n 'Svelte extraction: magic-string not found. Install it to enable Svelte content extraction.'\n );\n return null;\n }\n\n const magic = new MagicString(code);\n const extractedContent: ExtractedContent = {};\n const existingKeys = new Set<string>();\n const dictionaryKey = extractDictionaryKeyFromPath(filename);\n\n // Collect all replacements first, then apply them in reverse order\n // This prevents MagicString \"chunk already edited\" errors\n type Replacement = {\n start: number;\n end: number;\n replacement: string;\n key: string;\n value: string;\n };\n const replacements: Replacement[] = [];\n\n // Extract template content (everything outside <script> and <style> tags)\n // This regex-based approach works with both TypeScript and JavaScript files\n const scriptBlockRegex = /<script[^>]*>[\\s\\S]*?<\\/script>/gi;\n const styleBlockRegex = /<style[^>]*>[\\s\\S]*?<\\/style>/gi;\n\n // Get ranges of script and style blocks to skip\n const skipRanges: Array<{ start: number; end: number }> = [];\n\n // Find all script blocks\n const scriptMatches = code.matchAll(scriptBlockRegex);\n for (const match of scriptMatches) {\n if (match.index !== undefined) {\n skipRanges.push({\n start: match.index,\n end: match.index + match[0].length,\n });\n }\n }\n\n // Find all style blocks\n const styleMatches = code.matchAll(styleBlockRegex);\n for (const match of styleMatches) {\n if (match.index !== undefined) {\n skipRanges.push({\n start: match.index,\n end: match.index + match[0].length,\n });\n }\n }\n\n // Sort ranges by start position\n skipRanges.sort((a, b) => a.start - b.start);\n\n // Function to check if a position is within a skip range\n const isInSkipRange = (pos: number): boolean => {\n return skipRanges.some((range) => pos >= range.start && pos < range.end);\n };\n\n // Extract text content between HTML tags (but not inside script/style)\n // Match text that's between > and < (tag content)\n const textContentRegex = />([^<]+)</g;\n const textMatches = code.matchAll(textContentRegex);\n for (const match of textMatches) {\n if (match.index === undefined) continue;\n\n const textStart = match.index + 1; // Skip the >\n const text = match[1];\n const textEnd = textStart + text.length;\n\n // Skip if inside script or style block\n if (isInSkipRange(textStart)) {\n continue;\n }\n\n if (shouldExtract(text)) {\n const key = generateKey(text, existingKeys);\n existingKeys.add(key);\n const normalizedValue = text.replace(/\\s+/g, ' ').trim();\n // Collect replacement instead of applying immediately\n replacements.push({\n start: textStart,\n end: textEnd,\n replacement: `{$content.${key}}`,\n key,\n value: normalizedValue,\n });\n }\n }\n\n // Extract localizable attributes (title, placeholder, alt, aria-label, label)\n for (const attrName of ATTRIBUTES_TO_EXTRACT) {\n // Match attribute=\"value\" or attribute='value'\n const attrRegex = new RegExp(`(${attrName})=[\"']([^\"']+)[\"']`, 'gi');\n const attrMatches = code.matchAll(attrRegex);\n for (const match of attrMatches) {\n if (match.index === undefined) continue;\n\n const attrStart = match.index;\n const attrEnd = attrStart + match[0].length;\n const text = match[2];\n\n // Skip if inside script or style block\n if (isInSkipRange(attrStart)) {\n continue;\n }\n\n if (shouldExtract(text)) {\n const key = generateKey(text, existingKeys);\n existingKeys.add(key);\n // Collect replacement instead of applying immediately\n replacements.push({\n start: attrStart,\n end: attrEnd,\n replacement: `${attrName}={$content.${key}.value}`,\n key,\n value: text.trim(),\n });\n }\n }\n }\n\n // Sort replacements by start position in REVERSE order (end to start)\n // This ensures earlier edits don't affect the positions of later edits\n replacements.sort((a, b) => b.start - a.start);\n\n // Apply all replacements and collect extracted content\n for (const { start, end, replacement, key, value } of replacements) {\n magic.overwrite(start, end, replacement);\n extractedContent[key] = value;\n }\n\n // If nothing was extracted, return null\n if (Object.keys(extractedContent).length === 0) {\n return null;\n }\n\n // Find existing script tag\n const scriptRegex = /<script[^>]*>([\\s\\S]*?)<\\/script>/;\n const scriptMatch = scriptRegex.exec(code);\n const scriptContent = scriptMatch ? scriptMatch[1] : '';\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 (scriptMatch) {\n // Insert at the beginning of script content\n const scriptContentStart =\n scriptMatch.index + scriptMatch[0].indexOf('>') + 1;\n magic.appendLeft(scriptContentStart, injection);\n } else {\n // No script block, create one\n magic.prepend(`<script>\\n ${importStmt}\\n ${contentDecl}\\n</script>\\n\\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,IAAI,IAAI;AACR,SAAO,aAAa,IAAI,GAAG,MAAM,IAAI,CAAE;AACvC,QAAM,GAAG,MAAM;;AAEjB,QAAO;;;;;AAMT,MAAa,gCAAgC,aAA6B;CAExE,IAAI,WAAW,SAAS,UADZ,QAAQ,SAAS,CACS;AAEtC,KAAI,aAAa,QACf,YAAW,SAAS,QAAQ,SAAS,CAAC;AASxC,QAAO,QALK,SAC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AO,KAAK,aAAa,KAAK,WAAW,cAAc;AAClE,QAAM,UAAU,OAAO,KAAK,YAAY;AACxC,mBAAiB,OAAO;;AAI1B,KAAI,OAAO,KAAK,iBAAiB,CAAC,WAAW,EAC3C,QAAO;CAKT,MAAM,cADc,oCACY,KAAK,KAAK;CAC1C,MAAM,gBAAgB,cAAc,YAAY,KAAK;CAGrD,MAAM,uBACJ,2DAA2D,KACzD,cACD,IAAI,+CAA+C,KAAK,cAAc;CAGzE,MAAM,wBAAwB,yCAAyC,KACrE,cACD;AAGD,KAAI,wBAAwB,sBAC1B,QAAO;CAIT,MAAM,aAAa,uBACf,KACA,gCAAgC,YAAY;CAChD,MAAM,cAAc,wBAChB,KACA,gCAAgC,cAAc;CAGlD,MAAM,iBAAiB,CAAC,YAAY,YAAY,CAAC,OAAO,QAAQ;AAChE,KAAI,eAAe,WAAW,EAC5B,QAAO;CAET,MAAM,YAAY,OAAO,eAAe,KAAK,OAAO,CAAC;AAErD,KAAI,aAAa;EAEf,MAAM,qBACJ,YAAY,QAAQ,YAAY,GAAG,QAAQ,IAAI,GAAG;AACpD,QAAM,WAAW,oBAAoB,UAAU;OAG/C,OAAM,QAAQ,eAAe,WAAW,MAAM,YAAY,kBAAiB;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/SvelteIntlayerCompiler.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 Svelte compiler
37
+ */
38
+ type SvelteIntlayerCompilerOptions = {
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 SvelteIntlayerVitePlugin = {
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 SvelteIntlayerCompiler - A Vite-compatible compiler plugin for Svelte with Intlayer
75
+ *
76
+ * Handles Svelte components with special handling for:
77
+ * - Script blocks in .svelte files
78
+ * - TypeScript in Svelte files
79
+ * - Svelte-specific transformations
80
+ *
81
+ * @example
82
+ * ```ts
83
+ * // vite.config.ts
84
+ * import { defineConfig } from 'vite';
85
+ * import { svelte } from '@sveltejs/vite-plugin-svelte';
86
+ * import { svelteIntlayerCompiler } from '@intlayer/svelte-compiler';
87
+ *
88
+ * export default defineConfig({
89
+ * plugins: [svelte(), svelteIntlayerCompiler()],
90
+ * });
91
+ * ```
92
+ */
93
+ declare const createSvelteIntlayerCompiler: (options?: SvelteIntlayerCompilerOptions) => SvelteIntlayerVitePlugin;
94
+ /**
95
+ * Factory function for creating a Vite plugin
96
+ */
97
+ declare const svelteIntlayerCompiler: (options?: SvelteIntlayerCompilerOptions) => SvelteIntlayerVitePlugin;
98
+ declare const SvelteIntlayerCompiler: (options?: SvelteIntlayerCompilerOptions) => SvelteIntlayerVitePlugin;
99
+ //#endregion
100
+ export { CompilerMode, HotUpdateContext, SvelteIntlayerCompiler, SvelteIntlayerCompilerOptions, SvelteIntlayerVitePlugin, TransformResult, createSvelteIntlayerCompiler, svelteIntlayerCompiler };
101
+ //# sourceMappingURL=SvelteIntlayerCompiler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SvelteIntlayerCompiler.d.ts","names":[],"sources":["../../src/SvelteIntlayerCompiler.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;EA+UA,SAAA,EAAA,MAAA;AAOb,CAAA;;;;KAtZY,eAAA;;;;;;;KAQA,6BAAA;;;;kBAIM;;;;mBAKC,QAAQ;;;;;KAMf,wBAAA;;;;;;;;QAMJ;oBACY;yBACK;yBACA,qBAAqB;;;;;UAOrC,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;cAyBJ,yCACD,kCACT;;;;cA6UU,mCACD,kCACT;cAKU,mCArVD,kCACT"}
@@ -0,0 +1,3 @@
1
+ import { CompilerMode, HotUpdateContext, SvelteIntlayerCompiler, SvelteIntlayerCompilerOptions, SvelteIntlayerVitePlugin, TransformResult, createSvelteIntlayerCompiler, svelteIntlayerCompiler } from "./SvelteIntlayerCompiler.js";
2
+ import { ATTRIBUTES_TO_EXTRACT, ExtractPluginOptions, ExtractResult, ExtractedContent, defaultShouldExtract, extractDictionaryKeyFromPath, generateKey, intlayerSvelteExtract, shouldProcessFile } from "./svelte-intlayer-extract.js";
3
+ export { ATTRIBUTES_TO_EXTRACT, type CompilerMode, type ExtractPluginOptions, type ExtractResult, type ExtractedContent, type HotUpdateContext, SvelteIntlayerCompiler, type SvelteIntlayerCompilerOptions, type SvelteIntlayerVitePlugin, type TransformResult, createSvelteIntlayerCompiler, defaultShouldExtract, extractDictionaryKeyFromPath, generateKey, intlayerSvelteExtract, shouldProcessFile, svelteIntlayerCompiler };
@@ -0,0 +1,100 @@
1
+ //#region src/svelte-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 'svelte-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
+ * Svelte extraction plugin that extracts content and transforms Svelte components to use useIntlayer.
67
+ *
68
+ * This plugin:
69
+ * 1. Scans Svelte files for extractable text (template text, attributes)
70
+ * 2. Auto-injects useIntlayer import and store binding
71
+ * 3. Reports extracted content via onExtract callback (for the compiler to write dictionaries)
72
+ * 4. Replaces extractable strings with content references using Svelte's reactive `$` prefix
73
+ *
74
+ * ## Input
75
+ * ```svelte
76
+ * <h1>Hello World</h1>
77
+ * <p>Welcome to our app</p>
78
+ * ```
79
+ *
80
+ * ## Output
81
+ * ```svelte
82
+ * <script>
83
+ * import { useIntlayer } from 'svelte-intlayer';
84
+ * const content = useIntlayer('hello-world');
85
+ * </script>
86
+ * <h1>{$content.helloWorld}</h1>
87
+ * <p>{$content.welcomeToOurApp}</p>
88
+ * ```
89
+ *
90
+ * Note: Svelte uses reactive stores with `$` prefix for automatic subscription.
91
+ * The `useIntlayer` composable returns a Svelte store that can be accessed reactively.
92
+ */
93
+ declare const intlayerSvelteExtract: (code: string, filename: string, options?: ExtractPluginOptions) => Promise<{
94
+ code: string;
95
+ map?: unknown;
96
+ extracted: boolean;
97
+ } | null>;
98
+ //#endregion
99
+ export { ATTRIBUTES_TO_EXTRACT, ExtractPluginOptions, ExtractResult, ExtractedContent, defaultShouldExtract, extractDictionaryKeyFromPath, generateKey, intlayerSvelteExtract, shouldProcessFile };
100
+ //# sourceMappingURL=svelte-intlayer-extract.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"svelte-intlayer-extract.d.ts","names":[],"sources":["../../src/svelte-intlayer-extract.ts"],"sourcesContent":[],"mappings":";;AAOA;AAUA;AAKY,cAfC,qBAqBF,EAAA,MAAgB,EAAA;AAQf,KAnBA,gBAAA,GAAmB,MAmBC,CAwBT,MAAA,EAAA,MAAa,CAAA;AAQpC;AAeA;AAiCA;AAoBa,KAlHD,aAAA,GA+HX;EA8CY;;;;;WAvKF;;;;;;;KAQC,oBAAA;;;;;;;;;;;;;;;;;;;;;;;;uBAwBW;;;;;cAQV;;;;cAeA,0CAEG;;;;cA+BH;;;;cAoBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cA2DA,kEAGF,yBACR"}
package/package.json ADDED
@@ -0,0 +1,123 @@
1
+ {
2
+ "name": "@intlayer/svelte-compiler",
3
+ "version": "7.3.2-canary.0",
4
+ "private": false,
5
+ "description": "Vite-compatible compiler plugin for Svelte with Intlayer, providing HMR support, file transformation, and optimized dictionary loading for Svelte applications.",
6
+ "keywords": [
7
+ "intlayer",
8
+ "svelte",
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
+ "svelte": ">=4.0.0 || >=5.0.0",
107
+ "vite": ">=4.0.0"
108
+ },
109
+ "peerDependenciesMeta": {
110
+ "@babel/core": {
111
+ "optional": true
112
+ },
113
+ "svelte": {
114
+ "optional": true
115
+ }
116
+ },
117
+ "engines": {
118
+ "node": ">=14.18"
119
+ },
120
+ "bug": {
121
+ "url": "https://github.com/aymericzip/intlayer/issues"
122
+ }
123
+ }