@intlayer/babel 5.5.11 → 5.7.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 CHANGED
@@ -33,7 +33,7 @@
33
33
  <a href="https://www.facebook.com/intlayer" target="blank"><img align="center"
34
34
  src="https://img.shields.io/badge/facebook-4267B2.svg?style=for-the-badge&logo=facebook&logoColor=white"
35
35
  alt="Intlayer Facebook" height="30"/></a>
36
- <a href="https://www.instagram.com/intlayer_org/" target="blank"><img align="center"
36
+ <a href="https://www.instagram.com/intlayer/" target="blank"><img align="center"
37
37
  src="https://img.shields.io/badge/instagram-%23E4405F.svg?style=for-the-badge&logo=Instagram&logoColor=white"
38
38
  alt="Intlayer Instagram" height="30"/></a>
39
39
  <a href="https://x.com/Intlayer183096" target="blank"><img align="center"
@@ -57,12 +57,19 @@ const PACKAGE_LIST_DYNAMIC = [
57
57
  "next-intlayer",
58
58
  "next-intlayer/client",
59
59
  "next-intlayer/server",
60
- "preact-intlayer"
60
+ "preact-intlayer",
61
+ "vue-intlayer",
62
+ "solid-intlayer",
63
+ "svelte-intlayer",
64
+ "angular-intlayer"
61
65
  ];
62
66
  const STATIC_IMPORT_FUNCTION = {
63
67
  getIntlayer: "getDictionary",
64
68
  useIntlayer: "useDictionary"
65
69
  };
70
+ const ASYNC_IMPORT_FUNCTION = {
71
+ useIntlayer: "useDictionaryAsync"
72
+ };
66
73
  const DYNAMIC_IMPORT_FUNCTION = {
67
74
  useIntlayer: "useDictionaryDynamic"
68
75
  };
@@ -86,6 +93,7 @@ const intlayerBabelPlugin = () => {
86
93
  this._hasValidImport = false;
87
94
  this._isDictEntry = false;
88
95
  this._useDynamicHelpers = false;
96
+ this._useAsyncHelpers = false;
89
97
  const filename = this.file.opts.filename;
90
98
  if (this.opts.filesList && filename) {
91
99
  const isIncluded = this.opts.filesList.includes(filename);
@@ -174,15 +182,29 @@ const intlayerBabelPlugin = () => {
174
182
  for (const spec of path.node.specifiers) {
175
183
  if (!t.isImportSpecifier(spec)) continue;
176
184
  const importedName = t.isIdentifier(spec.imported) ? spec.imported.name : spec.imported.value;
177
- const activateDynamicImport = state.opts.activateDynamicImport;
178
- const shouldUseDynamicHelpers = activateDynamicImport && PACKAGE_LIST_DYNAMIC.includes(src);
185
+ const importMode = state.opts.importMode;
186
+ const shouldUseDynamicHelpers = importMode === "dynamic" && PACKAGE_LIST_DYNAMIC.includes(src);
187
+ const shouldUseAsyncHelpers = importMode === "async" && PACKAGE_LIST_DYNAMIC.includes(src);
179
188
  if (shouldUseDynamicHelpers) {
180
189
  state._useDynamicHelpers = true;
181
190
  }
182
- const helperMap = shouldUseDynamicHelpers ? {
183
- ...STATIC_IMPORT_FUNCTION,
184
- ...DYNAMIC_IMPORT_FUNCTION
185
- } : STATIC_IMPORT_FUNCTION;
191
+ if (shouldUseAsyncHelpers) {
192
+ state._useAsyncHelpers = true;
193
+ }
194
+ let helperMap;
195
+ if (shouldUseAsyncHelpers) {
196
+ helperMap = {
197
+ ...STATIC_IMPORT_FUNCTION,
198
+ ...ASYNC_IMPORT_FUNCTION
199
+ };
200
+ } else if (shouldUseDynamicHelpers) {
201
+ helperMap = {
202
+ ...STATIC_IMPORT_FUNCTION,
203
+ ...DYNAMIC_IMPORT_FUNCTION
204
+ };
205
+ } else {
206
+ helperMap = STATIC_IMPORT_FUNCTION;
207
+ }
186
208
  const newIdentifier = helperMap[importedName];
187
209
  if (newIdentifier) {
188
210
  spec.imported = t.identifier(newIdentifier);
@@ -200,9 +222,22 @@ const intlayerBabelPlugin = () => {
200
222
  if (!arg || !t.isStringLiteral(arg)) return;
201
223
  const key = arg.value;
202
224
  const useDynamic = Boolean(state._useDynamicHelpers);
225
+ const useAsync = Boolean(state._useAsyncHelpers);
203
226
  const shouldUseDynamicForThisCall = callee.name === "useIntlayer" && useDynamic;
227
+ const shouldUseAsyncForThisCall = callee.name === "useIntlayer" && useAsync;
204
228
  let ident;
205
- if (shouldUseDynamicForThisCall) {
229
+ if (shouldUseAsyncForThisCall) {
230
+ let dynamicIdent = state._newDynamicImports.get(key);
231
+ if (!dynamicIdent) {
232
+ const hash = (0, import_chokidar.getFileHash)(key);
233
+ dynamicIdent = t.identifier(`_${hash}_dyn`);
234
+ state._newDynamicImports.set(key, dynamicIdent);
235
+ }
236
+ ident = dynamicIdent;
237
+ path.node.arguments[0] = t.identifier(ident.name);
238
+ const awaitExpression = t.awaitExpression(path.node);
239
+ path.replaceWith(awaitExpression);
240
+ } else if (shouldUseDynamicForThisCall) {
206
241
  let dynamicIdent = state._newDynamicImports.get(key);
207
242
  if (!dynamicIdent) {
208
243
  const hash = (0, import_chokidar.getFileHash)(key);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/babel-plugin-intlayer.ts"],"sourcesContent":["import type { NodePath, PluginObj, PluginPass } from '@babel/core';\nimport * as t from '@babel/types';\nimport { getFileHash } from '@intlayer/chokidar';\nimport { dirname, join, relative } from 'node:path';\n\n/* ────────────────────────────────────────── constants ───────────────────── */\n\nconst PACKAGE_LIST = [\n 'intlayer',\n '@intlayer/core',\n 'react-intlayer',\n 'react-intlayer/client',\n 'react-intlayer/server',\n 'next-intlayer',\n 'next-intlayer/client',\n 'next-intlayer/server',\n 'svelte-intlayer',\n 'vue-intlayer',\n 'angular-intlayer',\n 'preact-intlayer',\n 'solid-intlayer',\n];\n\nconst CALLER_LIST = ['useIntlayer', 'getIntlayer'] as const;\n\n/**\n * Packages that support dynamic import\n */\nconst PACKAGE_LIST_DYNAMIC = [\n 'react-intlayer',\n 'react-intlayer/client',\n 'react-intlayer/server',\n 'next-intlayer',\n 'next-intlayer/client',\n 'next-intlayer/server',\n 'preact-intlayer',\n] as const;\n\nconst STATIC_IMPORT_FUNCTION = {\n getIntlayer: 'getDictionary',\n useIntlayer: 'useDictionary',\n} as const;\n\nconst DYNAMIC_IMPORT_FUNCTION = {\n useIntlayer: 'useDictionaryDynamic',\n} as const;\n\n/* ────────────────────────────────────────── types ───────────────────────── */\n\ntype State = PluginPass & {\n opts: {\n /**\n * The path to the dictionaries directory.\n */\n dictionariesDir: string;\n /**\n * The path to the dictionaries entry file.\n */\n dictionariesEntryPath: string;\n /**\n * The path to the dictionaries directory.\n */\n dynamicDictionariesDir: string;\n /**\n * The path to the dynamic dictionaries entry file.\n */\n dynamicDictionariesEntryPath: string;\n /**\n * If true, the plugin will replace the dictionary entry file with `export default {}`.\n */\n replaceDictionaryEntry?: boolean;\n /**\n * If true, the plugin will activate the dynamic import of the dictionaries.\n */\n activateDynamicImport?: boolean;\n /**\n * Files list to traverse.\n */\n filesList?: string[];\n };\n /** map key → generated ident (per-file) for static imports */\n _newStaticImports?: Map<string, t.Identifier>;\n /** map key → generated ident (per-file) for dynamic imports */\n _newDynamicImports?: Map<string, t.Identifier>;\n /** whether the current file imported *any* intlayer package */\n _hasValidImport?: boolean;\n /** whether the current file *is* the dictionaries entry file */\n _isDictEntry?: boolean;\n /** whether dynamic helpers are active for this file */\n _useDynamicHelpers?: boolean;\n};\n\n/* ────────────────────────────────────────── helpers ─────────────────────── */\n\n/**\n * Replicates the xxHash64 → Base-62 algorithm used by the SWC version\n * and prefixes an underscore so the generated identifiers never collide\n * with user-defined ones.\n */\nconst makeIdent = (key: string): t.Identifier => {\n const hash = getFileHash(key);\n return t.identifier(`_${hash}`);\n};\n\nconst computeRelativeImport = (\n fromFile: string,\n dictionariesDir: string,\n dynamicDictionariesDir: string,\n key: string,\n isDynamic = false\n): string => {\n const jsonPath = isDynamic\n ? join(dynamicDictionariesDir, `${key}.mjs`)\n : join(dictionariesDir, `${key}.json`);\n\n let rel = relative(dirname(fromFile), jsonPath).replace(/\\\\/g, '/'); // win →\n if (!rel.startsWith('./') && !rel.startsWith('../')) rel = `./${rel}`;\n return rel;\n};\n\n/* ────────────────────────────────────────── plugin ──────────────────────── */\n\n/**\n * Babel plugin that transforms `useIntlayer/getIntlayer` calls into\n * `useDictionary/getDictionary` and auto-imports the required JSON dictionaries.\n *\n *\n * This means cases like:\n *\n * ```ts\n * import { getIntlayer } from 'intlayer';\n * import { useIntlayer } from 'react-intlayer';\n *\n * // ...\n *\n * const content1 = getIntlayer('app');\n * const content2 = useIntlayer('app');\n * ```\n *\n * will be transformed into:\n *\n * ```ts\n * import _dicHash from '../../.intlayer/dictionaries/app.mjs';\n * import { getDictionary as getIntlayer } from 'intlayer';\n * import { useDictionaryDynamic as useIntlayer } from 'react-intlayer';\n *\n * // ...\n *\n * const content1 = getIntlayer(_dicHash);\n * const content2 = useIntlayer(_dicHash)\n * ```\n *\n * Or if the `activateDynamicImport` option is enabled:\n *\n * ```ts\n * import _dicHash from '../../.intlayer/dynamic_dictionaries/app.mjs';\n * import _dicHash_dyn from '../../.intlayer/dictionaries/app.mjs';\n *\n * import { useDictionary as getIntlayer } from 'intlayer';\n * import { useDictionaryDynamic as useIntlayer } from 'react-intlayer';\n *\n * // ...\n *\n * const content1 = getIntlayer(_dicHash);\n * const content2 = useIntlayer(_dicHash_dyn, 'app');\n * ```\n */\nexport const intlayerBabelPlugin = (): PluginObj<State> => {\n return {\n name: 'babel-plugin-intlayer-transform',\n\n pre() {\n this._newStaticImports = new Map();\n this._newDynamicImports = new Map();\n this._isIncluded = true;\n this._hasValidImport = false;\n this._isDictEntry = false;\n this._useDynamicHelpers = false;\n\n // If filesList is provided, check if current file is included\n const filename = this.file.opts.filename;\n if (this.opts.filesList && filename) {\n const isIncluded = this.opts.filesList.includes(filename);\n\n if (!isIncluded) {\n // Force _isIncluded to false to skip processing\n this._isIncluded = false;\n return;\n }\n }\n },\n\n visitor: {\n /* 0. If this file *is* the dictionaries entry, short-circuit: export {} */\n Program: {\n enter(programPath, state) {\n const filename = state.file.opts.filename!;\n if (\n state.opts.replaceDictionaryEntry &&\n filename === state.opts.dictionariesEntryPath\n ) {\n state._isDictEntry = true;\n // Replace all existing statements with: export default {}\n programPath.node.body = [\n t.exportDefaultDeclaration(t.objectExpression([])),\n ];\n // Stop further traversal for this plugin – nothing else to transform\n programPath.stop();\n }\n },\n\n /* 3. After full traversal, inject the JSON dictionary imports. */\n exit(programPath, state) {\n if (state._isDictEntry) return; // nothing else to do – already replaced\n if (!state._hasValidImport) return; // early-out if we touched nothing\n if (!state._isIncluded) return; // early-out if file is not included\n\n const file = state.file.opts.filename!;\n const dictionariesDir = state.opts.dictionariesDir;\n const dynamicDictionariesDir = state.opts.dynamicDictionariesDir;\n const imports: t.ImportDeclaration[] = [];\n\n // Generate static imports (for getIntlayer and useIntlayer when not using dynamic)\n for (const [key, ident] of state._newStaticImports!) {\n const rel = computeRelativeImport(\n file,\n dictionariesDir,\n dynamicDictionariesDir,\n key,\n false // Always static\n );\n const importDeclarationNode = t.importDeclaration(\n [t.importDefaultSpecifier(t.identifier(ident.name))],\n t.stringLiteral(rel)\n );\n\n importDeclarationNode.attributes = [\n t.importAttribute(t.identifier('type'), t.stringLiteral('json')),\n ];\n\n imports.push(importDeclarationNode);\n }\n\n // Generate dynamic imports (for useIntlayer when using dynamic helpers)\n for (const [key, ident] of state._newDynamicImports!) {\n const rel = computeRelativeImport(\n file,\n dictionariesDir,\n dynamicDictionariesDir,\n key,\n true // Always dynamic\n );\n imports.push(\n t.importDeclaration(\n [t.importDefaultSpecifier(t.identifier(ident.name))],\n t.stringLiteral(rel)\n )\n );\n }\n\n if (!imports.length) return;\n\n /* Keep \"use client\" / \"use server\" directives at the very top. */\n const bodyPaths = programPath.get('body') as NodePath<t.Statement>[];\n let insertPos = 0;\n for (const stmtPath of bodyPaths) {\n const stmt = stmtPath.node;\n if (\n t.isExpressionStatement(stmt) &&\n t.isStringLiteral(stmt.expression) &&\n !stmt.expression.value.startsWith('import') &&\n !stmt.expression.value.startsWith('require')\n ) {\n insertPos += 1;\n } else {\n break;\n }\n }\n\n programPath.node.body.splice(insertPos, 0, ...imports);\n },\n },\n\n /* 1. Inspect *every* intlayer import. */\n ImportDeclaration(path, state) {\n if (state._isDictEntry) return; // skip if entry file – already handled\n\n const src = path.node.source.value;\n if (!PACKAGE_LIST.includes(src)) return;\n\n // Mark that we do import from an intlayer package in this file; this is\n // enough to know that we *might* need to inject runtime helpers later.\n state._hasValidImport = true;\n\n for (const spec of path.node.specifiers) {\n if (!t.isImportSpecifier(spec)) continue;\n\n // ⚠️ We now key off *imported* name, *not* local name.\n const importedName = t.isIdentifier(spec.imported)\n ? spec.imported.name\n : (spec.imported as t.StringLiteral).value;\n\n const activateDynamicImport = state.opts.activateDynamicImport;\n // Determine whether this import should use the dynamic helpers. We\n // only switch to the dynamic helpers when (1) the option is turned\n // on AND (2) the package we are importing from supports the dynamic\n // helpers.\n const shouldUseDynamicHelpers =\n activateDynamicImport && PACKAGE_LIST_DYNAMIC.includes(src as any);\n\n // Remember for later (CallExpression) whether we are using the dynamic helpers\n if (shouldUseDynamicHelpers) {\n state._useDynamicHelpers = true;\n }\n\n const helperMap = shouldUseDynamicHelpers\n ? ({\n ...STATIC_IMPORT_FUNCTION,\n ...DYNAMIC_IMPORT_FUNCTION,\n } as Record<string, string>)\n : (STATIC_IMPORT_FUNCTION as Record<string, string>);\n\n const newIdentifier = helperMap[importedName];\n\n // Only rewrite when we actually have a mapping for the imported\n // specifier (ignore unrelated named imports).\n if (newIdentifier) {\n // Keep the local alias intact (so calls remain `useIntlayer` /\n // `getIntlayer`), but rewrite the imported identifier so it\n // points to our helper implementation.\n spec.imported = t.identifier(newIdentifier);\n }\n }\n },\n\n /* 2. Replace calls: useIntlayer(\"foo\") → useDictionary(_hash) or useDictionaryDynamic(_hash, \"foo\") */\n CallExpression(path, state) {\n if (state._isDictEntry) return; // skip if entry file – already handled\n\n const callee = path.node.callee;\n if (!t.isIdentifier(callee)) return;\n if (!CALLER_LIST.includes(callee.name as any)) return;\n\n // Ensure we ultimately emit helper imports for files that *invoke*\n // the hooks, even if they didn't import them directly (edge cases with\n // re-exports).\n state._hasValidImport = true;\n\n const arg = path.node.arguments[0];\n if (!arg || !t.isStringLiteral(arg)) return; // must be literal\n\n const key = arg.value;\n const useDynamic = Boolean(state._useDynamicHelpers);\n\n // Determine if this specific call should use dynamic imports\n const shouldUseDynamicForThisCall =\n callee.name === 'useIntlayer' && useDynamic;\n\n let ident: t.Identifier;\n\n if (shouldUseDynamicForThisCall) {\n // Use dynamic imports for useIntlayer when dynamic helpers are enabled\n let dynamicIdent = state._newDynamicImports!.get(key);\n if (!dynamicIdent) {\n // Create a unique identifier for dynamic imports by appending a suffix\n const hash = getFileHash(key);\n dynamicIdent = t.identifier(`_${hash}_dyn`);\n state._newDynamicImports!.set(key, dynamicIdent);\n }\n ident = dynamicIdent;\n\n // Dynamic helper: first argument is the dictionary, second is the key.\n path.node.arguments = [\n t.identifier(ident.name),\n ...path.node.arguments,\n ];\n } else {\n // Use static imports for getIntlayer or useIntlayer when not using dynamic helpers\n let staticIdent = state._newStaticImports!.get(key);\n if (!staticIdent) {\n staticIdent = makeIdent(key);\n state._newStaticImports!.set(key, staticIdent);\n }\n ident = staticIdent;\n\n // Static helper (useDictionary / getDictionary): replace key with ident.\n path.node.arguments[0] = t.identifier(ident.name);\n }\n },\n },\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,QAAmB;AACnB,sBAA4B;AAC5B,uBAAwC;AAIxC,MAAM,eAAe;AAAA,EACnB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,MAAM,cAAc,CAAC,eAAe,aAAa;AAKjD,MAAM,uBAAuB;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,MAAM,yBAAyB;AAAA,EAC7B,aAAa;AAAA,EACb,aAAa;AACf;AAEA,MAAM,0BAA0B;AAAA,EAC9B,aAAa;AACf;AAsDA,MAAM,YAAY,CAAC,QAA8B;AAC/C,QAAM,WAAO,6BAAY,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I,eAAe,MAAM,mBAAoB,IAAI,GAAG;AACpD,cAAI,CAAC,cAAc;AAEjB,kBAAM,WAAO,6BAAY,GAAG;AAC5B,2BAAe,EAAE,WAAW,IAAI,IAAI,MAAM;AAC1C,kBAAM,mBAAoB,IAAI,KAAK,YAAY;AAAA,UACjD;AACA,kBAAQ;AAGR,eAAK,KAAK,YAAY;AAAA,YACpB,EAAE,WAAW,MAAM,IAAI;AAAA,YACvB,GAAG,KAAK,KAAK;AAAA,UACf;AAAA,QACF,OAAO;AAEL,cAAI,cAAc,MAAM,kBAAmB,IAAI,GAAG;AAClD,cAAI,CAAC,aAAa;AAChB,0BAAc,UAAU,GAAG;AAC3B,kBAAM,kBAAmB,IAAI,KAAK,WAAW;AAAA,UAC/C;AACA,kBAAQ;AAGR,eAAK,KAAK,UAAU,CAAC,IAAI,EAAE,WAAW,MAAM,IAAI;AAAA,QAClD;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/babel-plugin-intlayer.ts"],"sourcesContent":["import type { NodePath, PluginObj, PluginPass } from '@babel/core';\nimport * as t from '@babel/types';\nimport { getFileHash } from '@intlayer/chokidar';\nimport { dirname, join, relative } from 'node:path';\n\n/* ────────────────────────────────────────── constants ───────────────────── */\n\nconst PACKAGE_LIST = [\n 'intlayer',\n '@intlayer/core',\n 'react-intlayer',\n 'react-intlayer/client',\n 'react-intlayer/server',\n 'next-intlayer',\n 'next-intlayer/client',\n 'next-intlayer/server',\n 'svelte-intlayer',\n 'vue-intlayer',\n 'angular-intlayer',\n 'preact-intlayer',\n 'solid-intlayer',\n];\n\nconst CALLER_LIST = ['useIntlayer', 'getIntlayer'] as const;\n\n/**\n * Packages that support dynamic import\n */\nconst PACKAGE_LIST_DYNAMIC = [\n 'react-intlayer',\n 'react-intlayer/client',\n 'react-intlayer/server',\n 'next-intlayer',\n 'next-intlayer/client',\n 'next-intlayer/server',\n 'preact-intlayer',\n 'vue-intlayer',\n 'solid-intlayer',\n 'svelte-intlayer',\n 'angular-intlayer',\n] as const;\n\nconst STATIC_IMPORT_FUNCTION = {\n getIntlayer: 'getDictionary',\n useIntlayer: 'useDictionary',\n} as const;\n\nconst ASYNC_IMPORT_FUNCTION = {\n useIntlayer: 'useDictionaryAsync',\n} as const;\n\nconst DYNAMIC_IMPORT_FUNCTION = {\n useIntlayer: 'useDictionaryDynamic',\n} as const;\n\n/* ────────────────────────────────────────── types ───────────────────────── */\n\ntype State = PluginPass & {\n opts: {\n /**\n * The path to the dictionaries directory.\n */\n dictionariesDir: string;\n /**\n * The path to the dictionaries entry file.\n */\n dictionariesEntryPath: string;\n /**\n * The path to the dictionaries directory.\n */\n dynamicDictionariesDir: string;\n /**\n * The path to the dynamic dictionaries entry file.\n */\n dynamicDictionariesEntryPath: string;\n /**\n * If true, the plugin will replace the dictionary entry file with `export default {}`.\n */\n replaceDictionaryEntry?: boolean;\n /**\n * If true, the plugin will activate the dynamic import of the dictionaries. It will rely on Suspense to load the dictionaries.\n */\n importMode?: 'static' | 'dynamic' | 'async';\n /**\n * Files list to traverse.\n */\n filesList?: string[];\n };\n /** map key → generated ident (per-file) for static imports */\n _newStaticImports?: Map<string, t.Identifier>;\n /** map key → generated ident (per-file) for dynamic imports */\n _newDynamicImports?: Map<string, t.Identifier>;\n /** whether the current file imported *any* intlayer package */\n _hasValidImport?: boolean;\n /** whether the current file *is* the dictionaries entry file */\n _isDictEntry?: boolean;\n /** whether dynamic helpers are active for this file */\n _useDynamicHelpers?: boolean;\n /** whether async helpers are active for this file */\n _useAsyncHelpers?: boolean;\n};\n\n/* ────────────────────────────────────────── helpers ─────────────────────── */\n\n/**\n * Replicates the xxHash64 → Base-62 algorithm used by the SWC version\n * and prefixes an underscore so the generated identifiers never collide\n * with user-defined ones.\n */\nconst makeIdent = (key: string): t.Identifier => {\n const hash = getFileHash(key);\n return t.identifier(`_${hash}`);\n};\n\nconst computeRelativeImport = (\n fromFile: string,\n dictionariesDir: string,\n dynamicDictionariesDir: string,\n key: string,\n isDynamic = false\n): string => {\n const jsonPath = isDynamic\n ? join(dynamicDictionariesDir, `${key}.mjs`)\n : join(dictionariesDir, `${key}.json`);\n\n let rel = relative(dirname(fromFile), jsonPath).replace(/\\\\/g, '/'); // win →\n if (!rel.startsWith('./') && !rel.startsWith('../')) rel = `./${rel}`;\n return rel;\n};\n\n/* ────────────────────────────────────────── plugin ──────────────────────── */\n\n/**\n * Babel plugin that transforms `useIntlayer/getIntlayer` calls into\n * `useDictionary/getDictionary` and auto-imports the required JSON dictionaries.\n *\n *\n * This means cases like:\n *\n * ```ts\n * import { getIntlayer } from 'intlayer';\n * import { useIntlayer } from 'react-intlayer';\n *\n * // ...\n *\n * const content1 = getIntlayer('app');\n * const content2 = useIntlayer('app');\n * ```\n *\n * will be transformed into:\n *\n * ```ts\n * import _dicHash from '../../.intlayer/dictionaries/app.mjs';\n * import { getDictionary as getIntlayer } from 'intlayer';\n * import { useDictionaryDynamic as useIntlayer } from 'react-intlayer';\n *\n * // ...\n *\n * const content1 = getIntlayer(_dicHash);\n * const content2 = useIntlayer(_dicHash)\n * ```\n *\n * If the `importMode = \"dynamic\"` option:\n *\n * ```ts\n * import _dicHash from '../../.intlayer/dynamic_dictionaries/app.mjs';\n * import _dicHash_dyn from '../../.intlayer/dictionaries/app.mjs';\n *\n * import { useDictionary as getIntlayer } from 'intlayer';\n * import { useDictionaryDynamic as useIntlayer } from 'react-intlayer';\n *\n * // ...\n *\n * const content1 = getIntlayer(_dicHash);\n * const content2 = useIntlayer(_dicHash_dyn, 'app');\n * ```\n *\n * Or if the `importMode = \"async\"` option:\n *\n * ```ts\n * import _dicHash from '../../.intlayer/dictionaries/app.mjs';\n * import { useDictionaryAsync as useIntlayer } from 'react-intlayer';\n *\n * // ...\n *\n * const content1 = getIntlayer(_dicHash);\n * const content2 = await useIntlayer(_dicHash_dyn);\n * ```\n */\nexport const intlayerBabelPlugin = (): PluginObj<State> => {\n return {\n name: 'babel-plugin-intlayer-transform',\n\n pre() {\n this._newStaticImports = new Map();\n this._newDynamicImports = new Map();\n this._isIncluded = true;\n this._hasValidImport = false;\n this._isDictEntry = false;\n this._useDynamicHelpers = false;\n this._useAsyncHelpers = false;\n\n // If filesList is provided, check if current file is included\n const filename = this.file.opts.filename;\n if (this.opts.filesList && filename) {\n const isIncluded = this.opts.filesList.includes(filename);\n\n if (!isIncluded) {\n // Force _isIncluded to false to skip processing\n this._isIncluded = false;\n return;\n }\n }\n },\n\n visitor: {\n /* 0. If this file *is* the dictionaries entry, short-circuit: export {} */\n Program: {\n enter(programPath, state) {\n const filename = state.file.opts.filename!;\n if (\n state.opts.replaceDictionaryEntry &&\n filename === state.opts.dictionariesEntryPath\n ) {\n state._isDictEntry = true;\n // Replace all existing statements with: export default {}\n programPath.node.body = [\n t.exportDefaultDeclaration(t.objectExpression([])),\n ];\n // Stop further traversal for this plugin – nothing else to transform\n programPath.stop();\n }\n },\n\n /* 3. After full traversal, inject the JSON dictionary imports. */\n exit(programPath, state) {\n if (state._isDictEntry) return; // nothing else to do – already replaced\n if (!state._hasValidImport) return; // early-out if we touched nothing\n if (!state._isIncluded) return; // early-out if file is not included\n\n const file = state.file.opts.filename!;\n const dictionariesDir = state.opts.dictionariesDir;\n const dynamicDictionariesDir = state.opts.dynamicDictionariesDir;\n const imports: t.ImportDeclaration[] = [];\n\n // Generate static imports (for getIntlayer and useIntlayer when not using dynamic)\n for (const [key, ident] of state._newStaticImports!) {\n const rel = computeRelativeImport(\n file,\n dictionariesDir,\n dynamicDictionariesDir,\n key,\n false // Always static\n );\n const importDeclarationNode = t.importDeclaration(\n [t.importDefaultSpecifier(t.identifier(ident.name))],\n t.stringLiteral(rel)\n );\n\n importDeclarationNode.attributes = [\n t.importAttribute(t.identifier('type'), t.stringLiteral('json')),\n ];\n\n imports.push(importDeclarationNode);\n }\n\n // Generate dynamic imports (for useIntlayer when using dynamic helpers)\n for (const [key, ident] of state._newDynamicImports!) {\n const rel = computeRelativeImport(\n file,\n dictionariesDir,\n dynamicDictionariesDir,\n key,\n true // Always dynamic\n );\n imports.push(\n t.importDeclaration(\n [t.importDefaultSpecifier(t.identifier(ident.name))],\n t.stringLiteral(rel)\n )\n );\n }\n\n if (!imports.length) return;\n\n /* Keep \"use client\" / \"use server\" directives at the very top. */\n const bodyPaths = programPath.get('body') as NodePath<t.Statement>[];\n let insertPos = 0;\n for (const stmtPath of bodyPaths) {\n const stmt = stmtPath.node;\n if (\n t.isExpressionStatement(stmt) &&\n t.isStringLiteral(stmt.expression) &&\n !stmt.expression.value.startsWith('import') &&\n !stmt.expression.value.startsWith('require')\n ) {\n insertPos += 1;\n } else {\n break;\n }\n }\n\n programPath.node.body.splice(insertPos, 0, ...imports);\n },\n },\n\n /* 1. Inspect *every* intlayer import. */\n ImportDeclaration(path, state) {\n if (state._isDictEntry) return; // skip if entry file – already handled\n\n const src = path.node.source.value;\n if (!PACKAGE_LIST.includes(src)) return;\n\n // Mark that we do import from an intlayer package in this file; this is\n // enough to know that we *might* need to inject runtime helpers later.\n state._hasValidImport = true;\n\n for (const spec of path.node.specifiers) {\n if (!t.isImportSpecifier(spec)) continue;\n\n // ⚠️ We now key off *imported* name, *not* local name.\n const importedName = t.isIdentifier(spec.imported)\n ? spec.imported.name\n : (spec.imported as t.StringLiteral).value;\n\n const importMode = state.opts.importMode;\n // Determine whether this import should use the dynamic helpers. We\n // only switch to the dynamic helpers when (1) the option is turned\n // on AND (2) the package we are importing from supports the dynamic\n // helpers.\n const shouldUseDynamicHelpers =\n importMode === 'dynamic' &&\n PACKAGE_LIST_DYNAMIC.includes(src as any);\n\n // Determine whether this import should use the async helpers. We\n // only switch to the async helpers when (1) the option is turned\n // on AND (2) the package we are importing from supports the async\n // helpers.\n const shouldUseAsyncHelpers =\n importMode === 'async' && PACKAGE_LIST_DYNAMIC.includes(src as any);\n\n // Remember for later (CallExpression) whether we are using the dynamic helpers\n if (shouldUseDynamicHelpers) {\n state._useDynamicHelpers = true;\n }\n\n // Remember for later (CallExpression) whether we are using the async helpers\n if (shouldUseAsyncHelpers) {\n state._useAsyncHelpers = true;\n }\n\n let helperMap: Record<string, string>;\n\n if (shouldUseAsyncHelpers) {\n // Use async helpers for useIntlayer when async mode is enabled\n helperMap = {\n ...STATIC_IMPORT_FUNCTION,\n ...ASYNC_IMPORT_FUNCTION,\n } as Record<string, string>;\n } else if (shouldUseDynamicHelpers) {\n // Use dynamic helpers for useIntlayer when dynamic mode is enabled\n helperMap = {\n ...STATIC_IMPORT_FUNCTION,\n ...DYNAMIC_IMPORT_FUNCTION,\n } as Record<string, string>;\n } else {\n // Use static helpers by default\n helperMap = STATIC_IMPORT_FUNCTION as Record<string, string>;\n }\n\n const newIdentifier = helperMap[importedName];\n\n // Only rewrite when we actually have a mapping for the imported\n // specifier (ignore unrelated named imports).\n if (newIdentifier) {\n // Keep the local alias intact (so calls remain `useIntlayer` /\n // `getIntlayer`), but rewrite the imported identifier so it\n // points to our helper implementation.\n spec.imported = t.identifier(newIdentifier);\n }\n }\n },\n\n /* 2. Replace calls: useIntlayer(\"foo\") → useDictionary(_hash) or useDictionaryDynamic(_hash, \"foo\") */\n CallExpression(path, state) {\n if (state._isDictEntry) return; // skip if entry file – already handled\n\n const callee = path.node.callee;\n if (!t.isIdentifier(callee)) return;\n if (!CALLER_LIST.includes(callee.name as any)) return;\n\n // Ensure we ultimately emit helper imports for files that *invoke*\n // the hooks, even if they didn't import them directly (edge cases with\n // re-exports).\n state._hasValidImport = true;\n\n const arg = path.node.arguments[0];\n if (!arg || !t.isStringLiteral(arg)) return; // must be literal\n\n const key = arg.value;\n const useDynamic = Boolean(state._useDynamicHelpers);\n const useAsync = Boolean(state._useAsyncHelpers);\n\n // Determine if this specific call should use dynamic imports\n const shouldUseDynamicForThisCall =\n callee.name === 'useIntlayer' && useDynamic;\n\n // Determine if this specific call should use async imports\n const shouldUseAsyncForThisCall =\n callee.name === 'useIntlayer' && useAsync;\n\n let ident: t.Identifier;\n\n if (shouldUseAsyncForThisCall) {\n // Use dynamic imports for useIntlayer when async helpers are enabled\n let dynamicIdent = state._newDynamicImports!.get(key);\n if (!dynamicIdent) {\n // Create a unique identifier for dynamic imports by appending a suffix\n const hash = getFileHash(key);\n dynamicIdent = t.identifier(`_${hash}_dyn`);\n state._newDynamicImports!.set(key, dynamicIdent);\n }\n ident = dynamicIdent;\n\n // Async helper: first argument is the dictionary promise.\n path.node.arguments[0] = t.identifier(ident.name);\n\n // Wrap the call with await for async helpers\n const awaitExpression = t.awaitExpression(path.node);\n path.replaceWith(awaitExpression);\n } else if (shouldUseDynamicForThisCall) {\n // Use dynamic imports for useIntlayer when dynamic helpers are enabled\n let dynamicIdent = state._newDynamicImports!.get(key);\n if (!dynamicIdent) {\n // Create a unique identifier for dynamic imports by appending a suffix\n const hash = getFileHash(key);\n dynamicIdent = t.identifier(`_${hash}_dyn`);\n state._newDynamicImports!.set(key, dynamicIdent);\n }\n ident = dynamicIdent;\n\n // Dynamic helper: first argument is the dictionary, second is the key.\n path.node.arguments = [\n t.identifier(ident.name),\n ...path.node.arguments,\n ];\n } else {\n // Use static imports for getIntlayer or useIntlayer when not using dynamic helpers\n let staticIdent = state._newStaticImports!.get(key);\n if (!staticIdent) {\n staticIdent = makeIdent(key);\n state._newStaticImports!.set(key, staticIdent);\n }\n ident = staticIdent;\n\n // Static helper (useDictionary / getDictionary): replace key with ident.\n path.node.arguments[0] = t.identifier(ident.name);\n }\n },\n },\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,QAAmB;AACnB,sBAA4B;AAC5B,uBAAwC;AAIxC,MAAM,eAAe;AAAA,EACnB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,MAAM,cAAc,CAAC,eAAe,aAAa;AAKjD,MAAM,uBAAuB;AAAA,E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Y;AAAA,UACjD;AACA,kBAAQ;AAGR,eAAK,KAAK,YAAY;AAAA,YACpB,EAAE,WAAW,MAAM,IAAI;AAAA,YACvB,GAAG,KAAK,KAAK;AAAA,UACf;AAAA,QACF,OAAO;AAEL,cAAI,cAAc,MAAM,kBAAmB,IAAI,GAAG;AAClD,cAAI,CAAC,aAAa;AAChB,0BAAc,UAAU,GAAG;AAC3B,kBAAM,kBAAmB,IAAI,KAAK,WAAW;AAAA,UAC/C;AACA,kBAAQ;AAGR,eAAK,KAAK,UAAU,CAAC,IAAI,EAAE,WAAW,MAAM,IAAI;AAAA,QAClD;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
@@ -24,12 +24,19 @@ const PACKAGE_LIST_DYNAMIC = [
24
24
  "next-intlayer",
25
25
  "next-intlayer/client",
26
26
  "next-intlayer/server",
27
- "preact-intlayer"
27
+ "preact-intlayer",
28
+ "vue-intlayer",
29
+ "solid-intlayer",
30
+ "svelte-intlayer",
31
+ "angular-intlayer"
28
32
  ];
29
33
  const STATIC_IMPORT_FUNCTION = {
30
34
  getIntlayer: "getDictionary",
31
35
  useIntlayer: "useDictionary"
32
36
  };
37
+ const ASYNC_IMPORT_FUNCTION = {
38
+ useIntlayer: "useDictionaryAsync"
39
+ };
33
40
  const DYNAMIC_IMPORT_FUNCTION = {
34
41
  useIntlayer: "useDictionaryDynamic"
35
42
  };
@@ -53,6 +60,7 @@ const intlayerBabelPlugin = () => {
53
60
  this._hasValidImport = false;
54
61
  this._isDictEntry = false;
55
62
  this._useDynamicHelpers = false;
63
+ this._useAsyncHelpers = false;
56
64
  const filename = this.file.opts.filename;
57
65
  if (this.opts.filesList && filename) {
58
66
  const isIncluded = this.opts.filesList.includes(filename);
@@ -141,15 +149,29 @@ const intlayerBabelPlugin = () => {
141
149
  for (const spec of path.node.specifiers) {
142
150
  if (!t.isImportSpecifier(spec)) continue;
143
151
  const importedName = t.isIdentifier(spec.imported) ? spec.imported.name : spec.imported.value;
144
- const activateDynamicImport = state.opts.activateDynamicImport;
145
- const shouldUseDynamicHelpers = activateDynamicImport && PACKAGE_LIST_DYNAMIC.includes(src);
152
+ const importMode = state.opts.importMode;
153
+ const shouldUseDynamicHelpers = importMode === "dynamic" && PACKAGE_LIST_DYNAMIC.includes(src);
154
+ const shouldUseAsyncHelpers = importMode === "async" && PACKAGE_LIST_DYNAMIC.includes(src);
146
155
  if (shouldUseDynamicHelpers) {
147
156
  state._useDynamicHelpers = true;
148
157
  }
149
- const helperMap = shouldUseDynamicHelpers ? {
150
- ...STATIC_IMPORT_FUNCTION,
151
- ...DYNAMIC_IMPORT_FUNCTION
152
- } : STATIC_IMPORT_FUNCTION;
158
+ if (shouldUseAsyncHelpers) {
159
+ state._useAsyncHelpers = true;
160
+ }
161
+ let helperMap;
162
+ if (shouldUseAsyncHelpers) {
163
+ helperMap = {
164
+ ...STATIC_IMPORT_FUNCTION,
165
+ ...ASYNC_IMPORT_FUNCTION
166
+ };
167
+ } else if (shouldUseDynamicHelpers) {
168
+ helperMap = {
169
+ ...STATIC_IMPORT_FUNCTION,
170
+ ...DYNAMIC_IMPORT_FUNCTION
171
+ };
172
+ } else {
173
+ helperMap = STATIC_IMPORT_FUNCTION;
174
+ }
153
175
  const newIdentifier = helperMap[importedName];
154
176
  if (newIdentifier) {
155
177
  spec.imported = t.identifier(newIdentifier);
@@ -167,9 +189,22 @@ const intlayerBabelPlugin = () => {
167
189
  if (!arg || !t.isStringLiteral(arg)) return;
168
190
  const key = arg.value;
169
191
  const useDynamic = Boolean(state._useDynamicHelpers);
192
+ const useAsync = Boolean(state._useAsyncHelpers);
170
193
  const shouldUseDynamicForThisCall = callee.name === "useIntlayer" && useDynamic;
194
+ const shouldUseAsyncForThisCall = callee.name === "useIntlayer" && useAsync;
171
195
  let ident;
172
- if (shouldUseDynamicForThisCall) {
196
+ if (shouldUseAsyncForThisCall) {
197
+ let dynamicIdent = state._newDynamicImports.get(key);
198
+ if (!dynamicIdent) {
199
+ const hash = getFileHash(key);
200
+ dynamicIdent = t.identifier(`_${hash}_dyn`);
201
+ state._newDynamicImports.set(key, dynamicIdent);
202
+ }
203
+ ident = dynamicIdent;
204
+ path.node.arguments[0] = t.identifier(ident.name);
205
+ const awaitExpression = t.awaitExpression(path.node);
206
+ path.replaceWith(awaitExpression);
207
+ } else if (shouldUseDynamicForThisCall) {
173
208
  let dynamicIdent = state._newDynamicImports.get(key);
174
209
  if (!dynamicIdent) {
175
210
  const hash = getFileHash(key);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/babel-plugin-intlayer.ts"],"sourcesContent":["import type { NodePath, PluginObj, PluginPass } from '@babel/core';\nimport * as t from '@babel/types';\nimport { getFileHash } from '@intlayer/chokidar';\nimport { dirname, join, relative } from 'node:path';\n\n/* ────────────────────────────────────────── constants ───────────────────── */\n\nconst PACKAGE_LIST = [\n 'intlayer',\n '@intlayer/core',\n 'react-intlayer',\n 'react-intlayer/client',\n 'react-intlayer/server',\n 'next-intlayer',\n 'next-intlayer/client',\n 'next-intlayer/server',\n 'svelte-intlayer',\n 'vue-intlayer',\n 'angular-intlayer',\n 'preact-intlayer',\n 'solid-intlayer',\n];\n\nconst CALLER_LIST = ['useIntlayer', 'getIntlayer'] as const;\n\n/**\n * Packages that support dynamic import\n */\nconst PACKAGE_LIST_DYNAMIC = [\n 'react-intlayer',\n 'react-intlayer/client',\n 'react-intlayer/server',\n 'next-intlayer',\n 'next-intlayer/client',\n 'next-intlayer/server',\n 'preact-intlayer',\n] as const;\n\nconst STATIC_IMPORT_FUNCTION = {\n getIntlayer: 'getDictionary',\n useIntlayer: 'useDictionary',\n} as const;\n\nconst DYNAMIC_IMPORT_FUNCTION = {\n useIntlayer: 'useDictionaryDynamic',\n} as const;\n\n/* ────────────────────────────────────────── types ───────────────────────── */\n\ntype State = PluginPass & {\n opts: {\n /**\n * The path to the dictionaries directory.\n */\n dictionariesDir: string;\n /**\n * The path to the dictionaries entry file.\n */\n dictionariesEntryPath: string;\n /**\n * The path to the dictionaries directory.\n */\n dynamicDictionariesDir: string;\n /**\n * The path to the dynamic dictionaries entry file.\n */\n dynamicDictionariesEntryPath: string;\n /**\n * If true, the plugin will replace the dictionary entry file with `export default {}`.\n */\n replaceDictionaryEntry?: boolean;\n /**\n * If true, the plugin will activate the dynamic import of the dictionaries.\n */\n activateDynamicImport?: boolean;\n /**\n * Files list to traverse.\n */\n filesList?: string[];\n };\n /** map key → generated ident (per-file) for static imports */\n _newStaticImports?: Map<string, t.Identifier>;\n /** map key → generated ident (per-file) for dynamic imports */\n _newDynamicImports?: Map<string, t.Identifier>;\n /** whether the current file imported *any* intlayer package */\n _hasValidImport?: boolean;\n /** whether the current file *is* the dictionaries entry file */\n _isDictEntry?: boolean;\n /** whether dynamic helpers are active for this file */\n _useDynamicHelpers?: boolean;\n};\n\n/* ────────────────────────────────────────── helpers ─────────────────────── */\n\n/**\n * Replicates the xxHash64 → Base-62 algorithm used by the SWC version\n * and prefixes an underscore so the generated identifiers never collide\n * with user-defined ones.\n */\nconst makeIdent = (key: string): t.Identifier => {\n const hash = getFileHash(key);\n return t.identifier(`_${hash}`);\n};\n\nconst computeRelativeImport = (\n fromFile: string,\n dictionariesDir: string,\n dynamicDictionariesDir: string,\n key: string,\n isDynamic = false\n): string => {\n const jsonPath = isDynamic\n ? join(dynamicDictionariesDir, `${key}.mjs`)\n : join(dictionariesDir, `${key}.json`);\n\n let rel = relative(dirname(fromFile), jsonPath).replace(/\\\\/g, '/'); // win →\n if (!rel.startsWith('./') && !rel.startsWith('../')) rel = `./${rel}`;\n return rel;\n};\n\n/* ────────────────────────────────────────── plugin ──────────────────────── */\n\n/**\n * Babel plugin that transforms `useIntlayer/getIntlayer` calls into\n * `useDictionary/getDictionary` and auto-imports the required JSON dictionaries.\n *\n *\n * This means cases like:\n *\n * ```ts\n * import { getIntlayer } from 'intlayer';\n * import { useIntlayer } from 'react-intlayer';\n *\n * // ...\n *\n * const content1 = getIntlayer('app');\n * const content2 = useIntlayer('app');\n * ```\n *\n * will be transformed into:\n *\n * ```ts\n * import _dicHash from '../../.intlayer/dictionaries/app.mjs';\n * import { getDictionary as getIntlayer } from 'intlayer';\n * import { useDictionaryDynamic as useIntlayer } from 'react-intlayer';\n *\n * // ...\n *\n * const content1 = getIntlayer(_dicHash);\n * const content2 = useIntlayer(_dicHash)\n * ```\n *\n * Or if the `activateDynamicImport` option is enabled:\n *\n * ```ts\n * import _dicHash from '../../.intlayer/dynamic_dictionaries/app.mjs';\n * import _dicHash_dyn from '../../.intlayer/dictionaries/app.mjs';\n *\n * import { useDictionary as getIntlayer } from 'intlayer';\n * import { useDictionaryDynamic as useIntlayer } from 'react-intlayer';\n *\n * // ...\n *\n * const content1 = getIntlayer(_dicHash);\n * const content2 = useIntlayer(_dicHash_dyn, 'app');\n * ```\n */\nexport const intlayerBabelPlugin = (): PluginObj<State> => {\n return {\n name: 'babel-plugin-intlayer-transform',\n\n pre() {\n this._newStaticImports = new Map();\n this._newDynamicImports = new Map();\n this._isIncluded = true;\n this._hasValidImport = false;\n this._isDictEntry = false;\n this._useDynamicHelpers = false;\n\n // If filesList is provided, check if current file is included\n const filename = this.file.opts.filename;\n if (this.opts.filesList && filename) {\n const isIncluded = this.opts.filesList.includes(filename);\n\n if (!isIncluded) {\n // Force _isIncluded to false to skip processing\n this._isIncluded = false;\n return;\n }\n }\n },\n\n visitor: {\n /* 0. If this file *is* the dictionaries entry, short-circuit: export {} */\n Program: {\n enter(programPath, state) {\n const filename = state.file.opts.filename!;\n if (\n state.opts.replaceDictionaryEntry &&\n filename === state.opts.dictionariesEntryPath\n ) {\n state._isDictEntry = true;\n // Replace all existing statements with: export default {}\n programPath.node.body = [\n t.exportDefaultDeclaration(t.objectExpression([])),\n ];\n // Stop further traversal for this plugin – nothing else to transform\n programPath.stop();\n }\n },\n\n /* 3. After full traversal, inject the JSON dictionary imports. */\n exit(programPath, state) {\n if (state._isDictEntry) return; // nothing else to do – already replaced\n if (!state._hasValidImport) return; // early-out if we touched nothing\n if (!state._isIncluded) return; // early-out if file is not included\n\n const file = state.file.opts.filename!;\n const dictionariesDir = state.opts.dictionariesDir;\n const dynamicDictionariesDir = state.opts.dynamicDictionariesDir;\n const imports: t.ImportDeclaration[] = [];\n\n // Generate static imports (for getIntlayer and useIntlayer when not using dynamic)\n for (const [key, ident] of state._newStaticImports!) {\n const rel = computeRelativeImport(\n file,\n dictionariesDir,\n dynamicDictionariesDir,\n key,\n false // Always static\n );\n const importDeclarationNode = t.importDeclaration(\n [t.importDefaultSpecifier(t.identifier(ident.name))],\n t.stringLiteral(rel)\n );\n\n importDeclarationNode.attributes = [\n t.importAttribute(t.identifier('type'), t.stringLiteral('json')),\n ];\n\n imports.push(importDeclarationNode);\n }\n\n // Generate dynamic imports (for useIntlayer when using dynamic helpers)\n for (const [key, ident] of state._newDynamicImports!) {\n const rel = computeRelativeImport(\n file,\n dictionariesDir,\n dynamicDictionariesDir,\n key,\n true // Always dynamic\n );\n imports.push(\n t.importDeclaration(\n [t.importDefaultSpecifier(t.identifier(ident.name))],\n t.stringLiteral(rel)\n )\n );\n }\n\n if (!imports.length) return;\n\n /* Keep \"use client\" / \"use server\" directives at the very top. */\n const bodyPaths = programPath.get('body') as NodePath<t.Statement>[];\n let insertPos = 0;\n for (const stmtPath of bodyPaths) {\n const stmt = stmtPath.node;\n if (\n t.isExpressionStatement(stmt) &&\n t.isStringLiteral(stmt.expression) &&\n !stmt.expression.value.startsWith('import') &&\n !stmt.expression.value.startsWith('require')\n ) {\n insertPos += 1;\n } else {\n break;\n }\n }\n\n programPath.node.body.splice(insertPos, 0, ...imports);\n },\n },\n\n /* 1. Inspect *every* intlayer import. */\n ImportDeclaration(path, state) {\n if (state._isDictEntry) return; // skip if entry file – already handled\n\n const src = path.node.source.value;\n if (!PACKAGE_LIST.includes(src)) return;\n\n // Mark that we do import from an intlayer package in this file; this is\n // enough to know that we *might* need to inject runtime helpers later.\n state._hasValidImport = true;\n\n for (const spec of path.node.specifiers) {\n if (!t.isImportSpecifier(spec)) continue;\n\n // ⚠️ We now key off *imported* name, *not* local name.\n const importedName = t.isIdentifier(spec.imported)\n ? spec.imported.name\n : (spec.imported as t.StringLiteral).value;\n\n const activateDynamicImport = state.opts.activateDynamicImport;\n // Determine whether this import should use the dynamic helpers. We\n // only switch to the dynamic helpers when (1) the option is turned\n // on AND (2) the package we are importing from supports the dynamic\n // helpers.\n const shouldUseDynamicHelpers =\n activateDynamicImport && PACKAGE_LIST_DYNAMIC.includes(src as any);\n\n // Remember for later (CallExpression) whether we are using the dynamic helpers\n if (shouldUseDynamicHelpers) {\n state._useDynamicHelpers = true;\n }\n\n const helperMap = shouldUseDynamicHelpers\n ? ({\n ...STATIC_IMPORT_FUNCTION,\n ...DYNAMIC_IMPORT_FUNCTION,\n } as Record<string, string>)\n : (STATIC_IMPORT_FUNCTION as Record<string, string>);\n\n const newIdentifier = helperMap[importedName];\n\n // Only rewrite when we actually have a mapping for the imported\n // specifier (ignore unrelated named imports).\n if (newIdentifier) {\n // Keep the local alias intact (so calls remain `useIntlayer` /\n // `getIntlayer`), but rewrite the imported identifier so it\n // points to our helper implementation.\n spec.imported = t.identifier(newIdentifier);\n }\n }\n },\n\n /* 2. Replace calls: useIntlayer(\"foo\") → useDictionary(_hash) or useDictionaryDynamic(_hash, \"foo\") */\n CallExpression(path, state) {\n if (state._isDictEntry) return; // skip if entry file – already handled\n\n const callee = path.node.callee;\n if (!t.isIdentifier(callee)) return;\n if (!CALLER_LIST.includes(callee.name as any)) return;\n\n // Ensure we ultimately emit helper imports for files that *invoke*\n // the hooks, even if they didn't import them directly (edge cases with\n // re-exports).\n state._hasValidImport = true;\n\n const arg = path.node.arguments[0];\n if (!arg || !t.isStringLiteral(arg)) return; // must be literal\n\n const key = arg.value;\n const useDynamic = Boolean(state._useDynamicHelpers);\n\n // Determine if this specific call should use dynamic imports\n const shouldUseDynamicForThisCall =\n callee.name === 'useIntlayer' && useDynamic;\n\n let ident: t.Identifier;\n\n if (shouldUseDynamicForThisCall) {\n // Use dynamic imports for useIntlayer when dynamic helpers are enabled\n let dynamicIdent = state._newDynamicImports!.get(key);\n if (!dynamicIdent) {\n // Create a unique identifier for dynamic imports by appending a suffix\n const hash = getFileHash(key);\n dynamicIdent = t.identifier(`_${hash}_dyn`);\n state._newDynamicImports!.set(key, dynamicIdent);\n }\n ident = dynamicIdent;\n\n // Dynamic helper: first argument is the dictionary, second is the key.\n path.node.arguments = [\n t.identifier(ident.name),\n ...path.node.arguments,\n ];\n } else {\n // Use static imports for getIntlayer or useIntlayer when not using dynamic helpers\n let staticIdent = state._newStaticImports!.get(key);\n if (!staticIdent) {\n staticIdent = makeIdent(key);\n state._newStaticImports!.set(key, staticIdent);\n }\n ident = staticIdent;\n\n // Static helper (useDictionary / getDictionary): replace key with ident.\n path.node.arguments[0] = t.identifier(ident.name);\n }\n },\n },\n };\n};\n"],"mappings":"AACA,YAAY,OAAO;AACnB,SAAS,mBAAmB;AAC5B,SAAS,SAAS,MAAM,gBAAgB;AAIxC,MAAM,eAAe;AAAA,EACnB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,MAAM,cAAc,CAAC,eAAe,aAAa;AAKjD,MAAM,uBAAuB;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,MAAM,yBAAyB;AAAA,EAC7B,aAAa;AAAA,EACb,aAAa;AACf;AAEA,MAAM,0BAA0B;AAAA,EAC9B,aAAa;AACf;AAsDA,MAAM,YAAY,CAAC,QAA8B;AAC/C,QAAM,OAAO,YAAY,GAAG;AAC5B,SAAO,EAAE,WAAW,IAAI,IAAI,EAAE;AAChC;AAEA,MAAM,wBAAwB,CAC5B,UACA,iBACA,wBACA,KACA,YAAY,UACD;AACX,QAAM,WAAW,YACb,KAAK,wBAAwB,GA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AAI,KAAK,YAAY;AAAA,UACjD;AACA,kBAAQ;AAGR,eAAK,KAAK,YAAY;AAAA,YACpB,EAAE,WAAW,MAAM,IAAI;AAAA,YACvB,GAAG,KAAK,KAAK;AAAA,UACf;AAAA,QACF,OAAO;AAEL,cAAI,cAAc,MAAM,kBAAmB,IAAI,GAAG;AAClD,cAAI,CAAC,aAAa;AAChB,0BAAc,UAAU,GAAG;AAC3B,kBAAM,kBAAmB,IAAI,KAAK,WAAW;AAAA,UAC/C;AACA,kBAAQ;AAGR,eAAK,KAAK,UAAU,CAAC,IAAI,EAAE,WAAW,MAAM,IAAI;AAAA,QAClD;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/babel-plugin-intlayer.ts"],"sourcesContent":["import type { NodePath, PluginObj, PluginPass } from '@babel/core';\nimport * as t from '@babel/types';\nimport { getFileHash } from '@intlayer/chokidar';\nimport { dirname, join, relative } from 'node:path';\n\n/* ────────────────────────────────────────── constants ───────────────────── */\n\nconst PACKAGE_LIST = [\n 'intlayer',\n '@intlayer/core',\n 'react-intlayer',\n 'react-intlayer/client',\n 'react-intlayer/server',\n 'next-intlayer',\n 'next-intlayer/client',\n 'next-intlayer/server',\n 'svelte-intlayer',\n 'vue-intlayer',\n 'angular-intlayer',\n 'preact-intlayer',\n 'solid-intlayer',\n];\n\nconst CALLER_LIST = ['useIntlayer', 'getIntlayer'] as const;\n\n/**\n * Packages that support dynamic import\n */\nconst PACKAGE_LIST_DYNAMIC = [\n 'react-intlayer',\n 'react-intlayer/client',\n 'react-intlayer/server',\n 'next-intlayer',\n 'next-intlayer/client',\n 'next-intlayer/server',\n 'preact-intlayer',\n 'vue-intlayer',\n 'solid-intlayer',\n 'svelte-intlayer',\n 'angular-intlayer',\n] as const;\n\nconst STATIC_IMPORT_FUNCTION = {\n getIntlayer: 'getDictionary',\n useIntlayer: 'useDictionary',\n} as const;\n\nconst ASYNC_IMPORT_FUNCTION = {\n useIntlayer: 'useDictionaryAsync',\n} as const;\n\nconst DYNAMIC_IMPORT_FUNCTION = {\n useIntlayer: 'useDictionaryDynamic',\n} as const;\n\n/* ────────────────────────────────────────── types ───────────────────────── */\n\ntype State = PluginPass & {\n opts: {\n /**\n * The path to the dictionaries directory.\n */\n dictionariesDir: string;\n /**\n * The path to the dictionaries entry file.\n */\n dictionariesEntryPath: string;\n /**\n * The path to the dictionaries directory.\n */\n dynamicDictionariesDir: string;\n /**\n * The path to the dynamic dictionaries entry file.\n */\n dynamicDictionariesEntryPath: string;\n /**\n * If true, the plugin will replace the dictionary entry file with `export default {}`.\n */\n replaceDictionaryEntry?: boolean;\n /**\n * If true, the plugin will activate the dynamic import of the dictionaries. It will rely on Suspense to load the dictionaries.\n */\n importMode?: 'static' | 'dynamic' | 'async';\n /**\n * Files list to traverse.\n */\n filesList?: string[];\n };\n /** map key → generated ident (per-file) for static imports */\n _newStaticImports?: Map<string, t.Identifier>;\n /** map key → generated ident (per-file) for dynamic imports */\n _newDynamicImports?: Map<string, t.Identifier>;\n /** whether the current file imported *any* intlayer package */\n _hasValidImport?: boolean;\n /** whether the current file *is* the dictionaries entry file */\n _isDictEntry?: boolean;\n /** whether dynamic helpers are active for this file */\n _useDynamicHelpers?: boolean;\n /** whether async helpers are active for this file */\n _useAsyncHelpers?: boolean;\n};\n\n/* ────────────────────────────────────────── helpers ─────────────────────── */\n\n/**\n * Replicates the xxHash64 → Base-62 algorithm used by the SWC version\n * and prefixes an underscore so the generated identifiers never collide\n * with user-defined ones.\n */\nconst makeIdent = (key: string): t.Identifier => {\n const hash = getFileHash(key);\n return t.identifier(`_${hash}`);\n};\n\nconst computeRelativeImport = (\n fromFile: string,\n dictionariesDir: string,\n dynamicDictionariesDir: string,\n key: string,\n isDynamic = false\n): string => {\n const jsonPath = isDynamic\n ? join(dynamicDictionariesDir, `${key}.mjs`)\n : join(dictionariesDir, `${key}.json`);\n\n let rel = relative(dirname(fromFile), jsonPath).replace(/\\\\/g, '/'); // win →\n if (!rel.startsWith('./') && !rel.startsWith('../')) rel = `./${rel}`;\n return rel;\n};\n\n/* ────────────────────────────────────────── plugin ──────────────────────── */\n\n/**\n * Babel plugin that transforms `useIntlayer/getIntlayer` calls into\n * `useDictionary/getDictionary` and auto-imports the required JSON dictionaries.\n *\n *\n * This means cases like:\n *\n * ```ts\n * import { getIntlayer } from 'intlayer';\n * import { useIntlayer } from 'react-intlayer';\n *\n * // ...\n *\n * const content1 = getIntlayer('app');\n * const content2 = useIntlayer('app');\n * ```\n *\n * will be transformed into:\n *\n * ```ts\n * import _dicHash from '../../.intlayer/dictionaries/app.mjs';\n * import { getDictionary as getIntlayer } from 'intlayer';\n * import { useDictionaryDynamic as useIntlayer } from 'react-intlayer';\n *\n * // ...\n *\n * const content1 = getIntlayer(_dicHash);\n * const content2 = useIntlayer(_dicHash)\n * ```\n *\n * If the `importMode = \"dynamic\"` option:\n *\n * ```ts\n * import _dicHash from '../../.intlayer/dynamic_dictionaries/app.mjs';\n * import _dicHash_dyn from '../../.intlayer/dictionaries/app.mjs';\n *\n * import { useDictionary as getIntlayer } from 'intlayer';\n * import { useDictionaryDynamic as useIntlayer } from 'react-intlayer';\n *\n * // ...\n *\n * const content1 = getIntlayer(_dicHash);\n * const content2 = useIntlayer(_dicHash_dyn, 'app');\n * ```\n *\n * Or if the `importMode = \"async\"` option:\n *\n * ```ts\n * import _dicHash from '../../.intlayer/dictionaries/app.mjs';\n * import { useDictionaryAsync as useIntlayer } from 'react-intlayer';\n *\n * // ...\n *\n * const content1 = getIntlayer(_dicHash);\n * const content2 = await useIntlayer(_dicHash_dyn);\n * ```\n */\nexport const intlayerBabelPlugin = (): PluginObj<State> => {\n return {\n name: 'babel-plugin-intlayer-transform',\n\n pre() {\n this._newStaticImports = new Map();\n this._newDynamicImports = new Map();\n this._isIncluded = true;\n this._hasValidImport = false;\n this._isDictEntry = false;\n this._useDynamicHelpers = false;\n this._useAsyncHelpers = false;\n\n // If filesList is provided, check if current file is included\n const filename = this.file.opts.filename;\n if (this.opts.filesList && filename) {\n const isIncluded = this.opts.filesList.includes(filename);\n\n if (!isIncluded) {\n // Force _isIncluded to false to skip processing\n this._isIncluded = false;\n return;\n }\n }\n },\n\n visitor: {\n /* 0. If this file *is* the dictionaries entry, short-circuit: export {} */\n Program: {\n enter(programPath, state) {\n const filename = state.file.opts.filename!;\n if (\n state.opts.replaceDictionaryEntry &&\n filename === state.opts.dictionariesEntryPath\n ) {\n state._isDictEntry = true;\n // Replace all existing statements with: export default {}\n programPath.node.body = [\n t.exportDefaultDeclaration(t.objectExpression([])),\n ];\n // Stop further traversal for this plugin – nothing else to transform\n programPath.stop();\n }\n },\n\n /* 3. After full traversal, inject the JSON dictionary imports. */\n exit(programPath, state) {\n if (state._isDictEntry) return; // nothing else to do – already replaced\n if (!state._hasValidImport) return; // early-out if we touched nothing\n if (!state._isIncluded) return; // early-out if file is not included\n\n const file = state.file.opts.filename!;\n const dictionariesDir = state.opts.dictionariesDir;\n const dynamicDictionariesDir = state.opts.dynamicDictionariesDir;\n const imports: t.ImportDeclaration[] = [];\n\n // Generate static imports (for getIntlayer and useIntlayer when not using dynamic)\n for (const [key, ident] of state._newStaticImports!) {\n const rel = computeRelativeImport(\n file,\n dictionariesDir,\n dynamicDictionariesDir,\n key,\n false // Always static\n );\n const importDeclarationNode = t.importDeclaration(\n [t.importDefaultSpecifier(t.identifier(ident.name))],\n t.stringLiteral(rel)\n );\n\n importDeclarationNode.attributes = [\n t.importAttribute(t.identifier('type'), t.stringLiteral('json')),\n ];\n\n imports.push(importDeclarationNode);\n }\n\n // Generate dynamic imports (for useIntlayer when using dynamic helpers)\n for (const [key, ident] of state._newDynamicImports!) {\n const rel = computeRelativeImport(\n file,\n dictionariesDir,\n dynamicDictionariesDir,\n key,\n true // Always dynamic\n );\n imports.push(\n t.importDeclaration(\n [t.importDefaultSpecifier(t.identifier(ident.name))],\n t.stringLiteral(rel)\n )\n );\n }\n\n if (!imports.length) return;\n\n /* Keep \"use client\" / \"use server\" directives at the very top. */\n const bodyPaths = programPath.get('body') as NodePath<t.Statement>[];\n let insertPos = 0;\n for (const stmtPath of bodyPaths) {\n const stmt = stmtPath.node;\n if (\n t.isExpressionStatement(stmt) &&\n t.isStringLiteral(stmt.expression) &&\n !stmt.expression.value.startsWith('import') &&\n !stmt.expression.value.startsWith('require')\n ) {\n insertPos += 1;\n } else {\n break;\n }\n }\n\n programPath.node.body.splice(insertPos, 0, ...imports);\n },\n },\n\n /* 1. Inspect *every* intlayer import. */\n ImportDeclaration(path, state) {\n if (state._isDictEntry) return; // skip if entry file – already handled\n\n const src = path.node.source.value;\n if (!PACKAGE_LIST.includes(src)) return;\n\n // Mark that we do import from an intlayer package in this file; this is\n // enough to know that we *might* need to inject runtime helpers later.\n state._hasValidImport = true;\n\n for (const spec of path.node.specifiers) {\n if (!t.isImportSpecifier(spec)) continue;\n\n // ⚠️ We now key off *imported* name, *not* local name.\n const importedName = t.isIdentifier(spec.imported)\n ? spec.imported.name\n : (spec.imported as t.StringLiteral).value;\n\n const importMode = state.opts.importMode;\n // Determine whether this import should use the dynamic helpers. We\n // only switch to the dynamic helpers when (1) the option is turned\n // on AND (2) the package we are importing from supports the dynamic\n // helpers.\n const shouldUseDynamicHelpers =\n importMode === 'dynamic' &&\n PACKAGE_LIST_DYNAMIC.includes(src as any);\n\n // Determine whether this import should use the async helpers. We\n // only switch to the async helpers when (1) the option is turned\n // on AND (2) the package we are importing from supports the async\n // helpers.\n const shouldUseAsyncHelpers =\n importMode === 'async' && PACKAGE_LIST_DYNAMIC.includes(src as any);\n\n // Remember for later (CallExpression) whether we are using the dynamic helpers\n if (shouldUseDynamicHelpers) {\n state._useDynamicHelpers = true;\n }\n\n // Remember for later (CallExpression) whether we are using the async helpers\n if (shouldUseAsyncHelpers) {\n state._useAsyncHelpers = true;\n }\n\n let helperMap: Record<string, string>;\n\n if (shouldUseAsyncHelpers) {\n // Use async helpers for useIntlayer when async mode is enabled\n helperMap = {\n ...STATIC_IMPORT_FUNCTION,\n ...ASYNC_IMPORT_FUNCTION,\n } as Record<string, string>;\n } else if (shouldUseDynamicHelpers) {\n // Use dynamic helpers for useIntlayer when dynamic mode is enabled\n helperMap = {\n ...STATIC_IMPORT_FUNCTION,\n ...DYNAMIC_IMPORT_FUNCTION,\n } as Record<string, string>;\n } else {\n // Use static helpers by default\n helperMap = STATIC_IMPORT_FUNCTION as Record<string, string>;\n }\n\n const newIdentifier = helperMap[importedName];\n\n // Only rewrite when we actually have a mapping for the imported\n // specifier (ignore unrelated named imports).\n if (newIdentifier) {\n // Keep the local alias intact (so calls remain `useIntlayer` /\n // `getIntlayer`), but rewrite the imported identifier so it\n // points to our helper implementation.\n spec.imported = t.identifier(newIdentifier);\n }\n }\n },\n\n /* 2. Replace calls: useIntlayer(\"foo\") → useDictionary(_hash) or useDictionaryDynamic(_hash, \"foo\") */\n CallExpression(path, state) {\n if (state._isDictEntry) return; // skip if entry file – already handled\n\n const callee = path.node.callee;\n if (!t.isIdentifier(callee)) return;\n if (!CALLER_LIST.includes(callee.name as any)) return;\n\n // Ensure we ultimately emit helper imports for files that *invoke*\n // the hooks, even if they didn't import them directly (edge cases with\n // re-exports).\n state._hasValidImport = true;\n\n const arg = path.node.arguments[0];\n if (!arg || !t.isStringLiteral(arg)) return; // must be literal\n\n const key = arg.value;\n const useDynamic = Boolean(state._useDynamicHelpers);\n const useAsync = Boolean(state._useAsyncHelpers);\n\n // Determine if this specific call should use dynamic imports\n const shouldUseDynamicForThisCall =\n callee.name === 'useIntlayer' && useDynamic;\n\n // Determine if this specific call should use async imports\n const shouldUseAsyncForThisCall =\n callee.name === 'useIntlayer' && useAsync;\n\n let ident: t.Identifier;\n\n if (shouldUseAsyncForThisCall) {\n // Use dynamic imports for useIntlayer when async helpers are enabled\n let dynamicIdent = state._newDynamicImports!.get(key);\n if (!dynamicIdent) {\n // Create a unique identifier for dynamic imports by appending a suffix\n const hash = getFileHash(key);\n dynamicIdent = t.identifier(`_${hash}_dyn`);\n state._newDynamicImports!.set(key, dynamicIdent);\n }\n ident = dynamicIdent;\n\n // Async helper: first argument is the dictionary promise.\n path.node.arguments[0] = t.identifier(ident.name);\n\n // Wrap the call with await for async helpers\n const awaitExpression = t.awaitExpression(path.node);\n path.replaceWith(awaitExpression);\n } else if (shouldUseDynamicForThisCall) {\n // Use dynamic imports for useIntlayer when dynamic helpers are enabled\n let dynamicIdent = state._newDynamicImports!.get(key);\n if (!dynamicIdent) {\n // Create a unique identifier for dynamic imports by appending a suffix\n const hash = getFileHash(key);\n dynamicIdent = t.identifier(`_${hash}_dyn`);\n state._newDynamicImports!.set(key, dynamicIdent);\n }\n ident = dynamicIdent;\n\n // Dynamic helper: first argument is the dictionary, second is the key.\n path.node.arguments = [\n t.identifier(ident.name),\n ...path.node.arguments,\n ];\n } else {\n // Use static imports for getIntlayer or useIntlayer when not using dynamic helpers\n let staticIdent = state._newStaticImports!.get(key);\n if (!staticIdent) {\n staticIdent = makeIdent(key);\n state._newStaticImports!.set(key, staticIdent);\n }\n ident = staticIdent;\n\n // Static helper (useDictionary / getDictionary): replace key with ident.\n path.node.arguments[0] = t.identifier(ident.name);\n }\n },\n },\n };\n};\n"],"mappings":"AACA,YAAY,OAAO;AACnB,SAAS,mBAAmB;AAC5B,SAAS,SAAS,MAAM,gBAAgB;AAIxC,MAAM,eAAe;AAAA,EACnB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,MAAM,cAAc,CAAC,eAAe,aAAa;AAKjD,MAAM,uBAAuB;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,MAAM,yBAAyB;AAAA,EAC7B,aAAa;AAAA,EACb,aAAa;AACf;AAEA,MAAM,wB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AC,aAAa;AAChB,0BAAc,UAAU,GAAG;AAC3B,kBAAM,kBAAmB,IAAI,KAAK,WAAW;AAAA,UAC/C;AACA,kBAAQ;AAGR,eAAK,KAAK,UAAU,CAAC,IAAI,EAAE,WAAW,MAAM,IAAI;AAAA,QAClD;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
@@ -23,9 +23,9 @@ type State = PluginPass & {
23
23
  */
24
24
  replaceDictionaryEntry?: boolean;
25
25
  /**
26
- * If true, the plugin will activate the dynamic import of the dictionaries.
26
+ * If true, the plugin will activate the dynamic import of the dictionaries. It will rely on Suspense to load the dictionaries.
27
27
  */
28
- activateDynamicImport?: boolean;
28
+ importMode?: 'static' | 'dynamic' | 'async';
29
29
  /**
30
30
  * Files list to traverse.
31
31
  */
@@ -41,6 +41,8 @@ type State = PluginPass & {
41
41
  _isDictEntry?: boolean;
42
42
  /** whether dynamic helpers are active for this file */
43
43
  _useDynamicHelpers?: boolean;
44
+ /** whether async helpers are active for this file */
45
+ _useAsyncHelpers?: boolean;
44
46
  };
45
47
  /**
46
48
  * Babel plugin that transforms `useIntlayer/getIntlayer` calls into
@@ -72,7 +74,7 @@ type State = PluginPass & {
72
74
  * const content2 = useIntlayer(_dicHash)
73
75
  * ```
74
76
  *
75
- * Or if the `activateDynamicImport` option is enabled:
77
+ * If the `importMode = "dynamic"` option:
76
78
  *
77
79
  * ```ts
78
80
  * import _dicHash from '../../.intlayer/dynamic_dictionaries/app.mjs';
@@ -86,6 +88,18 @@ type State = PluginPass & {
86
88
  * const content1 = getIntlayer(_dicHash);
87
89
  * const content2 = useIntlayer(_dicHash_dyn, 'app');
88
90
  * ```
91
+ *
92
+ * Or if the `importMode = "async"` option:
93
+ *
94
+ * ```ts
95
+ * import _dicHash from '../../.intlayer/dictionaries/app.mjs';
96
+ * import { useDictionaryAsync as useIntlayer } from 'react-intlayer';
97
+ *
98
+ * // ...
99
+ *
100
+ * const content1 = getIntlayer(_dicHash);
101
+ * const content2 = await useIntlayer(_dicHash_dyn);
102
+ * ```
89
103
  */
90
104
  export declare const intlayerBabelPlugin: () => PluginObj<State>;
91
105
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"babel-plugin-intlayer.d.ts","sourceRoot":"","sources":["../../src/babel-plugin-intlayer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAY,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACnE,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAgDlC,KAAK,KAAK,GAAG,UAAU,GAAG;IACxB,IAAI,EAAE;QACJ;;WAEG;QACH,eAAe,EAAE,MAAM,CAAC;QACxB;;WAEG;QACH,qBAAqB,EAAE,MAAM,CAAC;QAC9B;;WAEG;QACH,sBAAsB,EAAE,MAAM,CAAC;QAC/B;;WAEG;QACH,4BAA4B,EAAE,MAAM,CAAC;QACrC;;WAEG;QACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;QACjC;;WAEG;QACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;QAChC;;WAEG;QACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;KACtB,CAAC;IACF,8DAA8D;IAC9D,iBAAiB,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,UAAU,CAAC,CAAC;IAC9C,+DAA+D;IAC/D,kBAAkB,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,UAAU,CAAC,CAAC;IAC/C,+DAA+D;IAC/D,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,gEAAgE;IAChE,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,uDAAuD;IACvD,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B,CAAC;AAgCF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,eAAO,MAAM,mBAAmB,QAAO,SAAS,CAAC,KAAK,CAgOrD,CAAC"}
1
+ {"version":3,"file":"babel-plugin-intlayer.d.ts","sourceRoot":"","sources":["../../src/babel-plugin-intlayer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAY,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACnE,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAwDlC,KAAK,KAAK,GAAG,UAAU,GAAG;IACxB,IAAI,EAAE;QACJ;;WAEG;QACH,eAAe,EAAE,MAAM,CAAC;QACxB;;WAEG;QACH,qBAAqB,EAAE,MAAM,CAAC;QAC9B;;WAEG;QACH,sBAAsB,EAAE,MAAM,CAAC;QAC/B;;WAEG;QACH,4BAA4B,EAAE,MAAM,CAAC;QACrC;;WAEG;QACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;QACjC;;WAEG;QACH,UAAU,CAAC,EAAE,QAAQ,GAAG,SAAS,GAAG,OAAO,CAAC;QAC5C;;WAEG;QACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;KACtB,CAAC;IACF,8DAA8D;IAC9D,iBAAiB,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,UAAU,CAAC,CAAC;IAC9C,+DAA+D;IAC/D,kBAAkB,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,UAAU,CAAC,CAAC;IAC/C,+DAA+D;IAC/D,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,gEAAgE;IAChE,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,uDAAuD;IACvD,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,qDAAqD;IACrD,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B,CAAC;AAgCF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AACH,eAAO,MAAM,mBAAmB,QAAO,SAAS,CAAC,KAAK,CAgRrD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlayer/babel",
3
- "version": "5.5.11",
3
+ "version": "5.7.0",
4
4
  "private": false,
5
5
  "description": "A Babel plugin for Intlayer that transforms declaration files and provides internationalization features during the build process according to the Intlayer configuration.",
6
6
  "keywords": [
@@ -62,8 +62,8 @@
62
62
  "@babel/generator": "^7.27.5",
63
63
  "@babel/parser": "^7.27.5",
64
64
  "@babel/traverse": "^7.27.4",
65
- "@intlayer/config": "5.5.11",
66
- "@intlayer/chokidar": "5.5.11"
65
+ "@intlayer/config": "5.7.0",
66
+ "@intlayer/chokidar": "5.7.0"
67
67
  },
68
68
  "devDependencies": {
69
69
  "@babel/types": "^7.27.6",
@@ -83,14 +83,14 @@
83
83
  "tsup": "^8.5.0",
84
84
  "typescript": "^5.8.3",
85
85
  "vitest": "^3.2.2",
86
- "@utils/ts-config": "1.0.4",
87
86
  "@utils/eslint-config": "1.0.4",
88
- "@utils/tsup-config": "1.0.4",
89
- "@utils/ts-config-types": "1.0.4"
87
+ "@utils/ts-config": "1.0.4",
88
+ "@utils/ts-config-types": "1.0.4",
89
+ "@utils/tsup-config": "1.0.4"
90
90
  },
91
91
  "peerDependencies": {
92
- "@intlayer/chokidar": "5.5.11",
93
- "@intlayer/config": "5.5.11"
92
+ "@intlayer/chokidar": "5.7.0",
93
+ "@intlayer/config": "5.7.0"
94
94
  },
95
95
  "engines": {
96
96
  "node": ">=14.18"