@intlayer/babel 9.4.3 → 9.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -2
- package/dist/cjs/analyzeContentUsage.cjs +301 -0
- package/dist/cjs/analyzeContentUsage.cjs.map +1 -0
- package/dist/cjs/babel-plugin-intlayer-field-rename.cjs +5 -4
- package/dist/cjs/babel-plugin-intlayer-field-rename.cjs.map +1 -1
- package/dist/cjs/babel-plugin-intlayer-optimize.cjs +173 -282
- package/dist/cjs/babel-plugin-intlayer-optimize.cjs.map +1 -1
- package/dist/cjs/babel-plugin-intlayer-purge.cjs +4 -3
- package/dist/cjs/babel-plugin-intlayer-purge.cjs.map +1 -1
- package/dist/cjs/babel-plugin-intlayer-usage-analyzer.cjs +49 -699
- package/dist/cjs/babel-plugin-intlayer-usage-analyzer.cjs.map +1 -1
- package/dist/cjs/compat/namespaceResolution.cjs +124 -0
- package/dist/cjs/compat/namespaceResolution.cjs.map +1 -0
- package/dist/cjs/compat/optimizePass.cjs +144 -0
- package/dist/cjs/compat/optimizePass.cjs.map +1 -0
- package/dist/cjs/compat/usageAnalysis.cjs +195 -0
- package/dist/cjs/compat/usageAnalysis.cjs.map +1 -0
- package/dist/cjs/dictionaryImports.cjs +40 -0
- package/dist/cjs/dictionaryImports.cjs.map +1 -0
- package/dist/cjs/extractContent/utils/extractDictionaryInfo.cjs +1 -1
- package/dist/cjs/getExtractPluginOptions.cjs +1 -1
- package/dist/cjs/getOptimizePluginOptions.cjs +1 -1
- package/dist/cjs/getPurgePluginOptions.cjs +1 -1
- package/dist/cjs/index.cjs +9 -5
- package/dist/cjs/nativeCallers.cjs +41 -0
- package/dist/cjs/nativeCallers.cjs.map +1 -0
- package/dist/cjs/pruneContext.cjs +79 -0
- package/dist/cjs/pruneContext.cjs.map +1 -0
- package/dist/cjs/staticAstReaders.cjs +140 -0
- package/dist/cjs/staticAstReaders.cjs.map +1 -0
- package/dist/cjs/transformers.cjs +2 -1
- package/dist/cjs/transformers.cjs.map +1 -1
- package/dist/esm/analyzeContentUsage.mjs +300 -0
- package/dist/esm/analyzeContentUsage.mjs.map +1 -0
- package/dist/esm/babel-plugin-intlayer-field-rename.mjs +3 -2
- package/dist/esm/babel-plugin-intlayer-field-rename.mjs.map +1 -1
- package/dist/esm/babel-plugin-intlayer-optimize.mjs +173 -282
- package/dist/esm/babel-plugin-intlayer-optimize.mjs.map +1 -1
- package/dist/esm/babel-plugin-intlayer-purge.mjs +3 -2
- package/dist/esm/babel-plugin-intlayer-purge.mjs.map +1 -1
- package/dist/esm/babel-plugin-intlayer-usage-analyzer.mjs +43 -694
- package/dist/esm/babel-plugin-intlayer-usage-analyzer.mjs.map +1 -1
- package/dist/esm/compat/namespaceResolution.mjs +120 -0
- package/dist/esm/compat/namespaceResolution.mjs.map +1 -0
- package/dist/esm/compat/optimizePass.mjs +143 -0
- package/dist/esm/compat/optimizePass.mjs.map +1 -0
- package/dist/esm/compat/usageAnalysis.mjs +193 -0
- package/dist/esm/compat/usageAnalysis.mjs.map +1 -0
- package/dist/esm/dictionaryImports.mjs +38 -0
- package/dist/esm/dictionaryImports.mjs.map +1 -0
- package/dist/esm/extractContent/utils/extractDictionaryInfo.mjs +1 -1
- package/dist/esm/getExtractPluginOptions.mjs +1 -1
- package/dist/esm/getOptimizePluginOptions.mjs +1 -1
- package/dist/esm/getPurgePluginOptions.mjs +1 -1
- package/dist/esm/index.mjs +5 -2
- package/dist/esm/nativeCallers.mjs +38 -0
- package/dist/esm/nativeCallers.mjs.map +1 -0
- package/dist/esm/pruneContext.mjs +76 -0
- package/dist/esm/pruneContext.mjs.map +1 -0
- package/dist/esm/staticAstReaders.mjs +129 -0
- package/dist/esm/staticAstReaders.mjs.map +1 -0
- package/dist/esm/transformers.mjs +2 -1
- package/dist/esm/transformers.mjs.map +1 -1
- package/dist/types/analyzeContentUsage.d.ts +18 -0
- package/dist/types/analyzeContentUsage.d.ts.map +1 -0
- package/dist/types/babel-plugin-intlayer-extract.d.ts +3 -4
- package/dist/types/babel-plugin-intlayer-extract.d.ts.map +1 -1
- package/dist/types/babel-plugin-intlayer-field-rename.d.ts +10 -10
- package/dist/types/babel-plugin-intlayer-field-rename.d.ts.map +1 -1
- package/dist/types/babel-plugin-intlayer-minify.d.ts +2 -3
- package/dist/types/babel-plugin-intlayer-minify.d.ts.map +1 -1
- package/dist/types/babel-plugin-intlayer-optimize.d.ts +11 -18
- package/dist/types/babel-plugin-intlayer-optimize.d.ts.map +1 -1
- package/dist/types/babel-plugin-intlayer-purge.d.ts +7 -7
- package/dist/types/babel-plugin-intlayer-purge.d.ts.map +1 -1
- package/dist/types/babel-plugin-intlayer-usage-analyzer.d.ts +22 -189
- package/dist/types/babel-plugin-intlayer-usage-analyzer.d.ts.map +1 -1
- package/dist/types/babelParser.d.ts +3 -4
- package/dist/types/babelParser.d.ts.map +1 -1
- package/dist/types/compat/namespaceResolution.d.ts +66 -0
- package/dist/types/compat/namespaceResolution.d.ts.map +1 -0
- package/dist/types/compat/optimizePass.d.ts +58 -0
- package/dist/types/compat/optimizePass.d.ts.map +1 -0
- package/dist/types/compat/usageAnalysis.d.ts +75 -0
- package/dist/types/compat/usageAnalysis.d.ts.map +1 -0
- package/dist/types/dictionaryImports.d.ts +46 -0
- package/dist/types/dictionaryImports.d.ts.map +1 -0
- package/dist/types/extractContent/babelProcessor.d.ts +4 -5
- package/dist/types/extractContent/babelProcessor.d.ts.map +1 -1
- package/dist/types/extractContent/contentWriter.d.ts +3 -4
- package/dist/types/extractContent/contentWriter.d.ts.map +1 -1
- package/dist/types/extractContent/extractContent.d.ts +3 -4
- package/dist/types/extractContent/extractContent.d.ts.map +1 -1
- package/dist/types/extractContent/processTsxFile.d.ts +2 -3
- package/dist/types/extractContent/processTsxFile.d.ts.map +1 -1
- package/dist/types/extractContent/utils/constants.d.ts +3 -4
- package/dist/types/extractContent/utils/constants.d.ts.map +1 -1
- package/dist/types/extractContent/utils/detectPackageName.d.ts +1 -2
- package/dist/types/extractContent/utils/detectPackageName.d.ts.map +1 -1
- package/dist/types/extractContent/utils/extractDictionaryInfo.d.ts +5 -6
- package/dist/types/extractContent/utils/extractDictionaryInfo.d.ts.map +1 -1
- package/dist/types/extractContent/utils/extractDictionaryKey.d.ts +2 -3
- package/dist/types/extractContent/utils/extractDictionaryKey.d.ts.map +1 -1
- package/dist/types/extractContent/utils/generateKey.d.ts +1 -2
- package/dist/types/extractContent/utils/generateKey.d.ts.map +1 -1
- package/dist/types/extractContent/utils/getComponentName.d.ts +1 -2
- package/dist/types/extractContent/utils/getComponentName.d.ts.map +1 -1
- package/dist/types/extractContent/utils/getExistingIntlayerInfo.d.ts +2 -3
- package/dist/types/extractContent/utils/getExistingIntlayerInfo.d.ts.map +1 -1
- package/dist/types/extractContent/utils/getOrGenerateKey.d.ts +1 -2
- package/dist/types/extractContent/utils/getOrGenerateKey.d.ts.map +1 -1
- package/dist/types/extractContent/utils/resolveDictionaryKey.d.ts +1 -2
- package/dist/types/extractContent/utils/resolveDictionaryKey.d.ts.map +1 -1
- package/dist/types/extractScriptBlocks.d.ts +3 -4
- package/dist/types/extractScriptBlocks.d.ts.map +1 -1
- package/dist/types/getExtractPluginOptions.d.ts +2 -3
- package/dist/types/getExtractPluginOptions.d.ts.map +1 -1
- package/dist/types/getOptimizePluginOptions.d.ts +1 -2
- package/dist/types/getOptimizePluginOptions.d.ts.map +1 -1
- package/dist/types/getPurgePluginOptions.d.ts +2 -3
- package/dist/types/getPurgePluginOptions.d.ts.map +1 -1
- package/dist/types/index.d.ts +5 -2
- package/dist/types/nativeCallers.d.ts +33 -0
- package/dist/types/nativeCallers.d.ts.map +1 -0
- package/dist/types/normalizedFilesList.d.ts +1 -2
- package/dist/types/normalizedFilesList.d.ts.map +1 -1
- package/dist/types/pruneContext.d.ts +152 -0
- package/dist/types/pruneContext.d.ts.map +1 -0
- package/dist/types/staticAstReaders.d.ts +84 -0
- package/dist/types/staticAstReaders.d.ts.map +1 -0
- package/dist/types/transformers.d.ts +11 -11
- package/dist/types/transformers.d.ts.map +1 -1
- package/package.json +12 -12
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_staticAstReaders = require('./staticAstReaders.cjs');
|
|
3
|
+
const require_dictionaryImports = require('./dictionaryImports.cjs');
|
|
2
4
|
const require_normalizedFilesList = require('./normalizedFilesList.cjs');
|
|
5
|
+
const require_compat_optimizePass = require('./compat/optimizePass.cjs');
|
|
3
6
|
let node_path = require("node:path");
|
|
4
|
-
let _intlayer_engine_utils = require("@intlayer/engine/utils");
|
|
5
7
|
let _intlayer_config_utils = require("@intlayer/config/utils");
|
|
6
|
-
let _intlayer_config_callers = require("@intlayer/config/callers");
|
|
7
8
|
|
|
8
9
|
//#region src/babel-plugin-intlayer-optimize.ts
|
|
9
10
|
const PACKAGE_LIST = [
|
|
@@ -29,10 +30,10 @@ const CALLER_LIST = [
|
|
|
29
30
|
"getIntlayer",
|
|
30
31
|
"getIntlayerAsync"
|
|
31
32
|
];
|
|
32
|
-
/**
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
33
|
+
/** Membership sets for the tables above — hit once per import and per call. */
|
|
34
|
+
const NATIVE_PACKAGE_SET = new Set(PACKAGE_LIST);
|
|
35
|
+
const CALLER_NAME_SET = new Set(CALLER_LIST);
|
|
36
|
+
const DYNAMIC_PACKAGE_SET = /* @__PURE__ */ new Set([
|
|
36
37
|
"react-intlayer",
|
|
37
38
|
"react-intlayer/client",
|
|
38
39
|
"react-intlayer/server",
|
|
@@ -46,7 +47,7 @@ const PACKAGE_LIST_DYNAMIC = [
|
|
|
46
47
|
"angular-intlayer",
|
|
47
48
|
"lit-intlayer",
|
|
48
49
|
"vanilla-intlayer"
|
|
49
|
-
];
|
|
50
|
+
]);
|
|
50
51
|
const STATIC_IMPORT_FUNCTION = {
|
|
51
52
|
getIntlayer: "getDictionary",
|
|
52
53
|
getIntlayerAsync: "getDictionaryAsync",
|
|
@@ -67,15 +68,6 @@ const DYNAMIC_HELPER_MAP = {
|
|
|
67
68
|
*/
|
|
68
69
|
const SSR_STATIC_IMPORT_SOURCE = { "solid-intlayer": "solid-intlayer/server" };
|
|
69
70
|
/**
|
|
70
|
-
* Replicates the xxHash64 → Base-62 algorithm used by the SWC version
|
|
71
|
-
* and prefixes an underscore so the generated identifiers never collide
|
|
72
|
-
* with user-defined ones.
|
|
73
|
-
*/
|
|
74
|
-
const makeIdent = (key, t) => {
|
|
75
|
-
const hash = (0, _intlayer_engine_utils.getPathHash)(key);
|
|
76
|
-
return t.identifier(`_${hash}`);
|
|
77
|
-
};
|
|
78
|
-
/**
|
|
79
71
|
* Builds the module specifier pointing at `targetPath` from `fromFile`, with
|
|
80
72
|
* forward slashes and an explicit `./` prefix so bundlers never treat it as a
|
|
81
73
|
* bare package specifier.
|
|
@@ -97,66 +89,26 @@ const computeImport = (fromFile, dictionariesDir, dynamicDictionariesDir, fetchD
|
|
|
97
89
|
if (importMode === "dynamic") dictionaryPath = (0, node_path.join)(dynamicDictionariesDir, `${key}.mjs`);
|
|
98
90
|
return toRelativeSpecifier(fromFile, dictionaryPath);
|
|
99
91
|
};
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const
|
|
92
|
+
const isCallerName = (name) => CALLER_NAME_SET.has(name);
|
|
93
|
+
const isDynamicPackage = (packageName) => DYNAMIC_PACKAGE_SET.has(packageName);
|
|
94
|
+
/** The name an import specifier brings in, whether written as an identifier or a string. */
|
|
95
|
+
const importedSpecifierName = (babelTypes, specifier) => babelTypes.isIdentifier(specifier.imported) ? specifier.imported.name : specifier.imported.value;
|
|
96
|
+
const EMPTY_KEY_SET = /* @__PURE__ */ new Set();
|
|
105
97
|
/**
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
* `'about'` → `{ dictionaryKey: 'about', keyPrefix: '' }`
|
|
111
|
-
* `'about.counter'` → `{ dictionaryKey: 'about', keyPrefix: 'counter' }`
|
|
98
|
+
* `nestingDictionaryKeys` as a set, cached by array reference: Babel reuses one
|
|
99
|
+
* options object for the whole build, so the set is built once instead of once
|
|
100
|
+
* per transformed file.
|
|
112
101
|
*/
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
if (
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
dictionaryKey: namespace.slice(0, dotPosition),
|
|
121
|
-
keyPrefix: namespace.slice(dotPosition + 1)
|
|
122
|
-
};
|
|
123
|
-
};
|
|
124
|
-
/**
|
|
125
|
-
* Statically resolves the namespace of a compat caller call-site from its
|
|
126
|
-
* descriptor's `namespaceSources` (argument / option / fixed — the
|
|
127
|
-
* per-message-id `path-first-segment` form cannot bind one dictionary to the
|
|
128
|
-
* call site and is skipped). Returns `undefined` when the namespace is absent
|
|
129
|
-
* or dynamic, in which case the call is left untouched and resolves through
|
|
130
|
-
* the runtime dictionary registry.
|
|
131
|
-
*/
|
|
132
|
-
const resolveCompatNamespaceMatch = (t, callArguments, descriptor) => {
|
|
133
|
-
for (const source of descriptor.namespaceSources) {
|
|
134
|
-
if (source.from === "fixed") return { fullNamespace: source.value };
|
|
135
|
-
if (source.from === "argument") {
|
|
136
|
-
const namespace = getKeyFromArgument(callArguments[source.index], t);
|
|
137
|
-
if (namespace !== void 0) return {
|
|
138
|
-
fullNamespace: namespace,
|
|
139
|
-
argumentIndex: source.index
|
|
140
|
-
};
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
if (source.from === "option") {
|
|
144
|
-
const optionsArgument = callArguments[source.argumentIndex];
|
|
145
|
-
if (!optionsArgument || !t.isObjectExpression(optionsArgument)) continue;
|
|
146
|
-
for (const property of optionsArgument.properties) {
|
|
147
|
-
if (!t.isObjectProperty(property)) continue;
|
|
148
|
-
if (!(t.isIdentifier(property.key) && property.key.name === source.property || t.isStringLiteral(property.key) && property.key.value === source.property)) continue;
|
|
149
|
-
const namespace = getKeyFromArgument(property.value, t);
|
|
150
|
-
if (namespace !== void 0) return {
|
|
151
|
-
fullNamespace: namespace,
|
|
152
|
-
optionProperty: property,
|
|
153
|
-
optionsObject: optionsArgument
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
}
|
|
102
|
+
const nestingKeysCache = /* @__PURE__ */ new WeakMap();
|
|
103
|
+
const getNestingDictionaryKeySet = (nestingDictionaryKeys) => {
|
|
104
|
+
if (!nestingDictionaryKeys) return EMPTY_KEY_SET;
|
|
105
|
+
let keySet = nestingKeysCache.get(nestingDictionaryKeys);
|
|
106
|
+
if (!keySet) {
|
|
107
|
+
keySet = new Set(nestingDictionaryKeys);
|
|
108
|
+
nestingKeysCache.set(nestingDictionaryKeys, keySet);
|
|
157
109
|
}
|
|
110
|
+
return keySet;
|
|
158
111
|
};
|
|
159
|
-
const isDynamicPackage = (packageName) => PACKAGE_LIST_DYNAMIC.includes(packageName);
|
|
160
112
|
/**
|
|
161
113
|
* Decides, once per package import, which helper family applies to this file.
|
|
162
114
|
* The import rewrite and the per-call rewrite must both derive from this
|
|
@@ -172,6 +124,29 @@ const resolveHelperPlan = (packageName, importMode, isServer, packageHasDynamicC
|
|
|
172
124
|
return "static";
|
|
173
125
|
};
|
|
174
126
|
/**
|
|
127
|
+
* Builds the dictionary import declarations the rewrite accumulated for one
|
|
128
|
+
* file — the compiled JSON (or its `nest()` companion) for static reads, and
|
|
129
|
+
* the generated per-locale loader for dynamic and fetch ones.
|
|
130
|
+
*/
|
|
131
|
+
const buildDictionaryImportDeclarations = (babelTypes, state, imports) => {
|
|
132
|
+
const { dictionariesDir, dynamicDictionariesDir, fetchDictionariesDir, nestingDictionaryKeys } = state.opts;
|
|
133
|
+
const fromFile = state.file.opts.filename;
|
|
134
|
+
const nestingKeys = getNestingDictionaryKeySet(nestingDictionaryKeys);
|
|
135
|
+
const importDeclarations = [];
|
|
136
|
+
for (const [key, ident] of imports.staticImports) {
|
|
137
|
+
const hasNestedDictionaries = nestingKeys.has(key);
|
|
138
|
+
const specifier = computeImport(fromFile, dictionariesDir, dynamicDictionariesDir, fetchDictionariesDir, key, "static", hasNestedDictionaries);
|
|
139
|
+
const importDeclaration = babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(ident.name))], babelTypes.stringLiteral(specifier));
|
|
140
|
+
if (!hasNestedDictionaries) importDeclaration.attributes = [babelTypes.importAttribute(babelTypes.identifier("type"), babelTypes.stringLiteral("json"))];
|
|
141
|
+
importDeclarations.push(importDeclaration);
|
|
142
|
+
}
|
|
143
|
+
for (const [key, ident] of imports.dynamicImports) {
|
|
144
|
+
const mode = ident.name.endsWith("_fetch") ? "fetch" : "dynamic";
|
|
145
|
+
importDeclarations.push(babelTypes.importDeclaration([babelTypes.importDefaultSpecifier(babelTypes.identifier(ident.name))], babelTypes.stringLiteral(computeImport(fromFile, dictionariesDir, dynamicDictionariesDir, fetchDictionariesDir, key, mode))));
|
|
146
|
+
}
|
|
147
|
+
return importDeclarations;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
175
150
|
* Babel plugin that transforms Intlayer function calls and auto-imports dictionaries.
|
|
176
151
|
*
|
|
177
152
|
* This plugin transforms calls to `useIntlayer()`, `getIntlayer()` and
|
|
@@ -272,13 +247,7 @@ const intlayerOptimizeBabelPlugin = (babel) => {
|
|
|
272
247
|
return {
|
|
273
248
|
name: "babel-plugin-intlayer-transform",
|
|
274
249
|
pre() {
|
|
275
|
-
this._newStaticImports = /* @__PURE__ */ new Map();
|
|
276
|
-
this._newDynamicImports = /* @__PURE__ */ new Map();
|
|
277
|
-
this._callerMap = /* @__PURE__ */ new Map();
|
|
278
|
-
this._callerPackageMap = /* @__PURE__ */ new Map();
|
|
279
|
-
this._compatCallerMap = /* @__PURE__ */ new Map();
|
|
280
250
|
this._isIncluded = true;
|
|
281
|
-
this._hasValidImport = false;
|
|
282
251
|
this._isDictEntry = false;
|
|
283
252
|
if (this.opts.optimize === false) {
|
|
284
253
|
this._isIncluded = false;
|
|
@@ -296,16 +265,11 @@ const intlayerOptimizeBabelPlugin = (babel) => {
|
|
|
296
265
|
enter(programPath, state) {
|
|
297
266
|
const filename = state.file.opts.filename ? (0, _intlayer_config_utils.normalizePath)(state.file.opts.filename) : void 0;
|
|
298
267
|
const dictionariesEntryPath = state.opts.dictionariesEntryPath ? (0, _intlayer_config_utils.normalizePath)(state.opts.dictionariesEntryPath) : void 0;
|
|
299
|
-
|
|
268
|
+
const unmergedDictionariesEntryPath = state.opts.unmergedDictionariesEntryPath ? (0, _intlayer_config_utils.normalizePath)(state.opts.unmergedDictionariesEntryPath) : void 0;
|
|
269
|
+
const entryAccessorName = filename ? filename === dictionariesEntryPath ? "getDictionaries" : filename === unmergedDictionariesEntryPath ? "getUnmergedDictionaries" : void 0 : void 0;
|
|
270
|
+
if (state.opts.replaceDictionaryEntry && entryAccessorName) {
|
|
300
271
|
state._isDictEntry = true;
|
|
301
|
-
programPath.
|
|
302
|
-
ImportDeclaration(path) {
|
|
303
|
-
path.remove();
|
|
304
|
-
},
|
|
305
|
-
VariableDeclarator(path) {
|
|
306
|
-
if (t.isObjectExpression(path.node.init)) path.node.init.properties = [];
|
|
307
|
-
}
|
|
308
|
-
});
|
|
272
|
+
programPath.node.body = [t.exportDefaultDeclaration(t.objectExpression([])), t.exportNamedDeclaration(t.variableDeclaration("const", [t.variableDeclarator(t.identifier(entryAccessorName), t.arrowFunctionExpression([], t.objectExpression([])))]))];
|
|
309
273
|
}
|
|
310
274
|
},
|
|
311
275
|
/**
|
|
@@ -319,215 +283,142 @@ const intlayerOptimizeBabelPlugin = (babel) => {
|
|
|
319
283
|
exit(programPath, state) {
|
|
320
284
|
if (state._isDictEntry) return;
|
|
321
285
|
if (!state._isIncluded) return;
|
|
322
|
-
const
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
286
|
+
const imports = require_dictionaryImports.createDictionaryImportRegistry(t);
|
|
287
|
+
const compat = require_compat_optimizePass.createCompatOptimizePass(t, state.opts.compatCallers ?? [], imports, {
|
|
288
|
+
importMode: state.opts.importMode,
|
|
289
|
+
dictionaryModeMap: state.opts.dictionaryModeMap
|
|
290
|
+
});
|
|
291
|
+
const programBody = programPath.node.body;
|
|
292
|
+
const nativeImportNodes = [];
|
|
293
|
+
const compatImportNodes = [];
|
|
294
|
+
/** Local alias → the native caller it was imported as. */
|
|
295
|
+
const callerMap = /* @__PURE__ */ new Map();
|
|
296
|
+
/** Local alias → the intlayer package it was imported from. */
|
|
297
|
+
const callerPackageMap = /* @__PURE__ */ new Map();
|
|
298
|
+
/** Whether the file imported any native or compat caller at all. */
|
|
299
|
+
let hasValidImport = false;
|
|
300
|
+
for (const statement of programBody) {
|
|
301
|
+
if (!t.isImportDeclaration(statement)) continue;
|
|
302
|
+
const src = statement.source.value;
|
|
303
|
+
const isNativePackage = NATIVE_PACKAGE_SET.has(src);
|
|
304
|
+
const isCompatPackage = compat?.ownsImportSource(src) ?? false;
|
|
305
|
+
if (!isNativePackage && !isCompatPackage) continue;
|
|
306
|
+
hasValidImport = true;
|
|
307
|
+
if (isNativePackage) nativeImportNodes.push(statement);
|
|
308
|
+
if (isCompatPackage) compatImportNodes.push(statement);
|
|
309
|
+
for (const spec of statement.specifiers) {
|
|
330
310
|
if (!t.isImportSpecifier(spec)) continue;
|
|
331
|
-
const importedName = t
|
|
311
|
+
const importedName = importedSpecifierName(t, spec);
|
|
332
312
|
if (isNativePackage && isCallerName(importedName)) {
|
|
333
|
-
|
|
334
|
-
|
|
313
|
+
callerMap.set(spec.local.name, importedName);
|
|
314
|
+
callerPackageMap.set(spec.local.name, src);
|
|
335
315
|
}
|
|
336
|
-
|
|
337
|
-
if (compatDescriptor) state._compatCallerMap?.set(spec.local.name, compatDescriptor);
|
|
316
|
+
if (isCompatPackage) compat?.noteImport(src, importedName, spec.local.name);
|
|
338
317
|
}
|
|
318
|
+
}
|
|
319
|
+
const nativeCallNodes = [];
|
|
320
|
+
const compatCallNodes = [];
|
|
321
|
+
if (callerMap.size > 0 || compat?.hasCallers()) programPath.traverse({ CallExpression(path) {
|
|
322
|
+
const callee = path.node.callee;
|
|
323
|
+
if (!t.isIdentifier(callee)) return;
|
|
324
|
+
if (callerMap.has(callee.name)) nativeCallNodes.push(path.node);
|
|
325
|
+
else if (compat?.ownsLocalName(callee.name)) compatCallNodes.push(path.node);
|
|
339
326
|
} });
|
|
340
327
|
const packagesWithDynamicCall = /* @__PURE__ */ new Set();
|
|
341
328
|
const packagesWithFetchCall = /* @__PURE__ */ new Set();
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
const
|
|
346
|
-
if (!
|
|
347
|
-
const
|
|
348
|
-
if (
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
329
|
+
for (const callNode of nativeCallNodes) {
|
|
330
|
+
const callee = callNode.callee;
|
|
331
|
+
if (callerMap.get(callee.name) !== "useIntlayer") continue;
|
|
332
|
+
const callerPackage = callerPackageMap.get(callee.name);
|
|
333
|
+
if (!callerPackage) continue;
|
|
334
|
+
const key = require_staticAstReaders.readStaticString(t, callNode.arguments[0]);
|
|
335
|
+
if (!key) continue;
|
|
336
|
+
const overrideMode = state.opts.dictionaryModeMap?.[key];
|
|
337
|
+
if (overrideMode === "dynamic") packagesWithDynamicCall.add(callerPackage);
|
|
338
|
+
else if (overrideMode === "fetch") packagesWithFetchCall.add(callerPackage);
|
|
339
|
+
}
|
|
340
|
+
compat?.analyzeCalls(compatCallNodes);
|
|
341
|
+
const helperPlanCache = /* @__PURE__ */ new Map();
|
|
342
|
+
const getHelperPlan = (packageName) => {
|
|
343
|
+
let plan = helperPlanCache.get(packageName);
|
|
344
|
+
if (plan === void 0) {
|
|
345
|
+
plan = resolveHelperPlan(packageName, state.opts.importMode, state.opts.isServer, packagesWithDynamicCall.has(packageName), packagesWithFetchCall.has(packageName));
|
|
346
|
+
helperPlanCache.set(packageName, plan);
|
|
347
|
+
}
|
|
348
|
+
return plan;
|
|
349
|
+
};
|
|
350
|
+
const serverImportsByAnchor = /* @__PURE__ */ new Map();
|
|
351
|
+
const emptiedImportNodes = /* @__PURE__ */ new Set();
|
|
352
|
+
for (const importNode of compatImportNodes) compat?.rewriteImportSpecifiers(importNode);
|
|
353
|
+
for (const importNode of nativeImportNodes) {
|
|
354
|
+
const helperPlan = getHelperPlan(importNode.source.value);
|
|
355
|
+
const serverSource = helperPlan === "ssrStatic" ? SSR_STATIC_IMPORT_SOURCE[importNode.source.value] : void 0;
|
|
356
|
+
const helperMap = helperPlan === "dynamic" ? DYNAMIC_HELPER_MAP : STATIC_IMPORT_FUNCTION;
|
|
357
|
+
const serverSpecifiers = [];
|
|
358
|
+
for (const spec of importNode.specifiers) {
|
|
359
|
+
if (!t.isImportSpecifier(spec)) continue;
|
|
360
|
+
const importedName = importedSpecifierName(t, spec);
|
|
361
|
+
if (!isCallerName(importedName)) continue;
|
|
362
|
+
if (serverSource && importedName === "useIntlayer") {
|
|
363
|
+
spec.imported = t.identifier("useDictionary");
|
|
364
|
+
serverSpecifiers.push(spec);
|
|
365
|
+
continue;
|
|
353
366
|
}
|
|
354
|
-
const
|
|
355
|
-
|
|
356
|
-
if (compatOverrideMode === "dynamic" || compatOverrideMode === "fetch") compatHasDynamicCall = true;
|
|
357
|
-
return;
|
|
367
|
+
const newIdentifier = helperMap[importedName];
|
|
368
|
+
if (newIdentifier) spec.imported = t.identifier(newIdentifier);
|
|
358
369
|
}
|
|
359
|
-
if (
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
370
|
+
if (serverSpecifiers.length > 0 && serverSource) {
|
|
371
|
+
serverImportsByAnchor.set(importNode, t.importDeclaration(serverSpecifiers, t.stringLiteral(serverSource)));
|
|
372
|
+
const serverSpecifierSet = new Set(serverSpecifiers);
|
|
373
|
+
importNode.specifiers = importNode.specifiers.filter((spec) => !serverSpecifierSet.has(spec));
|
|
374
|
+
if (importNode.specifiers.length === 0) emptiedImportNodes.add(importNode);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
for (const callNode of compatCallNodes) compat?.rewriteCall(callNode);
|
|
378
|
+
for (const callNode of nativeCallNodes) {
|
|
379
|
+
const callee = callNode.callee;
|
|
380
|
+
const originalImportedName = callerMap.get(callee.name);
|
|
381
|
+
const key = require_staticAstReaders.readStaticString(t, callNode.arguments[0]);
|
|
382
|
+
if (!key) continue;
|
|
383
|
+
const callerPackage = callerPackageMap.get(callee.name);
|
|
384
|
+
const importMode = state.opts.importMode;
|
|
385
|
+
const isUseIntlayer = originalImportedName === "useIntlayer";
|
|
386
|
+
const isGetIntlayerAsync = originalImportedName === "getIntlayerAsync";
|
|
364
387
|
const dictionaryOverrideMode = state.opts.dictionaryModeMap?.[key];
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
const src = path.node.source.value;
|
|
374
|
-
for (const spec of path.node.specifiers) {
|
|
375
|
-
if (!t.isImportSpecifier(spec)) continue;
|
|
376
|
-
const compatDescriptor = state._compatCallerMap?.get(spec.local.name);
|
|
377
|
-
if (!compatDescriptor) continue;
|
|
378
|
-
if (!compatDescriptor.importSources.includes(src)) continue;
|
|
379
|
-
spec.imported = t.identifier(compatUseDynamicHelpers ? compatDescriptor.dynamicReplacement : compatDescriptor.staticReplacement);
|
|
380
|
-
}
|
|
381
|
-
if (!PACKAGE_LIST.includes(src)) return;
|
|
382
|
-
const helperPlan = getHelperPlan(src);
|
|
383
|
-
const serverSource = helperPlan === "ssrStatic" ? SSR_STATIC_IMPORT_SOURCE[src] : void 0;
|
|
384
|
-
const helperMap = helperPlan === "dynamic" ? DYNAMIC_HELPER_MAP : STATIC_IMPORT_FUNCTION;
|
|
385
|
-
const serverSpecifiers = [];
|
|
386
|
-
for (const spec of path.node.specifiers) {
|
|
387
|
-
if (!t.isImportSpecifier(spec)) continue;
|
|
388
|
-
const importedName = t.isIdentifier(spec.imported) ? spec.imported.name : spec.imported.value;
|
|
389
|
-
if (!isCallerName(importedName)) continue;
|
|
390
|
-
if (serverSource && importedName === "useIntlayer") {
|
|
391
|
-
spec.imported = t.identifier("useDictionary");
|
|
392
|
-
serverSpecifiers.push(spec);
|
|
393
|
-
continue;
|
|
394
|
-
}
|
|
395
|
-
const newIdentifier = helperMap[importedName];
|
|
396
|
-
if (newIdentifier) spec.imported = t.identifier(newIdentifier);
|
|
397
|
-
}
|
|
398
|
-
if (serverSpecifiers.length > 0 && serverSource) {
|
|
399
|
-
path.insertAfter(t.importDeclaration(serverSpecifiers, t.stringLiteral(serverSource)));
|
|
400
|
-
path.node.specifiers = path.node.specifiers.filter((spec) => !serverSpecifiers.includes(spec));
|
|
401
|
-
if (path.node.specifiers.length === 0) path.remove();
|
|
402
|
-
}
|
|
403
|
-
},
|
|
404
|
-
CallExpression(path) {
|
|
405
|
-
const callee = path.node.callee;
|
|
406
|
-
if (!t.isIdentifier(callee)) return;
|
|
407
|
-
const compatDescriptor = state._compatCallerMap?.get(callee.name);
|
|
408
|
-
if (compatDescriptor) {
|
|
409
|
-
const callArguments = path.node.arguments;
|
|
410
|
-
const namespaceMatch = resolveCompatNamespaceMatch(t, callArguments, compatDescriptor);
|
|
411
|
-
if (!namespaceMatch) return;
|
|
412
|
-
state._hasValidImport = true;
|
|
413
|
-
const { dictionaryKey, keyPrefix } = splitNamespace(namespaceMatch.fullNamespace);
|
|
414
|
-
const compatOverrideMode = state.opts.dictionaryModeMap?.[dictionaryKey];
|
|
415
|
-
let compatCallMode = "static";
|
|
416
|
-
if (compatUseDynamicHelpers) {
|
|
417
|
-
if (compatOverrideMode === "dynamic" || compatOverrideMode === "fetch") compatCallMode = compatOverrideMode;
|
|
418
|
-
else if (state.opts.importMode === "dynamic" || state.opts.importMode === "fetch") compatCallMode = state.opts.importMode;
|
|
419
|
-
else compatCallMode = "dynamic";
|
|
420
|
-
}
|
|
421
|
-
let dictionaryIdent;
|
|
422
|
-
if (compatCallMode === "static") {
|
|
423
|
-
let staticIdent = state._newStaticImports?.get(dictionaryKey);
|
|
424
|
-
if (!staticIdent) {
|
|
425
|
-
staticIdent = makeIdent(dictionaryKey, t);
|
|
426
|
-
state._newStaticImports?.set(dictionaryKey, staticIdent);
|
|
427
|
-
}
|
|
428
|
-
dictionaryIdent = staticIdent;
|
|
429
|
-
} else {
|
|
430
|
-
let dynamicIdent = state._newDynamicImports?.get(dictionaryKey);
|
|
431
|
-
if (!dynamicIdent) {
|
|
432
|
-
const hash = (0, _intlayer_engine_utils.getPathHash)(dictionaryKey);
|
|
433
|
-
const suffix = compatCallMode === "fetch" ? "fetch" : "dyn";
|
|
434
|
-
dynamicIdent = t.identifier(`_${hash}_${suffix}`);
|
|
435
|
-
state._newDynamicImports?.set(dictionaryKey, dynamicIdent);
|
|
436
|
-
}
|
|
437
|
-
dictionaryIdent = dynamicIdent;
|
|
438
|
-
}
|
|
439
|
-
if (namespaceMatch.argumentIndex !== void 0) {
|
|
440
|
-
callArguments[namespaceMatch.argumentIndex] = t.identifier(dictionaryIdent.name);
|
|
441
|
-
const insertAt = namespaceMatch.argumentIndex + 1;
|
|
442
|
-
const insertedArguments = [];
|
|
443
|
-
if (compatCallMode !== "static") insertedArguments.push(t.stringLiteral(dictionaryKey));
|
|
444
|
-
if (keyPrefix) insertedArguments.push(t.stringLiteral(keyPrefix));
|
|
445
|
-
callArguments.splice(insertAt, 0, ...insertedArguments);
|
|
446
|
-
} else if (compatCallMode !== "static") callArguments.unshift(t.identifier(dictionaryIdent.name), t.stringLiteral(dictionaryKey));
|
|
447
|
-
else callArguments.unshift(t.identifier(dictionaryIdent.name));
|
|
448
|
-
if (namespaceMatch.optionProperty && namespaceMatch.optionsObject) {
|
|
449
|
-
if (keyPrefix) namespaceMatch.optionProperty.value = t.stringLiteral(keyPrefix);
|
|
450
|
-
else namespaceMatch.optionsObject.properties = namespaceMatch.optionsObject.properties.filter((property) => property !== namespaceMatch.optionProperty);
|
|
451
|
-
}
|
|
452
|
-
return;
|
|
453
|
-
}
|
|
454
|
-
const originalImportedName = state._callerMap?.get(callee.name);
|
|
455
|
-
if (!originalImportedName) return;
|
|
456
|
-
state._hasValidImport = true;
|
|
457
|
-
const key = getKeyFromArgument(path.node.arguments[0], t);
|
|
458
|
-
if (!key) return;
|
|
459
|
-
const callerPackage = state._callerPackageMap?.get(callee.name);
|
|
460
|
-
const importMode = state.opts.importMode;
|
|
461
|
-
const isUseIntlayer = originalImportedName === "useIntlayer";
|
|
462
|
-
const isGetIntlayerAsync = originalImportedName === "getIntlayerAsync";
|
|
463
|
-
const dictionaryOverrideMode = state.opts.dictionaryModeMap?.[key];
|
|
464
|
-
const helperPlan = callerPackage === void 0 ? "static" : getHelperPlan(callerPackage);
|
|
465
|
-
let perCallMode = "static";
|
|
466
|
-
if (isGetIntlayerAsync) perCallMode = dictionaryOverrideMode === "fetch" ? "fetch" : "dynamic";
|
|
467
|
-
else if (isUseIntlayer && helperPlan === "dynamic") {
|
|
468
|
-
if (dictionaryOverrideMode) perCallMode = dictionaryOverrideMode;
|
|
469
|
-
else if (importMode === "dynamic" || importMode === "fetch") perCallMode = importMode;
|
|
470
|
-
} else if (isUseIntlayer && helperPlan === "static") {
|
|
471
|
-
if (dictionaryOverrideMode === "dynamic" || dictionaryOverrideMode === "fetch") perCallMode = dictionaryOverrideMode;
|
|
472
|
-
}
|
|
473
|
-
let ident;
|
|
474
|
-
if (perCallMode === "fetch") {
|
|
475
|
-
let dynamicIdent = state._newDynamicImports?.get(key);
|
|
476
|
-
if (!dynamicIdent) {
|
|
477
|
-
const hash = (0, _intlayer_engine_utils.getPathHash)(key);
|
|
478
|
-
dynamicIdent = t.identifier(`_${hash}_fetch`);
|
|
479
|
-
state._newDynamicImports?.set(key, dynamicIdent);
|
|
480
|
-
}
|
|
481
|
-
ident = dynamicIdent;
|
|
482
|
-
path.node.arguments = [t.identifier(ident.name), ...path.node.arguments];
|
|
483
|
-
} else if (perCallMode === "dynamic") {
|
|
484
|
-
let dynamicIdent = state._newDynamicImports?.get(key);
|
|
485
|
-
if (!dynamicIdent) {
|
|
486
|
-
const hash = (0, _intlayer_engine_utils.getPathHash)(key);
|
|
487
|
-
dynamicIdent = t.identifier(`_${hash}_dyn`);
|
|
488
|
-
state._newDynamicImports?.set(key, dynamicIdent);
|
|
489
|
-
}
|
|
490
|
-
ident = dynamicIdent;
|
|
491
|
-
path.node.arguments = [t.identifier(ident.name), ...path.node.arguments];
|
|
492
|
-
} else {
|
|
493
|
-
let staticIdent = state._newStaticImports?.get(key);
|
|
494
|
-
if (!staticIdent) {
|
|
495
|
-
staticIdent = makeIdent(key, t);
|
|
496
|
-
state._newStaticImports?.set(key, staticIdent);
|
|
497
|
-
}
|
|
498
|
-
ident = staticIdent;
|
|
499
|
-
path.node.arguments[0] = t.identifier(ident.name);
|
|
500
|
-
}
|
|
388
|
+
const helperPlan = callerPackage === void 0 ? "static" : getHelperPlan(callerPackage);
|
|
389
|
+
let perCallMode = "static";
|
|
390
|
+
if (isGetIntlayerAsync) perCallMode = dictionaryOverrideMode === "fetch" ? "fetch" : "dynamic";
|
|
391
|
+
else if (isUseIntlayer && helperPlan === "dynamic") {
|
|
392
|
+
if (dictionaryOverrideMode) perCallMode = dictionaryOverrideMode;
|
|
393
|
+
else if (importMode === "dynamic" || importMode === "fetch") perCallMode = importMode;
|
|
394
|
+
} else if (isUseIntlayer && helperPlan === "static") {
|
|
395
|
+
if (dictionaryOverrideMode === "dynamic" || dictionaryOverrideMode === "fetch") perCallMode = dictionaryOverrideMode;
|
|
501
396
|
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
const dictionariesDir = state.opts.dictionariesDir;
|
|
506
|
-
const dynamicDictionariesDir = state.opts.dynamicDictionariesDir;
|
|
507
|
-
const fetchDictionariesDir = state.opts.fetchDictionariesDir;
|
|
508
|
-
const imports = [];
|
|
509
|
-
const nestingDictionaryKeys = new Set(state.opts.nestingDictionaryKeys ?? []);
|
|
510
|
-
for (const [key, ident] of state._newStaticImports) {
|
|
511
|
-
const hasNestedDictionaries = nestingDictionaryKeys.has(key);
|
|
512
|
-
const rel = computeImport(file, dictionariesDir, dynamicDictionariesDir, fetchDictionariesDir, key, "static", hasNestedDictionaries);
|
|
513
|
-
const importDeclarationNode = t.importDeclaration([t.importDefaultSpecifier(t.identifier(ident.name))], t.stringLiteral(rel));
|
|
514
|
-
if (!hasNestedDictionaries) importDeclarationNode.attributes = [t.importAttribute(t.identifier("type"), t.stringLiteral("json"))];
|
|
515
|
-
imports.push(importDeclarationNode);
|
|
397
|
+
const ident = imports.identFor(key, perCallMode);
|
|
398
|
+
if (perCallMode === "static") callNode.arguments[0] = t.identifier(ident.name);
|
|
399
|
+
else callNode.arguments = [t.identifier(ident.name), ...callNode.arguments];
|
|
516
400
|
}
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
401
|
+
const dictionaryImports = hasValidImport ? buildDictionaryImportDeclarations(t, state, imports) : [];
|
|
402
|
+
const hasImportMoves = serverImportsByAnchor.size > 0 || emptiedImportNodes.size > 0;
|
|
403
|
+
if (dictionaryImports.length === 0 && !hasImportMoves) return;
|
|
404
|
+
const nextBody = [];
|
|
405
|
+
for (const statement of programBody) {
|
|
406
|
+
if (t.isImportDeclaration(statement) && emptiedImportNodes.has(statement)) {
|
|
407
|
+
const serverImport = serverImportsByAnchor.get(statement);
|
|
408
|
+
if (serverImport) nextBody.push(serverImport);
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
nextBody.push(statement);
|
|
412
|
+
if (t.isImportDeclaration(statement)) {
|
|
413
|
+
const serverImport = serverImportsByAnchor.get(statement);
|
|
414
|
+
if (serverImport) nextBody.push(serverImport);
|
|
415
|
+
}
|
|
521
416
|
}
|
|
522
|
-
if (!imports.length) return;
|
|
523
|
-
const bodyPaths = programPath.get("body");
|
|
524
417
|
let insertPos = 0;
|
|
525
|
-
for (const
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
}
|
|
530
|
-
programPath.node.body.splice(insertPos, 0, ...imports);
|
|
418
|
+
for (const statement of nextBody) if (t.isExpressionStatement(statement) && t.isStringLiteral(statement.expression) && !statement.expression.value.startsWith("import") && !statement.expression.value.startsWith("require")) insertPos += 1;
|
|
419
|
+
else break;
|
|
420
|
+
nextBody.splice(insertPos, 0, ...dictionaryImports);
|
|
421
|
+
programPath.node.body = nextBody;
|
|
531
422
|
}
|
|
532
423
|
} }
|
|
533
424
|
};
|