@intlayer/babel 8.1.11 → 8.2.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/dist/cjs/babel-plugin-intlayer-extract.cjs +1 -1
- package/dist/cjs/babel-plugin-intlayer-extract.cjs.map +1 -1
- package/dist/cjs/babel-plugin-intlayer-optimize.cjs +1 -1
- package/dist/cjs/babel-plugin-intlayer-optimize.cjs.map +1 -1
- package/dist/cjs/extractContent/babelProcessor.cjs +2 -0
- package/dist/cjs/extractContent/babelProcessor.cjs.map +1 -0
- package/dist/cjs/extractContent/contentWriter.cjs +2 -0
- package/dist/cjs/extractContent/contentWriter.cjs.map +1 -0
- package/dist/cjs/extractContent/extractContent.cjs +2 -0
- package/dist/cjs/extractContent/extractContent.cjs.map +1 -0
- package/dist/cjs/extractContent/index.cjs +1 -0
- package/dist/cjs/extractContent/processTsxFile.cjs +6 -0
- package/dist/cjs/extractContent/processTsxFile.cjs.map +1 -0
- package/dist/cjs/extractContent/utils/constants.cjs +2 -0
- package/dist/cjs/extractContent/utils/constants.cjs.map +1 -0
- package/dist/cjs/extractContent/utils/detectPackageName.cjs +2 -0
- package/dist/cjs/extractContent/utils/detectPackageName.cjs.map +1 -0
- package/dist/cjs/extractContent/utils/extractDictionaryKey.cjs +2 -0
- package/dist/cjs/extractContent/utils/extractDictionaryKey.cjs.map +1 -0
- package/dist/cjs/extractContent/utils/generateKey.cjs +2 -0
- package/dist/cjs/extractContent/utils/generateKey.cjs.map +1 -0
- package/dist/cjs/extractContent/utils/getComponentName.cjs +2 -0
- package/dist/cjs/extractContent/utils/getComponentName.cjs.map +1 -0
- package/dist/cjs/extractContent/utils/getExistingIntlayerInfo.cjs +2 -0
- package/dist/cjs/extractContent/utils/getExistingIntlayerInfo.cjs.map +1 -0
- package/dist/cjs/extractContent/utils/getOrGenerateKey.cjs +2 -0
- package/dist/cjs/extractContent/utils/getOrGenerateKey.cjs.map +1 -0
- package/dist/cjs/extractContent/utils/index.cjs +1 -0
- package/dist/cjs/extractContent/utils/resolveDictionaryKey.cjs +2 -0
- package/dist/cjs/extractContent/utils/resolveDictionaryKey.cjs.map +1 -0
- package/dist/cjs/extractContent/utils/shouldExtract.cjs +2 -0
- package/dist/cjs/extractContent/utils/shouldExtract.cjs.map +1 -0
- package/dist/cjs/getExtractPluginOptions.cjs +1 -1
- package/dist/cjs/getOptimizePluginOptions.cjs.map +1 -1
- package/dist/cjs/index.cjs +1 -1
- package/dist/esm/babel-plugin-intlayer-extract.mjs +1 -1
- package/dist/esm/babel-plugin-intlayer-extract.mjs.map +1 -1
- package/dist/esm/babel-plugin-intlayer-optimize.mjs +1 -1
- package/dist/esm/babel-plugin-intlayer-optimize.mjs.map +1 -1
- package/dist/esm/extractContent/babelProcessor.mjs +2 -0
- package/dist/esm/extractContent/babelProcessor.mjs.map +1 -0
- package/dist/esm/extractContent/contentWriter.mjs +2 -0
- package/dist/esm/extractContent/contentWriter.mjs.map +1 -0
- package/dist/esm/extractContent/extractContent.mjs +2 -0
- package/dist/esm/extractContent/extractContent.mjs.map +1 -0
- package/dist/esm/extractContent/index.mjs +1 -0
- package/dist/esm/extractContent/processTsxFile.mjs +6 -0
- package/dist/esm/extractContent/processTsxFile.mjs.map +1 -0
- package/dist/esm/extractContent/utils/constants.mjs +2 -0
- package/dist/esm/extractContent/utils/constants.mjs.map +1 -0
- package/dist/esm/extractContent/utils/detectPackageName.mjs +2 -0
- package/dist/esm/extractContent/utils/detectPackageName.mjs.map +1 -0
- package/dist/esm/extractContent/utils/extractDictionaryKey.mjs +2 -0
- package/dist/esm/extractContent/utils/extractDictionaryKey.mjs.map +1 -0
- package/dist/esm/extractContent/utils/generateKey.mjs +2 -0
- package/dist/esm/extractContent/utils/generateKey.mjs.map +1 -0
- package/dist/esm/extractContent/utils/getComponentName.mjs +2 -0
- package/dist/esm/extractContent/utils/getComponentName.mjs.map +1 -0
- package/dist/esm/extractContent/utils/getExistingIntlayerInfo.mjs +2 -0
- package/dist/esm/extractContent/utils/getExistingIntlayerInfo.mjs.map +1 -0
- package/dist/esm/extractContent/utils/getOrGenerateKey.mjs +2 -0
- package/dist/esm/extractContent/utils/getOrGenerateKey.mjs.map +1 -0
- package/dist/esm/extractContent/utils/index.mjs +1 -0
- package/dist/esm/extractContent/utils/resolveDictionaryKey.mjs +2 -0
- package/dist/esm/extractContent/utils/resolveDictionaryKey.mjs.map +1 -0
- package/dist/esm/extractContent/utils/shouldExtract.mjs +2 -0
- package/dist/esm/extractContent/utils/shouldExtract.mjs.map +1 -0
- package/dist/esm/getExtractPluginOptions.mjs +1 -1
- package/dist/esm/getOptimizePluginOptions.mjs.map +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/types/babel-plugin-intlayer-extract.d.ts +28 -58
- package/dist/types/babel-plugin-intlayer-extract.d.ts.map +1 -1
- package/dist/types/babel-plugin-intlayer-optimize.d.ts +2 -2
- package/dist/types/babel-plugin-intlayer-optimize.d.ts.map +1 -1
- package/dist/types/extractContent/babelProcessor.d.ts +41 -0
- package/dist/types/extractContent/babelProcessor.d.ts.map +1 -0
- package/dist/types/extractContent/contentWriter.d.ts +27 -0
- package/dist/types/extractContent/contentWriter.d.ts.map +1 -0
- package/dist/types/extractContent/extractContent.d.ts +52 -0
- package/dist/types/extractContent/extractContent.d.ts.map +1 -0
- package/dist/types/extractContent/index.d.ts +4 -0
- package/dist/types/extractContent/processTsxFile.d.ts +18 -0
- package/dist/types/extractContent/processTsxFile.d.ts.map +1 -0
- package/dist/types/extractContent/utils/constants.d.ts +16 -0
- package/dist/types/extractContent/utils/constants.d.ts.map +1 -0
- package/dist/types/extractContent/utils/detectPackageName.d.ts +8 -0
- package/dist/types/extractContent/utils/detectPackageName.d.ts.map +1 -0
- package/dist/types/extractContent/utils/extractDictionaryKey.d.ts +11 -0
- package/dist/types/extractContent/utils/extractDictionaryKey.d.ts.map +1 -0
- package/dist/types/extractContent/utils/generateKey.d.ts +5 -0
- package/dist/types/extractContent/utils/generateKey.d.ts.map +1 -0
- package/dist/types/extractContent/utils/getComponentName.d.ts +10 -0
- package/dist/types/extractContent/utils/getComponentName.d.ts.map +1 -0
- package/dist/types/extractContent/utils/getExistingIntlayerInfo.d.ts +13 -0
- package/dist/types/extractContent/utils/getExistingIntlayerInfo.d.ts.map +1 -0
- package/dist/types/extractContent/utils/getOrGenerateKey.d.ts +8 -0
- package/dist/types/extractContent/utils/getOrGenerateKey.d.ts.map +1 -0
- package/dist/types/extractContent/utils/index.d.ts +10 -0
- package/dist/types/extractContent/utils/resolveDictionaryKey.d.ts +12 -0
- package/dist/types/extractContent/utils/resolveDictionaryKey.d.ts.map +1 -0
- package/dist/types/extractContent/utils/shouldExtract.d.ts +14 -0
- package/dist/types/extractContent/utils/shouldExtract.d.ts.map +1 -0
- package/dist/types/getOptimizePluginOptions.d.ts +1 -1
- package/dist/types/index.d.ts +4 -1
- package/package.json +14 -9
- package/dist/cjs/test-babel-error.cjs +0 -7
- package/dist/cjs/test-babel-error.cjs.map +0 -1
- package/dist/cjs/test-merge.cjs +0 -2
- package/dist/cjs/test-merge.cjs.map +0 -1
- package/dist/cjs/test-preserve.cjs +0 -2
- package/dist/cjs/test-preserve.cjs.map +0 -1
- package/dist/esm/test-babel-error.mjs +0 -7
- package/dist/esm/test-babel-error.mjs.map +0 -1
- package/dist/esm/test-merge.mjs +0 -2
- package/dist/esm/test-merge.mjs.map +0 -1
- package/dist/esm/test-preserve.mjs +0 -2
- package/dist/esm/test-preserve.mjs.map +0 -1
- package/dist/types/test-babel-error.d.ts +0 -1
- package/dist/types/test-merge.d.ts +0 -1
- package/dist/types/test-preserve.d.ts +0 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`./_virtual/_rolldown/runtime.cjs`);const e=require(`./extractContent/utils/detectPackageName.cjs`),t=require(`./extractContent/utils/extractDictionaryKey.cjs`),n=require(`./extractContent/processTsxFile.cjs`);let r=require(`node:path`),i=require(`@babel/parser`),a=require(`@intlayer/config/client`),o=require(`@intlayer/config/node`);const s=new Set([`next-intlayer`]),c=c=>({name:`babel-plugin-intlayer-extract`,visitor:{Program:{enter(c,l){let u=l.opts;if(u.enabled===!1)return;let d=l.file.opts.filename;if(!d)return;let f=(0,r.extname)(d);if(![`.tsx`,`.jsx`,`.ts`,`.js`].includes(f)||u.filesList&&!u.filesList.includes(d))return;let p=l.file.code??``;if(!p)return;let m=(0,o.getConfiguration)(),h=(0,a.getAppLogger)(m),g=u.packageName??e.detectPackageName((0,r.dirname)(d))??`react-intlayer`,_=t.extractDictionaryKeyFromPath(d,u.prefix??`comp-`),v=u.saveComponents??!1,y=c.node.directives.some(e=>e.value.value===`use client`),b=n.processTsxFile(d,_,s.has(g)&&!y?`${g}/server`:g,m,v,{},p);if(!b)return;let{extractedContent:x,modifiedCode:S}=b;if(u.onExtract){let e=u.defaultLocale??m.internationalization.defaultLocale;for(let[t,n]of Object.entries(x))u.onExtract({dictionaryKey:t,filePath:d,content:n,locale:e})}try{let e=(0,i.parse)(S,{sourceType:`module`,plugins:[`jsx`,`typescript`]});c.node.body=e.program.body,c.node.directives=e.program.directives,h(`${(0,a.colorize)(`Compiler:`,a.ANSIColors.GREY_DARK)} Extracted content from ${(0,a.colorizePath)((0,r.relative)(m.content.baseDir,d))}`,{level:`debug`})}catch(e){console.error(`[intlayer] Failed to parse transformed code for ${d}:`,e)}}}}});exports.intlayerExtractBabelPlugin=c;
|
|
2
2
|
//# sourceMappingURL=babel-plugin-intlayer-extract.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"babel-plugin-intlayer-extract.cjs","names":["_generate","defaultShouldExtract","ATTRIBUTES_TO_EXTRACT","ANSIColors"],"sources":["../../src/babel-plugin-intlayer-extract.ts"],"sourcesContent":["import { execSync } from 'node:child_process';\nimport { existsSync, readFileSync, writeFileSync } from 'node:fs';\nimport { basename, dirname, extname, join, relative } from 'node:path';\nimport type { NodePath, PluginObj, PluginPass } from '@babel/core';\nimport _generate from '@babel/generator';\nimport type * as BabelTypes from '@babel/types';\nimport {\n ATTRIBUTES_TO_EXTRACT,\n shouldExtract as defaultShouldExtract,\n detectFormatCommand,\n} from '@intlayer/chokidar/cli';\nimport {\n ANSIColors,\n colorize,\n colorizePath,\n getAppLogger,\n} from '@intlayer/config/client';\nimport { getConfiguration } from '@intlayer/config/node';\nimport { generateKey } from '@intlayer/core/utils';\n\nconst generate = ((_generate as any).default ?? _generate) as typeof _generate;\n\n// Set this to true to enable debug logs\nconst DEBUG_LOG = false;\n\ntype 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 the extraction Babel plugin\n */\nexport type ExtractPluginOptions = {\n /**\n * The default locale for the extracted content\n */\n defaultLocale?: string;\n /**\n * The package to import useIntlayer from\n * @default 'react-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 * Whether the extraction compiler is enabled.\n * If false, the plugin will not process the file.\n */\n enabled?: boolean;\n\n /**\n * Prefix for the extracted dictionary keys.\n */\n prefix?: string;\n\n /**\n * Indicates if the components should be saved after being transformed.\n */\n saveComponents?: boolean;\n};\n\ntype State = PluginPass & {\n opts: ExtractPluginOptions;\n /** Extracted content from this file */\n _extractedContent?: ExtractedContent;\n /** Set of existing keys to avoid duplicates */\n _existingKeys?: Set<string>;\n /** The dictionary key for this file */\n _dictionaryKey?: string;\n /** whether the current file is included in the filesList */\n _isIncluded?: boolean;\n /** Whether this file has JSX (React component) */\n _hasJSX?: boolean;\n /** Whether we already have useIntlayer imported */\n _hasUseIntlayerImport?: boolean;\n /** The local name for useIntlayer (in case it's aliased) */\n _useIntlayerLocalName?: string;\n /** Whether we already have getIntlayer imported */\n _hasGetIntlayerImport?: boolean;\n /** The local name for getIntlayer (in case it's aliased) */\n _getIntlayerLocalName?: string;\n /** The variable name to use for content (content or _compContent if content is already used) */\n _contentVarName?: string;\n /** Whether the file has 'use client' directive */\n _isClient?: boolean;\n /** Whether there is extracted content at the top level (not in a function) */\n _hasTopLevelContent?: boolean;\n /** Targets to extract and modify */\n _extractionTargets?: {\n path: NodePath<any>;\n key: string;\n isAttribute: boolean;\n }[];\n /** Functions to inject the hook/core logic into */\n _functionsToInject?: Set<NodePath<BabelTypes.Function>>;\n};\nconst extractDictionaryKeyFromPath = (\n filePath: string,\n prefix = 'comp-'\n): string => {\n const ext = extname(filePath);\n let baseName = basename(filePath, ext);\n if (baseName === 'index') baseName = basename(dirname(filePath));\n return `${prefix}${baseName\n .replace(/([a-z])([A-Z])/g, '$1-$2')\n .replace(/[\\s_]+/g, '-')\n .toLowerCase()}`;\n};\n\nconst detectPackageName = (dir: string): string => {\n let currentDir = dir;\n while (true) {\n const pkgPath = join(currentDir, 'package.json');\n\n if (existsSync(pkgPath)) {\n try {\n const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));\n\n if (\n pkg.dependencies?.['next-intlayer'] ||\n pkg.devDependencies?.['next-intlayer']\n ) {\n return 'next-intlayer';\n }\n\n if (\n pkg.dependencies?.['react-intlayer'] ||\n pkg.devDependencies?.['react-intlayer']\n ) {\n return 'react-intlayer';\n }\n\n if (\n pkg.dependencies?.['preact-intlayer'] ||\n pkg.devDependencies?.['preact-intlayer']\n ) {\n return 'preact-intlayer';\n }\n } catch {}\n }\n const parentDir = dirname(currentDir);\n\n if (parentDir === currentDir) break;\n currentDir = parentDir;\n }\n return 'react-intlayer';\n};\n\nconst unwrapParentheses = (\n node: BabelTypes.Node,\n t: typeof BabelTypes\n): BabelTypes.Node => {\n let current = node;\n\n while (t.isParenthesizedExpression(current)) {\n current = current.expression;\n }\n\n return current;\n};\n\nconst isReactComponent = (\n funcPath: NodePath<BabelTypes.Function>,\n t: typeof BabelTypes\n): boolean => {\n const node = funcPath.node;\n\n if (!t.isBlockStatement(node.body)) {\n const unwrapped = unwrapParentheses(node.body, t);\n return t.isJSXElement(unwrapped) || t.isJSXFragment(unwrapped);\n }\n\n let returnsJSX = false;\n\n funcPath.traverse({\n Function(p) {\n p.skip();\n },\n ReturnStatement(p) {\n if (p.node.argument) {\n const unwrapped = unwrapParentheses(p.node.argument, t);\n\n if (t.isJSXElement(unwrapped) || t.isJSXFragment(unwrapped)) {\n returnsJSX = true;\n }\n }\n },\n });\n return returnsJSX;\n};\n\nconst findTargetFunction = (\n startPath: NodePath<any>,\n t: typeof BabelTypes\n): NodePath<BabelTypes.Function> | null => {\n const closestFunc =\n startPath.getFunctionParent() as NodePath<BabelTypes.Function> | null;\n if (!closestFunc) return null;\n\n let currentFunc: NodePath<BabelTypes.Function> | null = closestFunc;\n while (currentFunc) {\n if (isReactComponent(currentFunc, t)) {\n return currentFunc;\n }\n currentFunc =\n currentFunc.getFunctionParent() as NodePath<BabelTypes.Function> | null;\n }\n return closestFunc;\n};\n\nexport const intlayerExtractBabelPlugin = (babel: {\n types: typeof BabelTypes;\n}): PluginObj<State> => {\n const { types: t } = babel;\n const config = getConfiguration();\n const appLogger = getAppLogger(config);\n\n return {\n name: 'babel-plugin-intlayer-extract',\n\n pre() {\n this._extractedContent = {};\n this._existingKeys = new Set();\n this._isIncluded = true;\n this._hasJSX = false;\n this._hasUseIntlayerImport = false;\n this._useIntlayerLocalName = 'useIntlayer';\n this._hasGetIntlayerImport = false;\n this._getIntlayerLocalName = 'getIntlayer';\n this._contentVarName = 'content';\n this._isClient = false;\n this._hasTopLevelContent = false;\n this._extractionTargets = [];\n this._functionsToInject = new Set();\n\n const filename = this.file.opts.filename;\n\n if (!this.opts.packageName) {\n const searchDir = filename ? dirname(filename) : process.cwd();\n this.opts.packageName = detectPackageName(searchDir);\n }\n\n // Check if extraction is enabled\n const isEnabled = this.opts.enabled ?? true;\n\n if (!isEnabled) {\n this._isIncluded = false;\n return;\n }\n\n if (this.opts.filesList && filename) {\n const normalizedFilename = filename.replace(/\\\\/g, '/');\n this._isIncluded = this.opts.filesList.some(\n (f) => f.replace(/\\\\/g, '/') === normalizedFilename\n );\n }\n\n if (filename)\n this._dictionaryKey = extractDictionaryKeyFromPath(\n filename,\n this.opts.prefix\n );\n },\n visitor: {\n ImportDeclaration(path, state) {\n if (!state._isIncluded) return;\n\n for (const spec of path.node.specifiers) {\n if (!t.isImportSpecifier(spec)) continue;\n const importedName = t.isIdentifier(spec.imported)\n ? spec.imported.name\n : (spec.imported as BabelTypes.StringLiteral).value;\n\n if (importedName === 'useIntlayer') {\n state._hasUseIntlayerImport = true;\n state._useIntlayerLocalName = spec.local.name;\n }\n\n if (importedName === 'getIntlayer') {\n state._hasGetIntlayerImport = true;\n state._getIntlayerLocalName = spec.local.name;\n }\n }\n },\n\n JSXElement(_path, state) {\n if (!state._isIncluded) return;\n state._hasJSX = true;\n },\n\n JSXText(path, state) {\n if (!state._isIncluded || !state._dictionaryKey) return;\n const shouldExtract = state.opts.shouldExtract ?? defaultShouldExtract;\n const rawText = path.node.value;\n\n if (shouldExtract(rawText)) {\n const text = rawText.replace(/\\s+/g, ' ').trim();\n let key = Object.keys(state._extractedContent!).find(\n (k) => state._extractedContent![k] === text\n );\n\n if (!key) {\n key = generateKey(text, state._existingKeys!);\n state._existingKeys!.add(key);\n state._extractedContent![key] = text;\n }\n\n state._extractionTargets!.push({ path, key, isAttribute: false });\n const func = findTargetFunction(path, t);\n if (func) {\n state._functionsToInject!.add(func);\n } else {\n state._hasTopLevelContent = true;\n }\n }\n },\n\n JSXAttribute(path, state) {\n if (!state._isIncluded || !state._dictionaryKey) return;\n const shouldExtract = state.opts.shouldExtract ?? defaultShouldExtract;\n const attrName = path.node.name;\n\n if (!t.isJSXIdentifier(attrName)) return;\n const isKey = attrName.name === 'key';\n\n if (!ATTRIBUTES_TO_EXTRACT.includes(attrName.name) && !isKey) return;\n\n const value = path.node.value;\n let text: string | null = null;\n\n if (t.isStringLiteral(value)) text = value.value;\n else if (\n t.isJSXExpressionContainer(value) &&\n t.isStringLiteral(value.expression)\n )\n text = value.expression.value;\n\n if (text && shouldExtract(text)) {\n const cleanText = text.trim();\n let key = Object.keys(state._extractedContent!).find(\n (k) => state._extractedContent![k] === cleanText\n );\n\n if (!key) {\n key = generateKey(cleanText, state._existingKeys!);\n state._existingKeys!.add(key);\n state._extractedContent![key] = cleanText;\n }\n\n state._extractionTargets!.push({ path, key, isAttribute: true });\n const func = findTargetFunction(path, t);\n if (func) {\n state._functionsToInject!.add(func);\n } else {\n state._hasTopLevelContent = true;\n }\n }\n },\n\n StringLiteral(path, state) {\n if (!state._isIncluded || !state._dictionaryKey) return;\n const shouldExtract = state.opts.shouldExtract ?? defaultShouldExtract;\n const parent = path.parentPath;\n\n if (\n parent.isJSXAttribute() ||\n parent.isImportDeclaration() ||\n parent.isExportDeclaration() ||\n parent.isImportSpecifier()\n )\n return;\n\n if (parent.isObjectProperty() && path.key === 'key') return;\n\n if (parent.isMemberExpression() && path.key === 'property') return;\n\n if (parent.isCallExpression()) {\n const callee = (parent.node as BabelTypes.CallExpression).callee;\n\n if (\n (t.isMemberExpression(callee) &&\n t.isIdentifier(callee.object) &&\n callee.object.name === 'console') ||\n (t.isIdentifier(callee) &&\n (callee.name === state._useIntlayerLocalName ||\n callee.name === state._getIntlayerLocalName ||\n callee.name === 'require')) ||\n callee.type === 'Import'\n )\n return;\n }\n\n const text = path.node.value;\n\n if (shouldExtract(text)) {\n const cleanText = text.trim();\n let key = Object.keys(state._extractedContent!).find(\n (k) => state._extractedContent![k] === cleanText\n );\n\n if (!key) {\n key = generateKey(cleanText, state._existingKeys!);\n state._existingKeys!.add(key);\n state._extractedContent![key] = cleanText;\n }\n\n state._extractionTargets!.push({ path, key, isAttribute: false });\n const func = findTargetFunction(path, t);\n if (func) {\n state._functionsToInject!.add(func);\n } else {\n state._hasTopLevelContent = true;\n }\n }\n },\n\n Program: {\n enter(programPath, state) {\n if (!state._isIncluded) return;\n\n // Check for 'use client' directive\n state._isClient = programPath.node.directives.some(\n (d) => d.value.value === 'use client'\n );\n\n let contentVarUsed = false;\n programPath.traverse({\n VariableDeclarator(varPath) {\n if (\n t.isIdentifier(varPath.node.id) &&\n varPath.node.id.name === 'content'\n )\n contentVarUsed = true;\n },\n });\n state._contentVarName = contentVarUsed ? '_compContent' : 'content';\n },\n\n exit(programPath, state) {\n if (!state._isIncluded || !state._dictionaryKey) return;\n\n const extractionTargets = state._extractionTargets!;\n const functionsToInject = state._functionsToInject!;\n\n if (extractionTargets.length === 0) return;\n\n // Extraction (Modification)\n for (const { path, key, isAttribute } of extractionTargets) {\n let isHook = false;\n let hookDecided = false;\n\n const binding = path.scope.getBinding(state._contentVarName!);\n\n if (\n binding &&\n t.isVariableDeclarator(binding.path.node) &&\n t.isCallExpression(binding.path.node.init) &&\n t.isIdentifier(binding.path.node.init.callee)\n ) {\n if (\n binding.path.node.init.callee.name ===\n state._useIntlayerLocalName\n ) {\n isHook = true;\n hookDecided = true;\n } else if (\n binding.path.node.init.callee.name ===\n state._getIntlayerLocalName\n ) {\n isHook = false;\n hookDecided = true;\n }\n }\n\n if (!hookDecided) {\n const func = findTargetFunction(path, t);\n\n if (func) {\n isHook = isReactComponent(func, t);\n }\n }\n\n if (isAttribute) {\n const member = t.optionalMemberExpression(\n t.identifier(state._contentVarName!),\n t.stringLiteral(key),\n true,\n true\n );\n path.node.value = t.jsxExpressionContainer(\n isHook\n ? t.optionalMemberExpression(\n member,\n t.identifier('value'),\n false,\n true\n )\n : member\n );\n } else if (path.isJSXText()) {\n path.replaceWith(\n t.jsxExpressionContainer(\n t.optionalMemberExpression(\n t.identifier(state._contentVarName!),\n t.stringLiteral(key),\n true,\n true\n )\n )\n );\n } else {\n const member = t.optionalMemberExpression(\n t.identifier(state._contentVarName!),\n t.stringLiteral(key),\n true,\n true\n );\n path.replaceWith(\n isHook\n ? t.optionalMemberExpression(\n member,\n t.identifier('value'),\n false,\n true\n )\n : member\n );\n }\n }\n\n // Report\n\n if (state.opts.onExtract && state._dictionaryKey) {\n state.opts.onExtract({\n dictionaryKey: state._dictionaryKey,\n filePath: state.file.opts.filename!,\n content: { ...state._extractedContent! },\n locale: state.opts.defaultLocale!,\n });\n }\n\n // Pass 3: Injection\n let needsUseIntlayer = false;\n let needsGetIntlayer = false;\n\n for (const funcPath of functionsToInject) {\n const type = injectHook(funcPath, state, t);\n\n if (type === 'hook') needsUseIntlayer = true;\n\n if (type === 'core') needsGetIntlayer = true;\n }\n\n if (state._hasTopLevelContent) {\n needsGetIntlayer = true;\n const contentVarName = state._contentVarName!;\n const dictionaryKey = state._dictionaryKey!;\n const coreName = state._getIntlayerLocalName!;\n\n // Find injection position (after imports)\n let pos = 0;\n const body = programPath.node.body;\n while (\n pos < body.length &&\n (t.isImportDeclaration(body[pos]) ||\n (t.isExpressionStatement(body[pos]) &&\n t.isStringLiteral(\n (body[pos] as BabelTypes.ExpressionStatement).expression\n )))\n )\n pos++;\n\n programPath.node.body.splice(\n pos,\n 0,\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(contentVarName),\n t.callExpression(t.identifier(coreName), [\n t.stringLiteral(dictionaryKey),\n ])\n ),\n ])\n );\n }\n\n // Pass 4: Imports\n\n if (needsUseIntlayer || needsGetIntlayer) {\n let hookPkg = state.opts.packageName ?? 'react-intlayer';\n const corePkg = 'intlayer';\n\n // Handle next-intlayer server/client split\n\n if (hookPkg === 'next-intlayer' && !state._isClient) {\n hookPkg = `${hookPkg}/server`;\n }\n\n let pos = 0;\n const body = programPath.node.body;\n while (\n pos < body.length &&\n t.isExpressionStatement(body[pos]) &&\n t.isStringLiteral(\n (body[pos] as BabelTypes.ExpressionStatement).expression\n )\n )\n pos++;\n\n if (needsUseIntlayer && !state._hasUseIntlayerImport) {\n body.splice(\n pos++,\n 0,\n t.importDeclaration(\n [\n t.importSpecifier(\n t.identifier('useIntlayer'),\n t.identifier('useIntlayer')\n ),\n ],\n t.stringLiteral(hookPkg)\n )\n );\n }\n\n if (needsGetIntlayer && !state._hasGetIntlayerImport) {\n body.splice(\n pos,\n 0,\n t.importDeclaration(\n [\n t.importSpecifier(\n t.identifier('getIntlayer'),\n t.identifier('getIntlayer')\n ),\n ],\n t.stringLiteral(corePkg)\n )\n );\n }\n }\n\n if (state.opts.saveComponents && state.file.opts.filename) {\n try {\n const transformedCode = generate(programPath.node, {\n retainLines: true,\n }).code;\n\n writeFileSync(state.file.opts.filename, transformedCode, 'utf-8');\n\n const basedir = config.content?.baseDir ?? process.cwd();\n const relativePath = relative(basedir, state.file.opts.filename);\n\n appLogger(\n `${colorize('Compiler:', ANSIColors.GREY_DARK)} Saved component: ${colorizePath(relativePath)}`,\n { level: 'info' }\n );\n\n const formatCommand = detectFormatCommand(config);\n\n if (formatCommand) {\n try {\n execSync(\n formatCommand.replace('{{file}}', state.file.opts.filename),\n {\n stdio: 'ignore', // Use 'ignore' to prevent format output from cluttering the extraction process console\n cwd: basedir,\n }\n );\n } catch (error) {\n appLogger(`Extractor formatting error: ${String(error)}`, {\n level: 'error',\n });\n }\n }\n } catch (err: any) {\n appLogger(`Extractor plugin error: ${String(err)}`, {\n level: 'error',\n });\n }\n }\n\n if (DEBUG_LOG) {\n const basedir = config.content?.baseDir ?? process.cwd();\n const relativePath = state.file.opts.filename\n ? relative(basedir, state.file.opts.filename)\n : 'unknown';\n\n appLogger(\n `${colorize('Compiler:', ANSIColors.GREY_DARK)} Transformed: ${colorizePath(relativePath)}\\n${generate(programPath.node, { retainLines: false }).code}`,\n { level: 'debug' }\n );\n }\n },\n },\n },\n };\n};\n\nconst injectHook = (\n path: NodePath<BabelTypes.Function>,\n state: State,\n t: typeof BabelTypes\n): 'hook' | 'core' => {\n const node = path.node;\n const contentVarName = state._contentVarName!;\n const dictionaryKey = state._dictionaryKey!;\n const returnsJSX = isReactComponent(path, t);\n\n if (!t.isBlockStatement(node.body)) {\n const hookName = returnsJSX\n ? state._useIntlayerLocalName!\n : state._getIntlayerLocalName!;\n\n const hookCall = t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(contentVarName),\n t.callExpression(t.identifier(hookName), [\n t.stringLiteral(dictionaryKey),\n ])\n ),\n ]);\n\n node.body = t.blockStatement([\n hookCall,\n t.returnStatement(node.body as BabelTypes.Expression),\n ]);\n\n return returnsJSX ? 'hook' : 'core';\n }\n\n const hookName = returnsJSX\n ? state._useIntlayerLocalName!\n : state._getIntlayerLocalName!;\n\n const hasHook = node.body.body.some(\n (s) =>\n t.isVariableDeclaration(s) &&\n s.declarations.some(\n (d) => t.isIdentifier(d.id) && d.id.name === contentVarName\n )\n );\n\n if (!hasHook) {\n node.body.body.unshift(\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(contentVarName),\n t.callExpression(t.identifier(hookName), [\n t.stringLiteral(dictionaryKey),\n ])\n ),\n ])\n );\n }\n\n return returnsJSX ? 'hook' : 'core';\n};\n"],"mappings":"sYAoBA,MAAM,EAAaA,EAAAA,QAAkB,SAAWA,EAAAA,QAqG1C,GACJ,EACA,EAAS,UACE,CAEX,IAAI,GAAA,EAAA,EAAA,UAAoB,GAAA,EAAA,EAAA,SADJ,EAAS,CACS,CAEtC,OADI,IAAa,UAAS,GAAA,EAAA,EAAA,WAAA,EAAA,EAAA,SAA4B,EAAS,CAAC,EACzD,GAAG,IAAS,EAChB,QAAQ,kBAAmB,QAAQ,CACnC,QAAQ,UAAW,IAAI,CACvB,aAAa,IAGZ,EAAqB,GAAwB,CACjD,IAAI,EAAa,EACjB,OAAa,CACX,IAAM,GAAA,EAAA,EAAA,MAAe,EAA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,CACd,EAAU,+BAA+B,OAAO,EAAM,GAAI,CACxD,MAAO,QACR,CAAC,QAGC,EAAU,CACjB,EAAU,2BAA2B,OAAO,EAAI,GAAI,CAClD,MAAO,QACR,CAAC,GAgBT,CACF,CACF,EAGG,GACJ,EACA,EACA,IACoB,CACpB,IAAM,EAAO,EAAK,KACZ,EAAiB,EAAM,gBACvB,EAAgB,EAAM,eACtB,EAAa,EAAiB,EAAM,EAAE,CAE5C,GAAI,CAAC,EAAE,iBAAiB,EAAK,KAAK,CAAE,CAClC,IAAM,EAAW,EACb,EAAM,sBACN,EAAM,sBAEJ,EAAW,EAAE,oBAAoB,QAAS,CAC9C,EAAE,mBACA,EAAE,WAAW,EAAe,CAC5B,EAAE,eAAe,EAAE,WAAW,EAAS,CAAE,CACvC,EAAE,cAAc,EAAc,CAC/B,CAAC,CACH,CACF,CAAC,CAOF,MALA,GAAK,KAAO,EAAE,eAAe,CAC3B,EACA,EAAE,gBAAgB,EAAK,KAA8B,CACtD,CAAC,CAEK,EAAa,OAAS,OAG/B,IAAM,EAAW,EACb,EAAM,sBACN,EAAM,sBAuBV,OArBgB,EAAK,KAAK,KAAK,KAC5B,GACC,EAAE,sBAAsB,EAAE,EAC1B,EAAE,aAAa,KACZ,GAAM,EAAE,aAAa,EAAE,GAAG,EAAI,EAAE,GAAG,OAAS,EAC9C,CACJ,EAGC,EAAK,KAAK,KAAK,QACb,EAAE,oBAAoB,QAAS,CAC7B,EAAE,mBACA,EAAE,WAAW,EAAe,CAC5B,EAAE,eAAe,EAAE,WAAW,EAAS,CAAE,CACvC,EAAE,cAAc,EAAc,CAC/B,CAAC,CACH,CACF,CAAC,CACH,CAGI,EAAa,OAAS"}
|
|
1
|
+
{"version":3,"file":"babel-plugin-intlayer-extract.cjs","names":["detectPackageName","extractDictionaryKeyFromPath","processTsxFile","ANSIColors"],"sources":["../../src/babel-plugin-intlayer-extract.ts"],"sourcesContent":["import { dirname, extname, relative } from 'node:path';\nimport type { PluginObj, PluginPass } from '@babel/core';\nimport { parse } from '@babel/parser';\nimport type * as BabelTypes from '@babel/types';\nimport {\n ANSIColors,\n colorize,\n colorizePath,\n getAppLogger,\n} from '@intlayer/config/client';\nimport { getConfiguration } from '@intlayer/config/node';\nimport { processTsxFile } from './extractContent/processTsxFile';\nimport type { PackageName } from './extractContent/utils/constants';\nimport { detectPackageName } from './extractContent/utils/detectPackageName';\nimport { extractDictionaryKeyFromPath } from './extractContent/utils/extractDictionaryKey';\n\n/** Packages that support a `/server` sub-path for React Server Components. */\nconst SERVER_CAPABLE_PACKAGES: ReadonlySet<string> = new Set(['next-intlayer']);\n\nexport type ExtractResult = {\n dictionaryKey: string;\n filePath: string;\n content: Record<string, string>;\n locale: string;\n};\n\nexport type ExtractPluginOptions = {\n defaultLocale?: string;\n packageName?: string;\n filesList?: string[];\n shouldExtract?: (text: string) => boolean;\n enabled?: boolean;\n prefix?: string;\n saveComponents?: boolean;\n formatCommand?: string;\n /**\n * Callback invoked for each extracted dictionary key/content pair.\n * Used by `getExtractPluginOptions` to write dictionaries to disk.\n * May be async — the plugin will fire-and-forget (Babel transforms are sync).\n */\n onExtract?: (result: ExtractResult) => void | Promise<void>;\n};\n\ntype State = PluginPass & { opts: ExtractPluginOptions };\n\n/**\n * Babel plugin that extracts translatable content from source files and\n * injects Intlayer hooks (`useIntlayer` / `getIntlayer`) automatically.\n *\n * Designed for use with Babel-based build tools such as Next.js and Webpack.\n *\n * @example babel.config.js\n * ```js\n * const { intlayerExtractBabelPlugin, getExtractPluginOptions } = require('@intlayer/babel');\n * module.exports = {\n * presets: ['next/babel'],\n * plugins: [\n * [intlayerExtractBabelPlugin, getExtractPluginOptions()],\n * ],\n * };\n * ```\n */\nexport const intlayerExtractBabelPlugin = (babel: {\n types: typeof BabelTypes;\n}): PluginObj<State> => {\n return {\n name: 'babel-plugin-intlayer-extract',\n\n visitor: {\n Program: {\n enter(programPath, state) {\n const opts = state.opts;\n\n if (opts.enabled === false) return;\n\n const filename = state.file.opts.filename;\n if (!filename) return;\n\n const ext = extname(filename);\n if (!['.tsx', '.jsx', '.ts', '.js'].includes(ext)) return;\n\n if (opts.filesList && !opts.filesList.includes(filename)) return;\n\n // Grab the original source from the Babel file object\n const fileCode: string = (state.file as any).code ?? '';\n if (!fileCode) return;\n\n const configuration = getConfiguration();\n const appLogger = getAppLogger(configuration);\n\n const packageName = (opts.packageName ??\n detectPackageName(dirname(filename)) ??\n 'react-intlayer') as PackageName;\n\n const prefix = opts.prefix ?? 'comp-';\n const componentKey = extractDictionaryKeyFromPath(filename, prefix);\n const saveComponents = opts.saveComponents ?? false;\n\n // For packages that expose a `/server` entry (e.g. next-intlayer),\n // use that sub-path when no \"use client\" directive is present so that\n // React Server Components receive the correct import.\n const hasUseClient = programPath.node.directives.some(\n (d) => d.value.value === 'use client'\n );\n const effectivePackageName =\n SERVER_CAPABLE_PACKAGES.has(packageName) && !hasUseClient\n ? `${packageName}/server`\n : packageName;\n\n const result = processTsxFile(\n filename,\n componentKey,\n effectivePackageName,\n configuration,\n saveComponents,\n {}, // unmergedDictionaries — not needed for Babel plugin use case\n fileCode\n );\n\n if (!result) return;\n\n const { extractedContent, modifiedCode } = result;\n\n // Fire-and-forget: Babel transforms are synchronous, but onExtract may\n // be async (e.g. writing files). We don't await here.\n if (opts.onExtract) {\n const defaultLocale =\n opts.defaultLocale ??\n configuration.internationalization.defaultLocale;\n\n for (const [key, content] of Object.entries(extractedContent)) {\n void opts.onExtract({\n dictionaryKey: key,\n filePath: filename,\n content,\n locale: defaultLocale,\n });\n }\n }\n\n // Replace the Babel AST with the transformed code by re-parsing it.\n // This lets Babel serialise the injected hooks/imports through its\n // own code generator, preserving compatibility with other plugins.\n try {\n const newAst = parse(modifiedCode, {\n sourceType: 'module',\n plugins: ['jsx', 'typescript'],\n });\n\n programPath.node.body = newAst.program.body;\n programPath.node.directives = newAst.program.directives;\n\n appLogger(\n `${colorize('Compiler:', ANSIColors.GREY_DARK)} Extracted content from ${colorizePath(relative(configuration.content.baseDir, filename))}`,\n { level: 'debug' }\n );\n } catch (error) {\n console.error(\n `[intlayer] Failed to parse transformed code for ${filename}:`,\n error\n );\n }\n },\n },\n },\n };\n};\n"],"mappings":"2ZAiBA,MAAM,EAA+C,IAAI,IAAI,CAAC,gBAAgB,CAAC,CA6ClE,EAA8B,IAGlC,CACL,KAAM,gCAEN,QAAS,CACP,QAAS,CACP,MAAM,EAAa,EAAO,CACxB,IAAM,EAAO,EAAM,KAEnB,GAAI,EAAK,UAAY,GAAO,OAE5B,IAAM,EAAW,EAAM,KAAK,KAAK,SACjC,GAAI,CAAC,EAAU,OAEf,IAAM,GAAA,EAAA,EAAA,SAAc,EAAS,CAG7B,GAFI,CAAC,CAAC,OAAQ,OAAQ,MAAO,MAAM,CAAC,SAAS,EAAI,EAE7C,EAAK,WAAa,CAAC,EAAK,UAAU,SAAS,EAAS,CAAE,OAG1D,IAAM,EAAoB,EAAM,KAAa,MAAQ,GACrD,GAAI,CAAC,EAAU,OAEf,IAAM,GAAA,EAAA,EAAA,mBAAkC,CAClC,GAAA,EAAA,EAAA,cAAyB,EAAc,CAEvC,EAAe,EAAK,aACxBA,EAAAA,mBAAAA,EAAAA,EAAAA,SAA0B,EAAS,CAAC,EACpC,iBAGI,EAAeC,EAAAA,6BAA6B,EADnC,EAAK,QAAU,QACqC,CAC7D,EAAiB,EAAK,gBAAkB,GAKxC,EAAe,EAAY,KAAK,WAAW,KAC9C,GAAM,EAAE,MAAM,QAAU,aAC1B,CAMK,EAASC,EAAAA,eACb,EACA,EANA,EAAwB,IAAI,EAAY,EAAI,CAAC,EACzC,GAAG,EAAY,SACf,EAMJ,EACA,EACA,EAAE,CACF,EACD,CAED,GAAI,CAAC,EAAQ,OAEb,GAAM,CAAE,mBAAkB,gBAAiB,EAI3C,GAAI,EAAK,UAAW,CAClB,IAAM,EACJ,EAAK,eACL,EAAc,qBAAqB,cAErC,IAAK,GAAM,CAAC,EAAK,KAAY,OAAO,QAAQ,EAAiB,CACtD,EAAK,UAAU,CAClB,cAAe,EACf,SAAU,EACV,UACA,OAAQ,EACT,CAAC,CAON,GAAI,CACF,IAAM,GAAA,EAAA,EAAA,OAAe,EAAc,CACjC,WAAY,SACZ,QAAS,CAAC,MAAO,aAAa,CAC/B,CAAC,CAEF,EAAY,KAAK,KAAO,EAAO,QAAQ,KACvC,EAAY,KAAK,WAAa,EAAO,QAAQ,WAE7C,EACE,IAAA,EAAA,EAAA,UAAY,YAAaC,EAAAA,WAAW,UAAU,CAAC,2BAAA,EAAA,EAAA,eAAA,EAAA,EAAA,UAAgD,EAAc,QAAQ,QAAS,EAAS,CAAC,GACxI,CAAE,MAAO,QAAS,CACnB,OACM,EAAO,CACd,QAAQ,MACN,mDAAmD,EAAS,GAC5D,EACD,GAGN,CACF,CACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`./_virtual/_rolldown/runtime.cjs`);let e=require(`node:path`),t=require(`@intlayer/
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`./_virtual/_rolldown/runtime.cjs`);let e=require(`node:path`),t=require(`@intlayer/config/utils`),n=require(`@intlayer/chokidar/utils`);const r=[`intlayer`,`@intlayer/core`,`react-intlayer`,`react-intlayer/client`,`react-intlayer/server`,`next-intlayer`,`next-intlayer/client`,`next-intlayer/server`,`svelte-intlayer`,`vue-intlayer`,`angular-intlayer`,`preact-intlayer`,`solid-intlayer`],i=[`useIntlayer`,`getIntlayer`],a=[`react-intlayer`,`react-intlayer/client`,`react-intlayer/server`,`next-intlayer`,`next-intlayer/client`,`next-intlayer/server`,`preact-intlayer`,`vue-intlayer`,`solid-intlayer`,`svelte-intlayer`,`angular-intlayer`],o={getIntlayer:`getDictionary`,useIntlayer:`useDictionary`},s={useIntlayer:`useDictionaryDynamic`},c=(e,t)=>{let r=(0,n.getPathHash)(e);return t.identifier(`_${r}`)},l=(n,r,i,a,o,s)=>{let c=(0,e.join)(r,`${o}.json`);s===`fetch`&&(c=(0,e.join)(a,`${o}.mjs`)),s===`dynamic`&&(c=(0,e.join)(i,`${o}.mjs`));let l=(0,e.relative)((0,e.dirname)(n),c);return l=(0,t.normalizePath)(l),!l.startsWith(`./`)&&!l.startsWith(`../`)&&(l=`./${l}`),l},u=e=>{let{types:t}=e;return{name:`babel-plugin-intlayer-transform`,pre(){if(this._newStaticImports=new Map,this._newDynamicImports=new Map,this._callerMap=new Map,this._isIncluded=!0,this._hasValidImport=!1,this._isDictEntry=!1,this._useDynamicHelpers=!1,this.opts.optimize===!1){this._isIncluded=!1;return}let e=this.file.opts.filename;if(this.opts.filesList&&e&&!this.opts.filesList.includes(e)){this._isIncluded=!1;return}},visitor:{Program:{enter(e,n){let r=n.file.opts.filename;n.opts.replaceDictionaryEntry&&r===n.opts.dictionariesEntryPath&&(n._isDictEntry=!0,e.traverse({ImportDeclaration(e){e.remove()},VariableDeclarator(e){t.isObjectExpression(e.node.init)&&(e.node.init.properties=[])}}))},exit(e,u){if(u._isDictEntry||!u._isIncluded)return;let d=!1;if(e.traverse({CallExpression(e){let n=e.node.callee;if(!t.isIdentifier(n)||u._callerMap?.get(n.name)!==`useIntlayer`)return;let r=e.node.arguments[0],i;if(r&&t.isStringLiteral(r)?i=r.value:r&&t.isTemplateLiteral(r)&&r.expressions.length===0&&r.quasis.length===1&&(i=r.quasis[0].value.cooked??r.quasis[0].value.raw),!i)return;let a=u.opts.dictionaryModeMap?.[i];(a===`dynamic`||a===`fetch`)&&(d=!0)}}),e.traverse({ImportDeclaration(e){let n=e.node.source.value;if(r.includes(n)){u._hasValidImport=!0;for(let r of e.node.specifiers){if(!t.isImportSpecifier(r))continue;let e=t.isIdentifier(r.imported)?r.imported.name:r.imported.value;i.includes(e)&&u._callerMap?.set(r.local.name,e);let c=u.opts.importMode,l=(c===`dynamic`||c===`fetch`||d)&&a.includes(n);l&&(u._useDynamicHelpers=!0);let f;f=l?{...o,...s}:o;let p=f[e];p&&(r.imported=t.identifier(p))}}},CallExpression(e){let r=e.node.callee;if(!t.isIdentifier(r))return;let i=u._callerMap?.get(r.name);if(!i)return;u._hasValidImport=!0;let a=e.node.arguments[0],o;if(a&&t.isStringLiteral(a)?o=a.value:a&&t.isTemplateLiteral(a)&&a.expressions.length===0&&a.quasis.length===1&&(o=a.quasis[0].value.cooked??a.quasis[0].value.raw),!o)return;let s=u.opts.importMode,l=i===`useIntlayer`,d=!!u._useDynamicHelpers,f=`static`,p=u.opts.dictionaryModeMap?.[o];l&&d?p?f=p:s===`dynamic`?f=`dynamic`:s===`fetch`&&(f=`fetch`):l&&!d&&(p===`dynamic`||p===`fetch`)&&(f=p);let m;if(f===`fetch`){let r=u._newDynamicImports?.get(o);if(!r){let e=(0,n.getPathHash)(o);r=t.identifier(`_${e}_fetch`),u._newDynamicImports?.set(o,r)}m=r,e.node.arguments=[t.identifier(m.name),...e.node.arguments]}else if(f===`dynamic`){let r=u._newDynamicImports?.get(o);if(!r){let e=(0,n.getPathHash)(o);r=t.identifier(`_${e}_dyn`),u._newDynamicImports?.set(o,r)}m=r,e.node.arguments=[t.identifier(m.name),...e.node.arguments]}else{let n=u._newStaticImports?.get(o);n||(n=c(o,t),u._newStaticImports?.set(o,n)),m=n,e.node.arguments[0]=t.identifier(m.name)}}}),!u._hasValidImport)return;let f=u.file.opts.filename,p=u.opts.dictionariesDir,m=u.opts.dynamicDictionariesDir,h=u.opts.fetchDictionariesDir,g=[];for(let[e,n]of u._newStaticImports){let r=l(f,p,m,h,e,`static`),i=t.importDeclaration([t.importDefaultSpecifier(t.identifier(n.name))],t.stringLiteral(r));i.attributes=[t.importAttribute(t.identifier(`type`),t.stringLiteral(`json`))],g.push(i)}for(let[e,n]of u._newDynamicImports){let r=l(f,p,m,h,e,n.name.endsWith(`_fetch`)?`fetch`:`dynamic`);g.push(t.importDeclaration([t.importDefaultSpecifier(t.identifier(n.name))],t.stringLiteral(r)))}if(!g.length)return;let _=e.get(`body`),v=0;for(let e of _){let n=e.node;if(t.isExpressionStatement(n)&&t.isStringLiteral(n.expression)&&!n.expression.value.startsWith(`import`)&&!n.expression.value.startsWith(`require`))v+=1;else break}e.node.body.splice(v,0,...g)}}}}};exports.intlayerOptimizeBabelPlugin=u;
|
|
2
2
|
//# sourceMappingURL=babel-plugin-intlayer-optimize.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"babel-plugin-intlayer-optimize.cjs","names":[],"sources":["../../src/babel-plugin-intlayer-optimize.ts"],"sourcesContent":["import { dirname, join, relative } from 'node:path';\nimport type { NodePath, PluginObj, PluginPass } from '@babel/core';\nimport type * as BabelTypes from '@babel/types';\nimport { getFileHash } from '@intlayer/chokidar/utils';\nimport { normalizePath } from '@intlayer/config/utils';\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 DYNAMIC_IMPORT_FUNCTION = {\n useIntlayer: 'useDictionaryDynamic',\n} as const;\n\n/**\n * Options for the optimization Babel plugin\n */\nexport type OptimizePluginOptions = {\n /**\n * If false, the plugin will not apply any transformation.\n */\n optimize?: boolean;\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 unmerged dictionaries entry file.\n */\n unmergedDictionariesEntryPath: string;\n /**\n * The path to the unmerged dictionaries directory.\n */\n unmergedDictionariesDir: 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 * The path to the fetch dictionaries directory.\n */\n fetchDictionariesDir: string;\n /**\n * The path to the fetch dictionaries entry file.\n */\n fetchDictionariesEntryPath: 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' | 'fetch';\n /**\n * Map of dictionary keys to their specific import mode.\n */\n dictionaryModeMap?: Record<string, 'static' | 'dynamic' | 'fetch'>;\n /**\n * Files list to traverse.\n */\n filesList: string[];\n};\n\ntype State = PluginPass & {\n opts: OptimizePluginOptions;\n /** map key → generated ident (per-file) for static imports */\n _newStaticImports?: Map<string, BabelTypes.Identifier>;\n /** map key → generated ident (per-file) for dynamic imports */\n _newDynamicImports?: Map<string, BabelTypes.Identifier>;\n /** whether the current file imported *any* intlayer package */\n _hasValidImport?: boolean;\n /** map from local identifier name to the imported intlayer func name ('useIntlayer' | 'getIntlayer') */\n _callerMap?: Map<string, (typeof CALLER_LIST)[number]>;\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 the current file is included in the filesList */\n _isIncluded?: boolean;\n};\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 = (\n key: string,\n t: typeof BabelTypes\n): BabelTypes.Identifier => {\n const hash = getFileHash(key);\n return t.identifier(`_${hash}`);\n};\n\nconst computeImport = (\n fromFile: string,\n dictionariesDir: string,\n dynamicDictionariesDir: string,\n fetchDictionariesDir: string,\n key: string,\n importMode: 'static' | 'dynamic' | 'fetch'\n): string => {\n let relativePath = join(dictionariesDir, `${key}.json`);\n\n if (importMode === 'fetch') {\n relativePath = join(fetchDictionariesDir, `${key}.mjs`);\n }\n\n if (importMode === 'dynamic') {\n relativePath = join(dynamicDictionariesDir, `${key}.mjs`);\n }\n\n let rel = relative(dirname(fromFile), relativePath);\n\n // Fix windows path\n rel = normalizePath(rel);\n\n // Fix relative path\n if (!rel.startsWith('./') && !rel.startsWith('../')) {\n rel = `./${rel}`;\n }\n\n return rel;\n};\n\n/**\n * Babel plugin that transforms Intlayer function calls and auto-imports dictionaries.\n *\n * This plugin transforms calls to `useIntlayer()` and `getIntlayer()` from various Intlayer\n * packages into optimized dictionary access patterns, automatically importing the required\n * dictionary files based on the configured import mode.\n *\n * ## Supported Input Patterns\n *\n * The plugin recognizes these function calls:\n *\n * ```ts\n * // useIntlayer\n * import { useIntlayer } from 'react-intlayer';\n * import { useIntlayer } from 'next-intlayer';\n *\n * // getIntlayer\n * import { getIntlayer } from 'intlayer';\n *\n * // Usage\n * const content = useIntlayer('app');\n * const content = getIntlayer('app');\n * ```\n *\n * ## Transformation Modes\n *\n * ### Static Mode (default: `importMode = \"static\"`)\n *\n * Imports JSON dictionaries directly and replaces function calls with dictionary access:\n *\n * **Output:**\n * ```ts\n * import _dicHash from '../../.intlayer/dictionaries/app.json' with { type: 'json' };\n * import { useDictionary as useIntlayer } from 'react-intlayer';\n * import { getDictionary as getIntlayer } from 'intlayer';\n *\n * const content1 = useIntlayer(_dicHash);\n * const content2 = getIntlayer(_dicHash);\n * ```\n *\n * ### Dynamic Mode (`importMode = \"dynamic\"`)\n *\n * Uses dynamic dictionary loading with Suspense support:\n *\n * **Output:**\n * ```ts\n * import _dicHash from '../../.intlayer/dictionaries/app.json' with { type: 'json' };\n * import _dicHash_dyn from '../../.intlayer/dynamic_dictionaries/app.mjs';\n * import { useDictionaryDynamic as useIntlayer } from 'react-intlayer';\n * import { getDictionary as getIntlayer } from 'intlayer';\n *\n * const content1 = useIntlayer(_dicHash_dyn, 'app');\n * const content2 = getIntlayer(_dicHash);\n * ```\n *\n * ### Live Mode (`importMode = \"live\"`)\n *\n * Uses live-based dictionary loading for remote dictionaries:\n *\n * **Output if `dictionaryModeMap` includes the key with \"live\" value:**\n * ```ts\n * import _dicHash from '../../.intlayer/dictionaries/app.json' with { type: 'json' };\n * import _dicHash_fetch from '../../.intlayer/fetch_dictionaries/app.mjs';\n * import { useDictionaryDynamic as useIntlayer } from 'react-intlayer';\n * import { getDictionary as getIntlayer } from 'intlayer';\n *\n * const content1 = useIntlayer(_dicHash_fetch, \"app\");\n * const content2 = getIntlayer(_dicHash);\n * ```\n *\n * > If `dictionaryModeMap` does not include the key with \"live\" value, the plugin will fallback to the dynamic impor\n *\n * ```ts\n * import _dicHash from '../../.intlayer/dictionaries/app.json' with { type: 'json' };\n * import _dicHash_dyn from '../../.intlayer/dynamic_dictionaries/app.mjs';\n * import { useDictionaryDynamic as useIntlayer } from 'react-intlayer';\n * import { getDictionary as getIntlayer } from 'intlayer';\n *\n * const content1 = useIntlayer(_dicHash_dyn, 'app');\n * const content2 = getIntlayer(_dicHash);\n * ```\n */\nexport const intlayerOptimizeBabelPlugin = (babel: {\n types: typeof BabelTypes;\n}): PluginObj<State> => {\n const { types: t } = babel;\n\n return {\n name: 'babel-plugin-intlayer-transform',\n\n pre() {\n this._newStaticImports = new Map();\n this._newDynamicImports = new Map();\n this._callerMap = new Map();\n this._isIncluded = true;\n this._hasValidImport = false;\n this._isDictEntry = false;\n this._useDynamicHelpers = false;\n\n // If optimize is false, skip processing entirely\n if (this.opts.optimize === false) {\n this._isIncluded = false;\n return;\n }\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 /* If this file *is* the dictionaries entry, short-circuit: export {} */\n Program: {\n enter(programPath, state) {\n // Safe access to filename\n const filename = state.file.opts.filename;\n\n // Check if this is the correct file to transform\n\n if (\n state.opts.replaceDictionaryEntry &&\n filename === state.opts.dictionariesEntryPath\n ) {\n state._isDictEntry = true;\n\n // Traverse the program to surgically remove/edit specific parts\n programPath.traverse({\n // Remove all import statements (cleaning up 'sssss.json')\n ImportDeclaration(path) {\n path.remove();\n },\n\n // Find the variable definition and empty the object\n VariableDeclarator(path) {\n // We look for: const x = { ... }\n\n if (t.isObjectExpression(path.node.init)) {\n // Set the object properties to an empty array: {}\n path.node.init.properties = [];\n }\n },\n });\n\n // (Optional) Stop other plugins from processing this file further if needed\n // programPath.stop();\n }\n },\n\n /**\n * After full traversal, process imports and call expressions, then inject the JSON dictionary imports.\n *\n * We do the transformation in Program.exit (via a manual traverse) rather than using\n * top-level ImportDeclaration/CallExpression visitors. This ensures that if another plugin\n * (like babel-plugin-intlayer-extract) adds new useIntlayer calls in its Program.exit,\n * we will see and transform them here because our Program.exit runs after theirs.\n */\n exit(programPath, state) {\n if (state._isDictEntry) return; // nothing else to do – already replaced\n\n if (!state._isIncluded) return; // early-out if file is not included\n\n // Manual traversal to process imports and call expressions\n // This runs AFTER all other plugins' visitors have completed\n\n // Pre-pass to determine if we should use dynamic helpers\n let fileHasDynamicCall = false;\n programPath.traverse({\n CallExpression(path) {\n const callee = path.node.callee;\n\n if (!t.isIdentifier(callee)) return;\n\n const originalImportedName = state._callerMap?.get(callee.name);\n if (originalImportedName !== 'useIntlayer') return;\n\n const arg = path.node.arguments[0];\n\n let key: string | undefined;\n\n if (arg && t.isStringLiteral(arg)) {\n key = arg.value;\n } else if (\n arg &&\n t.isTemplateLiteral(arg) &&\n arg.expressions.length === 0 &&\n arg.quasis.length === 1\n ) {\n // If the bundler output is `breadcrumb` instead of 'breadcrumb'\n key = arg.quasis[0].value.cooked ?? arg.quasis[0].value.raw;\n }\n\n if (!key) return;\n const dictionaryOverrideMode =\n state.opts.dictionaryModeMap?.[key];\n\n if (\n dictionaryOverrideMode === 'dynamic' ||\n dictionaryOverrideMode === 'fetch'\n ) {\n fileHasDynamicCall = true;\n }\n },\n });\n\n programPath.traverse({\n /* Inspect every intlayer import */\n ImportDeclaration(path) {\n const src = path.node.source.value;\n\n if (!PACKAGE_LIST.includes(src)) return;\n\n // Mark that we do import from an intlayer package in this file\n state._hasValidImport = true;\n\n for (const spec of path.node.specifiers) {\n if (!t.isImportSpecifier(spec)) continue;\n\n const importedName = t.isIdentifier(spec.imported)\n ? spec.imported.name\n : (spec.imported as BabelTypes.StringLiteral).value;\n\n if (CALLER_LIST.includes(importedName as any)) {\n state._callerMap?.set(\n spec.local.name,\n importedName as (typeof CALLER_LIST)[number]\n );\n }\n\n const importMode = state.opts.importMode;\n // Determine whether this import should use the dynamic helpers.\n const shouldUseDynamicHelpers =\n (importMode === 'dynamic' ||\n importMode === 'fetch' ||\n fileHasDynamicCall) &&\n PACKAGE_LIST_DYNAMIC.includes(src as any);\n\n // Remember for later (CallExpression) whether we are using the dynamic helpers\n\n if (shouldUseDynamicHelpers) {\n state._useDynamicHelpers = true;\n }\n\n let helperMap: Record<string, string>;\n\n 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\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 /* Replace calls: useIntlayer(\"foo\") → useDictionary(_hash) or useDictionaryDynamic(_hash, \"foo\") */\n CallExpression(path) {\n const callee = path.node.callee;\n\n if (!t.isIdentifier(callee)) return;\n\n const originalImportedName = state._callerMap?.get(callee.name);\n if (!originalImportedName) 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\n let key: string | undefined;\n\n if (arg && t.isStringLiteral(arg)) {\n key = arg.value;\n } else if (\n arg &&\n t.isTemplateLiteral(arg) &&\n arg.expressions.length === 0 &&\n arg.quasis.length === 1\n ) {\n // If the bundler output is `breadcrumb` instead of 'breadcrumb'\n key = arg.quasis[0].value.cooked ?? arg.quasis[0].value.raw;\n }\n\n if (!key) return; // must be a static literal\n const importMode = state.opts.importMode;\n const isUseIntlayer = originalImportedName === 'useIntlayer';\n const useDynamicHelpers = Boolean(state._useDynamicHelpers);\n\n // Decide per-call mode: 'static' | 'dynamic' | 'fetch'\n let perCallMode: 'static' | 'dynamic' | 'fetch' = 'static';\n\n const dictionaryOverrideMode =\n state.opts.dictionaryModeMap?.[key];\n\n if (isUseIntlayer && useDynamicHelpers) {\n if (dictionaryOverrideMode) {\n perCallMode = dictionaryOverrideMode;\n } else if (importMode === 'dynamic') {\n perCallMode = 'dynamic';\n } else if (importMode === 'fetch') {\n perCallMode = 'fetch';\n }\n } else if (isUseIntlayer && !useDynamicHelpers) {\n // If dynamic helpers are NOT active (global mode is static),\n // we STILL might want to force dynamic/live for this specific call\n\n if (\n dictionaryOverrideMode === 'dynamic' ||\n dictionaryOverrideMode === 'fetch'\n ) {\n perCallMode = dictionaryOverrideMode;\n }\n }\n\n let ident: BabelTypes.Identifier;\n\n if (perCallMode === 'fetch') {\n // Use fetch dictionaries entry (live mode for selected keys)\n let dynamicIdent = state._newDynamicImports?.get(key);\n\n if (!dynamicIdent) {\n const hash = getFileHash(key);\n dynamicIdent = t.identifier(`_${hash}_fetch`);\n state._newDynamicImports?.set(key, dynamicIdent);\n }\n ident = dynamicIdent;\n\n // Helper: first argument is the dictionary entry, second is the key\n path.node.arguments = [\n t.identifier(ident.name),\n ...path.node.arguments,\n ];\n } else if (perCallMode === 'dynamic') {\n // Use dynamic dictionaries entry\n let dynamicIdent = state._newDynamicImports?.get(key);\n\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\n if (!staticIdent) {\n staticIdent = makeIdent(key, t);\n state._newStaticImports?.set(key, staticIdent);\n }\n ident = staticIdent;\n\n // Static helper (useDictionary / getDictionary): replace key with iden\n path.node.arguments[0] = t.identifier(ident.name);\n }\n },\n });\n\n // Early-out if we touched nothing\n\n if (!state._hasValidImport) return;\n\n const file = state.file.opts.filename!;\n const dictionariesDir = state.opts.dictionariesDir;\n const dynamicDictionariesDir = state.opts.dynamicDictionariesDir;\n const fetchDictionariesDir = state.opts.fetchDictionariesDir;\n const imports: BabelTypes.ImportDeclaration[] = [];\n\n // Generate static JSON imports (getIntlayer always uses JSON dictionaries)\n for (const [key, ident] of state._newStaticImports!) {\n const rel = computeImport(\n file,\n dictionariesDir,\n dynamicDictionariesDir,\n fetchDictionariesDir,\n key,\n 'static'\n );\n\n const importDeclarationNode = t.importDeclaration(\n [t.importDefaultSpecifier(t.identifier(ident.name))],\n t.stringLiteral(rel)\n );\n\n // Add 'type: json' attribute for JSON files\n importDeclarationNode.attributes = [\n t.importAttribute(t.identifier('type'), t.stringLiteral('json')),\n ];\n\n imports.push(importDeclarationNode);\n }\n\n // Generate dynamic/fetch imports (for useIntlayer when using dynamic/live helpers)\n for (const [key, ident] of state._newDynamicImports!) {\n const modeForThisIdent: 'dynamic' | 'fetch' = ident.name.endsWith(\n '_fetch'\n )\n ? 'fetch'\n : 'dynamic';\n\n const rel = computeImport(\n file,\n dictionariesDir,\n dynamicDictionariesDir,\n fetchDictionariesDir,\n key,\n modeForThisIdent\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(\n 'body'\n ) as NodePath<BabelTypes.Statement>[];\n let insertPos = 0;\n for (const stmtPath of bodyPaths) {\n const stmt = stmtPath.node;\n\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 };\n};\n"],"mappings":"oNAMA,MAAM,EAAe,CACnB,WACA,iBACA,iBACA,wBACA,wBACA,gBACA,uBACA,uBACA,kBACA,eACA,mBACA,kBACA,iBACD,CAEK,EAAc,CAAC,cAAe,cAAc,CAK5C,EAAuB,CAC3B,iBACA,wBACA,wBACA,gBACA,uBACA,uBACA,kBACA,eACA,iBACA,kBACA,mBACD,CAEK,EAAyB,CAC7B,YAAa,gBACb,YAAa,gBACd,CAEK,EAA0B,CAC9B,YAAa,uBACd,CAmFK,GACJ,EACA,IAC0B,CAC1B,IAAM,GAAA,EAAA,EAAA,aAAmB,EAAI,CAC7B,OAAO,EAAE,WAAW,IAAI,IAAO,EAG3B,GACJ,EACA,EACA,EACA,EACA,EACA,IACW,CACX,IAAI,GAAA,EAAA,EAAA,MAAoB,EAAiB,GAAG,EAAI,OAAO,CAEnD,IAAe,UACjB,GAAA,EAAA,EAAA,MAAoB,EAAsB,GAAG,EAAI,MAAM,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AAK,OAAO,EAAW,EAAG,GAAG,EAAQ,EAEzD,CACF,CACF"}
|
|
1
|
+
{"version":3,"file":"babel-plugin-intlayer-optimize.cjs","names":[],"sources":["../../src/babel-plugin-intlayer-optimize.ts"],"sourcesContent":["import { dirname, join, relative } from 'node:path';\nimport type { NodePath, PluginObj, PluginPass } from '@babel/core';\nimport type * as BabelTypes from '@babel/types';\nimport { getPathHash } from '@intlayer/chokidar/utils';\nimport { normalizePath } from '@intlayer/config/utils';\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 DYNAMIC_IMPORT_FUNCTION = {\n useIntlayer: 'useDictionaryDynamic',\n} as const;\n\n/**\n * Options for the optimization Babel plugin\n */\nexport type OptimizePluginOptions = {\n /**\n * If false, the plugin will not apply any transformation.\n */\n optimize?: boolean;\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 unmerged dictionaries entry file.\n */\n unmergedDictionariesEntryPath: string;\n /**\n * The path to the unmerged dictionaries directory.\n */\n unmergedDictionariesDir: 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 * The path to the fetch dictionaries directory.\n */\n fetchDictionariesDir: string;\n /**\n * The path to the fetch dictionaries entry file.\n */\n fetchDictionariesEntryPath: 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' | 'fetch' | undefined;\n /**\n * Map of dictionary keys to their specific import mode.\n */\n dictionaryModeMap?: Record<\n string,\n 'static' | 'dynamic' | 'fetch' | undefined\n >;\n /**\n * Files list to traverse.\n */\n filesList: string[];\n};\n\ntype State = PluginPass & {\n opts: OptimizePluginOptions;\n /** map key → generated ident (per-file) for static imports */\n _newStaticImports?: Map<string, BabelTypes.Identifier>;\n /** map key → generated ident (per-file) for dynamic imports */\n _newDynamicImports?: Map<string, BabelTypes.Identifier>;\n /** whether the current file imported *any* intlayer package */\n _hasValidImport?: boolean;\n /** map from local identifier name to the imported intlayer func name ('useIntlayer' | 'getIntlayer') */\n _callerMap?: Map<string, (typeof CALLER_LIST)[number]>;\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 the current file is included in the filesList */\n _isIncluded?: boolean;\n};\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 = (\n key: string,\n t: typeof BabelTypes\n): BabelTypes.Identifier => {\n const hash = getPathHash(key);\n return t.identifier(`_${hash}`);\n};\n\nconst computeImport = (\n fromFile: string,\n dictionariesDir: string,\n dynamicDictionariesDir: string,\n fetchDictionariesDir: string,\n key: string,\n importMode: 'static' | 'dynamic' | 'fetch'\n): string => {\n let relativePath = join(dictionariesDir, `${key}.json`);\n\n if (importMode === 'fetch') {\n relativePath = join(fetchDictionariesDir, `${key}.mjs`);\n }\n\n if (importMode === 'dynamic') {\n relativePath = join(dynamicDictionariesDir, `${key}.mjs`);\n }\n\n let rel = relative(dirname(fromFile), relativePath);\n\n // Fix windows path\n rel = normalizePath(rel);\n\n // Fix relative path\n if (!rel.startsWith('./') && !rel.startsWith('../')) {\n rel = `./${rel}`;\n }\n\n return rel;\n};\n\n/**\n * Babel plugin that transforms Intlayer function calls and auto-imports dictionaries.\n *\n * This plugin transforms calls to `useIntlayer()` and `getIntlayer()` from various Intlayer\n * packages into optimized dictionary access patterns, automatically importing the required\n * dictionary files based on the configured import mode.\n *\n * ## Supported Input Patterns\n *\n * The plugin recognizes these function calls:\n *\n * ```ts\n * // useIntlayer\n * import { useIntlayer } from 'react-intlayer';\n * import { useIntlayer } from 'next-intlayer';\n *\n * // getIntlayer\n * import { getIntlayer } from 'intlayer';\n *\n * // Usage\n * const content = useIntlayer('app');\n * const content = getIntlayer('app');\n * ```\n *\n * ## Transformation Modes\n *\n * ### Static Mode (default: `importMode = \"static\"`)\n *\n * Imports JSON dictionaries directly and replaces function calls with dictionary access:\n *\n * **Output:**\n * ```ts\n * import _dicHash from '../../.intlayer/dictionaries/app.json' with { type: 'json' };\n * import { useDictionary as useIntlayer } from 'react-intlayer';\n * import { getDictionary as getIntlayer } from 'intlayer';\n *\n * const content1 = useIntlayer(_dicHash);\n * const content2 = getIntlayer(_dicHash);\n * ```\n *\n * ### Dynamic Mode (`importMode = \"dynamic\"`)\n *\n * Uses dynamic dictionary loading with Suspense support:\n *\n * **Output:**\n * ```ts\n * import _dicHash from '../../.intlayer/dictionaries/app.json' with { type: 'json' };\n * import _dicHash_dyn from '../../.intlayer/dynamic_dictionaries/app.mjs';\n * import { useDictionaryDynamic as useIntlayer } from 'react-intlayer';\n * import { getDictionary as getIntlayer } from 'intlayer';\n *\n * const content1 = useIntlayer(_dicHash_dyn, 'app');\n * const content2 = getIntlayer(_dicHash);\n * ```\n *\n * ### Live Mode (`importMode = \"live\"`)\n *\n * Uses live-based dictionary loading for remote dictionaries:\n *\n * **Output if `dictionaryModeMap` includes the key with \"live\" value:**\n * ```ts\n * import _dicHash from '../../.intlayer/dictionaries/app.json' with { type: 'json' };\n * import _dicHash_fetch from '../../.intlayer/fetch_dictionaries/app.mjs';\n * import { useDictionaryDynamic as useIntlayer } from 'react-intlayer';\n * import { getDictionary as getIntlayer } from 'intlayer';\n *\n * const content1 = useIntlayer(_dicHash_fetch, \"app\");\n * const content2 = getIntlayer(_dicHash);\n * ```\n *\n * > If `dictionaryModeMap` does not include the key with \"live\" value, the plugin will fallback to the dynamic impor\n *\n * ```ts\n * import _dicHash from '../../.intlayer/dictionaries/app.json' with { type: 'json' };\n * import _dicHash_dyn from '../../.intlayer/dynamic_dictionaries/app.mjs';\n * import { useDictionaryDynamic as useIntlayer } from 'react-intlayer';\n * import { getDictionary as getIntlayer } from 'intlayer';\n *\n * const content1 = useIntlayer(_dicHash_dyn, 'app');\n * const content2 = getIntlayer(_dicHash);\n * ```\n */\nexport const intlayerOptimizeBabelPlugin = (babel: {\n types: typeof BabelTypes;\n}): PluginObj<State> => {\n const { types: t } = babel;\n\n return {\n name: 'babel-plugin-intlayer-transform',\n\n pre() {\n this._newStaticImports = new Map();\n this._newDynamicImports = new Map();\n this._callerMap = new Map();\n this._isIncluded = true;\n this._hasValidImport = false;\n this._isDictEntry = false;\n this._useDynamicHelpers = false;\n\n // If optimize is false, skip processing entirely\n if (this.opts.optimize === false) {\n this._isIncluded = false;\n return;\n }\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 /* If this file *is* the dictionaries entry, short-circuit: export {} */\n Program: {\n enter(programPath, state) {\n // Safe access to filename\n const filename = state.file.opts.filename;\n\n // Check if this is the correct file to transform\n\n if (\n state.opts.replaceDictionaryEntry &&\n filename === state.opts.dictionariesEntryPath\n ) {\n state._isDictEntry = true;\n\n // Traverse the program to surgically remove/edit specific parts\n programPath.traverse({\n // Remove all import statements (cleaning up 'sssss.json')\n ImportDeclaration(path) {\n path.remove();\n },\n\n // Find the variable definition and empty the object\n VariableDeclarator(path) {\n // We look for: const x = { ... }\n\n if (t.isObjectExpression(path.node.init)) {\n // Set the object properties to an empty array: {}\n path.node.init.properties = [];\n }\n },\n });\n\n // (Optional) Stop other plugins from processing this file further if needed\n // programPath.stop();\n }\n },\n\n /**\n * After full traversal, process imports and call expressions, then inject the JSON dictionary imports.\n *\n * We do the transformation in Program.exit (via a manual traverse) rather than using\n * top-level ImportDeclaration/CallExpression visitors. This ensures that if another plugin\n * (like babel-plugin-intlayer-extract) adds new useIntlayer calls in its Program.exit,\n * we will see and transform them here because our Program.exit runs after theirs.\n */\n exit(programPath, state) {\n if (state._isDictEntry) return; // nothing else to do – already replaced\n\n if (!state._isIncluded) return; // early-out if file is not included\n\n // Manual traversal to process imports and call expressions\n // This runs AFTER all other plugins' visitors have completed\n\n // Pre-pass to determine if we should use dynamic helpers\n let fileHasDynamicCall = false;\n programPath.traverse({\n CallExpression(path) {\n const callee = path.node.callee;\n\n if (!t.isIdentifier(callee)) return;\n\n const originalImportedName = state._callerMap?.get(callee.name);\n if (originalImportedName !== 'useIntlayer') return;\n\n const arg = path.node.arguments[0];\n\n let key: string | undefined;\n\n if (arg && t.isStringLiteral(arg)) {\n key = arg.value;\n } else if (\n arg &&\n t.isTemplateLiteral(arg) &&\n arg.expressions.length === 0 &&\n arg.quasis.length === 1\n ) {\n // If the bundler output is `breadcrumb` instead of 'breadcrumb'\n key = arg.quasis[0].value.cooked ?? arg.quasis[0].value.raw;\n }\n\n if (!key) return;\n const dictionaryOverrideMode =\n state.opts.dictionaryModeMap?.[key];\n\n if (\n dictionaryOverrideMode === 'dynamic' ||\n dictionaryOverrideMode === 'fetch'\n ) {\n fileHasDynamicCall = true;\n }\n },\n });\n\n programPath.traverse({\n /* Inspect every intlayer import */\n ImportDeclaration(path) {\n const src = path.node.source.value;\n\n if (!PACKAGE_LIST.includes(src)) return;\n\n // Mark that we do import from an intlayer package in this file\n state._hasValidImport = true;\n\n for (const spec of path.node.specifiers) {\n if (!t.isImportSpecifier(spec)) continue;\n\n const importedName = t.isIdentifier(spec.imported)\n ? spec.imported.name\n : (spec.imported as BabelTypes.StringLiteral).value;\n\n if (CALLER_LIST.includes(importedName as any)) {\n state._callerMap?.set(\n spec.local.name,\n importedName as (typeof CALLER_LIST)[number]\n );\n }\n\n const importMode = state.opts.importMode;\n // Determine whether this import should use the dynamic helpers.\n const shouldUseDynamicHelpers =\n (importMode === 'dynamic' ||\n importMode === 'fetch' ||\n fileHasDynamicCall) &&\n PACKAGE_LIST_DYNAMIC.includes(src as any);\n\n // Remember for later (CallExpression) whether we are using the dynamic helpers\n\n if (shouldUseDynamicHelpers) {\n state._useDynamicHelpers = true;\n }\n\n let helperMap: Record<string, string>;\n\n 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\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 /* Replace calls: useIntlayer(\"foo\") → useDictionary(_hash) or useDictionaryDynamic(_hash, \"foo\") */\n CallExpression(path) {\n const callee = path.node.callee;\n\n if (!t.isIdentifier(callee)) return;\n\n const originalImportedName = state._callerMap?.get(callee.name);\n if (!originalImportedName) 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\n let key: string | undefined;\n\n if (arg && t.isStringLiteral(arg)) {\n key = arg.value;\n } else if (\n arg &&\n t.isTemplateLiteral(arg) &&\n arg.expressions.length === 0 &&\n arg.quasis.length === 1\n ) {\n // If the bundler output is `breadcrumb` instead of 'breadcrumb'\n key = arg.quasis[0].value.cooked ?? arg.quasis[0].value.raw;\n }\n\n if (!key) return; // must be a static literal\n const importMode = state.opts.importMode;\n const isUseIntlayer = originalImportedName === 'useIntlayer';\n const useDynamicHelpers = Boolean(state._useDynamicHelpers);\n\n // Decide per-call mode: 'static' | 'dynamic' | 'fetch'\n let perCallMode: 'static' | 'dynamic' | 'fetch' = 'static';\n\n const dictionaryOverrideMode =\n state.opts.dictionaryModeMap?.[key];\n\n if (isUseIntlayer && useDynamicHelpers) {\n if (dictionaryOverrideMode) {\n perCallMode = dictionaryOverrideMode;\n } else if (importMode === 'dynamic') {\n perCallMode = 'dynamic';\n } else if (importMode === 'fetch') {\n perCallMode = 'fetch';\n }\n } else if (isUseIntlayer && !useDynamicHelpers) {\n // If dynamic helpers are NOT active (global mode is static),\n // we STILL might want to force dynamic/live for this specific call\n\n if (\n dictionaryOverrideMode === 'dynamic' ||\n dictionaryOverrideMode === 'fetch'\n ) {\n perCallMode = dictionaryOverrideMode;\n }\n }\n\n let ident: BabelTypes.Identifier;\n\n if (perCallMode === 'fetch') {\n // Use fetch dictionaries entry (live mode for selected keys)\n let dynamicIdent = state._newDynamicImports?.get(key);\n\n if (!dynamicIdent) {\n const hash = getPathHash(key);\n dynamicIdent = t.identifier(`_${hash}_fetch`);\n state._newDynamicImports?.set(key, dynamicIdent);\n }\n ident = dynamicIdent;\n\n // Helper: first argument is the dictionary entry, second is the key\n path.node.arguments = [\n t.identifier(ident.name),\n ...path.node.arguments,\n ];\n } else if (perCallMode === 'dynamic') {\n // Use dynamic dictionaries entry\n let dynamicIdent = state._newDynamicImports?.get(key);\n\n if (!dynamicIdent) {\n // Create a unique identifier for dynamic imports by appending a suffix\n const hash = getPathHash(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\n if (!staticIdent) {\n staticIdent = makeIdent(key, t);\n state._newStaticImports?.set(key, staticIdent);\n }\n ident = staticIdent;\n\n // Static helper (useDictionary / getDictionary): replace key with iden\n path.node.arguments[0] = t.identifier(ident.name);\n }\n },\n });\n\n // Early-out if we touched nothing\n\n if (!state._hasValidImport) return;\n\n const file = state.file.opts.filename!;\n const dictionariesDir = state.opts.dictionariesDir;\n const dynamicDictionariesDir = state.opts.dynamicDictionariesDir;\n const fetchDictionariesDir = state.opts.fetchDictionariesDir;\n const imports: BabelTypes.ImportDeclaration[] = [];\n\n // Generate static JSON imports (getIntlayer always uses JSON dictionaries)\n for (const [key, ident] of state._newStaticImports!) {\n const rel = computeImport(\n file,\n dictionariesDir,\n dynamicDictionariesDir,\n fetchDictionariesDir,\n key,\n 'static'\n );\n\n const importDeclarationNode = t.importDeclaration(\n [t.importDefaultSpecifier(t.identifier(ident.name))],\n t.stringLiteral(rel)\n );\n\n // Add 'type: json' attribute for JSON files\n importDeclarationNode.attributes = [\n t.importAttribute(t.identifier('type'), t.stringLiteral('json')),\n ];\n\n imports.push(importDeclarationNode);\n }\n\n // Generate dynamic/fetch imports (for useIntlayer when using dynamic/live helpers)\n for (const [key, ident] of state._newDynamicImports!) {\n const modeForThisIdent: 'dynamic' | 'fetch' = ident.name.endsWith(\n '_fetch'\n )\n ? 'fetch'\n : 'dynamic';\n\n const rel = computeImport(\n file,\n dictionariesDir,\n dynamicDictionariesDir,\n fetchDictionariesDir,\n key,\n modeForThisIdent\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(\n 'body'\n ) as NodePath<BabelTypes.Statement>[];\n let insertPos = 0;\n for (const stmtPath of bodyPaths) {\n const stmt = stmtPath.node;\n\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 };\n};\n"],"mappings":"oNAMA,MAAM,EAAe,CACnB,WACA,iBACA,iBACA,wBACA,wBACA,gBACA,uBACA,uBACA,kBACA,eACA,mBACA,kBACA,iBACD,CAEK,EAAc,CAAC,cAAe,cAAc,CAK5C,EAAuB,CAC3B,iBACA,wBACA,wBACA,gBACA,uBACA,uBACA,kBACA,eACA,iBACA,kBACA,mBACD,CAEK,EAAyB,CAC7B,YAAa,gBACb,YAAa,gBACd,CAEK,EAA0B,CAC9B,YAAa,uBACd,CAsFK,GACJ,EACA,IAC0B,CAC1B,IAAM,GAAA,EAAA,EAAA,aAAmB,EAAI,CAC7B,OAAO,EAAE,WAAW,IAAI,IAAO,EAG3B,GACJ,EACA,EACA,EACA,EACA,EACA,IACW,CACX,IAAI,GAAA,EAAA,EAAA,MAAoB,EAAiB,GAAG,EAAI,OAAO,CAEnD,IAAe,UACjB,GAAA,EAAA,EAAA,MAAoB,EAAsB,GAAG,EAAI,MAAM,EAGrD,IAAe,YACjB,GAAA,EAAA,EAAA,MAAoB,EAAwB,GAAG,EAAI,MAAM,EAG3D,IAAI,GAAA,EAAA,EAAA,WAAA,EAAA,EAAA,SAAuB,EAAS,CAAE,EAAa,CAUnD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own/runtime.cjs`),t=require(`./utils/constants.cjs`),n=require(`./utils/getComponentName.cjs`),r=require(`./utils/getExistingIntlayerInfo.cjs`),i=require(`./utils/getOrGenerateKey.cjs`),a=require(`./utils/resolveDictionaryKey.cjs`),o=require(`./utils/shouldExtract.cjs`);let s=require(`@babel/traverse`);s=e.__toESM(s);let c=require(`@babel/types`);c=e.__toESM(c);const l=typeof s.default==`function`?s.default:s.default.default,u=(e,t,n,r,a,s,l)=>{let u=e.node.children;if(u.length<=1)return!1;let d=[],f=!1,p=!1;for(let e of u)if(c.isJSXText(e)){let t=e.value;t.trim().length>0&&(f=!0),d.push({type:`text`,value:t})}else if(c.isJSXExpressionContainer(e))if(c.isJSXEmptyExpression(e.expression))d.push({type:`text`,value:``});else{let n=e.expression;if(c.isIdentifier(n))d.push({type:`var`,value:n.name,originalExpr:n.name}),p=!0;else if(c.isMemberExpression(n)){let e=t.substring(n.start,n.end),r=c.isIdentifier(n.property)?n.property.name:`var`;d.push({type:`var`,value:r,originalExpr:e}),p=!0}else return!1}else return!1;if(!f)return!1;let m=``;for(let e of d)e.type===`var`?m+=`{{${e.value}}}`:m+=e.value;let h=m.replace(/\s+/g,` `).trim();if(o.shouldExtract(h)){let t=r(e),o=i.getOrGenerateKey(h,t,n,a),c=d.filter(e=>e.type===`var`).map(e=>`${e.value}: ${e.originalExpr}`),f=Array.from(new Set(c));return p?s.push({path:e,key:o,type:`jsx-insertion`,componentKey:t,childrenToReplace:u,variables:f}):s.push({path:e,key:o,type:`jsx-text-combined`,componentKey:t,childrenToReplace:u}),u.forEach(e=>{l.add(e)}),!0}return!1},d=(e,s,d,f=`default`,p,m,h={})=>{let g={},_=[],v=new Set,y=new Map,b=new Map,x=new Set,S,C=[];l(e,{FunctionDeclaration(e){C.push(e)},ArrowFunctionExpression(e){C.push(e)},FunctionExpression(e){C.push(e)}});for(let e of C){let t=r.getExistingIntlayerInfo(e);if(t)y.set(e.node,t.key),x.add(t.key),b.set(e.node,t.hook);else{S||(S=a.resolveDictionaryKey(f,m,p,h,x),x.add(S)),y.set(e.node,S);let t=n.getComponentName(e),r=t?/^[A-Z]/.test(t):!1;b.set(e.node,r?`useIntlayer`:`getIntlayer`)}}let w=e=>{let t=e;for(;t;){if(y.has(t.node))return y.get(t.node);t=t.parentPath}return S||f};l(e,{JSXElement(e){v.has(e.node)||u(e,s,d,w,g,_,v)},JSXFragment(e){v.has(e.node)||u(e,s,d,w,g,_,v)},JSXText(e){if(v.has(e.node))return;let t=e.node.value;if(o.shouldExtract(t)){let n=w(e),r=i.getOrGenerateKey(t.replace(/\s+/g,` `).trim(),n,d,g);_.push({path:e,key:r,type:`jsx-text`,componentKey:n})}},JSXAttribute(e){if(v.has(e.node))return;let n=e.node.name.name;if(typeof n!=`string`||!t.ATTRIBUTES_TO_EXTRACT.includes(n))return;let r=e.node.value;if(c.isStringLiteral(r)&&o.shouldExtract(r.value)){let t=w(e),n=i.getOrGenerateKey(r.value.trim(),t,d,g);_.push({path:e,key:n,type:`jsx-attribute`,componentKey:t})}},StringLiteral(e){if(v.has(e.node))return;let t=e.node.value;if(!o.shouldExtract(t))return;let n=e.parentPath;if(n.isImportDeclaration()||n.isImportSpecifier()||n.isExportDeclaration()||n.isJSXAttribute()||n.isCallExpression()&&c.isMemberExpression(n.node.callee)&&c.isIdentifier(n.node.callee.object)&&n.node.callee.object.name===`console`&&c.isIdentifier(n.node.callee.property)&&n.node.callee.property.name===`log`||n.isObjectProperty()&&n.node.key===e.node||n.isMemberExpression()&&n.node.property===e.node)return;let r=w(e),a=i.getOrGenerateKey(t.trim(),r,d,g);_.push({path:e,key:a,type:`string-literal`,componentKey:r})}});let T=new Set;for(let e of C)if(_.some(t=>{let n=t.path;for(;n;){if(n.node===e.node)return!0;n=n.parentPath}return!1})){let t=y.get(e.node),n=!1,i=e.parentPath;for(;i;){let e=C.find(e=>e.node===i?.node);if(e&&y.get(e.node)===t){let t=_.some(t=>{let n=t.path;for(;n;){if(n.node===e.node)return!0;n=n.parentPath}return!1}),i=r.getExistingIntlayerInfo(e);if(t||i){n=!0;break}}i=i.parentPath}n||T.add(e)}return{extractedContent:g,replacements:_,componentsNeedingHooks:T,componentKeyMap:y,componentPaths:C,hookMap:b,isSolid:!1}},f=(e,t,n,r,i,a={})=>{let{extractedContent:o,replacements:s}=d(e,t,n,`default`,r,i,a),c={};for(let e of Object.values(o))Object.assign(c,e);return{extractedContent:c,replacements:s}};exports.extractBabelContentForComponents=d,exports.extractTsContent=f,exports.handleJsxInsertionBabel=u;
|
|
2
|
+
//# sourceMappingURL=babelProcessor.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"babelProcessor.cjs","names":["_traverse","t","shouldExtract","getOrGenerateKey","getExistingIntlayerInfo","resolveDictionaryKey","getComponentName","ATTRIBUTES_TO_EXTRACT"],"sources":["../../../src/extractContent/babelProcessor.ts"],"sourcesContent":["import _traverse, { type NodePath } from '@babel/traverse';\nimport * as t from '@babel/types';\nimport type { IntlayerConfig } from '@intlayer/types';\nimport { resolveDictionaryKey } from '../extractContent/utils';\nimport {\n ATTRIBUTES_TO_EXTRACT,\n getComponentName,\n getExistingIntlayerInfo,\n getOrGenerateKey,\n shouldExtract,\n} from './utils';\n\nexport type BabelReplacement = {\n path: NodePath;\n key: string;\n type:\n | 'jsx-text'\n | 'jsx-attribute'\n | 'string-literal'\n | 'jsx-insertion'\n | 'jsx-text-combined';\n componentKey: string;\n childrenToReplace?: t.Node[];\n variables?: string[];\n};\n\n// CJS/ESM interop: @babel/traverse exports its function as `.default` in CJS bundles\nconst traverse = (\n typeof _traverse === 'function' ? _traverse : (_traverse as any).default\n) as typeof _traverse;\n\n/**\n * Handles JSX insertions (elements with multiple children, including expressions).\n * Replaces complex JSX structures with variable-based translations.\n */\nexport const handleJsxInsertionBabel = (\n path: NodePath<t.JSXElement | t.JSXFragment>,\n fileCode: string,\n existingKeys: Set<string>,\n getComponentKeyForPath: (path: NodePath) => string,\n extractedContent: Record<string, Record<string, string>>,\n replacements: BabelReplacement[],\n handledNodes: Set<t.Node>\n): boolean => {\n const children = path.node.children;\n\n if (children.length <= 1) return false;\n\n const parts: {\n type: 'text' | 'var';\n value: string;\n originalExpr?: string;\n }[] = [];\n let hasSignificantText = false;\n let hasVariables = false;\n\n for (const child of children) {\n if (t.isJSXText(child)) {\n const text = child.value;\n\n if (text.trim().length > 0) hasSignificantText = true;\n\n parts.push({ type: 'text', value: text });\n } else if (t.isJSXExpressionContainer(child)) {\n if (t.isJSXEmptyExpression(child.expression)) {\n parts.push({ type: 'text', value: '' });\n } else {\n const expr = child.expression;\n\n if (t.isIdentifier(expr)) {\n parts.push({\n type: 'var',\n value: expr.name,\n originalExpr: expr.name,\n });\n hasVariables = true;\n } else if (t.isMemberExpression(expr)) {\n const code = fileCode.substring(expr.start!, expr.end!);\n\n const varName = t.isIdentifier(expr.property)\n ? expr.property.name\n : 'var';\n\n parts.push({ type: 'var', value: varName, originalExpr: code });\n\n hasVariables = true;\n } else {\n return false;\n }\n }\n } else {\n return false;\n }\n }\n\n if (!hasSignificantText) return false;\n\n let combinedString = '';\n for (const part of parts) {\n if (part.type === 'var') combinedString += `{{${part.value}}}`;\n else combinedString += part.value;\n }\n\n const cleanString = combinedString.replace(/\\s+/g, ' ').trim();\n\n if (shouldExtract(cleanString)) {\n const componentKey = getComponentKeyForPath(path);\n const key = getOrGenerateKey(\n cleanString,\n componentKey,\n existingKeys,\n extractedContent\n );\n\n const varMap = parts\n .filter((part) => part.type === 'var')\n .map((part) => `${part.value}: ${part.originalExpr}`);\n const uniqueVars = Array.from(new Set(varMap));\n\n if (hasVariables) {\n replacements.push({\n path,\n key,\n type: 'jsx-insertion',\n componentKey,\n childrenToReplace: children,\n variables: uniqueVars,\n });\n } else {\n replacements.push({\n path,\n key,\n type: 'jsx-text-combined',\n componentKey,\n childrenToReplace: children,\n });\n }\n\n children.forEach((child) => {\n handledNodes.add(child);\n });\n return true;\n }\n\n return false;\n};\n\n/**\n * Traverses the AST to identify components and extract content.\n * Returns extraction results and metadata about which components need Intlayer hooks.\n */\nexport const extractBabelContentForComponents = (\n ast: t.File,\n fileCode: string,\n existingKeys: Set<string>,\n defaultKey: string = 'default',\n configuration: IntlayerConfig,\n filePath: string,\n unmergedDictionaries: Record<string, unknown> = {}\n): {\n extractedContent: Record<string, Record<string, string>>;\n replacements: BabelReplacement[];\n componentsNeedingHooks: Set<NodePath>;\n componentKeyMap: Map<t.Node, string>;\n componentPaths: NodePath[];\n hookMap: Map<t.Node, 'useIntlayer' | 'getIntlayer'>;\n isSolid: boolean;\n} => {\n const extractedContent: Record<string, Record<string, string>> = {};\n const replacements: BabelReplacement[] = [];\n const handledNodes = new Set<t.Node>();\n const componentKeyMap = new Map<t.Node, string>();\n const hookMap = new Map<t.Node, 'useIntlayer' | 'getIntlayer'>();\n const usedKeysInFile = new Set<string>();\n let globalFileKey: string | undefined;\n\n const componentPaths: NodePath[] = [];\n\n traverse(ast, {\n FunctionDeclaration(path) {\n componentPaths.push(path);\n },\n ArrowFunctionExpression(path) {\n componentPaths.push(path);\n },\n FunctionExpression(path) {\n componentPaths.push(path);\n },\n });\n\n for (const path of componentPaths) {\n const existingInfo = getExistingIntlayerInfo(path);\n\n if (existingInfo) {\n componentKeyMap.set(path.node, existingInfo.key);\n usedKeysInFile.add(existingInfo.key);\n hookMap.set(path.node, existingInfo.hook);\n } else {\n if (!globalFileKey) {\n globalFileKey = resolveDictionaryKey(\n defaultKey,\n filePath,\n configuration,\n unmergedDictionaries,\n usedKeysInFile\n );\n usedKeysInFile.add(globalFileKey);\n }\n componentKeyMap.set(path.node, globalFileKey);\n\n const compName = getComponentName(path);\n const isComponent = compName ? /^[A-Z]/.test(compName) : false;\n hookMap.set(path.node, isComponent ? 'useIntlayer' : 'getIntlayer');\n }\n }\n\n const getComponentKeyForPath = (path: NodePath): string => {\n let current: NodePath | null = path;\n while (current) {\n if (componentKeyMap.has(current.node)) {\n return componentKeyMap.get(current.node)!;\n }\n current = current.parentPath;\n }\n return globalFileKey || defaultKey;\n };\n\n traverse(ast, {\n JSXElement(path) {\n if (handledNodes.has(path.node)) return;\n\n handleJsxInsertionBabel(\n path,\n fileCode,\n existingKeys,\n getComponentKeyForPath,\n extractedContent,\n replacements,\n handledNodes\n );\n },\n JSXFragment(path) {\n if (handledNodes.has(path.node)) return;\n\n handleJsxInsertionBabel(\n path,\n fileCode,\n existingKeys,\n getComponentKeyForPath,\n extractedContent,\n replacements,\n handledNodes\n );\n },\n JSXText(path) {\n if (handledNodes.has(path.node)) return;\n\n const text = path.node.value;\n\n if (shouldExtract(text)) {\n const componentKey = getComponentKeyForPath(path);\n const key = getOrGenerateKey(\n text.replace(/\\s+/g, ' ').trim(),\n componentKey,\n existingKeys,\n extractedContent\n );\n replacements.push({ path, key, type: 'jsx-text', componentKey });\n }\n },\n JSXAttribute(path) {\n if (handledNodes.has(path.node)) return;\n\n const name = path.node.name.name;\n\n if (\n typeof name !== 'string' ||\n !ATTRIBUTES_TO_EXTRACT.includes(name as any)\n )\n return;\n const value = path.node.value;\n\n if (t.isStringLiteral(value) && shouldExtract(value.value)) {\n const componentKey = getComponentKeyForPath(path);\n const key = getOrGenerateKey(\n value.value.trim(),\n componentKey,\n existingKeys,\n extractedContent\n );\n replacements.push({ path, key, type: 'jsx-attribute', componentKey });\n }\n },\n StringLiteral(path) {\n if (handledNodes.has(path.node)) return;\n\n const text = path.node.value;\n\n if (!shouldExtract(text)) return;\n\n const parent = path.parentPath;\n\n if (\n parent.isImportDeclaration() ||\n parent.isImportSpecifier() ||\n parent.isExportDeclaration()\n )\n return;\n\n if (parent.isJSXAttribute()) return;\n\n if (\n parent.isCallExpression() &&\n t.isMemberExpression(parent.node.callee)\n ) {\n if (\n t.isIdentifier(parent.node.callee.object) &&\n parent.node.callee.object.name === 'console' &&\n t.isIdentifier(parent.node.callee.property) &&\n parent.node.callee.property.name === 'log'\n ) {\n return;\n }\n }\n\n if (parent.isObjectProperty() && parent.node.key === path.node) return;\n\n if (parent.isMemberExpression() && parent.node.property === path.node)\n return;\n\n const componentKey = getComponentKeyForPath(path);\n const key = getOrGenerateKey(\n text.trim(),\n componentKey,\n existingKeys,\n extractedContent\n );\n replacements.push({ path, key, type: 'string-literal', componentKey });\n },\n });\n\n const componentsNeedingHooks = new Set<NodePath>();\n for (const componentPath of componentPaths) {\n const hasReplacements = replacements.some((replacement) => {\n let current: NodePath | null = replacement.path;\n while (current) {\n if (current.node === componentPath.node) return true;\n\n current = current.parentPath;\n }\n return false;\n });\n\n if (hasReplacements) {\n const key = componentKeyMap.get(componentPath.node)!;\n let ancestorProvidesKey = false;\n let currentPath: NodePath | null = componentPath.parentPath;\n while (currentPath) {\n const ancestorPath = componentPaths.find(\n (path) => path.node === currentPath?.node\n );\n\n if (ancestorPath) {\n const ancestorKey = componentKeyMap.get(ancestorPath.node);\n\n if (ancestorKey === key) {\n const ancestorHasReplacements = replacements.some((replacement) => {\n let rPath: NodePath | null = replacement.path;\n while (rPath) {\n if (rPath.node === ancestorPath.node) return true;\n\n rPath = rPath.parentPath;\n }\n return false;\n });\n const existingInfo = getExistingIntlayerInfo(ancestorPath);\n\n if (ancestorHasReplacements || existingInfo) {\n ancestorProvidesKey = true;\n break;\n }\n }\n }\n currentPath = currentPath.parentPath;\n }\n\n if (!ancestorProvidesKey) {\n componentsNeedingHooks.add(componentPath);\n }\n }\n }\n\n return {\n extractedContent,\n replacements,\n componentsNeedingHooks,\n componentKeyMap,\n componentPaths,\n hookMap,\n isSolid: false,\n };\n};\n\n/**\n * High-level function to extract content from TS/JS/JSX/TSX AST.\n */\nexport const extractTsContent = (\n ast: t.File,\n fileCode: string,\n existingKeys: Set<string>,\n configuration: IntlayerConfig,\n filePath: string,\n unmergedDictionaries: Record<string, unknown> = {}\n): {\n extractedContent: Record<string, string>;\n replacements: BabelReplacement[];\n} => {\n const { extractedContent, replacements } = extractBabelContentForComponents(\n ast,\n fileCode,\n existingKeys,\n 'default',\n configuration,\n filePath,\n unmergedDictionaries\n );\n\n const flatContent: Record<string, string> = {};\n for (const group of Object.values(extractedContent)) {\n Object.assign(flatContent, group);\n }\n\n return { extractedContent: flatContent, replacements };\n};\n"],"mappings":"kdA2BA,MAAM,EACJ,OAAOA,EAAAA,SAAc,WAAaA,EAAAA,QAAaA,EAAAA,QAAkB,QAOtD,GACX,EACA,EACA,EACA,EACA,EACA,EACA,IACY,CACZ,IAAM,EAAW,EAAK,KAAK,SAE3B,GAAI,EAAS,QAAU,EAAG,MAAO,GAEjC,IAAM,EAIA,EAAE,CACJ,EAAqB,GACrB,EAAe,GAEnB,IAAK,IAAM,KAAS,EAClB,GAAIC,EAAE,UAAU,EAAM,CAAE,CACtB,IAAM,EAAO,EAAM,MAEf,EAAK,MAAM,CAAC,OAAS,IAAG,EAAqB,IAEjD,EAAM,KAAK,CAAE,KAAM,OAAQ,MAAO,EAAM,CAAC,SAChCA,EAAE,yBAAyB,EAAM,CAC1C,GAAIA,EAAE,qBAAqB,EAAM,WAAW,CAC1C,EAAM,KAAK,CAAE,KAAM,OAAQ,MAAO,GAAI,CAAC,KAClC,CACL,IAAM,EAAO,EAAM,WAEnB,GAAIA,EAAE,aAAa,EAAK,CACtB,EAAM,KAAK,CACT,KAAM,MACN,MAAO,EAAK,KACZ,aAAc,EAAK,KACpB,CAAC,CACF,EAAe,WACNA,EAAE,mBAAmB,EAAK,CAAE,CACrC,IAAM,EAAO,EAAS,UAAU,EAAK,MAAQ,EAAK,IAAK,CAEjD,EAAUA,EAAE,aAAa,EAAK,SAAS,CACzC,EAAK,SAAS,KACd,MAEJ,EAAM,KAAK,CAAE,KAAM,MAAO,MAAO,EAAS,aAAc,EAAM,CAAC,CAE/D,EAAe,QAEf,MAAO,QAIX,MAAO,GAIX,GAAI,CAAC,EAAoB,MAAO,GAEhC,IAAI,EAAiB,GACrB,IAAK,IAAM,KAAQ,EACb,EAAK,OAAS,MAAO,GAAkB,KAAK,EAAK,MAAM,IACtD,GAAkB,EAAK,MAG9B,IAAM,EAAc,EAAe,QAAQ,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IAAM,EAA0B,EAAa,KAAM,GAAgB,CACjE,IAAI,EAAyB,EAAY,KACzC,KAAO,GAAO,CACZ,GAAI,EAAM,OAAS,EAAa,KAAM,MAAO,GAE7C,EAAQ,EAAM,WAEhB,MAAO,IACP,CACI,EAAeC,EAAAA,wBAAwB,EAAa,CAE1D,GAAI,GAA2B,EAAc,CAC3C,EAAsB,GACtB,OAIN,EAAc,EAAY,WAGvB,GACH,EAAuB,IAAI,EAAc,CAK/C,MAAO,CACL,mBACA,eACA,yBACA,kBACA,iBACA,UACA,QAAS,GACV,EAMU,GACX,EACA,EACA,EACA,EACA,EACA,EAAgD,EAAE,GAI/C,CACH,GAAM,CAAE,mBAAkB,gBAAiB,EACzC,EACA,EACA,EACA,UACA,EACA,EACA,EACD,CAEK,EAAsC,EAAE,CAC9C,IAAK,IAAM,KAAS,OAAO,OAAO,EAAiB,CACjD,OAAO,OAAO,EAAa,EAAM,CAGnC,MAAO,CAAE,iBAAkB,EAAa,eAAc"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`../_virtual/_rolldown/runtime.cjs`);let e=require(`node:path`),t=require(`@intlayer/chokidar/cli`),n=require(`@intlayer/unmerged-dictionaries-entry`);const r=(t,r,i,a)=>{let{baseDir:o,fileExtensions:s}=i.content,c=((0,n.getUnmergedDictionaries)(i)??{})[r];if(c?.[0]?.filePath)return{contentFilePath:(0,e.resolve)(o,c[0].filePath),relativeContentFilePath:c[0].filePath};let l=a?(0,e.resolve)(a):(0,e.dirname)(t),u=s[0],d=u.startsWith(`.`)?u:`.${u}`,f=(0,e.basename)(t,(0,e.extname)(t)),p=(0,e.join)(l,`${f.charAt(0).toLowerCase()+f.slice(1)}${d}`);return{contentFilePath:p,relativeContentFilePath:(0,e.relative)(o,p)}},i=(e,t,n,r)=>{let{defaultLocale:i}=r.internationalization;if(r?.dictionary?.locale)return{key:t,content:e,locale:i,filePath:n};let a={};for(let[t,n]of Object.entries(e))a[t]={nodeType:`translation`,translation:{[i]:n}};return{key:t,content:a,filePath:n}},a=async(n,a,o,s,c)=>{let{contentFilePath:l,relativeContentFilePath:u}=r(o,a,s,c);return await(0,t.writeContentDeclaration)(i(n,a,u,s),s,{newDictionariesPath:(0,e.relative)(s.content.baseDir,(0,e.dirname)(l))}),l};exports.createDictionary=i,exports.resolveContentFilePath=r,exports.writeContentHelper=a;
|
|
2
|
+
//# sourceMappingURL=contentWriter.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contentWriter.cjs","names":[],"sources":["../../../src/extractContent/contentWriter.ts"],"sourcesContent":["import { basename, dirname, extname, join, relative, resolve } from 'node:path';\nimport { writeContentDeclaration } from '@intlayer/chokidar/cli';\nimport type {\n Dictionary,\n DictionaryKey,\n IntlayerConfig,\n} from '@intlayer/types';\nimport { getUnmergedDictionaries } from '@intlayer/unmerged-dictionaries-entry';\n\nexport type TranslationNode = {\n nodeType: 'translation';\n translation: Record<string, string>;\n};\n\n/**\n * Resolves the path for the content file associated with a component.\n * Checks for existing dictionaries first.\n */\nexport const resolveContentFilePath = (\n filePath: string,\n componentKey: string,\n configuration: IntlayerConfig,\n outputDir?: string\n): { contentFilePath: string; relativeContentFilePath: string } => {\n const { baseDir, fileExtensions } = configuration.content;\n const unmergedDictionaries = getUnmergedDictionaries(configuration) ?? {};\n const existingDicts = unmergedDictionaries[componentKey] as\n | Dictionary[]\n | undefined;\n\n if (existingDicts?.[0]?.filePath) {\n return {\n contentFilePath: resolve(baseDir, existingDicts[0].filePath),\n relativeContentFilePath: existingDicts[0].filePath,\n };\n }\n\n const dirName = outputDir ? resolve(outputDir) : dirname(filePath);\n const firstExtension = fileExtensions[0];\n const extension = firstExtension.startsWith('.')\n ? firstExtension\n : `.${firstExtension}`;\n const originalFileBase = basename(filePath, extname(filePath));\n const contentBaseName =\n originalFileBase.charAt(0).toLowerCase() + originalFileBase.slice(1);\n const contentFilePath = join(dirName, `${contentBaseName}${extension}`);\n const relativeContentFilePath = relative(baseDir, contentFilePath);\n\n return { contentFilePath, relativeContentFilePath };\n};\n\n/**\n * Creates a dictionary object from extracted content.\n * Handles both single-locale and multi-locale formats based on configuration.\n */\nexport const createDictionary = (\n extractedContent: Record<string, string>,\n componentKey: string,\n relativeContentFilePath: string,\n configuration: IntlayerConfig\n): Dictionary => {\n const { defaultLocale } = configuration.internationalization;\n const isPerLocaleFile = configuration?.dictionary?.locale;\n\n if (isPerLocaleFile) {\n return {\n key: componentKey,\n content: extractedContent,\n locale: defaultLocale,\n filePath: relativeContentFilePath,\n } as Dictionary;\n }\n\n const multilingualContent: Record<string, TranslationNode> = {};\n\n for (const [key, value] of Object.entries(extractedContent)) {\n multilingualContent[key] = {\n nodeType: 'translation',\n translation: {\n [defaultLocale]: value,\n },\n };\n }\n\n return {\n key: componentKey as DictionaryKey,\n content: multilingualContent,\n filePath: relativeContentFilePath,\n } as Dictionary;\n};\n\n/**\n * Helper to write extracted content to a dictionary file.\n */\nexport const writeContentHelper = async (\n extractedContent: Record<string, string>,\n componentKey: string,\n filePath: string,\n configuration: IntlayerConfig,\n outputDir?: string\n): Promise<string> => {\n const { contentFilePath, relativeContentFilePath } = resolveContentFilePath(\n filePath,\n componentKey,\n configuration,\n outputDir\n );\n\n const dictionary = createDictionary(\n extractedContent,\n componentKey,\n relativeContentFilePath,\n configuration\n );\n\n const relativeDir = relative(\n configuration.content.baseDir,\n dirname(contentFilePath)\n );\n\n await writeContentDeclaration(dictionary, configuration, {\n newDictionariesPath: relativeDir,\n });\n\n return contentFilePath;\n};\n"],"mappings":"kOAkBA,MAAa,GACX,EACA,EACA,EACA,IACiE,CACjE,GAAM,CAAE,UAAS,kBAAmB,EAAc,QAE5C,IAAA,EAAA,EAAA,yBAD+C,EAAc,EAAI,EAAE,EAC9B,GAI3C,GAAI,IAAgB,IAAI,SACtB,MAAO,CACL,iBAAA,EAAA,EAAA,SAAyB,EAAS,EAAc,GAAG,SAAS,CAC5D,wBAAyB,EAAc,GAAG,SAC3C,CAGH,IAAM,EAAU,GAAA,EAAA,EAAA,SAAoB,EAAU,EAAA,EAAA,EAAA,SAAW,EAAS,CAC5D,EAAiB,EAAe,GAChC,EAAY,EAAe,WAAW,IAAI,CAC5C,EACA,IAAI,IACF,GAAA,EAAA,EAAA,UAA4B,GAAA,EAAA,EAAA,SAAkB,EAAS,CAAC,CAGxD,GAAA,EAAA,EAAA,MAAuB,EAAS,GADpC,EAAiB,OAAO,EAAE,CAAC,aAAa,CAAG,EAAiB,MAAM,EAAE,GACX,IAAY,CAGvE,MAAO,CAAE,kBAAiB,yBAAA,EAAA,EAAA,UAFe,EAAS,EAAgB,CAEf,EAOxC,GACX,EACA,EACA,EACA,IACe,CACf,GAAM,CAAE,iBAAkB,EAAc,qBAGxC,GAFwB,GAAe,YAAY,OAGjD,MAAO,CACL,IAAK,EACL,QAAS,EACT,OAAQ,EACR,SAAU,EACX,CAGH,IAAM,EAAuD,EAAE,CAE/D,IAAK,GAAM,CAAC,EAAK,KAAU,OAAO,QAAQ,EAAiB,CACzD,EAAoB,GAAO,CACzB,SAAU,cACV,YAAa,EACV,GAAgB,EAClB,CACF,CAGH,MAAO,CACL,IAAK,EACL,QAAS,EACT,SAAU,EACX,EAMU,EAAqB,MAChC,EACA,EACA,EACA,EACA,IACoB,CACpB,GAAM,CAAE,kBAAiB,2BAA4B,EACnD,EACA,EACA,EACA,EACD,CAkBD,OAJA,MAAA,EAAA,EAAA,yBAZmB,EACjB,EACA,EACA,EACA,EACD,CAOyC,EAAe,CACvD,qBAAA,EAAA,EAAA,UALA,EAAc,QAAQ,SAAA,EAAA,EAAA,SACd,EAAgB,CACzB,CAIA,CAAC,CAEK"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`../_virtual/_rolldown/runtime.cjs`);const e=require(`./utils/constants.cjs`),t=require(`./utils/extractDictionaryKey.cjs`),n=require(`./utils/generateKey.cjs`),r=require(`./utils/shouldExtract.cjs`),i=require(`./babelProcessor.cjs`),a=require(`./processTsxFile.cjs`),o=require(`./contentWriter.cjs`);let s=require(`node:path`),c=require(`@intlayer/config/node`),l=require(`node:child_process`),u=require(`node:fs`),d=require(`@intlayer/chokidar/cli`),f=require(`@intlayer/unmerged-dictionaries-entry`),p=require(`@intlayer/config/logger`),m=null,h=null;const g=async(g,_,v)=>{let y=!v?.declarationOnly,b=!v?.codeOnly&&!v?.onExtract,x=v?.configuration??(0,c.getConfiguration)(v?.configOptions),S=(0,p.getAppLogger)(x),{baseDir:C}=x.content,w=v?.unmergedDictionaries??(0,f.getUnmergedDictionaries)(x),T=v?.code??(0,u.readFileSync)(g,`utf-8`),E=t.extractDictionaryKey(g,T),D=(0,s.extname)(g);(0,s.basename)(g,D);let O=(0,s.relative)(C,g),k=null,A=null;if(D===`.vue`)try{m??=await import(`@intlayer/vue-compiler`);let a=await m.processVueFile(g,E,_,{generateKey:n.generateKey,shouldExtract:r.shouldExtract,attributesToExtract:e.ATTRIBUTES_TO_EXTRACT,extractDictionaryKeyFromPath:t.extractDictionaryKeyFromPath,extractTsContent:(e,t,n,r,a)=>i.extractTsContent(e,t,n,r,a,w)},y);a&&(k={[E]:a})}catch(e){throw e.code===`ERR_MODULE_NOT_FOUND`||e.message?.includes(`Cannot find module`)?Error(`Please install ${(0,p.colorizePath)(`@intlayer/vue-compiler`,p.ANSIColors.YELLOW)} to process Vue files.`):e}else if(D===`.svelte`)try{h??=await import(`@intlayer/svelte-compiler`);let a=await h.processSvelteFile(g,E,_,{generateKey:n.generateKey,shouldExtract:r.shouldExtract,attributesToExtract:e.ATTRIBUTES_TO_EXTRACT,extractDictionaryKeyFromPath:t.extractDictionaryKeyFromPath,extractTsContent:(e,t,n,r,a)=>i.extractTsContent(e,t,n,r,a,w)},y);a&&(k={[E]:a})}catch(e){throw e.code===`ERR_MODULE_NOT_FOUND`||e.message?.includes(`Cannot find module`)?Error(`Please install ${(0,p.colorizePath)(`@intlayer/svelte-compiler`,p.ANSIColors.YELLOW)} to process Svelte files.`):e}else if([`.tsx`,`.jsx`,`.ts`,`.js`].includes(D))try{let e=a.processTsxFile(g,E,_,x,y,w,T);e&&(k=e.extractedContent,A=e.modifiedCode)}catch(e){throw e.code===`ERR_MODULE_NOT_FOUND`||e.message?.includes(`Cannot find module`)?Error(`Please install ${(0,p.colorizePath)(`@intlayer/babel`,p.ANSIColors.YELLOW)} to process TSX/JSX/TS/JS files.`):e}if(!k||Object.keys(k).length===0){S(`No extractable text found in ${(0,p.colorizePath)(O)}`);return}if(v?.onExtract)for(let[e,t]of Object.entries(k))await v.onExtract({key:e,content:t,filePath:g});if(b)for(let[e,t]of Object.entries(k)){let n=await o.writeContentHelper(t,e,g,x,v?.outputDir);S(`Created content file: ${(0,p.colorizePath)((0,s.relative)(x.content.baseDir,n))}`)}if(y){let e=(0,d.detectFormatCommand)(x);if(e)try{(0,l.execSync)(e.replace(`{{file}}`,g),{stdio:`inherit`,cwd:C})}catch(e){console.error(e)}S(`Updated component: ${(0,p.colorizePath)((0,s.relative)(C,g))}`)}return{transformedCode:A}};exports.extractContent=g;
|
|
2
|
+
//# sourceMappingURL=extractContent.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extractContent.cjs","names":["extractDictionaryKey","ATTRIBUTES_TO_EXTRACT","extractTsContent","ANSIColors","processTsxFile","writeContentHelper"],"sources":["../../../src/extractContent/extractContent.ts"],"sourcesContent":["import { execSync } from 'node:child_process';\nimport { readFileSync } from 'node:fs';\nimport { basename, extname, relative } from 'node:path';\nimport { detectFormatCommand } from '@intlayer/chokidar/cli';\nimport {\n ANSIColors,\n colorizePath,\n getAppLogger,\n} from '@intlayer/config/logger';\nimport {\n type GetConfigurationOptions,\n getConfiguration,\n} from '@intlayer/config/node';\nimport type { IntlayerConfig } from '@intlayer/types';\nimport { getUnmergedDictionaries } from '@intlayer/unmerged-dictionaries-entry';\nimport { extractTsContent } from './babelProcessor';\nimport { writeContentHelper } from './contentWriter';\nimport { processTsxFile } from './processTsxFile';\nimport {\n ATTRIBUTES_TO_EXTRACT,\n extractDictionaryKeyFromPath,\n type PackageName,\n shouldExtract,\n} from './utils';\nimport { extractDictionaryKey } from './utils/extractDictionaryKey';\nimport { generateKey } from './utils/generateKey';\n\nexport type ExtractIntlayerOptions = {\n configOptions?: GetConfigurationOptions;\n outputDir?: string;\n /**\n * If true, only transform the source file — skip writing content declarations.\n */\n codeOnly?: boolean;\n /**\n * If true, only write content declarations — skip transforming the source file.\n * When set, `transformedCode` will still be returned (computed but not written to disk).\n */\n declarationOnly?: boolean;\n unmergedDictionaries?: Record<string, unknown>;\n configuration?: IntlayerConfig;\n /**\n * Raw source code to process instead of reading from disk.\n * Useful for Vite/webpack transform hooks where the code may already\n * have been modified by a previous plugin.\n */\n code?: string;\n /**\n * Callback invoked for each extracted dictionary key/content pair.\n * When provided, the caller is responsible for writing content declarations\n * (the built-in `writeContentHelper` is skipped unless `writeContent` is also true).\n * Used by Vite/webpack plugins that manage their own dictionary write pipeline.\n */\n onExtract?: (result: {\n key: string;\n content: Record<string, string>;\n filePath: string;\n }) => void | Promise<void>;\n};\n\n// Module caches for optional compiler packages\nlet vueCompiler: typeof import('@intlayer/vue-compiler') | null = null;\nlet svelteCompiler: typeof import('@intlayer/svelte-compiler') | null = null;\n\n/**\n * Extracts Intlayer content from a single source file and optionally transforms it.\n *\n * - For `.vue` / `.svelte` files the matching optional compiler package is used.\n * - For `.tsx` / `.jsx` / `.ts` / `.js` files the Babel-based `processTsxFile` is used.\n *\n * Returns `{ transformedCode }` so callers (e.g. the Vite plugin) can pass the\n * modified source back to the bundler without writing the file to disk.\n */\nexport const extractContent = async (\n filePath: string,\n packageName: PackageName,\n options?: ExtractIntlayerOptions\n): Promise<{ transformedCode: string | null } | undefined> => {\n const saveComponent = !options?.declarationOnly;\n const writeContent = !options?.codeOnly && !options?.onExtract;\n\n const configuration =\n options?.configuration ?? getConfiguration(options?.configOptions);\n const appLogger = getAppLogger(configuration);\n const { baseDir } = configuration.content;\n\n const unmergedDictionaries =\n options?.unmergedDictionaries ?? getUnmergedDictionaries(configuration);\n\n const fileText = options?.code ?? readFileSync(filePath, 'utf-8');\n const componentKey = extractDictionaryKey(filePath, fileText);\n const ext = extname(filePath);\n const fileBaseName = basename(filePath, ext);\n const relativeFilePath = relative(baseDir, filePath);\n\n let extractedContentMap: Record<string, Record<string, string>> | null = null;\n let transformedCode: string | null = null;\n\n if (ext === '.vue') {\n try {\n vueCompiler ??= await import('@intlayer/vue-compiler');\n\n const res = await vueCompiler.processVueFile(\n filePath,\n componentKey,\n packageName,\n {\n generateKey,\n shouldExtract,\n attributesToExtract: ATTRIBUTES_TO_EXTRACT,\n extractDictionaryKeyFromPath,\n extractTsContent: (\n ast: any,\n code: string,\n keys: Set<string>,\n config: any,\n path: string\n ) =>\n extractTsContent(\n ast,\n code,\n keys,\n config,\n path,\n unmergedDictionaries\n ),\n },\n saveComponent\n );\n\n if (res) {\n extractedContentMap = {\n [componentKey]: res as Record<string, string>,\n };\n }\n } catch (error: any) {\n if (\n error.code === 'ERR_MODULE_NOT_FOUND' ||\n error.message?.includes('Cannot find module')\n ) {\n throw new Error(\n `Please install ${colorizePath('@intlayer/vue-compiler', ANSIColors.YELLOW)} to process Vue files.`\n );\n }\n throw error;\n }\n } else if (ext === '.svelte') {\n try {\n svelteCompiler ??= await import('@intlayer/svelte-compiler');\n\n const res = await svelteCompiler.processSvelteFile(\n filePath,\n componentKey,\n packageName,\n {\n generateKey,\n shouldExtract,\n attributesToExtract: ATTRIBUTES_TO_EXTRACT,\n extractDictionaryKeyFromPath,\n extractTsContent: (\n ast: any,\n code: string,\n keys: Set<string>,\n config: any,\n path: string\n ) =>\n extractTsContent(\n ast,\n code,\n keys,\n config,\n path,\n unmergedDictionaries\n ),\n },\n saveComponent\n );\n\n if (res) {\n extractedContentMap = {\n [componentKey]: res as Record<string, string>,\n };\n }\n } catch (error: any) {\n if (\n error.code === 'ERR_MODULE_NOT_FOUND' ||\n error.message?.includes('Cannot find module')\n ) {\n throw new Error(\n `Please install ${colorizePath('@intlayer/svelte-compiler', ANSIColors.YELLOW)} to process Svelte files.`\n );\n }\n throw error;\n }\n } else if (['.tsx', '.jsx', '.ts', '.js'].includes(ext)) {\n try {\n const result = processTsxFile(\n filePath,\n componentKey,\n packageName,\n configuration,\n saveComponent,\n unmergedDictionaries,\n fileText\n );\n\n if (result) {\n extractedContentMap = result.extractedContent;\n transformedCode = result.modifiedCode;\n }\n } catch (error: any) {\n if (\n error.code === 'ERR_MODULE_NOT_FOUND' ||\n error.message?.includes('Cannot find module')\n ) {\n throw new Error(\n `Please install ${colorizePath('@intlayer/babel', ANSIColors.YELLOW)} to process TSX/JSX/TS/JS files.`\n );\n }\n throw error;\n }\n }\n\n if (!extractedContentMap || Object.keys(extractedContentMap).length === 0) {\n appLogger(`No extractable text found in ${colorizePath(relativeFilePath)}`);\n return undefined;\n }\n\n // Notify caller via callback (e.g. Vite plugin's own dictionary pipeline)\n if (options?.onExtract) {\n for (const [key, content] of Object.entries(extractedContentMap)) {\n await options.onExtract({ key, content, filePath });\n }\n }\n\n // Write content declarations using the built-in helper when no custom callback\n if (writeContent) {\n for (const [key, content] of Object.entries(extractedContentMap)) {\n const contentFilePath = await writeContentHelper(\n content,\n key,\n filePath,\n configuration,\n options?.outputDir\n );\n\n const relativeContentFilePath = relative(\n configuration.content.baseDir,\n contentFilePath\n );\n appLogger(\n `Created content file: ${colorizePath(relativeContentFilePath)}`\n );\n }\n }\n\n if (saveComponent) {\n const formatCommand = detectFormatCommand(configuration);\n\n if (formatCommand) {\n try {\n execSync(formatCommand.replace('{{file}}', filePath), {\n stdio: 'inherit',\n cwd: baseDir,\n });\n } catch (error) {\n console.error(error);\n }\n }\n\n appLogger(\n `Updated component: ${colorizePath(relative(baseDir, filePath))}`\n );\n }\n\n return { transformedCode };\n};\n"],"mappings":"umBA6DI,EAA8D,KAC9D,EAAoE,KAWxE,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,EACN,CAAC,OACK,EAAO,CACd,QAAQ,MAAM,EAAM,CAIxB,EACE,uBAAA,EAAA,EAAA,eAAA,EAAA,EAAA,UAA4C,EAAS,EAAS,CAAC,GAChE,CAGH,MAAO,CAAE,kBAAiB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./utils/constants.cjs`),t=require(`./utils/detectPackageName.cjs`),n=require(`./extractContent.cjs`);exports.ATTRIBUTES_TO_EXTRACT=e.ATTRIBUTES_TO_EXTRACT,exports.detectPackageName=t.detectPackageName,exports.extractContent=n.extractContent,exports.packageList=e.packageList;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`../_virtual/_rolldown/runtime.cjs`),t=require(`./utils/getExistingIntlayerInfo.cjs`),n=require(`./babelProcessor.cjs`);let r=require(`@babel/parser`),i=require(`node:child_process`),a=require(`node:fs`),o=require(`@babel/traverse`);o=e.__toESM(o);let s=require(`@babel/types`);s=e.__toESM(s);let c=require(`@intlayer/chokidar/cli`);const l=typeof o.default==`function`?o.default:o.default.default,u=(e,o,u,d,f=!0,p={},m)=>{let h=m??(0,a.readFileSync)(e,`utf-8`),g=(0,r.parse)(h,{sourceType:`module`,plugins:[`jsx`,`typescript`]}),_=u===`solid-intlayer`,{extractedContent:v,replacements:y,componentsNeedingHooks:b,componentKeyMap:x,componentPaths:S,hookMap:C}=n.extractBabelContentForComponents(g,h,new Set,o,d,e,p);if(Object.keys(v).length===0)return null;let w=[],T=e=>{let n=e;for(;n;){let e=S.find(e=>e.node===n?.node);if(e){let n=t.getExistingIntlayerInfo(e);if(n)return n.hook;if(b.has(e))return C.get(e.node)||`useIntlayer`}n=n.parentPath}return`useIntlayer`};for(let{path:e,key:t,type:n,variables:r,childrenToReplace:i}of y){let a=_?`content().${t}`:`content.${t}`,o=T(e)===`getIntlayer`?``:`.value`;if(n===`jsx-text`&&e.isJSXText())w.push({start:e.node.start,end:e.node.end,replacement:`{${a}}`});else if(n===`jsx-attribute`&&e.isJSXAttribute()){let t=e.node.value;t&&w.push({start:t.start,end:t.end,replacement:`{${a}${o}}`})}else if(n===`string-literal`&&e.isStringLiteral())w.push({start:e.node.start,end:e.node.end,replacement:`${a}${o}`});else if(n===`jsx-text-combined`&&i&&i.length>0){let e=`{${a}}`,t=i[0].start,n=i[i.length-1].end;w.push({start:t,end:n,replacement:e})}else if(n===`jsx-insertion`&&r&&i&&i.length>0){let e=`{${a}({ ${r.map(e=>{let[t,n]=e.split(`:`).map(e=>e.trim());return`${t}: ${n}`}).join(`, `)} })}`,t=i[0].start,n=i[i.length-1].end;w.push({start:t,end:n,replacement:e})}}let E=!1,D=!1;for(let e of b){let n=x.get(e.node),r=t.getExistingIntlayerInfo(e);if(r)r.hook===`useIntlayer`&&(E=!0),r.hook===`getIntlayer`&&(D=!0);else{let t=C.get(e.node)||`useIntlayer`;t===`useIntlayer`&&(E=!0),t===`getIntlayer`&&(D=!0);let r=e.get(`body`),i=`\n const content = ${t}('${n}');\n`;if(r.isBlockStatement())w.push({start:r.node.start+1,end:r.node.start+1,replacement:i});else if(r.isExpression()){let e=r.node.start,i=r.node.end,a=-1,o=-1;for(let t=e-1;t>=0;t--){let e=h[t];if(e===`(`){a=t;break}if(e!==` `&&e!==`
|
|
2
|
+
`&&e!==`\r`&&e!==` `)break}if(a!==-1)for(let e=i;e<h.length;e++){let t=h[e];if(t===`)`){o=e;break}if(t!==` `&&t!==`
|
|
3
|
+
`&&t!==`\r`&&t!==` `)break}a!==-1&&o!==-1?(w.push({start:o,end:o+1,replacement:`)
|
|
4
|
+
}`}),w.push({start:a,end:a+1,replacement:`{\n const content = ${t}('${n}');\n return (`})):(w.push({start:i,end:i,replacement:`
|
|
5
|
+
}`}),w.push({start:e,end:e,replacement:`{\n const content = ${t}('${n}');\n return `}))}}}let O=(e,t)=>{let n;if(l(g,{ImportDeclaration(e){e.node.source.value===t&&(n=e,e.stop())}}),!n){let n=`import { ${e} } from '${t}';\n`,r=0;if(g.program.body.length>0)r=g.program.body[0].start;else if(g.program.directives&&g.program.directives.length>0){r=g.program.directives[g.program.directives.length-1].end,h[r]===`;`&&r++,w.push({start:r,end:r,replacement:`\n${n}`});return}(r===0||g.program.body.length>0&&r===g.program.body[0].start)&&w.push({start:r,end:r,replacement:n})}else if(!n.node.specifiers.some(t=>s.isImportSpecifier(t)&&s.isIdentifier(t.imported)&&t.imported.name===e)){let t=h.slice(n.node.start,n.node.end),r=t.lastIndexOf(`}`);if(r!==-1){let i=!t.slice(0,r).trim().endsWith(`,`),a=n.node.start+r,o=i?`, `:` `;w.push({start:a,end:a,replacement:`${o}${e} `})}}};E&&O(`useIntlayer`,u),D&&O(`getIntlayer`,`intlayer`),w.sort((e,t)=>t.start===e.start?t.end-e.end:t.start-e.start);let k=h;for(let e of w)k=k.slice(0,e.start)+e.replacement+k.slice(e.end);if(f){(0,a.writeFileSync)(e,k);let t=(0,c.detectFormatCommand)(d);if(t)try{(0,i.execSync)(t.replace(`{{file}}`,e),{stdio:`inherit`,cwd:d.content.baseDir})}catch(e){console.error(e)}}return{extractedContent:v,modifiedCode:k}};exports.processTsxFile=u;
|
|
6
|
+
//# sourceMappingURL=processTsxFile.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"processTsxFile.cjs","names":["_traverse","extractBabelContentForComponents","getExistingIntlayerInfo","t"],"sources":["../../../src/extractContent/processTsxFile.ts"],"sourcesContent":["import { execSync } from 'node:child_process';\nimport { readFileSync, writeFileSync } from 'node:fs';\nimport { parse } from '@babel/parser';\nimport _traverse, { type NodePath } from '@babel/traverse';\nimport * as t from '@babel/types';\nimport { detectFormatCommand } from '@intlayer/chokidar/cli';\nimport type { IntlayerConfig } from '@intlayer/types';\nimport { extractBabelContentForComponents } from './babelProcessor';\nimport { getExistingIntlayerInfo } from './utils';\n\nexport type TextEdit = {\n start: number;\n end: number;\n replacement: string;\n};\n\nconst traverse = (\n typeof _traverse === 'function' ? _traverse : (_traverse as any).default\n) as typeof _traverse;\n\n/**\n * Processes a TSX/JSX/TS/JS file to extract content and inject Intlayer hooks/calls.\n */\nexport const processTsxFile = (\n filePath: string,\n componentKey: string,\n packageName: string,\n configuration: IntlayerConfig,\n save: boolean = true,\n unmergedDictionaries: Record<string, unknown> = {},\n providedFileCode?: string\n): {\n extractedContent: Record<string, Record<string, string>>;\n modifiedCode: string;\n} | null => {\n const fileCode = providedFileCode ?? readFileSync(filePath, 'utf-8');\n\n const ast = parse(fileCode, {\n sourceType: 'module',\n plugins: ['jsx', 'typescript'],\n });\n\n const isSolid = packageName === 'solid-intlayer';\n const existingKeys = new Set<string>();\n\n const {\n extractedContent,\n replacements,\n componentsNeedingHooks,\n componentKeyMap,\n componentPaths,\n hookMap,\n } = extractBabelContentForComponents(\n ast,\n fileCode,\n existingKeys,\n componentKey,\n configuration,\n filePath,\n unmergedDictionaries\n );\n\n if (Object.keys(extractedContent).length === 0) return null;\n\n const textEdits: TextEdit[] = [];\n\n const getProvidingHookType = (\n path: NodePath\n ): 'useIntlayer' | 'getIntlayer' => {\n let current: NodePath | null = path;\n while (current) {\n const functionPath = componentPaths.find(\n (path) => path.node === current?.node\n );\n\n if (functionPath) {\n const existingInfo = getExistingIntlayerInfo(functionPath);\n\n if (existingInfo) {\n return existingInfo.hook;\n }\n\n if (componentsNeedingHooks.has(functionPath)) {\n return hookMap.get(functionPath.node) || 'useIntlayer';\n }\n }\n current = current.parentPath;\n }\n return 'useIntlayer';\n };\n\n for (const {\n path,\n key,\n type,\n variables,\n childrenToReplace,\n } of replacements) {\n const contentAccessCode = isSolid ? `content().${key}` : `content.${key}`;\n const hookType = getProvidingHookType(path);\n const valueSuffix = hookType === 'getIntlayer' ? '' : '.value';\n\n if (type === 'jsx-text' && path.isJSXText()) {\n textEdits.push({\n start: path.node.start!,\n end: path.node.end!,\n replacement: `{${contentAccessCode}}`,\n });\n } else if (type === 'jsx-attribute' && path.isJSXAttribute()) {\n const valNode = path.node.value;\n\n if (valNode) {\n textEdits.push({\n start: valNode.start!,\n end: valNode.end!,\n replacement: `{${contentAccessCode}${valueSuffix}}`,\n });\n }\n } else if (type === 'string-literal' && path.isStringLiteral()) {\n textEdits.push({\n start: path.node.start!,\n end: path.node.end!,\n replacement: `${contentAccessCode}${valueSuffix}`,\n });\n } else if (\n type === 'jsx-text-combined' &&\n childrenToReplace &&\n childrenToReplace.length > 0\n ) {\n const accessStr = `{${contentAccessCode}}`;\n const start = childrenToReplace[0].start!;\n const end = childrenToReplace[childrenToReplace.length - 1].end!;\n textEdits.push({ start, end, replacement: accessStr });\n } else if (\n type === 'jsx-insertion' &&\n variables &&\n childrenToReplace &&\n childrenToReplace.length > 0\n ) {\n const objProps = variables\n .map((variableString) => {\n const [key, variableValue] = variableString\n .split(':')\n .map((part) => part.trim());\n return `${key}: ${variableValue}`;\n })\n .join(', ');\n\n const accessStr = `{${contentAccessCode}({ ${objProps} })}`;\n const start = childrenToReplace[0].start!;\n const end = childrenToReplace[childrenToReplace.length - 1].end!;\n textEdits.push({ start, end, replacement: accessStr });\n }\n }\n\n let needsUseIntlayer = false;\n let needsGetIntlayer = false;\n\n for (const componentPath of componentsNeedingHooks) {\n const finalKey = componentKeyMap.get(componentPath.node)!;\n const existingInfo = getExistingIntlayerInfo(componentPath);\n\n if (existingInfo) {\n if (existingInfo.hook === 'useIntlayer') needsUseIntlayer = true;\n\n if (existingInfo.hook === 'getIntlayer') needsGetIntlayer = true;\n } else {\n const hook = hookMap.get(componentPath.node) || 'useIntlayer';\n\n if (hook === 'useIntlayer') needsUseIntlayer = true;\n\n if (hook === 'getIntlayer') needsGetIntlayer = true;\n\n const bodyPath = componentPath.get('body') as NodePath;\n const hookStatementStr = `\\n const content = ${hook}('${finalKey}');\\n`;\n\n if (bodyPath.isBlockStatement()) {\n textEdits.push({\n start: bodyPath.node.start! + 1,\n end: bodyPath.node.start! + 1,\n replacement: hookStatementStr,\n });\n } else if (bodyPath.isExpression()) {\n const start = bodyPath.node.start!;\n const end = bodyPath.node.end!;\n\n // Detect wrapping parentheses: `() => (expr)` — scan backward from\n // the expression start and forward from its end for matching `(` / `)`.\n let parenStart = -1;\n let parenEnd = -1;\n\n for (let i = start - 1; i >= 0; i--) {\n const ch = fileCode[i];\n if (ch === '(') {\n parenStart = i;\n break;\n }\n if (ch !== ' ' && ch !== '\\n' && ch !== '\\r' && ch !== '\\t') break;\n }\n\n if (parenStart !== -1) {\n for (let i = end; i < fileCode.length; i++) {\n const ch = fileCode[i];\n if (ch === ')') {\n parenEnd = i;\n break;\n }\n if (ch !== ' ' && ch !== '\\n' && ch !== '\\r' && ch !== '\\t') break;\n }\n }\n\n if (parenStart !== -1 && parenEnd !== -1) {\n // Replace the surrounding `(` … `)` with a block statement. We keep\n // the parentheses as `return (…)` to prevent automatic semicolon\n // insertion when the returned expression starts on a new line.\n textEdits.push({\n start: parenEnd,\n end: parenEnd + 1,\n replacement: `)\\n}`,\n });\n textEdits.push({\n start: parenStart,\n end: parenStart + 1,\n replacement: `{\\n const content = ${hook}('${finalKey}');\\n return (`,\n });\n } else {\n textEdits.push({\n start: end,\n end: end,\n replacement: `\\n}`,\n });\n textEdits.push({\n start: start,\n end: start,\n replacement: `{\\n const content = ${hook}('${finalKey}');\\n return `,\n });\n }\n }\n }\n }\n\n const injectImport = (hookName: string, targetPackage: string) => {\n let existingImportPath: NodePath<t.ImportDeclaration> | undefined;\n\n traverse(ast, {\n ImportDeclaration(path) {\n if (path.node.source.value === targetPackage) {\n existingImportPath = path;\n path.stop();\n }\n },\n });\n\n if (!existingImportPath) {\n const newImportStr = `import { ${hookName} } from '${targetPackage}';\\n`;\n let insertPos = 0;\n\n if (ast.program.body.length > 0) {\n insertPos = ast.program.body[0].start!;\n } else if (ast.program.directives && ast.program.directives.length > 0) {\n insertPos =\n ast.program.directives[ast.program.directives.length - 1].end!;\n\n if (fileCode[insertPos] === ';') insertPos++;\n\n textEdits.push({\n start: insertPos,\n end: insertPos,\n replacement: `\\n${newImportStr}`,\n });\n return;\n }\n\n if (\n insertPos === 0 ||\n (ast.program.body.length > 0 &&\n insertPos === ast.program.body[0].start!)\n ) {\n textEdits.push({\n start: insertPos,\n end: insertPos,\n replacement: newImportStr,\n });\n }\n } else {\n const existingSpecifiers = existingImportPath.node.specifiers;\n const missingImport = !existingSpecifiers.some(\n (specifier) =>\n t.isImportSpecifier(specifier) &&\n t.isIdentifier(specifier.imported) &&\n specifier.imported.name === hookName\n );\n\n if (missingImport) {\n const importCode = fileCode.slice(\n existingImportPath.node.start!,\n existingImportPath.node.end!\n );\n const closingBraceIndex = importCode.lastIndexOf('}');\n\n if (closingBraceIndex !== -1) {\n const isCommaNeeded = !importCode\n .slice(0, closingBraceIndex)\n .trim()\n .endsWith(',');\n const absolutePos =\n existingImportPath.node.start! + closingBraceIndex;\n const prefix = isCommaNeeded ? ', ' : ' ';\n textEdits.push({\n start: absolutePos,\n end: absolutePos,\n replacement: `${prefix}${hookName} `,\n });\n }\n }\n }\n };\n\n if (needsUseIntlayer) injectImport('useIntlayer', packageName);\n\n if (needsGetIntlayer) injectImport('getIntlayer', 'intlayer');\n\n textEdits.sort((editA, editB) => {\n if (editB.start !== editA.start) return editB.start - editA.start;\n\n return editB.end - editA.end;\n });\n\n let formattedCode = fileCode;\n\n for (const edit of textEdits) {\n formattedCode =\n formattedCode.slice(0, edit.start) +\n edit.replacement +\n formattedCode.slice(edit.end);\n }\n\n if (save) {\n writeFileSync(filePath, formattedCode);\n\n const formatCommand = detectFormatCommand(configuration);\n\n if (formatCommand) {\n try {\n execSync(formatCommand.replace('{{file}}', filePath), {\n stdio: 'inherit',\n cwd: configuration.content.baseDir,\n });\n } catch (error) {\n console.error(error);\n }\n }\n }\n\n return { extractedContent, modifiedCode: formattedCode };\n};\n"],"mappings":"gaAgBA,MAAM,EACJ,OAAOA,EAAAA,SAAc,WAAaA,EAAAA,QAAaA,EAAAA,QAAkB,QAMtD,GACX,EACA,EAC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AAQ,MAAM,EAAM,EAK1B,MAAO,CAAE,mBAAkB,aAAc,EAAe"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=[`title`,`placeholder`,`alt`,`aria-label`,`label`],t=[`next-intlayer`,`react-intlayer`,`vue-intlayer`,`svelte-intlayer`,`preact-intlayer`,`solid-intlayer`,`angular-intlayer`,`express-intlayer`,`hono-intlayer`,`fastify-intlayer`,`adonis-intlayer`];exports.ATTRIBUTES_TO_EXTRACT=e,exports.packageList=t;
|
|
2
|
+
//# sourceMappingURL=constants.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.cjs","names":[],"sources":["../../../../src/extractContent/utils/constants.ts"],"sourcesContent":["/**\n * Attributes that should be extracted as translatable strings from JSX/HTML elements.\n * This is the single source of truth shared across all Intlayer compiler packages\n * (@intlayer/babel, @intlayer/vue-compiler, @intlayer/svelte-compiler, @intlayer/chokidar).\n */\nexport const ATTRIBUTES_TO_EXTRACT = [\n 'title',\n 'placeholder',\n 'alt',\n 'aria-label',\n 'label',\n] as const;\n\n/**\n * The list of supported Intlayer integration packages.\n * This is the single source of truth for package name validation.\n */\nexport const packageList = [\n 'next-intlayer',\n 'react-intlayer',\n 'vue-intlayer',\n 'svelte-intlayer',\n 'preact-intlayer',\n 'solid-intlayer',\n 'angular-intlayer',\n 'express-intlayer',\n 'hono-intlayer',\n 'fastify-intlayer',\n 'adonis-intlayer',\n] as const;\n\nexport type PackageName = (typeof packageList)[number];\n"],"mappings":"mEAKA,MAAa,EAAwB,CACnC,QACA,cACA,MACA,aACA,QACD,CAMY,EAAc,CACzB,gBACA,iBACA,eACA,kBACA,kBACA,iBACA,mBACA,mBACA,gBACA,mBACA,kBACD"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`../../_virtual/_rolldown/runtime.cjs`);const e=require(`./constants.cjs`);let t=require(`node:path`),n=require(`node:fs`);const r=r=>{let i=r;for(;i!==(0,t.dirname)(i);){let r=(0,t.join)(i,`package.json`);if((0,n.existsSync)(r))try{let t=JSON.parse((0,n.readFileSync)(r,`utf-8`)),i={...t.dependencies,...t.devDependencies,...t.peerDependencies};for(let t of e.packageList)if(i[t])return t}catch{}i=(0,t.dirname)(i)}};exports.detectPackageName=r;
|
|
2
|
+
//# sourceMappingURL=detectPackageName.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"detectPackageName.cjs","names":["packageList"],"sources":["../../../../src/extractContent/utils/detectPackageName.ts"],"sourcesContent":["import { existsSync, readFileSync } from 'node:fs';\nimport { dirname, join } from 'node:path';\nimport { packageList } from './constants';\n\n/**\n * Detects which intlayer package is used in the project by reading package.json.\n */\nexport const detectPackageName = (searchDir: string): string | undefined => {\n let currentDir = searchDir;\n\n while (currentDir !== dirname(currentDir)) {\n const pkgPath = join(currentDir, 'package.json');\n\n if (existsSync(pkgPath)) {\n try {\n const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));\n const allDeps = {\n ...pkg.dependencies,\n ...pkg.devDependencies,\n ...pkg.peerDependencies,\n };\n for (const pkgName of packageList) {\n if (allDeps[pkgName]) return pkgName;\n }\n } catch {\n // Ignore JSON errors\n }\n }\n currentDir = dirname(currentDir);\n }\n\n return undefined;\n};\n"],"mappings":"sMAOA,MAAa,EAAqB,GAA0C,CAC1E,IAAI,EAAa,EAEjB,KAAO,KAAA,EAAA,EAAA,SAAuB,EAAW,EAAE,CACzC,IAAM,GAAA,EAAA,EAAA,MAAe,EAAY,eAAe,CAEhD,IAAA,EAAA,EAAA,YAAe,EAAQ,CACrB,GAAI,CACF,IAAM,EAAM,KAAK,OAAA,EAAA,EAAA,cAAmB,EAAS,QAAQ,CAAC,CAChD,EAAU,CACd,GAAG,EAAI,aACP,GAAG,EAAI,gBACP,GAAG,EAAI,iBACR,CACD,IAAK,IAAM,KAAWA,EAAAA,YACpB,GAAI,EAAQ,GAAU,OAAO,OAEzB,EAIV,GAAA,EAAA,EAAA,SAAqB,EAAW"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`../../_virtual/_rolldown/runtime.cjs`);let e=require(`@intlayer/chokidar/cli`),t=require(`@intlayer/config/utils`);const n=(e,t=`comp-`)=>{let n=e.split(/[\\/]/),r=n.pop()||``,i=r.lastIndexOf(`.`),a=i===-1?r:r.slice(0,i);return a.toLowerCase()===`index`&&(a=n.pop()||a),`${t}${a.replace(/([a-z])([A-Z])/g,`$1-$2`).replace(/[\s_]+/g,`-`).toLowerCase()}`},r=(r,i)=>{let a=(0,e.detectExportedComponentName)(i);return a?(0,t.camelCaseToKebabCase)(a):n(r,``)};exports.extractDictionaryKey=r,exports.extractDictionaryKeyFromPath=n;
|
|
2
|
+
//# sourceMappingURL=extractDictionaryKey.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extractDictionaryKey.cjs","names":[],"sources":["../../../../src/extractContent/utils/extractDictionaryKey.ts"],"sourcesContent":["import { detectExportedComponentName } from '@intlayer/chokidar/cli';\nimport { camelCaseToKebabCase } from '@intlayer/config/utils';\n\n/**\n * Extracts a dictionary key from a file path.\n *\n * Example: \"src/components/MyComponent/index.tsx\" -> \"comp-my-component\"\n */\nexport const extractDictionaryKeyFromPath = (\n filePath: string,\n prefix = 'comp-'\n): string => {\n const pathParts = filePath.split(/[\\\\/]/);\n const fileNameWithExt = pathParts.pop() || '';\n const lastDotIndex = fileNameWithExt.lastIndexOf('.');\n let baseName =\n lastDotIndex !== -1\n ? fileNameWithExt.slice(0, lastDotIndex)\n : fileNameWithExt;\n\n if (baseName.toLowerCase() === 'index') {\n baseName = pathParts.pop() || baseName;\n }\n\n return `${prefix}${baseName\n .replace(/([a-z])([A-Z])/g, '$1-$2')\n .replace(/[\\s_]+/g, '-')\n .toLowerCase()}`;\n};\n\nexport const extractDictionaryKey = (\n filePath: string,\n fileText: string\n): string => {\n const componentName = detectExportedComponentName(fileText);\n\n if (componentName) {\n return camelCaseToKebabCase(componentName);\n }\n\n return extractDictionaryKeyFromPath(filePath, '');\n};\n"],"mappings":"+LAQA,MAAa,GACX,EACA,EAAS,UACE,CACX,IAAM,EAAY,EAAS,MAAM,QAAQ,CACnC,EAAkB,EAAU,KAAK,EAAI,GACrC,EAAe,EAAgB,YAAY,IAAI,CACjD,EACF,IAAiB,GAEb,EADA,EAAgB,MAAM,EAAG,EAAa,CAO5C,OAJI,EAAS,aAAa,GAAK,UAC7B,EAAW,EAAU,KAAK,EAAI,GAGzB,GAAG,IAAS,EAChB,QAAQ,kBAAmB,QAAQ,CACnC,QAAQ,UAAW,IAAI,CACvB,aAAa,IAGL,GACX,EACA,IACW,CACX,IAAM,GAAA,EAAA,EAAA,6BAA4C,EAAS,CAM3D,OAJI,GACF,EAAA,EAAA,sBAA4B,EAAc,CAGrC,EAA6B,EAAU,GAAG"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=(e,t)=>{let n=e.normalize(`NFD`).replace(/[\u0300-\u036f]/g,``).replace(/[\s_-]+/g,` `).replace(/[^\p{L}\p{N} ]/gu,``).trim().split(` `).filter(Boolean).slice(0,5).map((e,t)=>t===0?e.toLowerCase():e.charAt(0).toUpperCase()+e.slice(1).toLowerCase()).join(``);if(n||=`content`,t.has(n)){let e=1;for(;t.has(`${n}${e}`);)e++;n=`${n}${e}`}return n};exports.generateKey=e;
|
|
2
|
+
//# sourceMappingURL=generateKey.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateKey.cjs","names":[],"sources":["../../../../src/extractContent/utils/generateKey.ts"],"sourcesContent":["export const generateKey = (\n text: string,\n existingKeys: Set<string>\n): string => {\n const maxWords = 5;\n let key = text\n .normalize('NFD') // Normalize to decomposes combined characters (e.g., é -> e + ´)\n .replace(/[\\u0300-\\u036f]/g, '') // Remove the accent characters\n .replace(/[\\s_-]+/g, ' ')\n .replace(/[^\\p{L}\\p{N} ]/gu, '')\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"],"mappings":"mEAAA,MAAa,GACX,EACA,IACW,CAEX,IAAI,EAAM,EACP,UAAU,MAAM,CAChB,QAAQ,mBAAoB,GAAG,CAC/B,QAAQ,WAAY,IAAI,CACxB,QAAQ,mBAAoB,GAAG,CAC/B,MAAM,CACN,MAAM,IAAI,CACV,OAAO,QAAQ,CACf,MAAM,EATQ,EASI,CAClB,KAAK,EAAM,IACV,IAAU,EACN,EAAK,aAAa,CAClB,EAAK,OAAO,EAAE,CAAC,aAAa,CAAG,EAAK,MAAM,EAAE,CAAC,aAAa,CAC/D,CACA,KAAK,GAAG,CAGX,GADA,AAAU,IAAM,UACZ,EAAa,IAAI,EAAI,CAAE,CACzB,IAAI,EAAI,EACR,KAAO,EAAa,IAAI,GAAG,IAAM,IAAI,EAAE,IACvC,EAAM,GAAG,IAAM,IAEjB,OAAO"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`../../_virtual/_rolldown/runtime.cjs`);let t=require(`@babel/types`);t=e.__toESM(t);const n=e=>{if(e.isFunctionDeclaration())return e.node.id?.name;if((e.isArrowFunctionExpression()||e.isFunctionExpression())&&e.parentPath.isVariableDeclarator()&&t.isIdentifier(e.parentPath.node.id))return e.parentPath.node.id.name};exports.getComponentName=n;
|
|
2
|
+
//# sourceMappingURL=getComponentName.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getComponentName.cjs","names":["t"],"sources":["../../../../src/extractContent/utils/getComponentName.ts"],"sourcesContent":["import type { NodePath } from '@babel/traverse';\nimport * as t from '@babel/types';\n\n/**\n * Resolves the name of the component from various function declaration types.\n */\nexport const getComponentName = (path: NodePath): string | undefined => {\n if (path.isFunctionDeclaration()) {\n return path.node.id?.name;\n }\n\n if (path.isArrowFunctionExpression() || path.isFunctionExpression()) {\n if (\n path.parentPath.isVariableDeclarator() &&\n t.isIdentifier(path.parentPath.node.id)\n ) {\n return path.parentPath.node.id.name;\n }\n }\n\n return undefined;\n};\n"],"mappings":"wKAMA,MAAa,EAAoB,GAAuC,CACtE,GAAI,EAAK,uBAAuB,CAC9B,OAAO,EAAK,KAAK,IAAI,KAGvB,IAAI,EAAK,2BAA2B,EAAI,EAAK,sBAAsB,GAE/D,EAAK,WAAW,sBAAsB,EACtCA,EAAE,aAAa,EAAK,WAAW,KAAK,GAAG,CAEvC,OAAO,EAAK,WAAW,KAAK,GAAG"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`../../_virtual/_rolldown/runtime.cjs`);let t=require(`@babel/types`);t=e.__toESM(t);const n=e=>{let n;return e.traverse({CallExpression(e){let r=e.node.callee;if(t.isIdentifier(r)&&(r.name===`useIntlayer`||r.name===`getIntlayer`)){let i=e.node.arguments[0];t.isStringLiteral(i)&&(n={key:i.value,hook:r.name},e.stop())}},Function(e){e.skip()}}),n};exports.getExistingIntlayerInfo=n;
|
|
2
|
+
//# sourceMappingURL=getExistingIntlayerInfo.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getExistingIntlayerInfo.cjs","names":["t"],"sources":["../../../../src/extractContent/utils/getExistingIntlayerInfo.ts"],"sourcesContent":["import type { NodePath } from '@babel/traverse';\nimport * as t from '@babel/types';\n\n/**\n * Searches for an existing useIntlayer or getIntlayer call in the function body.\n */\nexport const getExistingIntlayerInfo = (\n path: NodePath\n): { key: string; hook: 'useIntlayer' | 'getIntlayer' } | undefined => {\n let result: { key: string; hook: 'useIntlayer' | 'getIntlayer' } | undefined;\n\n // We only check the direct body of the function, not nested functions\n path.traverse({\n CallExpression(childPath) {\n const callee = childPath.node.callee;\n if (\n t.isIdentifier(callee) &&\n (callee.name === 'useIntlayer' || callee.name === 'getIntlayer')\n ) {\n const arg = childPath.node.arguments[0];\n if (t.isStringLiteral(arg)) {\n result = {\n key: arg.value,\n hook: callee.name as 'useIntlayer' | 'getIntlayer',\n };\n childPath.stop();\n }\n }\n },\n Function(childPath) {\n childPath.skip(); // Don't look inside nested functions\n },\n });\n\n return result;\n};\n"],"mappings":"wKAMA,MAAa,EACX,GACqE,CACrE,IAAI,EAyBJ,OAtBA,EAAK,SAAS,CACZ,eAAe,EAAW,CACxB,IAAM,EAAS,EAAU,KAAK,OAC9B,GACEA,EAAE,aAAa,EAAO,GACrB,EAAO,OAAS,eAAiB,EAAO,OAAS,eAClD,CACA,IAAM,EAAM,EAAU,KAAK,UAAU,GACjCA,EAAE,gBAAgB,EAAI,GACxB,EAAS,CACP,IAAK,EAAI,MACT,KAAM,EAAO,KACd,CACD,EAAU,MAAM,IAItB,SAAS,EAAW,CAClB,EAAU,MAAM,EAEnB,CAAC,CAEK"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./generateKey.cjs`),t=(t,n,r,i)=>{i[n]||(i[n]={});let a=Object.entries(i[n]).find(([e,n])=>n===t);if(a)return a[0];let o=e.generateKey(t,r);return r.add(o),i[n][o]=t,o};exports.getOrGenerateKey=t;
|
|
2
|
+
//# sourceMappingURL=getOrGenerateKey.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getOrGenerateKey.cjs","names":["generateKey"],"sources":["../../../../src/extractContent/utils/getOrGenerateKey.ts"],"sourcesContent":["import { generateKey } from './generateKey';\n\n/**\n * Gets an existing key for a given text or generates a new one.\n */\nexport const getOrGenerateKey = (\n text: string,\n componentKey: string,\n existingKeys: Set<string>,\n extractedContent: Record<string, Record<string, string>>\n): string => {\n if (!extractedContent[componentKey]) {\n extractedContent[componentKey] = {};\n }\n const existingEntry = Object.entries(extractedContent[componentKey]).find(\n ([_, value]) => value === text\n );\n\n if (existingEntry) {\n return existingEntry[0];\n }\n const key = generateKey(text, existingKeys);\n existingKeys.add(key);\n extractedContent[componentKey][key] = text;\n return key;\n};\n"],"mappings":"wGAKa,GACX,EACA,EACA,EACA,IACW,CACN,EAAiB,KACpB,EAAiB,GAAgB,EAAE,EAErC,IAAM,EAAgB,OAAO,QAAQ,EAAiB,GAAc,CAAC,MAClE,CAAC,EAAG,KAAW,IAAU,EAC3B,CAED,GAAI,EACF,OAAO,EAAc,GAEvB,IAAM,EAAMA,EAAAA,YAAY,EAAM,EAAa,CAG3C,OAFA,EAAa,IAAI,EAAI,CACrB,EAAiB,GAAc,GAAO,EAC/B"}
|