@fluentic/i18n-compiler 0.0.0-stage → 0.1.0-beta.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/CHANGELOG.md +26 -0
- package/LICENSE +21 -0
- package/README.md +23 -2
- package/dist/analyze.d.ts +25 -0
- package/dist/babel.d.ts +9 -0
- package/dist/babel.mjs +33 -0
- package/dist/babel.mjs.map +1 -0
- package/dist/esbuild.d.ts +5 -0
- package/dist/esbuild.mjs +35 -0
- package/dist/esbuild.mjs.map +1 -0
- package/dist/filter-Bc7CtppJ.mjs +73 -0
- package/dist/filter-Bc7CtppJ.mjs.map +1 -0
- package/dist/filter.d.ts +28 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.mjs +3 -0
- package/dist/message.d.ts +12 -0
- package/dist/nextjs.d.ts +6 -0
- package/dist/nextjs.mjs +58 -0
- package/dist/nextjs.mjs.map +1 -0
- package/dist/options-CHPeMkCj.mjs +19 -0
- package/dist/options-CHPeMkCj.mjs.map +1 -0
- package/dist/options.d.ts +13 -0
- package/dist/rollup.d.ts +21 -0
- package/dist/rollup.mjs +30 -0
- package/dist/rollup.mjs.map +1 -0
- package/dist/src-BBeF3k1d.mjs +420 -0
- package/dist/src-BBeF3k1d.mjs.map +1 -0
- package/dist/vite.d.ts +33 -0
- package/dist/vite.mjs +17 -0
- package/dist/vite.mjs.map +1 -0
- package/dist/webpack-loader.d.ts +1 -0
- package/dist/webpack-loader.mjs +18 -0
- package/dist/webpack-loader.mjs.map +1 -0
- package/dist/webpack.d.ts +6 -0
- package/dist/webpack.mjs +24 -0
- package/dist/webpack.mjs.map +1 -0
- package/dist-cjs/babel.cjs +491 -0
- package/dist-cjs/babel.cjs.map +1 -0
- package/package.json +87 -4
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
import "./filter-Bc7CtppJ.mjs";
|
|
2
|
+
import * as generatorModule from "@babel/generator";
|
|
3
|
+
import * as b from "@babel/types";
|
|
4
|
+
import { COMPACT_VERSION, DEFAULT_HASH_LENGTH, messageHash, normalizeHashLength, normalizeMessage } from "@fluentic/i18n/compiler-support";
|
|
5
|
+
import { parse } from "@babel/parser";
|
|
6
|
+
import * as traverseModule from "@babel/traverse";
|
|
7
|
+
import { TYPE, parse as parse$1 } from "@formatjs/icu-messageformat-parser";
|
|
8
|
+
// src/analyze.ts
|
|
9
|
+
const traverse = traverseModule.default.default ?? traverseModule.default;
|
|
10
|
+
const importMap = {
|
|
11
|
+
"@fluentic/i18n": "@fluentic/i18n/compiled",
|
|
12
|
+
"@fluentic/i18n/core": "@fluentic/i18n/compiled"
|
|
13
|
+
};
|
|
14
|
+
function parseSource(code, filename) {
|
|
15
|
+
return parse(code, {
|
|
16
|
+
sourceFilename: filename,
|
|
17
|
+
sourceType: "unambiguous",
|
|
18
|
+
plugins: [
|
|
19
|
+
.../\.[cm]?tsx?$/.test(filename) ? ["typescript"] : [],
|
|
20
|
+
...!/\.[cm]?ts$/.test(filename) ? ["jsx"] : [],
|
|
21
|
+
"decorators-legacy",
|
|
22
|
+
"importAttributes"
|
|
23
|
+
]
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
function unwrap(path) {
|
|
27
|
+
while (path.isTSAsExpression() || path.isTSSatisfiesExpression() || path.isTSNonNullExpression() || path.isTypeCastExpression() || path.isParenthesizedExpression()) path = path.get("expression");
|
|
28
|
+
return path;
|
|
29
|
+
}
|
|
30
|
+
function literal(path, seen = /* @__PURE__ */ new Set()) {
|
|
31
|
+
path = unwrap(path);
|
|
32
|
+
if (path.isStringLiteral()) return normalizeMessage(path.node.value);
|
|
33
|
+
if (path.isTemplateLiteral() && !path.node.expressions.length) return normalizeMessage(path.node.quasis[0].value.cooked ?? "");
|
|
34
|
+
if (path.isIdentifier()) {
|
|
35
|
+
const binding = path.scope.getBinding(path.node.name);
|
|
36
|
+
if (binding?.constant && binding.path.isVariableDeclarator() && !seen.has(binding)) {
|
|
37
|
+
seen.add(binding);
|
|
38
|
+
return literal(binding.path.get("init"), seen);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
function memberName(path) {
|
|
44
|
+
if (!path.isMemberExpression()) return null;
|
|
45
|
+
const property = path.get("property");
|
|
46
|
+
return !path.node.computed && property.isIdentifier() ? property.node.name : property.isStringLiteral() ? property.node.value : null;
|
|
47
|
+
}
|
|
48
|
+
function analyzer(imports = Object.keys(importMap)) {
|
|
49
|
+
const memo = /* @__PURE__ */ new Map();
|
|
50
|
+
const scopedBindings = /* @__PURE__ */ new Set();
|
|
51
|
+
const scopeCalls = /* @__PURE__ */ new Set();
|
|
52
|
+
const resolve = (path, visiting = /* @__PURE__ */ new Set()) => {
|
|
53
|
+
path = unwrap(path);
|
|
54
|
+
if (path.isIdentifier()) {
|
|
55
|
+
const binding = path.scope.getBinding(path.node.name);
|
|
56
|
+
if (!binding) return null;
|
|
57
|
+
if (memo.has(binding)) return memo.get(binding);
|
|
58
|
+
if (visiting.has(binding)) throw new Error("[i18n] circular translator alias");
|
|
59
|
+
if (binding.path.isImportSpecifier()) {
|
|
60
|
+
const declaration = binding.path.parentPath;
|
|
61
|
+
const imported = binding.path.node.imported;
|
|
62
|
+
return declaration.isImportDeclaration() && imports.includes(declaration.node.source.value) && (b.isIdentifier(imported) ? imported.name : imported.value) === "t" && declaration.node.importKind !== "type" ? {
|
|
63
|
+
context: null,
|
|
64
|
+
notes: []
|
|
65
|
+
} : null;
|
|
66
|
+
}
|
|
67
|
+
if (binding.path.isVariableDeclarator()) {
|
|
68
|
+
visiting.add(binding);
|
|
69
|
+
const scope = resolve(binding.path.get("init"), visiting);
|
|
70
|
+
visiting.delete(binding);
|
|
71
|
+
if (scope && !binding.constant) throw new Error("[i18n] translator aliases must not be reassigned: " + path.node.name);
|
|
72
|
+
memo.set(binding, scope);
|
|
73
|
+
if (scope) scopedBindings.add(binding);
|
|
74
|
+
return scope;
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
if (path.isMemberExpression() && memberName(path) === "t") {
|
|
79
|
+
const object = path.get("object");
|
|
80
|
+
if (!object.isIdentifier()) return null;
|
|
81
|
+
const binding = object.scope.getBinding(object.node.name);
|
|
82
|
+
const declaration = binding?.path.parentPath;
|
|
83
|
+
if (binding?.path.isImportNamespaceSpecifier() && declaration?.isImportDeclaration() && imports.includes(declaration.node.source.value)) return {
|
|
84
|
+
context: null,
|
|
85
|
+
notes: []
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (!path.isCallExpression()) return null;
|
|
89
|
+
const callee = path.get("callee");
|
|
90
|
+
const method = memberName(callee);
|
|
91
|
+
if (!method || ![
|
|
92
|
+
"context",
|
|
93
|
+
"note",
|
|
94
|
+
"meta"
|
|
95
|
+
].includes(method)) return null;
|
|
96
|
+
const parent = resolve(callee.get("object"), visiting);
|
|
97
|
+
if (!parent) return null;
|
|
98
|
+
if (path.node.arguments.length !== 1) throw new Error("[i18n] scope methods require one static argument");
|
|
99
|
+
const argument = path.get("arguments.0");
|
|
100
|
+
scopeCalls.add(path);
|
|
101
|
+
if (method === "meta") {
|
|
102
|
+
const evaluated = argument.evaluate();
|
|
103
|
+
if (!evaluated.confident) throw new Error("[i18n] metadata must be statically known");
|
|
104
|
+
return {
|
|
105
|
+
...parent,
|
|
106
|
+
meta: evaluated.value
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const value = literal(argument);
|
|
110
|
+
if (value === null) throw new Error("[i18n] " + method + " must be a static string");
|
|
111
|
+
return method === "note" ? {
|
|
112
|
+
...parent,
|
|
113
|
+
notes: [...parent.notes, value]
|
|
114
|
+
} : {
|
|
115
|
+
...parent,
|
|
116
|
+
context: parent.context ? `${parent.context}.${value}` : value
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
return {
|
|
120
|
+
resolve,
|
|
121
|
+
scopedBindings,
|
|
122
|
+
scopeCalls
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function collectMessages(code, filename, imports) {
|
|
126
|
+
const ast = parseSource(code, filename);
|
|
127
|
+
const { resolve } = analyzer(imports);
|
|
128
|
+
const messages = [];
|
|
129
|
+
traverse(ast, { CallExpression(path) {
|
|
130
|
+
const callee = path.get("callee");
|
|
131
|
+
const method = memberName(callee);
|
|
132
|
+
if (!method || ![
|
|
133
|
+
"msg",
|
|
134
|
+
"text",
|
|
135
|
+
"jsx"
|
|
136
|
+
].includes(method)) return;
|
|
137
|
+
const scope = resolve(callee.get("object"));
|
|
138
|
+
if (!scope) return;
|
|
139
|
+
const source = literal(path.get("arguments.0"));
|
|
140
|
+
if (source === null) {
|
|
141
|
+
if (method === "msg") throw new Error(`[i18n] ${filename}:${path.node.loc?.start.line}: t.msg requires a static message`);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
messages.push({
|
|
145
|
+
...scope,
|
|
146
|
+
id: scope.context ? `${scope.context}\0${source}` : source,
|
|
147
|
+
source
|
|
148
|
+
});
|
|
149
|
+
} });
|
|
150
|
+
return [...new Map(messages.map((message) => [message.id, message])).values()];
|
|
151
|
+
}
|
|
152
|
+
// src/message.ts
|
|
153
|
+
function messageIdentity(context, source) {
|
|
154
|
+
source = normalizeMessage(source);
|
|
155
|
+
return context ? `${context}\0${source}` : source;
|
|
156
|
+
}
|
|
157
|
+
function messageId(context, source, hashLength = DEFAULT_HASH_LENGTH) {
|
|
158
|
+
return messageHash(messageIdentity(context, source), hashLength);
|
|
159
|
+
}
|
|
160
|
+
function compileMessage(source) {
|
|
161
|
+
source = normalizeMessage(source);
|
|
162
|
+
const compact = (elements) => {
|
|
163
|
+
const result = elements.map((element) => {
|
|
164
|
+
switch (element.type) {
|
|
165
|
+
case TYPE.literal: return element.value;
|
|
166
|
+
case TYPE.argument: return [0, element.value];
|
|
167
|
+
case TYPE.number:
|
|
168
|
+
case TYPE.date:
|
|
169
|
+
case TYPE.time: return [
|
|
170
|
+
element.type === TYPE.number ? 1 : element.type === TYPE.date ? 2 : 3,
|
|
171
|
+
element.value,
|
|
172
|
+
typeof element.style === "object" && element.style ? element.style.parsedOptions : element.style ?? null
|
|
173
|
+
];
|
|
174
|
+
case TYPE.select: return [
|
|
175
|
+
4,
|
|
176
|
+
element.value,
|
|
177
|
+
Object.fromEntries(Object.entries(element.options).map(([key, option]) => [key, compact(option.value)]))
|
|
178
|
+
];
|
|
179
|
+
case TYPE.plural: return [
|
|
180
|
+
5,
|
|
181
|
+
element.value,
|
|
182
|
+
element.offset,
|
|
183
|
+
element.pluralType === "ordinal" ? 1 : 0,
|
|
184
|
+
Object.fromEntries(Object.entries(element.options).map(([key, option]) => [key, compact(option.value)]))
|
|
185
|
+
];
|
|
186
|
+
case TYPE.pound: return [6];
|
|
187
|
+
case TYPE.tag: return [
|
|
188
|
+
7,
|
|
189
|
+
element.value,
|
|
190
|
+
compact(element.children)
|
|
191
|
+
];
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
return result.every((part) => typeof part === "string") ? result.join("") : result;
|
|
195
|
+
};
|
|
196
|
+
return compact(parse$1(source, { captureLocation: false }));
|
|
197
|
+
}
|
|
198
|
+
function compileCatalog(records, options = {}) {
|
|
199
|
+
const seen = /* @__PURE__ */ new Map();
|
|
200
|
+
return records.flatMap((record) => {
|
|
201
|
+
const id = messageId(record.context, record.source, options.hashLength);
|
|
202
|
+
const identity = messageIdentity(record.context, record.source);
|
|
203
|
+
if (seen.has(id) && seen.get(id) !== identity) throw new Error("[i18n] message hash collision: " + id);
|
|
204
|
+
seen.set(id, identity);
|
|
205
|
+
return record.translation === "" ? [] : [[id, compileMessage(record.translation)]];
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
// src/index.ts
|
|
209
|
+
const generate = generatorModule.default.default ?? generatorModule.default;
|
|
210
|
+
function reactFunction(path) {
|
|
211
|
+
for (let current = path.findParent((parent) => parent.isFunction()); current; current = current.findParent((parent) => parent.isFunction())) {
|
|
212
|
+
const fn = current;
|
|
213
|
+
const parent = fn.parentPath;
|
|
214
|
+
const ownName = b.isFunctionDeclaration(fn.node) || b.isFunctionExpression(fn.node) ? fn.node.id?.name : null;
|
|
215
|
+
const variableId = parent?.isVariableDeclarator() ? parent.get("id") : null;
|
|
216
|
+
const name = ownName ?? (variableId?.isIdentifier() ? variableId.node.name : null);
|
|
217
|
+
if (parent?.isExportDefaultDeclaration() || name && (/^[A-Z]/.test(name) || /^use[A-Z]/.test(name))) return fn;
|
|
218
|
+
}
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
function hashLength(length) {
|
|
222
|
+
return normalizeHashLength(length ?? DEFAULT_HASH_LENGTH);
|
|
223
|
+
}
|
|
224
|
+
function resolveJsxCallback(path, seen = /* @__PURE__ */ new Set()) {
|
|
225
|
+
while (path.isTSAsExpression() || path.isTSSatisfiesExpression() || path.isTSNonNullExpression() || path.isTypeCastExpression() || path.isParenthesizedExpression()) path = path.get("expression");
|
|
226
|
+
if (path.isArrowFunctionExpression() || path.isFunctionExpression() || path.isFunctionDeclaration()) return path;
|
|
227
|
+
if (!path.isIdentifier()) return null;
|
|
228
|
+
const binding = path.scope.getBinding(path.node.name);
|
|
229
|
+
if (!binding?.constant || seen.has(binding)) return null;
|
|
230
|
+
seen.add(binding);
|
|
231
|
+
if (binding.path.isFunctionDeclaration()) return binding.path;
|
|
232
|
+
if (!binding.path.isVariableDeclarator()) return null;
|
|
233
|
+
return resolveJsxCallback(binding.path.get("init"), seen);
|
|
234
|
+
}
|
|
235
|
+
function compileProgram(code, filename, options = {}) {
|
|
236
|
+
const ast = parseSource(code, filename);
|
|
237
|
+
const messages = [];
|
|
238
|
+
const analysis = analyzer();
|
|
239
|
+
const plans = [];
|
|
240
|
+
const declarations = [];
|
|
241
|
+
const jsxCallbacks = /* @__PURE__ */ new Set();
|
|
242
|
+
traverse(ast, { CallExpression(path) {
|
|
243
|
+
const callee = path.get("callee");
|
|
244
|
+
if (memberName(callee) !== "jsx") return;
|
|
245
|
+
if (!analysis.resolve(callee.get("object"))) return;
|
|
246
|
+
const callback = resolveJsxCallback(path.get("arguments.0"));
|
|
247
|
+
if (callback) jsxCallbacks.add(callback.node);
|
|
248
|
+
} });
|
|
249
|
+
let program;
|
|
250
|
+
const clientModule = ast.program.directives.some((directive) => directive.value.value === "use client");
|
|
251
|
+
traverse(ast, {
|
|
252
|
+
Program(path) {
|
|
253
|
+
program = path;
|
|
254
|
+
},
|
|
255
|
+
ExportAllDeclaration(path) {
|
|
256
|
+
if (importMap[path.node.source.value]) throw new Error("[i18n] do not re-export the translator in compiled mode; import t directly at each use");
|
|
257
|
+
},
|
|
258
|
+
ExportNamedDeclaration(path) {
|
|
259
|
+
if (path.node.source && importMap[path.node.source.value] && path.node.specifiers.some((specifier) => b.isExportSpecifier(specifier) && (b.isIdentifier(specifier.local) ? specifier.local.name : "") === "t")) throw new Error("[i18n] do not re-export the translator in compiled mode; export message descriptors instead");
|
|
260
|
+
},
|
|
261
|
+
ImportDeclaration(path) {
|
|
262
|
+
if (importMap[path.node.source.value] && path.node.importKind !== "type") declarations.push(path);
|
|
263
|
+
},
|
|
264
|
+
CallExpression(path) {
|
|
265
|
+
const callee = path.get("callee");
|
|
266
|
+
const method = memberName(callee);
|
|
267
|
+
if (!method || ![
|
|
268
|
+
"msg",
|
|
269
|
+
"jsx",
|
|
270
|
+
"text"
|
|
271
|
+
].includes(method)) return;
|
|
272
|
+
const scope = analysis.resolve(callee.get("object"));
|
|
273
|
+
if (!scope) return;
|
|
274
|
+
if (method === "jsx" && resolveJsxCallback(path.get("arguments.0"))) return;
|
|
275
|
+
const source = literal(path.get("arguments.0"));
|
|
276
|
+
if (source === null && method === "msg") throw new Error(`[i18n] ${filename}:${path.node.loc?.start.line}: t.msg requires a static message`);
|
|
277
|
+
const callbackOwned = method === "text" && Boolean(path.findParent((parent) => parent.isFunction() && jsxCallbacks.has(parent.node)));
|
|
278
|
+
const reactOwner = options.bindReactText && clientModule && method === "text" && !callbackOwned ? reactFunction(path) : void 0;
|
|
279
|
+
if (options.bindReactText && clientModule && method === "text" && !callbackOwned && !reactOwner) throw new Error(`[i18n] ${filename}:${path.node.loc?.start.line}: client t.text must be rendered inside a component or hook`);
|
|
280
|
+
plans.push({
|
|
281
|
+
path,
|
|
282
|
+
source,
|
|
283
|
+
scope,
|
|
284
|
+
reactOwner: reactOwner ?? void 0
|
|
285
|
+
});
|
|
286
|
+
if (source !== null) messages.push({
|
|
287
|
+
...scope,
|
|
288
|
+
source,
|
|
289
|
+
id: scope.context ? `${scope.context}\0${source}` : source
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
traverse(ast, {
|
|
294
|
+
VariableDeclarator(path) {
|
|
295
|
+
analysis.resolve(path.get("id"));
|
|
296
|
+
},
|
|
297
|
+
CallExpression(path) {
|
|
298
|
+
const name = memberName(path.get("callee"));
|
|
299
|
+
if (name && [
|
|
300
|
+
"context",
|
|
301
|
+
"note",
|
|
302
|
+
"meta"
|
|
303
|
+
].includes(name)) analysis.resolve(path);
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
const expressionUse = (path) => {
|
|
307
|
+
while (path.parentPath && (path.parentPath.isTSAsExpression() || path.parentPath.isTSSatisfiesExpression() || path.parentPath.isTSNonNullExpression() || path.parentPath.isTypeCastExpression() || path.parentPath.isParenthesizedExpression())) path = path.parentPath;
|
|
308
|
+
return path;
|
|
309
|
+
};
|
|
310
|
+
for (const path of analysis.scopeCalls) {
|
|
311
|
+
const use = expressionUse(path);
|
|
312
|
+
const parent = use.parentPath;
|
|
313
|
+
if (parent?.isVariableDeclarator() || parent?.isExpressionStatement()) continue;
|
|
314
|
+
if (parent?.isMemberExpression() && parent.get("object") === use && parent.parentPath?.isCallExpression()) continue;
|
|
315
|
+
throw new Error("[i18n] scoped translator escapes same-file message calls");
|
|
316
|
+
}
|
|
317
|
+
for (const binding of analysis.scopedBindings) {
|
|
318
|
+
if (binding.path.parentPath?.parentPath?.isExportNamedDeclaration()) throw new Error("[i18n] scoped translators cannot be exported; repeat the context in the consuming file");
|
|
319
|
+
for (const reference of binding.referencePaths) {
|
|
320
|
+
if (reference.findParent((parent) => parent.isTSType())) continue;
|
|
321
|
+
const use = expressionUse(reference);
|
|
322
|
+
const parent = use.parentPath;
|
|
323
|
+
if (parent?.isVariableDeclarator() && parent.get("init") === use) continue;
|
|
324
|
+
if (parent?.isMemberExpression() && parent.get("object") === use) {
|
|
325
|
+
const method = memberName(parent);
|
|
326
|
+
if (method && [
|
|
327
|
+
"msg",
|
|
328
|
+
"text",
|
|
329
|
+
"jsx",
|
|
330
|
+
"context",
|
|
331
|
+
"note",
|
|
332
|
+
"meta"
|
|
333
|
+
].includes(method) && parent.parentPath?.isCallExpression()) continue;
|
|
334
|
+
}
|
|
335
|
+
throw new Error("[i18n] scoped translator escapes same-file message calls: " + binding.identifier.name);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
if (!declarations.length) return {
|
|
339
|
+
ast,
|
|
340
|
+
messages,
|
|
341
|
+
changed: false
|
|
342
|
+
};
|
|
343
|
+
const helper = program.scope.generateUidIdentifier("tc");
|
|
344
|
+
const rendererHook = program.scope.generateUidIdentifier("useI18nRenderer");
|
|
345
|
+
const rendererBindings = /* @__PURE__ */ new Map();
|
|
346
|
+
if (plans.length) program.unshiftContainer("body", b.importDeclaration([b.importSpecifier(helper, b.identifier("t"))], b.stringLiteral("@fluentic/i18n/compiled")));
|
|
347
|
+
if (plans.some((plan) => plan.reactOwner)) {
|
|
348
|
+
program.unshiftContainer("body", b.importDeclaration([b.importSpecifier(rendererHook, b.identifier("useI18nRenderer"))], b.stringLiteral("@fluentic/i18n/react")));
|
|
349
|
+
for (const owner of new Set(plans.map((plan) => plan.reactOwner).filter(Boolean))) {
|
|
350
|
+
const binding = owner.scope.generateUidIdentifier("text");
|
|
351
|
+
rendererBindings.set(owner, binding);
|
|
352
|
+
const body = owner.get("body");
|
|
353
|
+
if (!body.isBlockStatement()) {
|
|
354
|
+
const expression = body.node;
|
|
355
|
+
body.replaceWith(b.blockStatement([b.returnStatement(expression)]));
|
|
356
|
+
}
|
|
357
|
+
owner.get("body").unshiftContainer("body", b.variableDeclaration("const", [b.variableDeclarator(b.cloneNode(binding), b.callExpression(b.cloneNode(rendererHook), [b.memberExpression(b.cloneNode(helper), b.identifier("text"))]))]));
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
for (const { path, source, scope, reactOwner } of plans) {
|
|
361
|
+
const callee = path.get("callee");
|
|
362
|
+
const renderer = reactOwner && rendererBindings.get(reactOwner);
|
|
363
|
+
if (renderer) callee.replaceWith(b.cloneNode(renderer));
|
|
364
|
+
else callee.get("object").replaceWith(b.cloneNode(helper));
|
|
365
|
+
if (source !== null) {
|
|
366
|
+
const replacement = b.valueToNode([messageId(scope.context, source, hashLength(options.hashLength)), compileMessage(source)]);
|
|
367
|
+
b.inherits(replacement, path.node.arguments[0]);
|
|
368
|
+
path.node.arguments[0] = replacement;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
let removed = true;
|
|
372
|
+
while (removed) {
|
|
373
|
+
removed = false;
|
|
374
|
+
program.scope.crawl();
|
|
375
|
+
for (const binding of analysis.scopedBindings) {
|
|
376
|
+
if (binding.path.removed) continue;
|
|
377
|
+
const current = binding.path.scope.getBinding(binding.identifier.name);
|
|
378
|
+
if (current && !current.referenced) {
|
|
379
|
+
binding.path.remove();
|
|
380
|
+
removed = true;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
for (const path of analysis.scopeCalls) if (!path.removed) {
|
|
385
|
+
const use = expressionUse(path);
|
|
386
|
+
if (use.parentPath?.isExpressionStatement()) use.parentPath.remove();
|
|
387
|
+
}
|
|
388
|
+
for (const declaration of declarations) declaration.node.source.value = importMap[declaration.node.source.value];
|
|
389
|
+
program.scope.crawl();
|
|
390
|
+
for (const declaration of declarations) {
|
|
391
|
+
declaration.node.specifiers = declaration.node.specifiers.filter((specifier) => {
|
|
392
|
+
if (!b.isImportSpecifier(specifier) || (b.isIdentifier(specifier.imported) ? specifier.imported.name : specifier.imported.value) !== "t") return true;
|
|
393
|
+
return Boolean(program.scope.getBinding(specifier.local.name)?.referenced);
|
|
394
|
+
});
|
|
395
|
+
if (!declaration.node.specifiers.length) declaration.remove();
|
|
396
|
+
}
|
|
397
|
+
return {
|
|
398
|
+
ast,
|
|
399
|
+
messages,
|
|
400
|
+
changed: true
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
function transform(code, options = {}) {
|
|
404
|
+
const filename = options.filename ?? "source.tsx";
|
|
405
|
+
const result = compileProgram(code, filename, options);
|
|
406
|
+
if (!result.changed) return null;
|
|
407
|
+
const output = generate(result.ast, {
|
|
408
|
+
sourceMaps: true,
|
|
409
|
+
sourceFileName: filename,
|
|
410
|
+
comments: true
|
|
411
|
+
}, code);
|
|
412
|
+
return {
|
|
413
|
+
code: output.code,
|
|
414
|
+
map: output.map,
|
|
415
|
+
messages: result.messages
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
export { compileMessage as a, collectMessages as c, compileCatalog as i, traverse as l, transform as n, messageId as o, COMPACT_VERSION as r, messageIdentity as s, compileProgram as t };
|
|
419
|
+
|
|
420
|
+
//# sourceMappingURL=src-BBeF3k1d.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"src-BBeF3k1d.mjs","names":["parse"],"sources":["../src/analyze.ts","../src/message.ts","../src/index.ts"],"sourcesContent":["import { parse } from '@babel/parser';\nimport * as traverseModule from '@babel/traverse';\nimport type { Binding, NodePath } from '@babel/traverse';\nimport * as b from '@babel/types';\nimport { normalizeMessage } from '@fluentic/i18n/compiler-support';\n\nconst traverse = ((traverseModule.default as unknown as { default?: typeof traverseModule.default; }).default ??\n traverseModule.default) as typeof traverseModule.default;\nexport { traverse };\n\nexport const importMap: Record<string, string> = {\n '@fluentic/i18n': '@fluentic/i18n/compiled',\n '@fluentic/i18n/core': '@fluentic/i18n/compiled',\n};\nexport type Scope = { context: string | null; notes: string[]; meta?: unknown; };\nexport type ExtractedMessage = Scope & { id: string; source: string; };\n\nexport function parseSource(code: string, filename: string) {\n return parse(code, {\n sourceFilename: filename,\n sourceType: 'unambiguous',\n plugins: [\n ...(/\\.[cm]?tsx?$/.test(filename) ? ['typescript' as const] : []),\n ...(!/\\.[cm]?ts$/.test(filename) ? ['jsx' as const] : []),\n 'decorators-legacy',\n 'importAttributes',\n ],\n });\n}\n\nexport function unwrap(path: NodePath): NodePath {\n while (\n path.isTSAsExpression() || path.isTSSatisfiesExpression() || path.isTSNonNullExpression() ||\n path.isTypeCastExpression() || path.isParenthesizedExpression()\n ) path = path.get('expression') as NodePath;\n\n return path;\n}\n\nexport function literal(path: NodePath, seen = new Set<Binding>()): string | null {\n path = unwrap(path);\n if (path.isStringLiteral()) return normalizeMessage(path.node.value);\n if (path.isTemplateLiteral() && !path.node.expressions.length) {\n return normalizeMessage(path.node.quasis[0].value.cooked ?? '');\n }\n if (path.isIdentifier()) {\n const binding = path.scope.getBinding(path.node.name);\n\n if (binding?.constant && binding.path.isVariableDeclarator() && !seen.has(binding)) {\n seen.add(binding);\n\n return literal(binding.path.get('init') as NodePath, seen);\n }\n }\n\n return null;\n}\n\nexport function memberName(path: NodePath): string | null {\n if (!path.isMemberExpression()) return null;\n\n const property = path.get('property');\n\n return !path.node.computed && property.isIdentifier()\n ? property.node.name\n : property.isStringLiteral()\n ? property.node.value\n : null;\n}\n\nexport function analyzer(imports = Object.keys(importMap)) {\n const memo = new Map<Binding, Scope | null>();\n const scopedBindings = new Set<Binding>();\n const scopeCalls = new Set<NodePath>();\n\n const resolve = (path: NodePath, visiting = new Set<Binding>()): Scope | null => {\n path = unwrap(path);\n if (path.isIdentifier()) {\n const binding = path.scope.getBinding(path.node.name);\n\n if (!binding) return null;\n if (memo.has(binding)) return memo.get(binding)!;\n if (visiting.has(binding)) throw new Error('[i18n] circular translator alias');\n if (binding.path.isImportSpecifier()) {\n const declaration = binding.path.parentPath;\n const imported = binding.path.node.imported;\n\n return declaration.isImportDeclaration() && imports.includes(declaration.node.source.value) &&\n (b.isIdentifier(imported) ? imported.name : imported.value) === 't' &&\n declaration.node.importKind !== 'type'\n ? { context: null, notes: [] }\n : null;\n }\n if (binding.path.isVariableDeclarator()) {\n visiting.add(binding);\n const scope = resolve(binding.path.get('init') as NodePath, visiting);\n visiting.delete(binding);\n if (scope && !binding.constant) {\n throw new Error('[i18n] translator aliases must not be reassigned: ' + path.node.name);\n }\n memo.set(binding, scope);\n if (scope) scopedBindings.add(binding);\n\n return scope;\n }\n\n return null;\n }\n if (path.isMemberExpression() && memberName(path) === 't') {\n const object = path.get('object');\n\n if (!object.isIdentifier()) return null;\n\n const binding = object.scope.getBinding(object.node.name);\n const declaration = binding?.path.parentPath;\n\n if (\n binding?.path.isImportNamespaceSpecifier() && declaration?.isImportDeclaration() &&\n imports.includes(declaration.node.source.value)\n ) return { context: null, notes: [] };\n }\n if (!path.isCallExpression()) return null;\n\n const callee = path.get('callee');\n const method = memberName(callee);\n\n if (!method || !['context', 'note', 'meta'].includes(method)) return null;\n\n const parent = resolve((callee as NodePath<b.MemberExpression>).get('object'), visiting);\n\n if (!parent) return null;\n if (path.node.arguments.length !== 1) throw new Error('[i18n] scope methods require one static argument');\n\n const argument = path.get('arguments.0') as NodePath;\n scopeCalls.add(path);\n if (method === 'meta') {\n const evaluated = argument.evaluate();\n\n if (!evaluated.confident) throw new Error('[i18n] metadata must be statically known');\n\n return { ...parent, meta: evaluated.value };\n }\n\n const value = literal(argument);\n\n if (value === null) throw new Error('[i18n] ' + method + ' must be a static string');\n\n return method === 'note'\n ? { ...parent, notes: [...parent.notes, value] }\n : { ...parent, context: parent.context ? `${parent.context}.${value}` : value };\n };\n\n return { resolve, scopedBindings, scopeCalls };\n}\n\nexport function collectMessages(code: string, filename: string, imports?: string[]): ExtractedMessage[] {\n const ast = parseSource(code, filename);\n const { resolve } = analyzer(imports);\n const messages: ExtractedMessage[] = [];\n traverse(ast, {\n CallExpression(path) {\n const callee = path.get('callee');\n const method = memberName(callee);\n\n if (!method || !['msg', 'text', 'jsx'].includes(method)) return;\n\n const scope = resolve((callee as NodePath<b.MemberExpression>).get('object'));\n\n if (!scope) return;\n\n const source = literal(path.get('arguments.0') as NodePath);\n\n if (source === null) {\n if (method === 'msg') {\n throw new Error(`[i18n] ${filename}:${path.node.loc?.start.line}: t.msg requires a static message`);\n }\n\n return;\n }\n messages.push({ ...scope, id: scope.context ? `${scope.context}\\0${source}` : source, source });\n },\n });\n\n return [...new Map(messages.map((message) => [message.id, message])).values()];\n}\n","import {\n type CompactMessage,\n type CompactPart,\n DEFAULT_HASH_LENGTH,\n messageHash,\n normalizeMessage,\n} from '@fluentic/i18n/compiler-support';\nimport { type MessageFormatElement, parse, TYPE } from '@formatjs/icu-messageformat-parser';\n\nexport { COMPACT_VERSION } from '@fluentic/i18n/compiler-support';\n\nexport function messageIdentity(context: string | null, source: string): string {\n source = normalizeMessage(source);\n return context ? `${context}\\0${source}` : source;\n}\n\nexport function messageId(context: string | null, source: string, hashLength = DEFAULT_HASH_LENGTH): string {\n return messageHash(messageIdentity(context, source), hashLength);\n}\n\nexport function compileMessage(source: string): CompactMessage {\n source = normalizeMessage(source);\n const compact = (elements: MessageFormatElement[]): CompactMessage => {\n const result: CompactPart[] = elements.map((element): CompactPart => {\n switch (element.type) {\n case TYPE.literal:\n return element.value;\n case TYPE.argument:\n return [0, element.value];\n case TYPE.number:\n case TYPE.date:\n case TYPE.time:\n return [\n element.type === TYPE.number ? 1 : element.type === TYPE.date ? 2 : 3,\n element.value,\n typeof element.style === 'object' && element.style\n ? element.style.parsedOptions as Record<string, unknown>\n : element.style ?? null,\n ];\n case TYPE.select:\n return [\n 4,\n element.value,\n Object.fromEntries(Object.entries(element.options).map(([key, option]) => [key, compact(option.value)])),\n ];\n case TYPE.plural:\n return [\n 5,\n element.value,\n element.offset,\n element.pluralType === 'ordinal' ? 1 : 0,\n Object.fromEntries(Object.entries(element.options).map(([key, option]) => [key, compact(option.value)])),\n ];\n case TYPE.pound:\n return [6];\n case TYPE.tag:\n return [7, element.value, compact(element.children)];\n }\n });\n\n return result.every((part) => typeof part === 'string') ? result.join('') : result;\n };\n\n return compact(parse(source, { captureLocation: false }));\n}\n\nexport function compileCatalog(\n records: Array<{ source: string; context: string | null; translation: string; }>,\n options: { hashLength?: number; } = {},\n): Array<[string, CompactMessage]> {\n const seen = new Map<string, string>();\n\n return records.flatMap((record) => {\n const id = messageId(record.context, record.source, options.hashLength);\n const identity = messageIdentity(record.context, record.source);\n\n if (seen.has(id) && seen.get(id) !== identity) throw new Error('[i18n] message hash collision: ' + id);\n seen.set(id, identity);\n\n return record.translation === '' ? [] : [[id, compileMessage(record.translation)] as [string, CompactMessage]];\n });\n}\n","import * as generatorModule from '@babel/generator';\nimport type { NodePath } from '@babel/traverse';\nimport * as b from '@babel/types';\nimport { DEFAULT_HASH_LENGTH, normalizeHashLength } from '@fluentic/i18n/compiler-support';\nimport {\n analyzer,\n collectMessages,\n type ExtractedMessage,\n importMap,\n literal,\n memberName,\n parseSource,\n traverse,\n} from './analyze';\nimport { compileMessage, messageId } from './message';\n\nexport { collectMessages } from './analyze';\nexport * from './filter';\nexport * from './message';\nexport type { PluginOptions } from './options';\n\nconst generate = ((generatorModule.default as unknown as { default?: typeof generatorModule.default; }).default ??\n generatorModule.default) as typeof generatorModule.default;\nexport type CompiledOptions = { hashLength?: number; };\nexport type CompilerOptions = CompiledOptions & { filename?: string; bindReactText?: boolean; };\n\ntype FunctionPath = NodePath<\n b.ArrowFunctionExpression | b.FunctionDeclaration | b.FunctionExpression | b.ObjectMethod | b.ClassMethod\n>;\n\nfunction reactFunction(path: NodePath): FunctionPath | null {\n for (\n let current = path.findParent((parent) => parent.isFunction());\n current;\n current = current.findParent(\n (parent) => parent.isFunction(),\n )\n ) {\n const fn = current as FunctionPath;\n const parent = fn.parentPath;\n const ownName = (b.isFunctionDeclaration(fn.node) || b.isFunctionExpression(fn.node)) ? fn.node.id?.name : null;\n const variableId = parent?.isVariableDeclarator() ? parent.get('id') : null;\n const name = ownName ?? (variableId?.isIdentifier() ? variableId.node.name : null);\n const exported = parent?.isExportDefaultDeclaration();\n\n if (exported || (name && (/^[A-Z]/.test(name) || /^use[A-Z]/.test(name)))) return fn;\n }\n\n return null;\n}\n\nfunction hashLength(length: CompilerOptions['hashLength']): number {\n return normalizeHashLength(length ?? DEFAULT_HASH_LENGTH);\n}\n\nfunction resolveJsxCallback(path: NodePath, seen = new Set<unknown>()): FunctionPath | null {\n while (\n path.isTSAsExpression() || path.isTSSatisfiesExpression() || path.isTSNonNullExpression() ||\n path.isTypeCastExpression() || path.isParenthesizedExpression()\n ) path = path.get('expression') as NodePath;\n if (path.isArrowFunctionExpression() || path.isFunctionExpression() || path.isFunctionDeclaration()) {\n return path as FunctionPath;\n }\n if (!path.isIdentifier()) return null;\n const binding = path.scope.getBinding(path.node.name);\n if (!binding?.constant || seen.has(binding)) return null;\n seen.add(binding);\n if (binding.path.isFunctionDeclaration()) return binding.path as FunctionPath;\n if (!binding.path.isVariableDeclarator()) return null;\n return resolveJsxCallback(binding.path.get('init') as NodePath, seen);\n}\n\nexport function compileProgram(code: string, filename: string, options: CompilerOptions = {}) {\n const ast = parseSource(code, filename);\n const messages: ExtractedMessage[] = [];\n\n const analysis = analyzer();\n\n const plans: Array<\n {\n path: NodePath<b.CallExpression>;\n source: string | null;\n scope: NonNullable<ReturnType<typeof analysis.resolve>>;\n reactOwner?: FunctionPath;\n }\n > = [];\n const declarations: NodePath<b.ImportDeclaration>[] = [];\n const jsxCallbacks = new Set<b.Function>();\n\n traverse(ast, {\n CallExpression(path) {\n const callee = path.get('callee');\n if (memberName(callee) !== 'jsx') return;\n const scope = analysis.resolve((callee as NodePath<b.MemberExpression>).get('object'));\n if (!scope) return;\n const callback = resolveJsxCallback(path.get('arguments.0') as NodePath);\n if (callback) jsxCallbacks.add(callback.node);\n },\n });\n\n let program: NodePath<b.Program> | undefined;\n const clientModule = ast.program.directives.some((directive) => directive.value.value === 'use client');\n traverse(ast, {\n Program(path) {\n program = path;\n },\n ExportAllDeclaration(path) {\n if (importMap[path.node.source.value]) {\n throw new Error('[i18n] do not re-export the translator in compiled mode; import t directly at each use');\n }\n },\n ExportNamedDeclaration(path) {\n if (\n path.node.source && importMap[path.node.source.value] &&\n path.node.specifiers.some((specifier) =>\n b.isExportSpecifier(specifier) && (b.isIdentifier(specifier.local) ? specifier.local.name : '') === 't'\n )\n ) throw new Error('[i18n] do not re-export the translator in compiled mode; export message descriptors instead');\n },\n ImportDeclaration(path) {\n if (importMap[path.node.source.value] && path.node.importKind !== 'type') declarations.push(path);\n },\n CallExpression(path) {\n const callee = path.get('callee');\n const method = memberName(callee);\n\n if (!method || !['msg', 'jsx', 'text'].includes(method)) return;\n\n const scope = analysis.resolve((callee as NodePath<b.MemberExpression>).get('object'));\n\n if (!scope) return;\n\n if (method === 'jsx' && resolveJsxCallback(path.get('arguments.0') as NodePath)) return;\n\n const source = literal(path.get('arguments.0') as NodePath);\n\n if (source === null && method === 'msg') {\n throw new Error(`[i18n] ${filename}:${path.node.loc?.start.line}: t.msg requires a static message`);\n }\n const callbackOwned = method === 'text' && Boolean(\n path.findParent((parent) => parent.isFunction() && jsxCallbacks.has(parent.node as b.Function)),\n );\n const reactOwner = options.bindReactText && clientModule && method === 'text' && !callbackOwned\n ? reactFunction(path)\n : undefined;\n if (options.bindReactText && clientModule && method === 'text' && !callbackOwned && !reactOwner) {\n throw new Error(\n `[i18n] ${filename}:${path.node.loc?.start.line}: client t.text must be rendered inside a component or hook`,\n );\n }\n plans.push({ path, source, scope, reactOwner: reactOwner ?? undefined });\n if (source !== null) {\n messages.push({ ...scope, source, id: scope.context ? `${scope.context}\\0${source}` : source });\n }\n },\n });\n // Discover unused/escaping scopes as well as scopes reached by message calls.\n traverse(ast, {\n VariableDeclarator(path) {\n analysis.resolve(path.get('id') as NodePath);\n },\n CallExpression(path) {\n const name = memberName(path.get('callee'));\n\n if (name && ['context', 'note', 'meta'].includes(name)) analysis.resolve(path);\n },\n });\n const expressionUse = (path: NodePath): NodePath => {\n while (\n path.parentPath &&\n (path.parentPath.isTSAsExpression() || path.parentPath.isTSSatisfiesExpression() ||\n path.parentPath.isTSNonNullExpression() || path.parentPath.isTypeCastExpression() ||\n path.parentPath.isParenthesizedExpression())\n ) path = path.parentPath;\n\n return path;\n };\n\n for (const path of analysis.scopeCalls) {\n const use = expressionUse(path);\n const parent = use.parentPath;\n\n if (parent?.isVariableDeclarator() || parent?.isExpressionStatement()) continue;\n if (parent?.isMemberExpression() && parent.get('object') === use && parent.parentPath?.isCallExpression()) continue;\n throw new Error('[i18n] scoped translator escapes same-file message calls');\n }\n for (const binding of analysis.scopedBindings) {\n if (binding.path.parentPath?.parentPath?.isExportNamedDeclaration()) {\n throw new Error('[i18n] scoped translators cannot be exported; repeat the context in the consuming file');\n }\n\n for (const reference of binding.referencePaths) {\n if (reference.findParent((parent) => parent.isTSType())) continue;\n\n const use = expressionUse(reference);\n const parent = use.parentPath;\n\n if (parent?.isVariableDeclarator() && parent.get('init') === use) continue;\n if (parent?.isMemberExpression() && parent.get('object') === use) {\n const method = memberName(parent);\n\n if (\n method && ['msg', 'text', 'jsx', 'context', 'note', 'meta'].includes(method) &&\n parent.parentPath?.isCallExpression()\n ) continue;\n }\n throw new Error('[i18n] scoped translator escapes same-file message calls: ' + binding.identifier.name);\n }\n }\n if (!declarations.length) return { ast, messages, changed: false };\n\n const helper = program!.scope.generateUidIdentifier('tc');\n const rendererHook = program!.scope.generateUidIdentifier('useI18nRenderer');\n const rendererBindings = new Map<FunctionPath, b.Identifier>();\n\n if (plans.length) {\n program!.unshiftContainer(\n 'body',\n b.importDeclaration([b.importSpecifier(helper, b.identifier('t'))], b.stringLiteral('@fluentic/i18n/compiled')),\n );\n }\n if (plans.some((plan) => plan.reactOwner)) {\n program!.unshiftContainer(\n 'body',\n b.importDeclaration(\n [b.importSpecifier(rendererHook, b.identifier('useI18nRenderer'))],\n b.stringLiteral('@fluentic/i18n/react'),\n ),\n );\n for (const owner of new Set(plans.map((plan) => plan.reactOwner).filter(Boolean) as FunctionPath[])) {\n const binding = owner.scope.generateUidIdentifier('text');\n rendererBindings.set(owner, binding);\n const body = owner.get('body');\n if (!body.isBlockStatement()) {\n const expression = body.node as b.Expression;\n body.replaceWith(b.blockStatement([b.returnStatement(expression)]));\n }\n (owner.get('body') as NodePath<b.BlockStatement>).unshiftContainer(\n 'body',\n b.variableDeclaration('const', [\n b.variableDeclarator(\n b.cloneNode(binding),\n b.callExpression(b.cloneNode(rendererHook), [\n b.memberExpression(b.cloneNode(helper), b.identifier('text')),\n ]),\n ),\n ]),\n );\n }\n }\n\n for (const { path, source, scope, reactOwner } of plans) {\n const callee = path.get('callee') as NodePath<b.MemberExpression>;\n const renderer = reactOwner && rendererBindings.get(reactOwner);\n if (renderer) callee.replaceWith(b.cloneNode(renderer));\n else callee.get('object').replaceWith(b.cloneNode(helper));\n if (source !== null) {\n const replacement = b.valueToNode([\n messageId(scope.context, source, hashLength(options.hashLength)),\n compileMessage(source),\n ]);\n b.inherits(replacement, path.node.arguments[0]);\n path.node.arguments[0] = replacement;\n }\n }\n // All scope uses were checked before mutation. Remove aliases from leaves to roots.\n let removed = true;\n\n while (removed) {\n removed = false;\n program!.scope.crawl();\n\n for (const binding of analysis.scopedBindings) {\n if (binding.path.removed) continue;\n\n const current = binding.path.scope.getBinding(binding.identifier.name);\n\n if (current && !current.referenced) {\n binding.path.remove();\n removed = true;\n }\n }\n }\n\n for (const path of analysis.scopeCalls) {\n if (!path.removed) {\n const use = expressionUse(path);\n\n if (use.parentPath?.isExpressionStatement()) use.parentPath.remove();\n }\n }\n for (const declaration of declarations) declaration.node.source.value = importMap[declaration.node.source.value];\n program!.scope.crawl();\n\n for (const declaration of declarations) {\n declaration.node.specifiers = declaration.node.specifiers.filter((specifier) => {\n if (\n !b.isImportSpecifier(specifier) ||\n (b.isIdentifier(specifier.imported) ? specifier.imported.name : specifier.imported.value) !== 't'\n ) return true;\n\n return Boolean(program!.scope.getBinding(specifier.local.name)?.referenced);\n });\n if (!declaration.node.specifiers.length) declaration.remove();\n }\n\n return { ast, messages, changed: true };\n}\n\nexport function transform(code: string, options: CompilerOptions = {}) {\n const filename = options.filename ?? 'source.tsx';\n const result = compileProgram(code, filename, options);\n\n if (!result.changed) return null;\n\n const output = generate(result.ast, { sourceMaps: true, sourceFileName: filename, comments: true }, code);\n\n return { code: output.code, map: output.map, messages: result.messages };\n}\n"],"mappings":";;;;;;;;AAMA,MAAM,WAAa,eAAe,QAAoE,WACpG,eAAe;AAGjB,MAAa,YAAoC;CAC/C,kBAAkB;CAClB,uBAAuB;AACzB;AAIA,SAAgB,YAAY,MAAc,UAAkB;CAC1D,OAAO,MAAM,MAAM;EACjB,gBAAgB;EAChB,YAAY;EACZ,SAAS;GACP,GAAI,eAAe,KAAK,QAAQ,IAAI,CAAC,YAAqB,IAAI,CAAC;GAC/D,GAAI,CAAC,aAAa,KAAK,QAAQ,IAAI,CAAC,KAAc,IAAI,CAAC;GACvD;GACA;EACF;CACF,CAAC;AACH;AAEA,SAAgB,OAAO,MAA0B;CAC/C,OACE,KAAK,iBAAiB,KAAK,KAAK,wBAAwB,KAAK,KAAK,sBAAsB,KACxF,KAAK,qBAAqB,KAAK,KAAK,0BAA0B,GAC9D,OAAO,KAAK,IAAI,YAAY;CAE9B,OAAO;AACT;AAEA,SAAgB,QAAQ,MAAgB,uBAAO,IAAI,IAAa,GAAkB;CAChF,OAAO,OAAO,IAAI;CAClB,IAAI,KAAK,gBAAgB,GAAG,OAAO,iBAAiB,KAAK,KAAK,KAAK;CACnE,IAAI,KAAK,kBAAkB,KAAK,CAAC,KAAK,KAAK,YAAY,QACrD,OAAO,iBAAiB,KAAK,KAAK,OAAO,EAAE,CAAC,MAAM,UAAU,EAAE;CAEhE,IAAI,KAAK,aAAa,GAAG;EACvB,MAAM,UAAU,KAAK,MAAM,WAAW,KAAK,KAAK,IAAI;EAEpD,IAA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BAAsB,GAAG,IAAI,WAAW,OAAO;CACrE;CAEF,KAAK,MAAM,eAAe,cAAc,YAAY,KAAK,OAAO,QAAQ,UAAU,YAAY,KAAK,OAAO;CAC1G,QAAS,MAAM,MAAM;CAErB,KAAK,MAAM,eAAe,cAAc;EACtC,YAAY,KAAK,aAAa,YAAY,KAAK,WAAW,QAAQ,cAAc;GAC9E,IACE,CAAC,EAAE,kBAAkB,SAAS,MAC7B,EAAE,aAAa,UAAU,QAAQ,IAAI,UAAU,SAAS,OAAO,UAAU,SAAS,WAAW,KAC9F,OAAO;GAET,OAAO,QAAQ,QAAS,MAAM,WAAW,UAAU,MAAM,IAAI,CAAC,EAAE,UAAU;EAC5E,CAAC;EACD,IAAI,CAAC,YAAY,KAAK,WAAW,QAAQ,YAAY,OAAO;CAC9D;CAEA,OAAO;EAAE;EAAK;EAAU,SAAS;CAAK;AACxC;AAEA,SAAgB,UAAU,MAAc,UAA2B,CAAC,GAAG;CACrE,MAAM,WAAW,QAAQ,YAAY;CACrC,MAAM,SAAS,eAAe,MAAM,UAAU,OAAO;CAErD,IAAI,CAAC,OAAO,SAAS,OAAO;CAE5B,MAAM,SAAS,SAAS,OAAO,KAAK;EAAE,YAAY;EAAM,gBAAgB;EAAU,UAAU;CAAK,GAAG,IAAI;CAExG,OAAO;EAAE,MAAM,OAAO;EAAM,KAAK,OAAO;EAAK,UAAU,OAAO;CAAS;AACzE"}
|
package/dist/vite.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { PluginOptions } from './options';
|
|
2
|
+
export type { PluginOptions } from './options';
|
|
3
|
+
export default function fluentic(options?: PluginOptions): {
|
|
4
|
+
name: string;
|
|
5
|
+
buildStart(this: {
|
|
6
|
+
addWatchFile(file: string): void;
|
|
7
|
+
}): void;
|
|
8
|
+
watchChange(id: string): void;
|
|
9
|
+
enforce: 'pre';
|
|
10
|
+
transform: {
|
|
11
|
+
filter: {
|
|
12
|
+
id: {
|
|
13
|
+
include: RegExp | undefined;
|
|
14
|
+
exclude: RegExp[];
|
|
15
|
+
};
|
|
16
|
+
} | {
|
|
17
|
+
id: {};
|
|
18
|
+
};
|
|
19
|
+
handler: (code: string, id: string) => {
|
|
20
|
+
code: string;
|
|
21
|
+
map: {
|
|
22
|
+
version: number;
|
|
23
|
+
sources: string[];
|
|
24
|
+
names: string[];
|
|
25
|
+
sourceRoot?: string | undefined;
|
|
26
|
+
sourcesContent?: string[] | undefined;
|
|
27
|
+
mappings: string;
|
|
28
|
+
file: string;
|
|
29
|
+
} | null;
|
|
30
|
+
messages: import("./analyze").ExtractedMessage[];
|
|
31
|
+
} | null;
|
|
32
|
+
};
|
|
33
|
+
};
|
package/dist/vite.mjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { a as createTransformHookFilter } from "./filter-Bc7CtppJ.mjs";
|
|
2
|
+
import fluentic$1 from "./rollup.mjs";
|
|
3
|
+
// src/vite.ts
|
|
4
|
+
function fluentic(options = {}) {
|
|
5
|
+
const adapter = fluentic$1(options);
|
|
6
|
+
return {
|
|
7
|
+
...adapter,
|
|
8
|
+
enforce: "pre",
|
|
9
|
+
transform: {
|
|
10
|
+
filter: options.config ? { id: {} } : createTransformHookFilter(options),
|
|
11
|
+
handler: adapter.transform
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export { fluentic as default };
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=vite.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite.mjs","names":["rollup"],"sources":["../src/vite.ts"],"sourcesContent":["import { createTransformHookFilter } from './filter';\nimport type { PluginOptions } from './options';\nimport rollup from './rollup';\n\nexport type { PluginOptions } from './options';\n\nexport default function fluentic(options: PluginOptions = {}) {\n const adapter = rollup(options);\n return {\n ...adapter,\n enforce: 'pre' as const,\n transform: {\n // Config can change during watch mode. Keep native filtering broad in that\n // case; the adapter always checks the current authoritative shared filter.\n filter: options.config ? { id: {} } : createTransformHookFilter(options),\n handler: adapter.transform,\n },\n };\n}\n"],"mappings":";;;AAMA,SAAwB,SAAS,UAAyB,CAAC,GAAG;CAC5D,MAAM,UAAUA,WAAO,OAAO;CAC9B,OAAO;EACL,GAAG;EACH,SAAS;EACT,WAAW;GAGT,QAAQ,QAAQ,SAAS,EAAE,IAAI,CAAC,EAAE,IAAI,0BAA0B,OAAO;GACvE,SAAS,QAAQ;EACnB;CACF;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function loader(this: any, code: string, inputMap: unknown): void;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { n as transform } from "./src-BBeF3k1d.mjs";
|
|
2
|
+
import { i as createTransformFilter } from "./filter-Bc7CtppJ.mjs";
|
|
3
|
+
import { t as loadOptions } from "./options-CHPeMkCj.mjs";
|
|
4
|
+
import remapping from "@jridgewell/remapping";
|
|
5
|
+
// src/webpack-loader.ts
|
|
6
|
+
function loader(code, inputMap) {
|
|
7
|
+
this.cacheable?.();
|
|
8
|
+
const options = loadOptions(this.getOptions?.() ?? {}, this.rootContext);
|
|
9
|
+
if (options.configFile) this.addDependency(options.configFile);
|
|
10
|
+
const result = createTransformFilter(options)(this.resourcePath) ? transform(code, {
|
|
11
|
+
...options,
|
|
12
|
+
filename: this.resourcePath
|
|
13
|
+
}) : null;
|
|
14
|
+
this.callback(null, result?.code ?? code, result?.map && inputMap ? remapping([result.map, inputMap], () => null) : result?.map ?? inputMap);
|
|
15
|
+
}
|
|
16
|
+
export { loader as default };
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=webpack-loader.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webpack-loader.mjs","names":[],"sources":["../src/webpack-loader.ts"],"sourcesContent":["import remapping from '@jridgewell/remapping';\nimport { createTransformFilter } from './filter';\nimport { transform } from './index';\nimport { loadOptions } from './options';\n\nexport default function loader(this: any, code: string, inputMap: unknown) {\n this.cacheable?.();\n const options = loadOptions(this.getOptions?.() ?? {}, this.rootContext);\n\n if (options.configFile) this.addDependency(options.configFile);\n\n const result = createTransformFilter(options)(this.resourcePath)\n ? transform(code, { ...options, filename: this.resourcePath })\n : null;\n this.callback(\n null,\n result?.code ?? code,\n result?.map && inputMap ? remapping([result.map as any, inputMap as any], () => null) : result?.map ?? inputMap,\n );\n}\n"],"mappings":";;;;;AAKA,SAAwB,OAAkB,MAAc,UAAmB;CACzE,KAAK,YAAY;CACjB,MAAM,UAAU,YAAY,KAAK,aAAa,KAAK,CAAC,GAAG,KAAK,WAAW;CAEvE,IAAI,QAAQ,YAAY,KAAK,cAAc,QAAQ,UAAU;CAE7D,MAAM,SAAS,sBAAsB,OAAO,CAAC,CAAC,KAAK,YAAY,IAC3D,UAAU,MAAM;EAAE,GAAG;EAAS,UAAU,KAAK;CAAa,CAAC,IAC3D;CACJ,KAAK,SACH,MACA,QAAQ,QAAQ,MAChB,QAAQ,OAAO,WAAW,UAAU,CAAC,OAAO,KAAY,QAAe,SAAS,IAAI,IAAI,QAAQ,OAAO,QACzG;AACF"}
|
package/dist/webpack.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { i as createTransformFilter } from "./filter-Bc7CtppJ.mjs";
|
|
2
|
+
import { t as loadOptions } from "./options-CHPeMkCj.mjs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
// src/webpack.ts
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
var FluenticI18nPlugin = class {
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
this.options = options;
|
|
9
|
+
}
|
|
10
|
+
apply(compiler) {
|
|
11
|
+
const filter = this.options.config ? (id) => createTransformFilter(loadOptions(this.options, compiler.context))(id) : createTransformFilter(this.options);
|
|
12
|
+
compiler.options.module.rules.push({
|
|
13
|
+
test: filter,
|
|
14
|
+
enforce: "pre",
|
|
15
|
+
use: [{
|
|
16
|
+
loader: require.resolve("@fluentic/i18n-compiler/webpack-loader"),
|
|
17
|
+
options: this.options
|
|
18
|
+
}]
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
export { FluenticI18nPlugin as default };
|
|
23
|
+
|
|
24
|
+
//# sourceMappingURL=webpack.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webpack.mjs","names":[],"sources":["../src/webpack.ts"],"sourcesContent":["import { createRequire } from 'node:module';\nimport { createTransformFilter } from './filter';\nimport { loadOptions, type PluginOptions } from './options';\n\nconst require = createRequire(import.meta.url);\nexport default class FluenticI18nPlugin {\n constructor(private options: PluginOptions = {}) {}\n apply(compiler: any) {\n const filter = this.options.config\n ? (id: string) => createTransformFilter(loadOptions(this.options, compiler.context))(id)\n : createTransformFilter(this.options);\n compiler.options.module.rules.push({\n test: filter,\n enforce: 'pre',\n use: [{ loader: require.resolve('@fluentic/i18n-compiler/webpack-loader'), options: this.options }],\n });\n }\n}\n"],"mappings":";;;;AAIA,MAAM,UAAU,cAAc,YAAY,GAAG;AAC7C,IAAqB,qBAArB,MAAwC;CACtC,YAAY,UAAiC,CAAC,GAAG;EAA7B,KAAA,UAAA;CAA8B;CAClD,MAAM,UAAe;EACnB,MAAM,SAAS,KAAK,QAAQ,UACvB,OAAe,sBAAsB,YAAY,KAAK,SAAS,SAAS,OAAO,CAAC,CAAC,CAAC,EAAE,IACrF,sBAAsB,KAAK,OAAO;EACtC,SAAS,QAAQ,OAAO,MAAM,KAAK;GACjC,MAAM;GACN,SAAS;GACT,KAAK,CAAC;IAAE,QAAQ,QAAQ,QAAQ,wCAAwC;IAAG,SAAS,KAAK;GAAQ,CAAC;EACpG,CAAC;CACH;AACF"}
|