@intlayer/babel 8.12.1 → 8.12.3
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.map +1 -1
- package/dist/cjs/babel-plugin-intlayer-field-rename.cjs +14 -9
- package/dist/cjs/babel-plugin-intlayer-field-rename.cjs.map +1 -1
- package/dist/cjs/babel-plugin-intlayer-minify.cjs +83 -0
- package/dist/cjs/babel-plugin-intlayer-minify.cjs.map +1 -0
- package/dist/cjs/babel-plugin-intlayer-optimize.cjs +1 -0
- package/dist/cjs/babel-plugin-intlayer-optimize.cjs.map +1 -1
- package/dist/cjs/babel-plugin-intlayer-purge.cjs +403 -0
- package/dist/cjs/babel-plugin-intlayer-purge.cjs.map +1 -0
- package/dist/cjs/babel-plugin-intlayer-usage-analyzer.cjs +293 -33
- package/dist/cjs/babel-plugin-intlayer-usage-analyzer.cjs.map +1 -1
- package/dist/cjs/extractContent/babelProcessor.cjs.map +1 -1
- package/dist/cjs/extractContent/contentWriter.cjs.map +1 -1
- package/dist/cjs/extractContent/extractContent.cjs.map +1 -1
- package/dist/cjs/extractContent/processTsxFile.cjs.map +1 -1
- package/dist/cjs/extractContent/utils/constants.cjs.map +1 -1
- package/dist/cjs/extractContent/utils/detectPackageName.cjs +1 -0
- package/dist/cjs/extractContent/utils/detectPackageName.cjs.map +1 -1
- package/dist/cjs/extractContent/utils/extractDictionaryInfo.cjs.map +1 -1
- package/dist/cjs/extractContent/utils/extractDictionaryKey.cjs +1 -0
- package/dist/cjs/extractContent/utils/extractDictionaryKey.cjs.map +1 -1
- package/dist/cjs/extractContent/utils/generateKey.cjs.map +1 -1
- package/dist/cjs/extractContent/utils/getComponentName.cjs.map +1 -1
- package/dist/cjs/extractContent/utils/getExistingIntlayerInfo.cjs.map +1 -1
- package/dist/cjs/extractContent/utils/getOrGenerateKey.cjs.map +1 -1
- package/dist/cjs/extractContent/utils/resolveDictionaryKey.cjs +1 -0
- package/dist/cjs/extractContent/utils/resolveDictionaryKey.cjs.map +1 -1
- package/dist/cjs/extractContent/utils/shouldExtract.cjs.map +1 -1
- package/dist/cjs/extractScriptBlocks.cjs +1 -0
- package/dist/cjs/extractScriptBlocks.cjs.map +1 -1
- package/dist/cjs/getExtractPluginOptions.cjs.map +1 -1
- package/dist/cjs/getOptimizePluginOptions.cjs +1 -0
- package/dist/cjs/getOptimizePluginOptions.cjs.map +1 -1
- package/dist/cjs/getPurgePluginOptions.cjs +108 -0
- package/dist/cjs/getPurgePluginOptions.cjs.map +1 -0
- package/dist/cjs/index.cjs +12 -1
- package/dist/cjs/transformers.cjs +22 -7
- package/dist/cjs/transformers.cjs.map +1 -1
- package/dist/esm/babel-plugin-intlayer-extract.mjs.map +1 -1
- package/dist/esm/babel-plugin-intlayer-field-rename.mjs +14 -9
- package/dist/esm/babel-plugin-intlayer-field-rename.mjs.map +1 -1
- package/dist/esm/babel-plugin-intlayer-minify.mjs +82 -0
- package/dist/esm/babel-plugin-intlayer-minify.mjs.map +1 -0
- package/dist/esm/babel-plugin-intlayer-optimize.mjs.map +1 -1
- package/dist/esm/babel-plugin-intlayer-purge.mjs +400 -0
- package/dist/esm/babel-plugin-intlayer-purge.mjs.map +1 -0
- package/dist/esm/babel-plugin-intlayer-usage-analyzer.mjs +293 -34
- package/dist/esm/babel-plugin-intlayer-usage-analyzer.mjs.map +1 -1
- package/dist/esm/extractContent/babelProcessor.mjs.map +1 -1
- package/dist/esm/extractContent/contentWriter.mjs.map +1 -1
- package/dist/esm/extractContent/extractContent.mjs.map +1 -1
- package/dist/esm/extractContent/processTsxFile.mjs.map +1 -1
- package/dist/esm/extractContent/utils/constants.mjs.map +1 -1
- package/dist/esm/extractContent/utils/detectPackageName.mjs.map +1 -1
- package/dist/esm/extractContent/utils/extractDictionaryInfo.mjs.map +1 -1
- package/dist/esm/extractContent/utils/extractDictionaryKey.mjs.map +1 -1
- package/dist/esm/extractContent/utils/generateKey.mjs.map +1 -1
- package/dist/esm/extractContent/utils/getComponentName.mjs.map +1 -1
- package/dist/esm/extractContent/utils/getExistingIntlayerInfo.mjs.map +1 -1
- package/dist/esm/extractContent/utils/getOrGenerateKey.mjs.map +1 -1
- package/dist/esm/extractContent/utils/resolveDictionaryKey.mjs.map +1 -1
- package/dist/esm/extractContent/utils/shouldExtract.mjs.map +1 -1
- package/dist/esm/extractScriptBlocks.mjs.map +1 -1
- package/dist/esm/getExtractPluginOptions.mjs.map +1 -1
- package/dist/esm/getOptimizePluginOptions.mjs.map +1 -1
- package/dist/esm/getPurgePluginOptions.mjs +106 -0
- package/dist/esm/getPurgePluginOptions.mjs.map +1 -0
- package/dist/esm/index.mjs +7 -4
- package/dist/esm/transformers.mjs +20 -8
- package/dist/esm/transformers.mjs.map +1 -1
- package/dist/types/babel-plugin-intlayer-extract.d.ts.map +1 -1
- package/dist/types/babel-plugin-intlayer-field-rename.d.ts.map +1 -1
- package/dist/types/babel-plugin-intlayer-minify.d.ts +84 -0
- package/dist/types/babel-plugin-intlayer-minify.d.ts.map +1 -0
- package/dist/types/babel-plugin-intlayer-optimize.d.ts.map +1 -1
- package/dist/types/babel-plugin-intlayer-purge.d.ts +127 -0
- package/dist/types/babel-plugin-intlayer-purge.d.ts.map +1 -0
- package/dist/types/babel-plugin-intlayer-usage-analyzer.d.ts +72 -4
- package/dist/types/babel-plugin-intlayer-usage-analyzer.d.ts.map +1 -1
- package/dist/types/extractContent/babelProcessor.d.ts.map +1 -1
- package/dist/types/extractContent/contentWriter.d.ts.map +1 -1
- package/dist/types/extractContent/extractContent.d.ts.map +1 -1
- package/dist/types/extractContent/utils/constants.d.ts.map +1 -1
- package/dist/types/extractContent/utils/detectPackageName.d.ts.map +1 -1
- package/dist/types/extractContent/utils/extractDictionaryInfo.d.ts.map +1 -1
- package/dist/types/extractContent/utils/extractDictionaryKey.d.ts.map +1 -1
- package/dist/types/extractContent/utils/generateKey.d.ts.map +1 -1
- package/dist/types/extractContent/utils/getComponentName.d.ts.map +1 -1
- package/dist/types/extractContent/utils/getExistingIntlayerInfo.d.ts.map +1 -1
- package/dist/types/extractContent/utils/shouldExtract.d.ts.map +1 -1
- package/dist/types/extractScriptBlocks.d.ts.map +1 -1
- package/dist/types/getExtractPluginOptions.d.ts.map +1 -1
- package/dist/types/getOptimizePluginOptions.d.ts.map +1 -1
- package/dist/types/getPurgePluginOptions.d.ts +83 -0
- package/dist/types/getPurgePluginOptions.d.ts.map +1 -0
- package/dist/types/index.d.ts +6 -3
- package/dist/types/transformers.d.ts +17 -5
- package/dist/types/transformers.d.ts.map +1 -1
- package/package.json +12 -12
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import { createPruneContext, makeUsageAnalyzerBabelPlugin } from "./babel-plugin-intlayer-usage-analyzer.mjs";
|
|
2
|
+
import { extractScriptBlocks } from "./extractScriptBlocks.mjs";
|
|
3
|
+
import { buildNestedRenameMapFromContent } from "./babel-plugin-intlayer-field-rename.mjs";
|
|
4
|
+
import { BABEL_PARSER_OPTIONS, INTLAYER_OR_COMPAT_USAGE_REGEX, SOURCE_FILE_REGEX } from "./transformers.mjs";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { existsSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { transformSync } from "@babel/core";
|
|
8
|
+
|
|
9
|
+
//#region src/babel-plugin-intlayer-purge.ts
|
|
10
|
+
/**
|
|
11
|
+
* Cache of built {@link PruneContext} objects, keyed by the project's
|
|
12
|
+
* `baseDir`. Each context is built exactly once per Node.js process.
|
|
13
|
+
*/
|
|
14
|
+
const _pruneContextCache = /* @__PURE__ */ new Map();
|
|
15
|
+
/**
|
|
16
|
+
* Tracks base directories whose full analysis + dictionary-write cycle has
|
|
17
|
+
* already completed, to avoid repeating work across file transforms.
|
|
18
|
+
*/
|
|
19
|
+
const _completedBaseDirs = /* @__PURE__ */ new Set();
|
|
20
|
+
/**
|
|
21
|
+
* Returns the shared {@link PruneContext} for the given base directory, or
|
|
22
|
+
* `null` if {@link intlayerPurgeBabelPlugin} has not yet been initialised for
|
|
23
|
+
* that directory.
|
|
24
|
+
*
|
|
25
|
+
* Used by {@link intlayerMinifyBabelPlugin} to read the rename map without
|
|
26
|
+
* creating a circular dependency.
|
|
27
|
+
*/
|
|
28
|
+
const getSharedPruneContext = (baseDir) => _pruneContextCache.get(baseDir) ?? null;
|
|
29
|
+
const isTranslationNode = (value) => typeof value === "object" && value !== null && value.nodeType === "translation" && typeof value.translation === "object";
|
|
30
|
+
const isPlainRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
31
|
+
/**
|
|
32
|
+
* Removes unused fields from a **static** dictionary (all locales in one
|
|
33
|
+
* file). Supports shape A (translation node at the root) and shape B (flat
|
|
34
|
+
* record of translation nodes per field).
|
|
35
|
+
*/
|
|
36
|
+
const pruneStaticDictionaryContent = (dictionary, usedFieldNames) => {
|
|
37
|
+
const { content } = dictionary;
|
|
38
|
+
if (isTranslationNode(content)) {
|
|
39
|
+
const firstLocaleValue = Object.values(content.translation)[0];
|
|
40
|
+
if (isPlainRecord(firstLocaleValue)) {
|
|
41
|
+
const prunedTranslation = {};
|
|
42
|
+
for (const [locale, localeContent] of Object.entries(content.translation)) {
|
|
43
|
+
if (!isPlainRecord(localeContent)) {
|
|
44
|
+
prunedTranslation[locale] = localeContent;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const prunedLocaleFields = {};
|
|
48
|
+
for (const [fieldName, fieldValue] of Object.entries(localeContent)) if (usedFieldNames.has(fieldName)) prunedLocaleFields[fieldName] = fieldValue;
|
|
49
|
+
prunedTranslation[locale] = prunedLocaleFields;
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
prunedDictionary: {
|
|
53
|
+
...dictionary,
|
|
54
|
+
content: {
|
|
55
|
+
...content,
|
|
56
|
+
translation: prunedTranslation
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
wasRecognised: true
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (isPlainRecord(content) && !isTranslationNode(content)) {
|
|
64
|
+
const prunedContent = {};
|
|
65
|
+
for (const [fieldName, fieldValue] of Object.entries(content)) if (usedFieldNames.has(fieldName)) prunedContent[fieldName] = fieldValue;
|
|
66
|
+
return {
|
|
67
|
+
prunedDictionary: {
|
|
68
|
+
...dictionary,
|
|
69
|
+
content: prunedContent
|
|
70
|
+
},
|
|
71
|
+
wasRecognised: true
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
prunedDictionary: dictionary,
|
|
76
|
+
wasRecognised: false
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Removes unused fields from a **dynamic / per-locale** dictionary file
|
|
81
|
+
* (one JSON per locale, flat `content` record).
|
|
82
|
+
*/
|
|
83
|
+
const pruneDynamicDictionaryContent = (dictionary, usedFieldNames) => {
|
|
84
|
+
const { content } = dictionary;
|
|
85
|
+
if (!isPlainRecord(content)) return {
|
|
86
|
+
prunedDictionary: dictionary,
|
|
87
|
+
wasRecognised: false
|
|
88
|
+
};
|
|
89
|
+
const prunedContent = {};
|
|
90
|
+
for (const [fieldName, fieldValue] of Object.entries(content)) if (usedFieldNames.has(fieldName)) prunedContent[fieldName] = fieldValue;
|
|
91
|
+
return {
|
|
92
|
+
prunedDictionary: {
|
|
93
|
+
...dictionary,
|
|
94
|
+
content: prunedContent
|
|
95
|
+
},
|
|
96
|
+
wasRecognised: true
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Recursively renames user-defined content fields using `renameMap`.
|
|
101
|
+
* Translation nodes, arrays, and primitives follow the same traversal rules
|
|
102
|
+
* as in the Vite-based minify plugin.
|
|
103
|
+
*/
|
|
104
|
+
const renameContentRecursively = (value, renameMap) => {
|
|
105
|
+
if (Array.isArray(value)) return value.map((element) => renameContentRecursively(element, renameMap));
|
|
106
|
+
if (!value || typeof value !== "object") return value;
|
|
107
|
+
const record = value;
|
|
108
|
+
if (typeof record.nodeType === "string" && record.translation && typeof record.translation === "object" && !Array.isArray(record.translation)) {
|
|
109
|
+
const renamedTranslation = {};
|
|
110
|
+
for (const [locale, localeValue] of Object.entries(record.translation)) renamedTranslation[locale] = renameContentRecursively(localeValue, renameMap);
|
|
111
|
+
return {
|
|
112
|
+
...record,
|
|
113
|
+
translation: renamedTranslation
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
const result = {};
|
|
117
|
+
for (const [key, val] of Object.entries(record)) {
|
|
118
|
+
const renameEntry = renameMap.get(key);
|
|
119
|
+
if (renameEntry) result[renameEntry.shortName] = renameContentRecursively(val, renameEntry.children);
|
|
120
|
+
else result[key] = val;
|
|
121
|
+
}
|
|
122
|
+
return result;
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Applies a {@link NestedRenameMap} to a parsed dictionary object, renaming
|
|
126
|
+
* only the keys inside `content` while leaving top-level metadata untouched.
|
|
127
|
+
*/
|
|
128
|
+
const applyFieldRenameToDict = (dict, renameMap) => {
|
|
129
|
+
const content = dict.content;
|
|
130
|
+
if (!content || typeof content !== "object" || Array.isArray(content)) return dict;
|
|
131
|
+
return {
|
|
132
|
+
...dict,
|
|
133
|
+
content: renameContentRecursively(content, renameMap)
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Runs the usage-analyser Babel plugin synchronously on a single code block,
|
|
138
|
+
* accumulating results into `pruneContext`.
|
|
139
|
+
*/
|
|
140
|
+
const analyzeCodeBlockSync = (code, sourceFilePath, pruneContext) => {
|
|
141
|
+
try {
|
|
142
|
+
transformSync(code, {
|
|
143
|
+
filename: sourceFilePath,
|
|
144
|
+
plugins: [makeUsageAnalyzerBabelPlugin(pruneContext)],
|
|
145
|
+
parserOpts: BABEL_PARSER_OPTIONS,
|
|
146
|
+
ast: false,
|
|
147
|
+
code: false
|
|
148
|
+
});
|
|
149
|
+
} catch {
|
|
150
|
+
pruneContext.hasUnparsableSourceFiles = true;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Reads a source file from disk and runs the usage-analyser synchronously.
|
|
155
|
+
* SFC files (Vue / Svelte) are handled by extracting script blocks first.
|
|
156
|
+
*/
|
|
157
|
+
const analyzeSourceFileSync = (sourceFilePath, pruneContext) => {
|
|
158
|
+
let code;
|
|
159
|
+
try {
|
|
160
|
+
code = readFileSync(sourceFilePath, "utf-8");
|
|
161
|
+
} catch {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (!INTLAYER_OR_COMPAT_USAGE_REGEX.test(code)) return;
|
|
165
|
+
const scriptBlocks = extractScriptBlocks(sourceFilePath, code);
|
|
166
|
+
for (const block of scriptBlocks) {
|
|
167
|
+
if (!INTLAYER_OR_COMPAT_USAGE_REGEX.test(block.content)) continue;
|
|
168
|
+
analyzeCodeBlockSync(block.content, sourceFilePath, pruneContext);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
/**
|
|
172
|
+
* Reads compiled dictionary JSON files to build the nested field-rename maps,
|
|
173
|
+
* mirroring the Phase 4 logic in the Vite `intlayerOptimize` plugin's
|
|
174
|
+
* `buildStart` hook. Results are stored in
|
|
175
|
+
* `pruneContext.dictionaryKeyToFieldRenameMap`.
|
|
176
|
+
*/
|
|
177
|
+
const buildRenameMapsSynchronously = (dictionariesDir, dynamicDictionariesDir, dictionaryKeyToImportModeMap, pruneContext) => {
|
|
178
|
+
for (const [dictionaryKey, fieldUsage] of pruneContext.dictionaryKeyToFieldUsageMap) {
|
|
179
|
+
if (fieldUsage === "all") continue;
|
|
180
|
+
if (dictionaryKeyToImportModeMap[dictionaryKey] === "fetch") continue;
|
|
181
|
+
if (pruneContext.dictionariesSkippingFieldRename.has(dictionaryKey)) continue;
|
|
182
|
+
let dictionaryContent = null;
|
|
183
|
+
const staticJsonPath = join(dictionariesDir, `${dictionaryKey}.json`);
|
|
184
|
+
if (existsSync(staticJsonPath)) try {
|
|
185
|
+
const raw = readFileSync(staticJsonPath, "utf-8");
|
|
186
|
+
dictionaryContent = JSON.parse(raw).content;
|
|
187
|
+
} catch {}
|
|
188
|
+
if (!dictionaryContent) {
|
|
189
|
+
const dynamicDirPath = join(dynamicDictionariesDir, dictionaryKey);
|
|
190
|
+
if (existsSync(dynamicDirPath)) try {
|
|
191
|
+
const firstJsonFile = readdirSync(dynamicDirPath).find((file) => file.endsWith(".json"));
|
|
192
|
+
if (firstJsonFile) {
|
|
193
|
+
const raw = readFileSync(join(dynamicDirPath, firstJsonFile), "utf-8");
|
|
194
|
+
dictionaryContent = JSON.parse(raw).content;
|
|
195
|
+
}
|
|
196
|
+
} catch {}
|
|
197
|
+
}
|
|
198
|
+
if (!dictionaryContent) continue;
|
|
199
|
+
const nestedRenameMap = buildNestedRenameMapFromContent(dictionaryContent);
|
|
200
|
+
const opaqueFieldMap = pruneContext.dictionaryKeysWithOpaqueTopLevelFields.get(dictionaryKey);
|
|
201
|
+
if (opaqueFieldMap) {
|
|
202
|
+
const dangerousEntries = [...opaqueFieldMap.entries()].filter(([fieldName]) => (nestedRenameMap.get(fieldName)?.children.size ?? 0) > 0);
|
|
203
|
+
for (const [fieldName] of dangerousEntries) {
|
|
204
|
+
const entry = nestedRenameMap.get(fieldName);
|
|
205
|
+
if (entry) entry.children = /* @__PURE__ */ new Map();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (nestedRenameMap.size > 0) pruneContext.dictionaryKeyToFieldRenameMap.set(dictionaryKey, nestedRenameMap);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
const processStaticDictionaryFile = (filePath, pruneContext, shouldPurge, shouldMinify) => {
|
|
212
|
+
let rawJson;
|
|
213
|
+
try {
|
|
214
|
+
rawJson = readFileSync(filePath, "utf-8");
|
|
215
|
+
} catch {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
let parsedDict;
|
|
219
|
+
try {
|
|
220
|
+
parsedDict = JSON.parse(rawJson);
|
|
221
|
+
} catch {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const { key: dictionaryKey } = parsedDict;
|
|
225
|
+
if (!dictionaryKey) return;
|
|
226
|
+
if (pruneContext.dictionariesWithEdgeCases.has(dictionaryKey)) return;
|
|
227
|
+
let modified = false;
|
|
228
|
+
if (shouldPurge) {
|
|
229
|
+
const fieldUsage = pruneContext.dictionaryKeyToFieldUsageMap.get(dictionaryKey);
|
|
230
|
+
if (fieldUsage && fieldUsage !== "all") {
|
|
231
|
+
const { prunedDictionary, wasRecognised } = pruneStaticDictionaryContent(parsedDict, fieldUsage);
|
|
232
|
+
if (!wasRecognised) {
|
|
233
|
+
pruneContext.dictionariesWithEdgeCases.add(dictionaryKey);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
parsedDict = prunedDictionary;
|
|
237
|
+
modified = true;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (shouldMinify) {
|
|
241
|
+
const fieldRenameMap = pruneContext.dictionaryKeyToFieldRenameMap.get(dictionaryKey);
|
|
242
|
+
if (fieldRenameMap && fieldRenameMap.size > 0) {
|
|
243
|
+
parsedDict = applyFieldRenameToDict(parsedDict, fieldRenameMap);
|
|
244
|
+
modified = true;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (!modified) return;
|
|
248
|
+
const outputDict = shouldMinify ? {
|
|
249
|
+
key: parsedDict.key,
|
|
250
|
+
content: parsedDict.content
|
|
251
|
+
} : parsedDict;
|
|
252
|
+
try {
|
|
253
|
+
writeFileSync(filePath, JSON.stringify(outputDict), "utf-8");
|
|
254
|
+
} catch {}
|
|
255
|
+
};
|
|
256
|
+
const processDynamicDictionaryFile = (filePath, pruneContext, shouldPurge, shouldMinify) => {
|
|
257
|
+
let rawJson;
|
|
258
|
+
try {
|
|
259
|
+
rawJson = readFileSync(filePath, "utf-8");
|
|
260
|
+
} catch {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
let parsedDict;
|
|
264
|
+
try {
|
|
265
|
+
parsedDict = JSON.parse(rawJson);
|
|
266
|
+
} catch {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
const { key: dictionaryKey } = parsedDict;
|
|
270
|
+
if (!dictionaryKey) return;
|
|
271
|
+
if (pruneContext.dictionariesWithEdgeCases.has(dictionaryKey)) return;
|
|
272
|
+
let modified = false;
|
|
273
|
+
if (shouldPurge) {
|
|
274
|
+
const fieldUsage = pruneContext.dictionaryKeyToFieldUsageMap.get(dictionaryKey);
|
|
275
|
+
if (fieldUsage && fieldUsage !== "all") {
|
|
276
|
+
const { prunedDictionary, wasRecognised } = pruneDynamicDictionaryContent(parsedDict, fieldUsage);
|
|
277
|
+
if (!wasRecognised) {
|
|
278
|
+
pruneContext.dictionariesWithEdgeCases.add(dictionaryKey);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
parsedDict = prunedDictionary;
|
|
282
|
+
modified = true;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (shouldMinify) {
|
|
286
|
+
const fieldRenameMap = pruneContext.dictionaryKeyToFieldRenameMap.get(dictionaryKey);
|
|
287
|
+
if (fieldRenameMap && fieldRenameMap.size > 0) {
|
|
288
|
+
parsedDict = applyFieldRenameToDict(parsedDict, fieldRenameMap);
|
|
289
|
+
modified = true;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
if (!modified) return;
|
|
293
|
+
const outputDict = shouldMinify ? {
|
|
294
|
+
key: parsedDict.key,
|
|
295
|
+
content: parsedDict.content,
|
|
296
|
+
locale: parsedDict.locale
|
|
297
|
+
} : parsedDict;
|
|
298
|
+
try {
|
|
299
|
+
writeFileSync(filePath, JSON.stringify(outputDict), "utf-8");
|
|
300
|
+
} catch {}
|
|
301
|
+
};
|
|
302
|
+
const processAllDictionaryFiles = (dictionariesDir, dynamicDictionariesDir, pruneContext, shouldPurge, shouldMinify) => {
|
|
303
|
+
if (existsSync(dictionariesDir)) for (const entry of readdirSync(dictionariesDir)) {
|
|
304
|
+
if (!entry.endsWith(".json")) continue;
|
|
305
|
+
processStaticDictionaryFile(join(dictionariesDir, entry), pruneContext, shouldPurge, shouldMinify);
|
|
306
|
+
}
|
|
307
|
+
if (existsSync(dynamicDictionariesDir)) for (const keyDir of readdirSync(dynamicDictionariesDir)) {
|
|
308
|
+
const keyDirPath = join(dynamicDictionariesDir, keyDir);
|
|
309
|
+
try {
|
|
310
|
+
for (const localeFile of readdirSync(keyDirPath)) {
|
|
311
|
+
if (!localeFile.endsWith(".json")) continue;
|
|
312
|
+
processDynamicDictionaryFile(join(keyDirPath, localeFile), pruneContext, shouldPurge, shouldMinify);
|
|
313
|
+
}
|
|
314
|
+
} catch {}
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
/**
|
|
318
|
+
* Runs the full purge/minify pipeline for the given options, using a
|
|
319
|
+
* module-level cache so the work happens at most once per process per
|
|
320
|
+
* unique `baseDir`.
|
|
321
|
+
*/
|
|
322
|
+
const runPurgePipeline = (options) => {
|
|
323
|
+
const { baseDir, purge, minify, optimize, editorEnabled, dictionariesDir, dynamicDictionariesDir, componentFilesList, dictionaryKeyToImportModeMap } = options;
|
|
324
|
+
const cachedContext = _pruneContextCache.get(baseDir);
|
|
325
|
+
if (cachedContext) return cachedContext;
|
|
326
|
+
const pruneContext = createPruneContext();
|
|
327
|
+
_pruneContextCache.set(baseDir, pruneContext);
|
|
328
|
+
const shouldPurge = purge && !editorEnabled;
|
|
329
|
+
const shouldMinify = minify && !editorEnabled;
|
|
330
|
+
if (!shouldPurge && !shouldMinify || optimize === false) return pruneContext;
|
|
331
|
+
for (const sourceFilePath of componentFilesList) {
|
|
332
|
+
if (!SOURCE_FILE_REGEX.test(sourceFilePath)) continue;
|
|
333
|
+
analyzeSourceFileSync(sourceFilePath, pruneContext);
|
|
334
|
+
}
|
|
335
|
+
if (shouldMinify) buildRenameMapsSynchronously(dictionariesDir, dynamicDictionariesDir, dictionaryKeyToImportModeMap, pruneContext);
|
|
336
|
+
processAllDictionaryFiles(dictionariesDir, dynamicDictionariesDir, pruneContext, shouldPurge, shouldMinify);
|
|
337
|
+
return pruneContext;
|
|
338
|
+
};
|
|
339
|
+
/**
|
|
340
|
+
* Babel plugin that analyses all project source files and rewrites compiled
|
|
341
|
+
* dictionary JSON files in-place to remove unused content fields
|
|
342
|
+
* (`build.purge`) and/or rename them to short alphabetic aliases
|
|
343
|
+
* (`build.minify`).
|
|
344
|
+
*
|
|
345
|
+
* All option values must be pre-resolved via {@link getPurgePluginOptions}
|
|
346
|
+
* before being passed here — the plugin does not load the intlayer
|
|
347
|
+
* configuration itself.
|
|
348
|
+
*
|
|
349
|
+
* This plugin performs **file I/O as a side effect**: on the very first Babel
|
|
350
|
+
* transform in a given Node.js process it synchronously scans the component
|
|
351
|
+
* files listed in `options.componentFilesList`, builds field-usage data, and
|
|
352
|
+
* writes the processed dictionaries to disk. Subsequent transforms are
|
|
353
|
+
* no-ops.
|
|
354
|
+
*
|
|
355
|
+
* Source-code field renames (rewriting `content.title` → `content.a`) are
|
|
356
|
+
* handled by the companion {@link intlayerMinifyBabelPlugin}.
|
|
357
|
+
*
|
|
358
|
+
* @example
|
|
359
|
+
* ```js
|
|
360
|
+
* // babel.config.js
|
|
361
|
+
* const {
|
|
362
|
+
* intlayerPurgeBabelPlugin,
|
|
363
|
+
* intlayerMinifyBabelPlugin,
|
|
364
|
+
* intlayerOptimizeBabelPlugin,
|
|
365
|
+
* getPurgePluginOptions,
|
|
366
|
+
* getMinifyPluginOptions,
|
|
367
|
+
* getOptimizePluginOptions,
|
|
368
|
+
* } = require("@intlayer/babel");
|
|
369
|
+
*
|
|
370
|
+
* module.exports = {
|
|
371
|
+
* presets: ["next/babel"],
|
|
372
|
+
* plugins: [
|
|
373
|
+
* [intlayerPurgeBabelPlugin, getPurgePluginOptions()],
|
|
374
|
+
* [intlayerMinifyBabelPlugin, getMinifyPluginOptions()],
|
|
375
|
+
* [intlayerOptimizeBabelPlugin, getOptimizePluginOptions()],
|
|
376
|
+
* ],
|
|
377
|
+
* };
|
|
378
|
+
* ```
|
|
379
|
+
*
|
|
380
|
+
* @remarks
|
|
381
|
+
* - Intended for **production builds** only. Dictionary JSON files are
|
|
382
|
+
* overwritten in-place; running `intlayer build` afterwards restores the
|
|
383
|
+
* originals.
|
|
384
|
+
* - The plugin is a no-op when `optimize` is `false` or `editorEnabled` is
|
|
385
|
+
* `true`.
|
|
386
|
+
*/
|
|
387
|
+
const intlayerPurgeBabelPlugin = (_babel) => ({
|
|
388
|
+
name: "intlayer-purge",
|
|
389
|
+
pre() {
|
|
390
|
+
const { baseDir } = this.opts;
|
|
391
|
+
if (_completedBaseDirs.has(baseDir)) return;
|
|
392
|
+
_completedBaseDirs.add(baseDir);
|
|
393
|
+
runPurgePipeline(this.opts);
|
|
394
|
+
},
|
|
395
|
+
visitor: {}
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
//#endregion
|
|
399
|
+
export { getSharedPruneContext, intlayerPurgeBabelPlugin };
|
|
400
|
+
//# sourceMappingURL=babel-plugin-intlayer-purge.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"babel-plugin-intlayer-purge.mjs","names":[],"sources":["../../src/babel-plugin-intlayer-purge.ts"],"sourcesContent":["import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';\nimport { join } from 'node:path';\nimport type { PluginObj, PluginPass } from '@babel/core';\nimport { transformSync } from '@babel/core';\nimport type * as BabelTypes from '@babel/types';\nimport { buildNestedRenameMapFromContent } from './babel-plugin-intlayer-field-rename';\nimport {\n createPruneContext,\n makeUsageAnalyzerBabelPlugin,\n type NestedRenameMap,\n type PruneContext,\n} from './babel-plugin-intlayer-usage-analyzer';\nimport { extractScriptBlocks } from './extractScriptBlocks';\nimport {\n BABEL_PARSER_OPTIONS,\n INTLAYER_OR_COMPAT_USAGE_REGEX,\n SOURCE_FILE_REGEX,\n} from './transformers';\n\n// ── Plugin options ────────────────────────────────────────────────────────────\n\n/**\n * Pre-resolved options accepted by {@link intlayerPurgeBabelPlugin}.\n *\n * All values are resolved at babel.config.js load time (via\n * {@link getPurgePluginOptions}) so the plugin itself does not need to read\n * the configuration file on every file transform.\n */\nexport type PurgePluginOptions = {\n /**\n * Absolute path to the project root. Used as the cache key for the shared\n * {@link PruneContext} so two Babel transform pipelines for different\n * workspaces in the same process do not share state.\n */\n baseDir: string;\n\n /**\n * When `true`, remove unused content fields from compiled dictionary JSON\n * files. Mirrors `build.purge` in `intlayer.config.ts`.\n */\n purge: boolean;\n\n /**\n * When `true`, rename content fields to short alphabetic aliases\n * (`title` → `a`, etc.) and strip top-level metadata from compiled\n * dictionaries. Mirrors `build.minify` in `intlayer.config.ts`.\n */\n minify: boolean;\n\n /**\n * Build optimisation toggle. `undefined` means \"auto\" (active for\n * production builds). When explicitly `false`, the plugin is a no-op.\n * Mirrors `build.optimize`.\n */\n optimize: boolean | undefined;\n\n /**\n * When `true` the plugin skips all processing to preserve full dictionary\n * content for the visual editor. Mirrors `editor.enabled`.\n */\n editorEnabled: boolean;\n\n /**\n * Absolute path to the compiled static dictionaries directory\n * (`.intlayer/dictionaries/` by default).\n */\n dictionariesDir: string;\n\n /**\n * Absolute path to the compiled per-locale dynamic dictionaries directory\n * (`.intlayer/dynamic_dictionaries/` by default).\n */\n dynamicDictionariesDir: string;\n\n /**\n * Pre-built list of component source file paths to analyse for field-usage.\n * Populated by {@link getPurgePluginOptions} from the intlayer config's\n * `content` glob patterns.\n */\n componentFilesList: string[];\n\n /**\n * Per-dictionary import-mode overrides, keyed by dictionary `key`.\n * Dictionaries with mode `'fetch'` are excluded from field renaming because\n * their JSON is served from a remote API using the original field names.\n */\n dictionaryKeyToImportModeMap: Record<\n string,\n 'static' | 'dynamic' | 'fetch' | undefined\n >;\n};\n\n// ── Shared module-level state ─────────────────────────────────────────────────\n\n/**\n * Cache of built {@link PruneContext} objects, keyed by the project's\n * `baseDir`. Each context is built exactly once per Node.js process.\n */\nconst _pruneContextCache = new Map<string, PruneContext>();\n\n/**\n * Tracks base directories whose full analysis + dictionary-write cycle has\n * already completed, to avoid repeating work across file transforms.\n */\nconst _completedBaseDirs = new Set<string>();\n\n/**\n * Returns the shared {@link PruneContext} for the given base directory, or\n * `null` if {@link intlayerPurgeBabelPlugin} has not yet been initialised for\n * that directory.\n *\n * Used by {@link intlayerMinifyBabelPlugin} to read the rename map without\n * creating a circular dependency.\n */\nexport const getSharedPruneContext = (baseDir: string): PruneContext | null =>\n _pruneContextCache.get(baseDir) ?? null;\n\n// ── Dictionary JSON types ─────────────────────────────────────────────────────\n\ntype TranslationNode = {\n nodeType: 'translation';\n translation: Record<string, unknown>;\n};\n\ntype CompiledDictionaryJson = {\n key: string;\n content: TranslationNode | Record<string, unknown>;\n locale?: string;\n [extraKey: string]: unknown;\n};\n\n// ── Type guards ───────────────────────────────────────────────────────────────\n\nconst isTranslationNode = (value: unknown): value is TranslationNode =>\n typeof value === 'object' &&\n value !== null &&\n (value as Record<string, unknown>).nodeType === 'translation' &&\n typeof (value as Record<string, unknown>).translation === 'object';\n\nconst isPlainRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === 'object' && value !== null && !Array.isArray(value);\n\n// ── Prune helpers (mirrors intlayerPrunePlugin) ───────────────────────────────\n\ntype PruneResult = {\n prunedDictionary: CompiledDictionaryJson;\n wasRecognised: boolean;\n};\n\n/**\n * Removes unused fields from a **static** dictionary (all locales in one\n * file). Supports shape A (translation node at the root) and shape B (flat\n * record of translation nodes per field).\n */\nconst pruneStaticDictionaryContent = (\n dictionary: CompiledDictionaryJson,\n usedFieldNames: Set<string>\n): PruneResult => {\n const { content } = dictionary;\n\n // Shape A: { nodeType: \"translation\", translation: { en: { f1, f2 } } }\n if (isTranslationNode(content)) {\n const firstLocaleValue = Object.values(content.translation)[0];\n if (isPlainRecord(firstLocaleValue)) {\n const prunedTranslation: Record<string, unknown> = {};\n for (const [locale, localeContent] of Object.entries(\n content.translation\n )) {\n if (!isPlainRecord(localeContent)) {\n prunedTranslation[locale] = localeContent;\n continue;\n }\n const prunedLocaleFields: Record<string, unknown> = {};\n for (const [fieldName, fieldValue] of Object.entries(localeContent)) {\n if (usedFieldNames.has(fieldName)) {\n prunedLocaleFields[fieldName] = fieldValue;\n }\n }\n prunedTranslation[locale] = prunedLocaleFields;\n }\n return {\n prunedDictionary: {\n ...dictionary,\n content: { ...content, translation: prunedTranslation },\n },\n wasRecognised: true,\n };\n }\n }\n\n // Shape B: { field1: { nodeType: \"translation\", … }, field2: { … } }\n if (isPlainRecord(content) && !isTranslationNode(content)) {\n const prunedContent: Record<string, unknown> = {};\n for (const [fieldName, fieldValue] of Object.entries(content)) {\n if (usedFieldNames.has(fieldName)) {\n prunedContent[fieldName] = fieldValue;\n }\n }\n return {\n prunedDictionary: {\n ...dictionary,\n content: prunedContent as CompiledDictionaryJson['content'],\n },\n wasRecognised: true,\n };\n }\n\n return { prunedDictionary: dictionary, wasRecognised: false };\n};\n\n/**\n * Removes unused fields from a **dynamic / per-locale** dictionary file\n * (one JSON per locale, flat `content` record).\n */\nconst pruneDynamicDictionaryContent = (\n dictionary: CompiledDictionaryJson,\n usedFieldNames: Set<string>\n): PruneResult => {\n const { content } = dictionary;\n if (!isPlainRecord(content)) {\n return { prunedDictionary: dictionary, wasRecognised: false };\n }\n const prunedContent: Record<string, unknown> = {};\n for (const [fieldName, fieldValue] of Object.entries(content)) {\n if (usedFieldNames.has(fieldName)) {\n prunedContent[fieldName] = fieldValue;\n }\n }\n return {\n prunedDictionary: {\n ...dictionary,\n content: prunedContent as CompiledDictionaryJson['content'],\n },\n wasRecognised: true,\n };\n};\n\n// ── Minify helpers (mirrors intlayerMinifyPlugin) ─────────────────────────────\n\n/**\n * Recursively renames user-defined content fields using `renameMap`.\n * Translation nodes, arrays, and primitives follow the same traversal rules\n * as in the Vite-based minify plugin.\n */\nconst renameContentRecursively = (\n value: unknown,\n renameMap: NestedRenameMap\n): unknown => {\n if (Array.isArray(value)) {\n return (value as unknown[]).map((element) =>\n renameContentRecursively(element, renameMap)\n );\n }\n if (!value || typeof value !== 'object') return value;\n\n const record = value as Record<string, unknown>;\n\n // Translation node: recurse into each locale value with the same map.\n if (\n typeof record.nodeType === 'string' &&\n record.translation &&\n typeof record.translation === 'object' &&\n !Array.isArray(record.translation)\n ) {\n const renamedTranslation: Record<string, unknown> = {};\n for (const [locale, localeValue] of Object.entries(\n record.translation as Record<string, unknown>\n )) {\n renamedTranslation[locale] = renameContentRecursively(\n localeValue,\n renameMap\n );\n }\n return { ...record, translation: renamedTranslation };\n }\n\n // User-defined record: rename keys and recurse into values.\n const result: Record<string, unknown> = {};\n for (const [key, val] of Object.entries(record)) {\n const renameEntry = renameMap.get(key);\n if (renameEntry) {\n result[renameEntry.shortName] = renameContentRecursively(\n val,\n renameEntry.children\n );\n } else {\n result[key] = val;\n }\n }\n return result;\n};\n\n/**\n * Applies a {@link NestedRenameMap} to a parsed dictionary object, renaming\n * only the keys inside `content` while leaving top-level metadata untouched.\n */\nconst applyFieldRenameToDict = (\n dict: Record<string, unknown>,\n renameMap: NestedRenameMap\n): Record<string, unknown> => {\n const content = dict.content;\n if (!content || typeof content !== 'object' || Array.isArray(content))\n return dict;\n return {\n ...dict,\n content: renameContentRecursively(content, renameMap),\n };\n};\n\n// ── Synchronous source-file analysis ─────────────────────────────────────────\n\n/**\n * Runs the usage-analyser Babel plugin synchronously on a single code block,\n * accumulating results into `pruneContext`.\n */\nconst analyzeCodeBlockSync = (\n code: string,\n sourceFilePath: string,\n pruneContext: PruneContext\n): void => {\n try {\n transformSync(code, {\n filename: sourceFilePath,\n plugins: [makeUsageAnalyzerBabelPlugin(pruneContext)],\n parserOpts: BABEL_PARSER_OPTIONS,\n ast: false,\n code: false,\n });\n } catch {\n pruneContext.hasUnparsableSourceFiles = true;\n }\n};\n\n/**\n * Reads a source file from disk and runs the usage-analyser synchronously.\n * SFC files (Vue / Svelte) are handled by extracting script blocks first.\n */\nconst analyzeSourceFileSync = (\n sourceFilePath: string,\n pruneContext: PruneContext\n): void => {\n let code: string;\n try {\n code = readFileSync(sourceFilePath, 'utf-8');\n } catch {\n return;\n }\n\n if (!INTLAYER_OR_COMPAT_USAGE_REGEX.test(code)) return;\n\n const scriptBlocks = extractScriptBlocks(sourceFilePath, code);\n for (const block of scriptBlocks) {\n if (!INTLAYER_OR_COMPAT_USAGE_REGEX.test(block.content)) continue;\n analyzeCodeBlockSync(block.content, sourceFilePath, pruneContext);\n }\n};\n\n// ── Build rename maps ─────────────────────────────────────────────────────────\n\n/**\n * Reads compiled dictionary JSON files to build the nested field-rename maps,\n * mirroring the Phase 4 logic in the Vite `intlayerOptimize` plugin's\n * `buildStart` hook. Results are stored in\n * `pruneContext.dictionaryKeyToFieldRenameMap`.\n */\nconst buildRenameMapsSynchronously = (\n dictionariesDir: string,\n dynamicDictionariesDir: string,\n dictionaryKeyToImportModeMap: PurgePluginOptions['dictionaryKeyToImportModeMap'],\n pruneContext: PruneContext\n): void => {\n for (const [\n dictionaryKey,\n fieldUsage,\n ] of pruneContext.dictionaryKeyToFieldUsageMap) {\n if (fieldUsage === 'all') continue;\n if (dictionaryKeyToImportModeMap[dictionaryKey] === 'fetch') continue;\n if (pruneContext.dictionariesSkippingFieldRename.has(dictionaryKey))\n continue;\n\n let dictionaryContent: unknown = null;\n\n const staticJsonPath = join(dictionariesDir, `${dictionaryKey}.json`);\n if (existsSync(staticJsonPath)) {\n try {\n const raw = readFileSync(staticJsonPath, 'utf-8');\n const parsed = JSON.parse(raw) as Record<string, unknown>;\n dictionaryContent = parsed.content;\n } catch {\n // Fall through to dynamic dict.\n }\n }\n\n if (!dictionaryContent) {\n const dynamicDirPath = join(dynamicDictionariesDir, dictionaryKey);\n if (existsSync(dynamicDirPath)) {\n try {\n const localeFiles = readdirSync(dynamicDirPath);\n const firstJsonFile = localeFiles.find((file) =>\n file.endsWith('.json')\n );\n if (firstJsonFile) {\n const raw = readFileSync(\n join(dynamicDirPath, firstJsonFile),\n 'utf-8'\n );\n const parsed = JSON.parse(raw) as Record<string, unknown>;\n dictionaryContent = parsed.content;\n }\n } catch {\n // Dictionary not readable – skip rename for this key.\n }\n }\n }\n\n if (!dictionaryContent) continue;\n\n const nestedRenameMap = buildNestedRenameMapFromContent(dictionaryContent);\n\n // Preserve children of opaque fields to avoid breaking child components.\n const opaqueFieldMap =\n pruneContext.dictionaryKeysWithOpaqueTopLevelFields.get(dictionaryKey);\n if (opaqueFieldMap) {\n const dangerousEntries = [...opaqueFieldMap.entries()].filter(\n ([fieldName]) =>\n (nestedRenameMap.get(fieldName)?.children.size ?? 0) > 0\n );\n for (const [fieldName] of dangerousEntries) {\n const entry = nestedRenameMap.get(fieldName);\n if (entry) {\n entry.children = new Map();\n }\n }\n }\n\n if (nestedRenameMap.size > 0) {\n pruneContext.dictionaryKeyToFieldRenameMap.set(\n dictionaryKey,\n nestedRenameMap\n );\n }\n }\n};\n\n// ── Dictionary file writing ───────────────────────────────────────────────────\n\nconst processStaticDictionaryFile = (\n filePath: string,\n pruneContext: PruneContext,\n shouldPurge: boolean,\n shouldMinify: boolean\n): void => {\n let rawJson: string;\n try {\n rawJson = readFileSync(filePath, 'utf-8');\n } catch {\n return;\n }\n\n let parsedDict: CompiledDictionaryJson;\n try {\n parsedDict = JSON.parse(rawJson) as CompiledDictionaryJson;\n } catch {\n return;\n }\n\n const { key: dictionaryKey } = parsedDict;\n if (!dictionaryKey) return;\n if (pruneContext.dictionariesWithEdgeCases.has(dictionaryKey)) return;\n\n let modified = false;\n\n if (shouldPurge) {\n const fieldUsage =\n pruneContext.dictionaryKeyToFieldUsageMap.get(dictionaryKey);\n if (fieldUsage && fieldUsage !== 'all') {\n const { prunedDictionary, wasRecognised } = pruneStaticDictionaryContent(\n parsedDict,\n fieldUsage\n );\n if (!wasRecognised) {\n pruneContext.dictionariesWithEdgeCases.add(dictionaryKey);\n return;\n }\n parsedDict = prunedDictionary;\n modified = true;\n }\n }\n\n if (shouldMinify) {\n const fieldRenameMap =\n pruneContext.dictionaryKeyToFieldRenameMap.get(dictionaryKey);\n if (fieldRenameMap && fieldRenameMap.size > 0) {\n parsedDict = applyFieldRenameToDict(\n parsedDict as Record<string, unknown>,\n fieldRenameMap\n ) as CompiledDictionaryJson;\n modified = true;\n }\n }\n\n if (!modified) return;\n\n const outputDict = shouldMinify\n ? { key: parsedDict.key, content: parsedDict.content }\n : parsedDict;\n\n try {\n writeFileSync(filePath, JSON.stringify(outputDict), 'utf-8');\n } catch {\n // Write failure – leave file unchanged.\n }\n};\n\nconst processDynamicDictionaryFile = (\n filePath: string,\n pruneContext: PruneContext,\n shouldPurge: boolean,\n shouldMinify: boolean\n): void => {\n let rawJson: string;\n try {\n rawJson = readFileSync(filePath, 'utf-8');\n } catch {\n return;\n }\n\n let parsedDict: CompiledDictionaryJson;\n try {\n parsedDict = JSON.parse(rawJson) as CompiledDictionaryJson;\n } catch {\n return;\n }\n\n const { key: dictionaryKey } = parsedDict;\n if (!dictionaryKey) return;\n if (pruneContext.dictionariesWithEdgeCases.has(dictionaryKey)) return;\n\n let modified = false;\n\n if (shouldPurge) {\n const fieldUsage =\n pruneContext.dictionaryKeyToFieldUsageMap.get(dictionaryKey);\n if (fieldUsage && fieldUsage !== 'all') {\n const { prunedDictionary, wasRecognised } = pruneDynamicDictionaryContent(\n parsedDict,\n fieldUsage\n );\n if (!wasRecognised) {\n pruneContext.dictionariesWithEdgeCases.add(dictionaryKey);\n return;\n }\n parsedDict = prunedDictionary;\n modified = true;\n }\n }\n\n if (shouldMinify) {\n const fieldRenameMap =\n pruneContext.dictionaryKeyToFieldRenameMap.get(dictionaryKey);\n if (fieldRenameMap && fieldRenameMap.size > 0) {\n parsedDict = applyFieldRenameToDict(\n parsedDict as Record<string, unknown>,\n fieldRenameMap\n ) as CompiledDictionaryJson;\n modified = true;\n }\n }\n\n if (!modified) return;\n\n const outputDict = shouldMinify\n ? {\n key: parsedDict.key,\n content: parsedDict.content,\n locale: parsedDict.locale,\n }\n : parsedDict;\n\n try {\n writeFileSync(filePath, JSON.stringify(outputDict), 'utf-8');\n } catch {\n // Write failure – leave file unchanged.\n }\n};\n\nconst processAllDictionaryFiles = (\n dictionariesDir: string,\n dynamicDictionariesDir: string,\n pruneContext: PruneContext,\n shouldPurge: boolean,\n shouldMinify: boolean\n): void => {\n if (existsSync(dictionariesDir)) {\n for (const entry of readdirSync(dictionariesDir)) {\n if (!entry.endsWith('.json')) continue;\n processStaticDictionaryFile(\n join(dictionariesDir, entry),\n pruneContext,\n shouldPurge,\n shouldMinify\n );\n }\n }\n\n if (existsSync(dynamicDictionariesDir)) {\n for (const keyDir of readdirSync(dynamicDictionariesDir)) {\n const keyDirPath = join(dynamicDictionariesDir, keyDir);\n try {\n for (const localeFile of readdirSync(keyDirPath)) {\n if (!localeFile.endsWith('.json')) continue;\n processDynamicDictionaryFile(\n join(keyDirPath, localeFile),\n pruneContext,\n shouldPurge,\n shouldMinify\n );\n }\n } catch {\n // Unreadable key directory – skip.\n }\n }\n }\n};\n\n// ── Main initialisation ───────────────────────────────────────────────────────\n\n/**\n * Runs the full purge/minify pipeline for the given options, using a\n * module-level cache so the work happens at most once per process per\n * unique `baseDir`.\n */\nconst runPurgePipeline = (options: PurgePluginOptions): PruneContext => {\n const {\n baseDir,\n purge,\n minify,\n optimize,\n editorEnabled,\n dictionariesDir,\n dynamicDictionariesDir,\n componentFilesList,\n dictionaryKeyToImportModeMap,\n } = options;\n\n const cachedContext = _pruneContextCache.get(baseDir);\n if (cachedContext) return cachedContext;\n\n const pruneContext = createPruneContext();\n _pruneContextCache.set(baseDir, pruneContext);\n\n const shouldPurge = purge && !editorEnabled;\n const shouldMinify = minify && !editorEnabled;\n\n if ((!shouldPurge && !shouldMinify) || optimize === false)\n return pruneContext;\n\n // Phase 1: Synchronously analyse all component source files.\n for (const sourceFilePath of componentFilesList) {\n if (!SOURCE_FILE_REGEX.test(sourceFilePath)) continue;\n analyzeSourceFileSync(sourceFilePath, pruneContext);\n }\n\n // Phase 2: Build field-rename maps (minify only).\n if (shouldMinify) {\n buildRenameMapsSynchronously(\n dictionariesDir,\n dynamicDictionariesDir,\n dictionaryKeyToImportModeMap,\n pruneContext\n );\n }\n\n // Phase 3: Write pruned / minified dictionary JSON files to disk.\n processAllDictionaryFiles(\n dictionariesDir,\n dynamicDictionariesDir,\n pruneContext,\n shouldPurge,\n shouldMinify\n );\n\n return pruneContext;\n};\n\n// ── Babel plugin ──────────────────────────────────────────────────────────────\n\n/**\n * Babel plugin that analyses all project source files and rewrites compiled\n * dictionary JSON files in-place to remove unused content fields\n * (`build.purge`) and/or rename them to short alphabetic aliases\n * (`build.minify`).\n *\n * All option values must be pre-resolved via {@link getPurgePluginOptions}\n * before being passed here — the plugin does not load the intlayer\n * configuration itself.\n *\n * This plugin performs **file I/O as a side effect**: on the very first Babel\n * transform in a given Node.js process it synchronously scans the component\n * files listed in `options.componentFilesList`, builds field-usage data, and\n * writes the processed dictionaries to disk. Subsequent transforms are\n * no-ops.\n *\n * Source-code field renames (rewriting `content.title` → `content.a`) are\n * handled by the companion {@link intlayerMinifyBabelPlugin}.\n *\n * @example\n * ```js\n * // babel.config.js\n * const {\n * intlayerPurgeBabelPlugin,\n * intlayerMinifyBabelPlugin,\n * intlayerOptimizeBabelPlugin,\n * getPurgePluginOptions,\n * getMinifyPluginOptions,\n * getOptimizePluginOptions,\n * } = require(\"@intlayer/babel\");\n *\n * module.exports = {\n * presets: [\"next/babel\"],\n * plugins: [\n * [intlayerPurgeBabelPlugin, getPurgePluginOptions()],\n * [intlayerMinifyBabelPlugin, getMinifyPluginOptions()],\n * [intlayerOptimizeBabelPlugin, getOptimizePluginOptions()],\n * ],\n * };\n * ```\n *\n * @remarks\n * - Intended for **production builds** only. Dictionary JSON files are\n * overwritten in-place; running `intlayer build` afterwards restores the\n * originals.\n * - The plugin is a no-op when `optimize` is `false` or `editorEnabled` is\n * `true`.\n */\nexport const intlayerPurgeBabelPlugin = (_babel: {\n types: typeof BabelTypes;\n}): PluginObj => ({\n name: 'intlayer-purge',\n\n pre(this: PluginPass & { opts: PurgePluginOptions }) {\n const { baseDir } = this.opts;\n\n if (_completedBaseDirs.has(baseDir)) return;\n _completedBaseDirs.add(baseDir);\n\n runPurgePipeline(this.opts);\n },\n\n visitor: {\n // No AST transforms: all work is done as a side effect in pre().\n },\n});\n"],"mappings":";;;;;;;;;;;;;AAkGA,MAAM,qCAAqB,IAAI,KAA2B;;;;;AAM1D,MAAM,qCAAqB,IAAI,KAAa;;;;;;;;;AAU5C,MAAa,yBAAyB,YACpC,mBAAmB,IAAI,QAAQ,IAAI;AAkBrC,MAAM,qBAAqB,UACzB,OAAO,UAAU,YACjB,UAAU,QACT,MAAkC,aAAa,iBAChD,OAAQ,MAAkC,gBAAgB;AAE5D,MAAM,iBAAiB,UACrB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,MAAM;;;;;;AActE,MAAM,gCACJ,YACA,mBACgB;CAChB,MAAM,EAAE,YAAY;AAGpB,KAAI,kBAAkB,QAAQ,EAAE;EAC9B,MAAM,mBAAmB,OAAO,OAAO,QAAQ,YAAY,CAAC;AAC5D,MAAI,cAAc,iBAAiB,EAAE;GACnC,MAAM,oBAA6C,EAAE;AACrD,QAAK,MAAM,CAAC,QAAQ,kBAAkB,OAAO,QAC3C,QAAQ,YACT,EAAE;AACD,QAAI,CAAC,cAAc,cAAc,EAAE;AACjC,uBAAkB,UAAU;AAC5B;;IAEF,MAAM,qBAA8C,EAAE;AACtD,SAAK,MAAM,CAAC,WAAW,eAAe,OAAO,QAAQ,cAAc,CACjE,KAAI,eAAe,IAAI,UAAU,CAC/B,oBAAmB,aAAa;AAGpC,sBAAkB,UAAU;;AAE9B,UAAO;IACL,kBAAkB;KAChB,GAAG;KACH,SAAS;MAAE,GAAG;MAAS,aAAa;MAAmB;KACxD;IACD,eAAe;IAChB;;;AAKL,KAAI,cAAc,QAAQ,IAAI,CAAC,kBAAkB,QAAQ,EAA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