@intlify/bundle-utils 12.0.0-alpha.2 → 12.0.0-alpha.4
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/lib/index.cjs +202 -115
- package/lib/index.d.cts +6 -5
- package/lib/index.d.mts +6 -5
- package/lib/index.d.ts +6 -5
- package/lib/index.mjs +190 -115
- package/package.json +6 -7
package/lib/index.cjs
CHANGED
|
@@ -2,19 +2,32 @@
|
|
|
2
2
|
|
|
3
3
|
const module$1 = require('node:module');
|
|
4
4
|
const shared = require('@intlify/shared');
|
|
5
|
-
const acorn = require('acorn');
|
|
6
5
|
const escodegen = require('escodegen');
|
|
7
|
-
const
|
|
6
|
+
const oxcParser = require('oxc-parser');
|
|
7
|
+
const oxcWalker = require('oxc-walker');
|
|
8
8
|
const messageCompiler = require('@intlify/message-compiler');
|
|
9
9
|
const sourceMapJs = require('source-map-js');
|
|
10
10
|
const jsoncEslintParser = require('jsonc-eslint-parser');
|
|
11
|
-
const
|
|
11
|
+
const oxcTransform = require('oxc-transform');
|
|
12
12
|
const yamlEslintParser = require('yaml-eslint-parser');
|
|
13
13
|
|
|
14
14
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
15
15
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
16
16
|
|
|
17
|
+
function _interopNamespaceCompat(e) {
|
|
18
|
+
if (e && typeof e === 'object' && 'default' in e) return e;
|
|
19
|
+
const n = Object.create(null);
|
|
20
|
+
if (e) {
|
|
21
|
+
for (const k in e) {
|
|
22
|
+
n[k] = e[k];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
n.default = e;
|
|
26
|
+
return n;
|
|
27
|
+
}
|
|
28
|
+
|
|
17
29
|
const module__default = /*#__PURE__*/_interopDefaultCompat(module$1);
|
|
30
|
+
const messageCompiler__namespace = /*#__PURE__*/_interopNamespaceCompat(messageCompiler);
|
|
18
31
|
|
|
19
32
|
const _require = module__default.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
|
|
20
33
|
function checkInstallPackage(pkg, debug) {
|
|
@@ -160,6 +173,14 @@ const ON_ERROR_NOOP = () => {
|
|
|
160
173
|
function parsePath(path) {
|
|
161
174
|
return path ? path.join(".") : "";
|
|
162
175
|
}
|
|
176
|
+
const COMPILE_ERROR_MESSAGES = messageCompiler__namespace.errorMessages;
|
|
177
|
+
function getCompileErrorMessage(err) {
|
|
178
|
+
if (err.message !== String(err.code)) {
|
|
179
|
+
return err.message;
|
|
180
|
+
}
|
|
181
|
+
const text = COMPILE_ERROR_MESSAGES?.[err.code];
|
|
182
|
+
return text ? text.replace(/:\s*\\?'?\{\d+\}'?/g, "") : err.message;
|
|
183
|
+
}
|
|
163
184
|
function generateMessageFunction(msg, options = {}, path) {
|
|
164
185
|
const env = options.env != null ? options.env : "development";
|
|
165
186
|
const strictMessage = shared.isBoolean(options.strictMessage) ? options.strictMessage : true;
|
|
@@ -188,7 +209,7 @@ function generateMessageFunction(msg, options = {}, path) {
|
|
|
188
209
|
domain: err.domain,
|
|
189
210
|
location: err.location
|
|
190
211
|
};
|
|
191
|
-
onError(err
|
|
212
|
+
onError(getCompileErrorMessage(err), extra);
|
|
192
213
|
errors.push(err);
|
|
193
214
|
}
|
|
194
215
|
};
|
|
@@ -210,39 +231,46 @@ function mapLinesColumns(resMap, codeMaps, inSourceMap) {
|
|
|
210
231
|
if (inMapFirstItem) {
|
|
211
232
|
return;
|
|
212
233
|
}
|
|
234
|
+
if (m.originalLine == null || m.originalColumn == null) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
213
237
|
inMapFirstItem = m;
|
|
214
238
|
});
|
|
215
239
|
}
|
|
216
240
|
resMapConsumer.eachMapping((res) => {
|
|
217
|
-
|
|
241
|
+
const resOriginalLine = res.originalLine;
|
|
242
|
+
const resOriginalColumn = res.originalColumn;
|
|
243
|
+
const resName = res.name;
|
|
244
|
+
if (resOriginalLine == null || resOriginalColumn == null || resName == null) {
|
|
218
245
|
return;
|
|
219
246
|
}
|
|
220
|
-
const map = codeMaps.get(
|
|
247
|
+
const map = codeMaps.get(resName);
|
|
221
248
|
if (!map) {
|
|
222
249
|
return;
|
|
223
250
|
}
|
|
224
251
|
let inMapOrigin = null;
|
|
225
252
|
if (inMapConsumer) {
|
|
226
253
|
inMapOrigin = inMapConsumer.originalPositionFor({
|
|
227
|
-
line:
|
|
228
|
-
column:
|
|
254
|
+
line: resOriginalLine,
|
|
255
|
+
column: resOriginalColumn - 1
|
|
229
256
|
});
|
|
230
257
|
if (inMapOrigin.source == null) {
|
|
231
258
|
inMapOrigin = null;
|
|
232
259
|
return;
|
|
233
260
|
}
|
|
234
261
|
}
|
|
262
|
+
const inMapFirst = inMapFirstItem;
|
|
235
263
|
const mapConsumer = new sourceMapJs.SourceMapConsumer(map);
|
|
236
264
|
mapConsumer.eachMapping((m) => {
|
|
237
265
|
mergedMapGenerator.addMapping({
|
|
238
266
|
original: {
|
|
239
|
-
line:
|
|
240
|
-
column:
|
|
267
|
+
line: inMapFirst ? inMapFirst.originalLine + resOriginalLine - 2 : resOriginalLine,
|
|
268
|
+
column: inMapFirst ? inMapFirst.originalColumn + resOriginalColumn : resOriginalColumn
|
|
241
269
|
},
|
|
242
270
|
generated: {
|
|
243
|
-
line:
|
|
271
|
+
line: inMapFirst ? inMapFirst.generatedLine + resOriginalLine - 2 : resOriginalLine,
|
|
244
272
|
// map column with message format compilation code map
|
|
245
|
-
column:
|
|
273
|
+
column: inMapFirst ? inMapFirst.generatedColumn + resOriginalColumn + m.generatedColumn : resOriginalColumn + m.generatedColumn
|
|
246
274
|
},
|
|
247
275
|
source: inMapOrigin ? inMapOrigin.source : res.source,
|
|
248
276
|
name: m.name
|
|
@@ -300,7 +328,7 @@ function generateResourceAst(msg, options = {}, path) {
|
|
|
300
328
|
domain: err.domain,
|
|
301
329
|
location: err.location
|
|
302
330
|
};
|
|
303
|
-
onError(err
|
|
331
|
+
onError(getCompileErrorMessage(err), extra);
|
|
304
332
|
errors.push(err);
|
|
305
333
|
}
|
|
306
334
|
};
|
|
@@ -309,6 +337,41 @@ function generateResourceAst(msg, options = {}, path) {
|
|
|
309
337
|
const code = !occured ? `${shared.friendlyJSONstringify(ast)}` : `\`${_msg}\``;
|
|
310
338
|
return { code, ast, map, errors };
|
|
311
339
|
}
|
|
340
|
+
function describeResource({ type, filename }) {
|
|
341
|
+
return type === "sfc" ? `the i18n custom block of ${filename}` : `${filename}`;
|
|
342
|
+
}
|
|
343
|
+
function describeValue(value) {
|
|
344
|
+
if (Array.isArray(value)) return "an array";
|
|
345
|
+
if (value === void 0) return "undefined";
|
|
346
|
+
return `a ${typeof value}`;
|
|
347
|
+
}
|
|
348
|
+
function stringifyValue(value) {
|
|
349
|
+
if (typeof value === "number") return Object.is(value, -0) ? "-0" : String(value);
|
|
350
|
+
if (value === void 0) return "undefined";
|
|
351
|
+
return JSON.stringify(value);
|
|
352
|
+
}
|
|
353
|
+
function createResourceError(message, options) {
|
|
354
|
+
return new Error(`${message} in ${describeResource(options)}.`);
|
|
355
|
+
}
|
|
356
|
+
function createResourceParseError(err, options, columnOffset = 0) {
|
|
357
|
+
if (!(err instanceof Error)) return err;
|
|
358
|
+
const { lineNumber, column } = err;
|
|
359
|
+
const position = lineNumber != null ? ` (${lineNumber}:${column + columnOffset})` : "";
|
|
360
|
+
return new SyntaxError(`${err.message} in ${describeResource(options)}${position}`, {
|
|
361
|
+
cause: err
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
function createOxcParseError(errors, source, options, offsetUnit = "utf-16") {
|
|
365
|
+
const error = errors.find((e) => e.severity === "Error") ?? errors[0];
|
|
366
|
+
const err = new Error(error.message);
|
|
367
|
+
const start = error.labels[0]?.start;
|
|
368
|
+
if (start != null) {
|
|
369
|
+
const before = offsetUnit === "utf-8" ? Buffer.from(source).subarray(0, start).toString() : source.slice(0, start);
|
|
370
|
+
err.lineNumber = before.split("\n").length;
|
|
371
|
+
err.column = before.length - before.lastIndexOf("\n");
|
|
372
|
+
}
|
|
373
|
+
return createResourceParseError(err, options);
|
|
374
|
+
}
|
|
312
375
|
function excludeLocales({
|
|
313
376
|
messages,
|
|
314
377
|
onlyLocales
|
|
@@ -324,48 +387,38 @@ function excludeLocales({
|
|
|
324
387
|
|
|
325
388
|
class DynamicResourceError extends Error {
|
|
326
389
|
}
|
|
327
|
-
|
|
328
|
-
type
|
|
329
|
-
filename
|
|
330
|
-
inSourceMap
|
|
331
|
-
locale
|
|
332
|
-
isGlobal
|
|
333
|
-
sourceMap
|
|
334
|
-
env
|
|
335
|
-
forceStringify
|
|
336
|
-
onError
|
|
337
|
-
onWarn
|
|
338
|
-
strictMessage
|
|
339
|
-
escapeHtml
|
|
340
|
-
allowDynamic
|
|
341
|
-
jit
|
|
342
|
-
}
|
|
390
|
+
const DEFAULT_OPTIONS = {
|
|
391
|
+
type: "plain",
|
|
392
|
+
filename: "vue-i18n-loader.js",
|
|
393
|
+
inSourceMap: void 0,
|
|
394
|
+
locale: "",
|
|
395
|
+
isGlobal: false,
|
|
396
|
+
sourceMap: false,
|
|
397
|
+
env: "development",
|
|
398
|
+
forceStringify: false,
|
|
399
|
+
onError: void 0,
|
|
400
|
+
onWarn: void 0,
|
|
401
|
+
strictMessage: true,
|
|
402
|
+
escapeHtml: false,
|
|
403
|
+
allowDynamic: false,
|
|
404
|
+
jit: false
|
|
405
|
+
};
|
|
406
|
+
function generate$3(targetSource, options) {
|
|
343
407
|
const value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
sourceMap,
|
|
348
|
-
locale,
|
|
349
|
-
isGlobal,
|
|
350
|
-
inSourceMap,
|
|
351
|
-
env,
|
|
352
|
-
filename,
|
|
353
|
-
forceStringify,
|
|
354
|
-
onError,
|
|
355
|
-
onWarn,
|
|
356
|
-
strictMessage,
|
|
357
|
-
escapeHtml,
|
|
358
|
-
jit
|
|
359
|
-
};
|
|
360
|
-
const generator = createCodeGenerator(options);
|
|
361
|
-
const ast = acorn.parse(value, {
|
|
362
|
-
ecmaVersion: "latest",
|
|
408
|
+
const _options = Object.assign({}, DEFAULT_OPTIONS, options, { source: value });
|
|
409
|
+
const generator = createCodeGenerator(_options);
|
|
410
|
+
const { program: ast, errors } = oxcParser.parseSync(_options.filename ?? "", value, {
|
|
363
411
|
sourceType: "module",
|
|
364
|
-
|
|
365
|
-
|
|
412
|
+
lang: "js",
|
|
413
|
+
// `(value)` is the value itself: `export default ({ ... })` is an object, a parenthesized
|
|
414
|
+
// message is compiled, and escodegen does not know `ParenthesizedExpression`
|
|
415
|
+
preserveParens: false
|
|
366
416
|
});
|
|
417
|
+
if (errors.length > 0) {
|
|
418
|
+
throw createOxcParseError(errors, value, _options);
|
|
419
|
+
}
|
|
367
420
|
const exportResult = scanAst(ast);
|
|
368
|
-
if (!allowDynamic) {
|
|
421
|
+
if (!_options.allowDynamic) {
|
|
369
422
|
if (!exportResult) {
|
|
370
423
|
throw new Error(`You need to define an object as the locale message with 'export default'.`);
|
|
371
424
|
}
|
|
@@ -382,13 +435,13 @@ function generate$3(targetSource, {
|
|
|
382
435
|
return {
|
|
383
436
|
ast,
|
|
384
437
|
code: value,
|
|
385
|
-
map: inSourceMap
|
|
438
|
+
map: _options.inSourceMap
|
|
386
439
|
};
|
|
387
440
|
}
|
|
388
441
|
}
|
|
389
|
-
const codeMaps = _generate$2(generator, ast,
|
|
442
|
+
const codeMaps = _generate$2(generator, ast, _options);
|
|
390
443
|
const { code, map } = generator.context();
|
|
391
|
-
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
444
|
+
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, _options.inSourceMap) || null : null;
|
|
392
445
|
return {
|
|
393
446
|
ast,
|
|
394
447
|
code,
|
|
@@ -428,16 +481,10 @@ function _generate$2(generator, node, options = {}) {
|
|
|
428
481
|
}
|
|
429
482
|
const componentNamespace = "_Component";
|
|
430
483
|
const variableDeclarations = [];
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
|
-
* NOTE:
|
|
434
|
-
* force cast to Node of `estree-walker@3.x`,
|
|
435
|
-
* because `estree-walker@3.x` is not dual packages,
|
|
436
|
-
* so it's support only esm only ...
|
|
437
|
-
*/
|
|
484
|
+
oxcWalker.walk(node, {
|
|
438
485
|
// @ts-ignore
|
|
439
|
-
enter(node2,
|
|
440
|
-
if (
|
|
486
|
+
enter(node2, parent) {
|
|
487
|
+
if (parent?.type != null) this.skip();
|
|
441
488
|
switch (node2.type) {
|
|
442
489
|
case "ExportDefaultDeclaration":
|
|
443
490
|
this.skip();
|
|
@@ -462,7 +509,7 @@ function _generate$2(generator, node, options = {}) {
|
|
|
462
509
|
Variable declarations are not optimized - found ${variableDeclarations.join(", ")}`
|
|
463
510
|
);
|
|
464
511
|
}
|
|
465
|
-
|
|
512
|
+
oxcWalker.walk(node, {
|
|
466
513
|
/**
|
|
467
514
|
* NOTE:
|
|
468
515
|
* force cast to Node of `estree-walker@3.x`,
|
|
@@ -581,12 +628,6 @@ Variable declarations are not optimized - found ${variableDeclarations.join(", "
|
|
|
581
628
|
break;
|
|
582
629
|
}
|
|
583
630
|
},
|
|
584
|
-
/**
|
|
585
|
-
* NOTE:
|
|
586
|
-
* force cast to Node of `estree-walker@3.x`,
|
|
587
|
-
* because `estree-walker@3.x` is not dual packages,
|
|
588
|
-
* so it's support only esm only ...
|
|
589
|
-
*/
|
|
590
631
|
// @ts-ignore
|
|
591
632
|
leave(node2, parent) {
|
|
592
633
|
switch (node2.type) {
|
|
@@ -667,7 +708,25 @@ function generate$2(targetSource, {
|
|
|
667
708
|
escapeHtml,
|
|
668
709
|
jit
|
|
669
710
|
};
|
|
670
|
-
let ast
|
|
711
|
+
let ast;
|
|
712
|
+
try {
|
|
713
|
+
ast = jsoncEslintParser.parseJSON(value, { filePath: filename });
|
|
714
|
+
} catch (err) {
|
|
715
|
+
throw createResourceParseError(err, options);
|
|
716
|
+
}
|
|
717
|
+
const root = ast.body[0].expression;
|
|
718
|
+
if (root.type !== "JSONObjectExpression") {
|
|
719
|
+
const rootValue = jsoncEslintParser.getStaticJSONValue(root);
|
|
720
|
+
if (rootValue !== null) {
|
|
721
|
+
throw createResourceError(
|
|
722
|
+
`You need to define an object as the locale messages at the top level, but got ${describeValue(rootValue)}`,
|
|
723
|
+
options
|
|
724
|
+
);
|
|
725
|
+
}
|
|
726
|
+
value = JSON.stringify({});
|
|
727
|
+
ast = jsoncEslintParser.parseJSON(value, { filePath: filename });
|
|
728
|
+
options.source = void 0;
|
|
729
|
+
}
|
|
671
730
|
if (!locale && type === "sfc" && onlyLocales?.length) {
|
|
672
731
|
const messages = jsoncEslintParser.getStaticJSONValue(ast);
|
|
673
732
|
value = JSON.stringify(
|
|
@@ -707,6 +766,19 @@ function _generate$1(generator, node, options = {}) {
|
|
|
707
766
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
708
767
|
return code;
|
|
709
768
|
}
|
|
769
|
+
function pushExpressionValue(node2) {
|
|
770
|
+
const value = jsoncEslintParser.getStaticJSONValue(node2);
|
|
771
|
+
if (shared.isString(value)) {
|
|
772
|
+
generator.push(codegenFn(value), node2, value);
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
const strValue = stringifyValue(value);
|
|
776
|
+
if (forceStringify) {
|
|
777
|
+
generator.push(codegenFn(strValue), node2, strValue);
|
|
778
|
+
} else {
|
|
779
|
+
generator.push(strValue);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
710
782
|
const componentNamespace = "_Component";
|
|
711
783
|
jsoncEslintParser.traverseNodes(node, {
|
|
712
784
|
enterNode(node2, parent) {
|
|
@@ -763,6 +835,8 @@ function _generate$1(generator, node, options = {}) {
|
|
|
763
835
|
} else {
|
|
764
836
|
generator.push(strValue2);
|
|
765
837
|
}
|
|
838
|
+
} else if (node2.value.type !== "JSONObjectExpression" && node2.value.type !== "JSONArrayExpression") {
|
|
839
|
+
pushExpressionValue(node2.value);
|
|
766
840
|
}
|
|
767
841
|
break;
|
|
768
842
|
}
|
|
@@ -778,6 +852,14 @@ function _generate$1(generator, node, options = {}) {
|
|
|
778
852
|
generator.push(strValue);
|
|
779
853
|
}
|
|
780
854
|
break;
|
|
855
|
+
case "JSONUnaryExpression":
|
|
856
|
+
case "JSONIdentifier":
|
|
857
|
+
case "JSONTemplateLiteral":
|
|
858
|
+
case "JSONBinaryExpression":
|
|
859
|
+
if (parent.type === "JSONArrayExpression") {
|
|
860
|
+
pushExpressionValue(node2);
|
|
861
|
+
}
|
|
862
|
+
break;
|
|
781
863
|
}
|
|
782
864
|
},
|
|
783
865
|
leaveNode(node2, parent) {
|
|
@@ -823,48 +905,18 @@ function _generate$1(generator, node, options = {}) {
|
|
|
823
905
|
return codeMaps;
|
|
824
906
|
}
|
|
825
907
|
|
|
826
|
-
|
|
827
|
-
type = "plain",
|
|
828
|
-
filename = "vue-i18n-loader.js",
|
|
829
|
-
inSourceMap = void 0,
|
|
830
|
-
locale = "",
|
|
831
|
-
isGlobal = false,
|
|
832
|
-
sourceMap = false,
|
|
833
|
-
env = "development",
|
|
834
|
-
forceStringify = false,
|
|
835
|
-
onError = void 0,
|
|
836
|
-
onWarn = void 0,
|
|
837
|
-
strictMessage = true,
|
|
838
|
-
escapeHtml = false,
|
|
839
|
-
allowDynamic = false,
|
|
840
|
-
jit = false
|
|
841
|
-
}) {
|
|
908
|
+
function generate$1(targetSource, options) {
|
|
842
909
|
let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
843
|
-
const
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
filename,
|
|
852
|
-
forceStringify,
|
|
853
|
-
onError,
|
|
854
|
-
onWarn,
|
|
855
|
-
strictMessage,
|
|
856
|
-
escapeHtml,
|
|
857
|
-
allowDynamic,
|
|
858
|
-
jit
|
|
859
|
-
};
|
|
860
|
-
if (options.filename && /.[c|m]?ts$/.test(options.filename)) {
|
|
861
|
-
const transformed = await esbuild.transform(value, { loader: "ts" });
|
|
862
|
-
if (transformed && transformed.code) {
|
|
863
|
-
value = transformed.code;
|
|
864
|
-
options.source = transformed.code;
|
|
865
|
-
}
|
|
910
|
+
const _options = Object.assign({}, DEFAULT_OPTIONS, options, { source: value });
|
|
911
|
+
const transformed = oxcTransform.transform(_options.filename ?? "", value, { lang: "ts" });
|
|
912
|
+
if (transformed.errors.length > 0) {
|
|
913
|
+
throw createOxcParseError(transformed.errors, value, _options, "utf-8");
|
|
914
|
+
}
|
|
915
|
+
if (transformed && transformed.code) {
|
|
916
|
+
value = transformed.code;
|
|
917
|
+
_options.source = transformed.code;
|
|
866
918
|
}
|
|
867
|
-
return generate$3(value,
|
|
919
|
+
return generate$3(value, _options);
|
|
868
920
|
}
|
|
869
921
|
|
|
870
922
|
function generate(targetSource, {
|
|
@@ -883,6 +935,9 @@ function generate(targetSource, {
|
|
|
883
935
|
jit = false
|
|
884
936
|
}) {
|
|
885
937
|
let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
938
|
+
if (value.charCodeAt(0) === 65279) {
|
|
939
|
+
value = value.slice(1);
|
|
940
|
+
}
|
|
886
941
|
const options = {
|
|
887
942
|
type,
|
|
888
943
|
source: value,
|
|
@@ -898,9 +953,31 @@ function generate(targetSource, {
|
|
|
898
953
|
escapeHtml,
|
|
899
954
|
jit
|
|
900
955
|
};
|
|
901
|
-
let ast
|
|
956
|
+
let ast;
|
|
957
|
+
try {
|
|
958
|
+
ast = yamlEslintParser.parseYAML(value, { filePath: filename });
|
|
959
|
+
} catch (err) {
|
|
960
|
+
throw createResourceParseError(err, options, 1);
|
|
961
|
+
}
|
|
962
|
+
const rootDocument = getRootDocument(ast, options);
|
|
963
|
+
const content = rootDocument?.content;
|
|
964
|
+
const root = content?.type === "YAMLWithMeta" ? content.value : content;
|
|
965
|
+
if (root?.type !== "YAMLMapping") {
|
|
966
|
+
const isAlias = root?.type === "YAMLAlias";
|
|
967
|
+
const rootValue = rootDocument == null || isAlias ? null : yamlEslintParser.getStaticYAMLValue(rootDocument);
|
|
968
|
+
if (isAlias || rootValue !== null) {
|
|
969
|
+
const actual = isAlias ? "an alias" : describeValue(rootValue);
|
|
970
|
+
throw createResourceError(
|
|
971
|
+
`You need to define an object as the locale messages at the top level, but got ${actual}`,
|
|
972
|
+
options
|
|
973
|
+
);
|
|
974
|
+
}
|
|
975
|
+
value = JSON.stringify({});
|
|
976
|
+
ast = yamlEslintParser.parseYAML(value, { filePath: filename });
|
|
977
|
+
options.source = void 0;
|
|
978
|
+
}
|
|
902
979
|
if (!locale && type === "sfc" && onlyLocales?.length) {
|
|
903
|
-
const messages = yamlEslintParser.getStaticYAMLValue(ast);
|
|
980
|
+
const messages = yamlEslintParser.getStaticYAMLValue(getRootDocument(ast, options));
|
|
904
981
|
value = JSON.stringify(
|
|
905
982
|
excludeLocales({
|
|
906
983
|
messages,
|
|
@@ -925,6 +1002,16 @@ function generate(targetSource, {
|
|
|
925
1002
|
map: newMap != null ? newMap : void 0
|
|
926
1003
|
};
|
|
927
1004
|
}
|
|
1005
|
+
function getRootDocument(ast, options) {
|
|
1006
|
+
const documents = ast.body.filter((doc) => doc.content != null);
|
|
1007
|
+
if (documents.length > 1) {
|
|
1008
|
+
throw createResourceError(
|
|
1009
|
+
`You need to define the locale messages in a single YAML document, but got ${documents.length} documents`,
|
|
1010
|
+
options
|
|
1011
|
+
);
|
|
1012
|
+
}
|
|
1013
|
+
return documents[0] ?? null;
|
|
1014
|
+
}
|
|
928
1015
|
function _generate(generator, node, options = {}) {
|
|
929
1016
|
const propsCountStack = [];
|
|
930
1017
|
const pathStack = [];
|
|
@@ -985,7 +1072,7 @@ function _generate(generator, node, options = {}) {
|
|
|
985
1072
|
const name = node2.key.value;
|
|
986
1073
|
const value = node2.value.value;
|
|
987
1074
|
const strName = JSON.stringify(name);
|
|
988
|
-
const strValue =
|
|
1075
|
+
const strValue = stringifyValue(value);
|
|
989
1076
|
name && pathStack.push(name.toString());
|
|
990
1077
|
generator.push(`${strName}: `);
|
|
991
1078
|
if (shared.isString(value)) {
|
|
@@ -1004,7 +1091,7 @@ function _generate(generator, node, options = {}) {
|
|
|
1004
1091
|
case "YAMLScalar":
|
|
1005
1092
|
if (parent.type === "YAMLSequence") {
|
|
1006
1093
|
const value = node2.value;
|
|
1007
|
-
const strValue =
|
|
1094
|
+
const strValue = stringifyValue(value);
|
|
1008
1095
|
if (shared.isString(value)) {
|
|
1009
1096
|
generator.push(codegenFn(value), node2, value);
|
|
1010
1097
|
} else if (forceStringify) {
|
package/lib/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { RawSourceMap } from 'source-map-js';
|
|
2
2
|
import { CompileError } from '@intlify/message-compiler';
|
|
3
|
-
import { Node } from '
|
|
3
|
+
import { Node } from 'oxc-parser';
|
|
4
4
|
import { JSONProgram } from 'jsonc-eslint-parser/lib/parser/ast';
|
|
5
5
|
import { YAMLProgram } from 'yaml-eslint-parser/lib/ast';
|
|
6
6
|
|
|
@@ -61,7 +61,7 @@ declare class DynamicResourceError extends Error {
|
|
|
61
61
|
/**
|
|
62
62
|
* @internal
|
|
63
63
|
*/
|
|
64
|
-
declare function generate$3(targetSource: string | Buffer,
|
|
64
|
+
declare function generate$3(targetSource: string | Buffer, options: CodeGenOptions): CodeGenResult<Node>;
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* Code / AST generator for i18n json/json5 resource
|
|
@@ -72,13 +72,13 @@ declare function generate$2(targetSource: string | Buffer, { type, onlyLocales,
|
|
|
72
72
|
/**
|
|
73
73
|
* Code generator for i18n ts resource
|
|
74
74
|
*
|
|
75
|
-
*
|
|
75
|
+
* A wrapper for the js generator which transforms ts to js before generating
|
|
76
76
|
*/
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
79
|
* @internal
|
|
80
80
|
*/
|
|
81
|
-
declare function generate$1(targetSource: string | Buffer,
|
|
81
|
+
declare function generate$1(targetSource: string | Buffer, options: CodeGenOptions): CodeGenResult<Node>;
|
|
82
82
|
|
|
83
83
|
/**
|
|
84
84
|
* Code generator for i18n yaml resource
|
|
@@ -89,4 +89,5 @@ declare function generate$1(targetSource: string | Buffer, { type, filename, inS
|
|
|
89
89
|
*/
|
|
90
90
|
declare function generate(targetSource: string | Buffer, { type, onlyLocales, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, jit }: CodeGenOptions): CodeGenResult<YAMLProgram>;
|
|
91
91
|
|
|
92
|
-
export {
|
|
92
|
+
export { DynamicResourceError, checkInstallPackage, generate$2 as generateJSON, generate$3 as generateJavaScript, generate$1 as generateTypescript, generate as generateYAML, getVueI18nVersion };
|
|
93
|
+
export type { CodeGenOptions, CodeGenResult, DevEnv, InstalledPackage };
|
package/lib/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { RawSourceMap } from 'source-map-js';
|
|
2
2
|
import { CompileError } from '@intlify/message-compiler';
|
|
3
|
-
import { Node } from '
|
|
3
|
+
import { Node } from 'oxc-parser';
|
|
4
4
|
import { JSONProgram } from 'jsonc-eslint-parser/lib/parser/ast';
|
|
5
5
|
import { YAMLProgram } from 'yaml-eslint-parser/lib/ast';
|
|
6
6
|
|
|
@@ -61,7 +61,7 @@ declare class DynamicResourceError extends Error {
|
|
|
61
61
|
/**
|
|
62
62
|
* @internal
|
|
63
63
|
*/
|
|
64
|
-
declare function generate$3(targetSource: string | Buffer,
|
|
64
|
+
declare function generate$3(targetSource: string | Buffer, options: CodeGenOptions): CodeGenResult<Node>;
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* Code / AST generator for i18n json/json5 resource
|
|
@@ -72,13 +72,13 @@ declare function generate$2(targetSource: string | Buffer, { type, onlyLocales,
|
|
|
72
72
|
/**
|
|
73
73
|
* Code generator for i18n ts resource
|
|
74
74
|
*
|
|
75
|
-
*
|
|
75
|
+
* A wrapper for the js generator which transforms ts to js before generating
|
|
76
76
|
*/
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
79
|
* @internal
|
|
80
80
|
*/
|
|
81
|
-
declare function generate$1(targetSource: string | Buffer,
|
|
81
|
+
declare function generate$1(targetSource: string | Buffer, options: CodeGenOptions): CodeGenResult<Node>;
|
|
82
82
|
|
|
83
83
|
/**
|
|
84
84
|
* Code generator for i18n yaml resource
|
|
@@ -89,4 +89,5 @@ declare function generate$1(targetSource: string | Buffer, { type, filename, inS
|
|
|
89
89
|
*/
|
|
90
90
|
declare function generate(targetSource: string | Buffer, { type, onlyLocales, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, jit }: CodeGenOptions): CodeGenResult<YAMLProgram>;
|
|
91
91
|
|
|
92
|
-
export {
|
|
92
|
+
export { DynamicResourceError, checkInstallPackage, generate$2 as generateJSON, generate$3 as generateJavaScript, generate$1 as generateTypescript, generate as generateYAML, getVueI18nVersion };
|
|
93
|
+
export type { CodeGenOptions, CodeGenResult, DevEnv, InstalledPackage };
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { RawSourceMap } from 'source-map-js';
|
|
2
2
|
import { CompileError } from '@intlify/message-compiler';
|
|
3
|
-
import { Node } from '
|
|
3
|
+
import { Node } from 'oxc-parser';
|
|
4
4
|
import { JSONProgram } from 'jsonc-eslint-parser/lib/parser/ast';
|
|
5
5
|
import { YAMLProgram } from 'yaml-eslint-parser/lib/ast';
|
|
6
6
|
|
|
@@ -61,7 +61,7 @@ declare class DynamicResourceError extends Error {
|
|
|
61
61
|
/**
|
|
62
62
|
* @internal
|
|
63
63
|
*/
|
|
64
|
-
declare function generate$3(targetSource: string | Buffer,
|
|
64
|
+
declare function generate$3(targetSource: string | Buffer, options: CodeGenOptions): CodeGenResult<Node>;
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* Code / AST generator for i18n json/json5 resource
|
|
@@ -72,13 +72,13 @@ declare function generate$2(targetSource: string | Buffer, { type, onlyLocales,
|
|
|
72
72
|
/**
|
|
73
73
|
* Code generator for i18n ts resource
|
|
74
74
|
*
|
|
75
|
-
*
|
|
75
|
+
* A wrapper for the js generator which transforms ts to js before generating
|
|
76
76
|
*/
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
79
|
* @internal
|
|
80
80
|
*/
|
|
81
|
-
declare function generate$1(targetSource: string | Buffer,
|
|
81
|
+
declare function generate$1(targetSource: string | Buffer, options: CodeGenOptions): CodeGenResult<Node>;
|
|
82
82
|
|
|
83
83
|
/**
|
|
84
84
|
* Code generator for i18n yaml resource
|
|
@@ -89,4 +89,5 @@ declare function generate$1(targetSource: string | Buffer, { type, filename, inS
|
|
|
89
89
|
*/
|
|
90
90
|
declare function generate(targetSource: string | Buffer, { type, onlyLocales, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, jit }: CodeGenOptions): CodeGenResult<YAMLProgram>;
|
|
91
91
|
|
|
92
|
-
export {
|
|
92
|
+
export { DynamicResourceError, checkInstallPackage, generate$2 as generateJSON, generate$3 as generateJavaScript, generate$1 as generateTypescript, generate as generateYAML, getVueI18nVersion };
|
|
93
|
+
export type { CodeGenOptions, CodeGenResult, DevEnv, InstalledPackage };
|
package/lib/index.mjs
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import module from 'node:module';
|
|
1
|
+
import module$1 from 'node:module';
|
|
2
2
|
import { isBoolean, format, escapeHtml, friendlyJSONstringify, isString, isNumber } from '@intlify/shared';
|
|
3
|
-
import { parse } from 'acorn';
|
|
4
3
|
import { generate as generate$4 } from 'escodegen';
|
|
5
|
-
import {
|
|
4
|
+
import { parseSync } from 'oxc-parser';
|
|
5
|
+
import { walk } from 'oxc-walker';
|
|
6
|
+
import * as messageCompiler from '@intlify/message-compiler';
|
|
6
7
|
import { LOCATION_STUB, detectHtmlTag, baseCompile } from '@intlify/message-compiler';
|
|
7
8
|
import { SourceMapGenerator, SourceMapConsumer } from 'source-map-js';
|
|
8
9
|
import { parseJSON, getStaticJSONValue, traverseNodes } from 'jsonc-eslint-parser';
|
|
9
|
-
import { transform } from '
|
|
10
|
+
import { transform } from 'oxc-transform';
|
|
10
11
|
import { parseYAML, getStaticYAMLValue, traverseNodes as traverseNodes$1 } from 'yaml-eslint-parser';
|
|
11
12
|
|
|
12
|
-
const _require = module.createRequire(import.meta.url);
|
|
13
|
+
const _require = module$1.createRequire(import.meta.url);
|
|
13
14
|
function checkInstallPackage(pkg, debug) {
|
|
14
15
|
let installedVueI18n = false;
|
|
15
16
|
try {
|
|
@@ -153,6 +154,14 @@ const ON_ERROR_NOOP = () => {
|
|
|
153
154
|
function parsePath(path) {
|
|
154
155
|
return path ? path.join(".") : "";
|
|
155
156
|
}
|
|
157
|
+
const COMPILE_ERROR_MESSAGES = messageCompiler.errorMessages;
|
|
158
|
+
function getCompileErrorMessage(err) {
|
|
159
|
+
if (err.message !== String(err.code)) {
|
|
160
|
+
return err.message;
|
|
161
|
+
}
|
|
162
|
+
const text = COMPILE_ERROR_MESSAGES?.[err.code];
|
|
163
|
+
return text ? text.replace(/:\s*\\?'?\{\d+\}'?/g, "") : err.message;
|
|
164
|
+
}
|
|
156
165
|
function generateMessageFunction(msg, options = {}, path) {
|
|
157
166
|
const env = options.env != null ? options.env : "development";
|
|
158
167
|
const strictMessage = isBoolean(options.strictMessage) ? options.strictMessage : true;
|
|
@@ -181,7 +190,7 @@ function generateMessageFunction(msg, options = {}, path) {
|
|
|
181
190
|
domain: err.domain,
|
|
182
191
|
location: err.location
|
|
183
192
|
};
|
|
184
|
-
onError(err
|
|
193
|
+
onError(getCompileErrorMessage(err), extra);
|
|
185
194
|
errors.push(err);
|
|
186
195
|
}
|
|
187
196
|
};
|
|
@@ -203,39 +212,46 @@ function mapLinesColumns(resMap, codeMaps, inSourceMap) {
|
|
|
203
212
|
if (inMapFirstItem) {
|
|
204
213
|
return;
|
|
205
214
|
}
|
|
215
|
+
if (m.originalLine == null || m.originalColumn == null) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
206
218
|
inMapFirstItem = m;
|
|
207
219
|
});
|
|
208
220
|
}
|
|
209
221
|
resMapConsumer.eachMapping((res) => {
|
|
210
|
-
|
|
222
|
+
const resOriginalLine = res.originalLine;
|
|
223
|
+
const resOriginalColumn = res.originalColumn;
|
|
224
|
+
const resName = res.name;
|
|
225
|
+
if (resOriginalLine == null || resOriginalColumn == null || resName == null) {
|
|
211
226
|
return;
|
|
212
227
|
}
|
|
213
|
-
const map = codeMaps.get(
|
|
228
|
+
const map = codeMaps.get(resName);
|
|
214
229
|
if (!map) {
|
|
215
230
|
return;
|
|
216
231
|
}
|
|
217
232
|
let inMapOrigin = null;
|
|
218
233
|
if (inMapConsumer) {
|
|
219
234
|
inMapOrigin = inMapConsumer.originalPositionFor({
|
|
220
|
-
line:
|
|
221
|
-
column:
|
|
235
|
+
line: resOriginalLine,
|
|
236
|
+
column: resOriginalColumn - 1
|
|
222
237
|
});
|
|
223
238
|
if (inMapOrigin.source == null) {
|
|
224
239
|
inMapOrigin = null;
|
|
225
240
|
return;
|
|
226
241
|
}
|
|
227
242
|
}
|
|
243
|
+
const inMapFirst = inMapFirstItem;
|
|
228
244
|
const mapConsumer = new SourceMapConsumer(map);
|
|
229
245
|
mapConsumer.eachMapping((m) => {
|
|
230
246
|
mergedMapGenerator.addMapping({
|
|
231
247
|
original: {
|
|
232
|
-
line:
|
|
233
|
-
column:
|
|
248
|
+
line: inMapFirst ? inMapFirst.originalLine + resOriginalLine - 2 : resOriginalLine,
|
|
249
|
+
column: inMapFirst ? inMapFirst.originalColumn + resOriginalColumn : resOriginalColumn
|
|
234
250
|
},
|
|
235
251
|
generated: {
|
|
236
|
-
line:
|
|
252
|
+
line: inMapFirst ? inMapFirst.generatedLine + resOriginalLine - 2 : resOriginalLine,
|
|
237
253
|
// map column with message format compilation code map
|
|
238
|
-
column:
|
|
254
|
+
column: inMapFirst ? inMapFirst.generatedColumn + resOriginalColumn + m.generatedColumn : resOriginalColumn + m.generatedColumn
|
|
239
255
|
},
|
|
240
256
|
source: inMapOrigin ? inMapOrigin.source : res.source,
|
|
241
257
|
name: m.name
|
|
@@ -293,7 +309,7 @@ function generateResourceAst(msg, options = {}, path) {
|
|
|
293
309
|
domain: err.domain,
|
|
294
310
|
location: err.location
|
|
295
311
|
};
|
|
296
|
-
onError(err
|
|
312
|
+
onError(getCompileErrorMessage(err), extra);
|
|
297
313
|
errors.push(err);
|
|
298
314
|
}
|
|
299
315
|
};
|
|
@@ -302,6 +318,41 @@ function generateResourceAst(msg, options = {}, path) {
|
|
|
302
318
|
const code = !occured ? `${friendlyJSONstringify(ast)}` : `\`${_msg}\``;
|
|
303
319
|
return { code, ast, map, errors };
|
|
304
320
|
}
|
|
321
|
+
function describeResource({ type, filename }) {
|
|
322
|
+
return type === "sfc" ? `the i18n custom block of ${filename}` : `${filename}`;
|
|
323
|
+
}
|
|
324
|
+
function describeValue(value) {
|
|
325
|
+
if (Array.isArray(value)) return "an array";
|
|
326
|
+
if (value === void 0) return "undefined";
|
|
327
|
+
return `a ${typeof value}`;
|
|
328
|
+
}
|
|
329
|
+
function stringifyValue(value) {
|
|
330
|
+
if (typeof value === "number") return Object.is(value, -0) ? "-0" : String(value);
|
|
331
|
+
if (value === void 0) return "undefined";
|
|
332
|
+
return JSON.stringify(value);
|
|
333
|
+
}
|
|
334
|
+
function createResourceError(message, options) {
|
|
335
|
+
return new Error(`${message} in ${describeResource(options)}.`);
|
|
336
|
+
}
|
|
337
|
+
function createResourceParseError(err, options, columnOffset = 0) {
|
|
338
|
+
if (!(err instanceof Error)) return err;
|
|
339
|
+
const { lineNumber, column } = err;
|
|
340
|
+
const position = lineNumber != null ? ` (${lineNumber}:${column + columnOffset})` : "";
|
|
341
|
+
return new SyntaxError(`${err.message} in ${describeResource(options)}${position}`, {
|
|
342
|
+
cause: err
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
function createOxcParseError(errors, source, options, offsetUnit = "utf-16") {
|
|
346
|
+
const error = errors.find((e) => e.severity === "Error") ?? errors[0];
|
|
347
|
+
const err = new Error(error.message);
|
|
348
|
+
const start = error.labels[0]?.start;
|
|
349
|
+
if (start != null) {
|
|
350
|
+
const before = offsetUnit === "utf-8" ? Buffer.from(source).subarray(0, start).toString() : source.slice(0, start);
|
|
351
|
+
err.lineNumber = before.split("\n").length;
|
|
352
|
+
err.column = before.length - before.lastIndexOf("\n");
|
|
353
|
+
}
|
|
354
|
+
return createResourceParseError(err, options);
|
|
355
|
+
}
|
|
305
356
|
function excludeLocales({
|
|
306
357
|
messages,
|
|
307
358
|
onlyLocales
|
|
@@ -317,48 +368,38 @@ function excludeLocales({
|
|
|
317
368
|
|
|
318
369
|
class DynamicResourceError extends Error {
|
|
319
370
|
}
|
|
320
|
-
|
|
321
|
-
type
|
|
322
|
-
filename
|
|
323
|
-
inSourceMap
|
|
324
|
-
locale
|
|
325
|
-
isGlobal
|
|
326
|
-
sourceMap
|
|
327
|
-
env
|
|
328
|
-
forceStringify
|
|
329
|
-
onError
|
|
330
|
-
onWarn
|
|
331
|
-
strictMessage
|
|
332
|
-
escapeHtml
|
|
333
|
-
allowDynamic
|
|
334
|
-
jit
|
|
335
|
-
}
|
|
371
|
+
const DEFAULT_OPTIONS = {
|
|
372
|
+
type: "plain",
|
|
373
|
+
filename: "vue-i18n-loader.js",
|
|
374
|
+
inSourceMap: void 0,
|
|
375
|
+
locale: "",
|
|
376
|
+
isGlobal: false,
|
|
377
|
+
sourceMap: false,
|
|
378
|
+
env: "development",
|
|
379
|
+
forceStringify: false,
|
|
380
|
+
onError: void 0,
|
|
381
|
+
onWarn: void 0,
|
|
382
|
+
strictMessage: true,
|
|
383
|
+
escapeHtml: false,
|
|
384
|
+
allowDynamic: false,
|
|
385
|
+
jit: false
|
|
386
|
+
};
|
|
387
|
+
function generate$3(targetSource, options) {
|
|
336
388
|
const value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
337
|
-
const
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
sourceMap,
|
|
341
|
-
locale,
|
|
342
|
-
isGlobal,
|
|
343
|
-
inSourceMap,
|
|
344
|
-
env,
|
|
345
|
-
filename,
|
|
346
|
-
forceStringify,
|
|
347
|
-
onError,
|
|
348
|
-
onWarn,
|
|
349
|
-
strictMessage,
|
|
350
|
-
escapeHtml,
|
|
351
|
-
jit
|
|
352
|
-
};
|
|
353
|
-
const generator = createCodeGenerator(options);
|
|
354
|
-
const ast = parse(value, {
|
|
355
|
-
ecmaVersion: "latest",
|
|
389
|
+
const _options = Object.assign({}, DEFAULT_OPTIONS, options, { source: value });
|
|
390
|
+
const generator = createCodeGenerator(_options);
|
|
391
|
+
const { program: ast, errors } = parseSync(_options.filename ?? "", value, {
|
|
356
392
|
sourceType: "module",
|
|
357
|
-
|
|
358
|
-
|
|
393
|
+
lang: "js",
|
|
394
|
+
// `(value)` is the value itself: `export default ({ ... })` is an object, a parenthesized
|
|
395
|
+
// message is compiled, and escodegen does not know `ParenthesizedExpression`
|
|
396
|
+
preserveParens: false
|
|
359
397
|
});
|
|
398
|
+
if (errors.length > 0) {
|
|
399
|
+
throw createOxcParseError(errors, value, _options);
|
|
400
|
+
}
|
|
360
401
|
const exportResult = scanAst(ast);
|
|
361
|
-
if (!allowDynamic) {
|
|
402
|
+
if (!_options.allowDynamic) {
|
|
362
403
|
if (!exportResult) {
|
|
363
404
|
throw new Error(`You need to define an object as the locale message with 'export default'.`);
|
|
364
405
|
}
|
|
@@ -375,13 +416,13 @@ function generate$3(targetSource, {
|
|
|
375
416
|
return {
|
|
376
417
|
ast,
|
|
377
418
|
code: value,
|
|
378
|
-
map: inSourceMap
|
|
419
|
+
map: _options.inSourceMap
|
|
379
420
|
};
|
|
380
421
|
}
|
|
381
422
|
}
|
|
382
|
-
const codeMaps = _generate$2(generator, ast,
|
|
423
|
+
const codeMaps = _generate$2(generator, ast, _options);
|
|
383
424
|
const { code, map } = generator.context();
|
|
384
|
-
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
425
|
+
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, _options.inSourceMap) || null : null;
|
|
385
426
|
return {
|
|
386
427
|
ast,
|
|
387
428
|
code,
|
|
@@ -422,15 +463,9 @@ function _generate$2(generator, node, options = {}) {
|
|
|
422
463
|
const componentNamespace = "_Component";
|
|
423
464
|
const variableDeclarations = [];
|
|
424
465
|
walk(node, {
|
|
425
|
-
/**
|
|
426
|
-
* NOTE:
|
|
427
|
-
* force cast to Node of `estree-walker@3.x`,
|
|
428
|
-
* because `estree-walker@3.x` is not dual packages,
|
|
429
|
-
* so it's support only esm only ...
|
|
430
|
-
*/
|
|
431
466
|
// @ts-ignore
|
|
432
|
-
enter(node2,
|
|
433
|
-
if (
|
|
467
|
+
enter(node2, parent) {
|
|
468
|
+
if (parent?.type != null) this.skip();
|
|
434
469
|
switch (node2.type) {
|
|
435
470
|
case "ExportDefaultDeclaration":
|
|
436
471
|
this.skip();
|
|
@@ -574,12 +609,6 @@ Variable declarations are not optimized - found ${variableDeclarations.join(", "
|
|
|
574
609
|
break;
|
|
575
610
|
}
|
|
576
611
|
},
|
|
577
|
-
/**
|
|
578
|
-
* NOTE:
|
|
579
|
-
* force cast to Node of `estree-walker@3.x`,
|
|
580
|
-
* because `estree-walker@3.x` is not dual packages,
|
|
581
|
-
* so it's support only esm only ...
|
|
582
|
-
*/
|
|
583
612
|
// @ts-ignore
|
|
584
613
|
leave(node2, parent) {
|
|
585
614
|
switch (node2.type) {
|
|
@@ -660,7 +689,25 @@ function generate$2(targetSource, {
|
|
|
660
689
|
escapeHtml,
|
|
661
690
|
jit
|
|
662
691
|
};
|
|
663
|
-
let ast
|
|
692
|
+
let ast;
|
|
693
|
+
try {
|
|
694
|
+
ast = parseJSON(value, { filePath: filename });
|
|
695
|
+
} catch (err) {
|
|
696
|
+
throw createResourceParseError(err, options);
|
|
697
|
+
}
|
|
698
|
+
const root = ast.body[0].expression;
|
|
699
|
+
if (root.type !== "JSONObjectExpression") {
|
|
700
|
+
const rootValue = getStaticJSONValue(root);
|
|
701
|
+
if (rootValue !== null) {
|
|
702
|
+
throw createResourceError(
|
|
703
|
+
`You need to define an object as the locale messages at the top level, but got ${describeValue(rootValue)}`,
|
|
704
|
+
options
|
|
705
|
+
);
|
|
706
|
+
}
|
|
707
|
+
value = JSON.stringify({});
|
|
708
|
+
ast = parseJSON(value, { filePath: filename });
|
|
709
|
+
options.source = void 0;
|
|
710
|
+
}
|
|
664
711
|
if (!locale && type === "sfc" && onlyLocales?.length) {
|
|
665
712
|
const messages = getStaticJSONValue(ast);
|
|
666
713
|
value = JSON.stringify(
|
|
@@ -700,6 +747,19 @@ function _generate$1(generator, node, options = {}) {
|
|
|
700
747
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
701
748
|
return code;
|
|
702
749
|
}
|
|
750
|
+
function pushExpressionValue(node2) {
|
|
751
|
+
const value = getStaticJSONValue(node2);
|
|
752
|
+
if (isString(value)) {
|
|
753
|
+
generator.push(codegenFn(value), node2, value);
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
const strValue = stringifyValue(value);
|
|
757
|
+
if (forceStringify) {
|
|
758
|
+
generator.push(codegenFn(strValue), node2, strValue);
|
|
759
|
+
} else {
|
|
760
|
+
generator.push(strValue);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
703
763
|
const componentNamespace = "_Component";
|
|
704
764
|
traverseNodes(node, {
|
|
705
765
|
enterNode(node2, parent) {
|
|
@@ -756,6 +816,8 @@ function _generate$1(generator, node, options = {}) {
|
|
|
756
816
|
} else {
|
|
757
817
|
generator.push(strValue2);
|
|
758
818
|
}
|
|
819
|
+
} else if (node2.value.type !== "JSONObjectExpression" && node2.value.type !== "JSONArrayExpression") {
|
|
820
|
+
pushExpressionValue(node2.value);
|
|
759
821
|
}
|
|
760
822
|
break;
|
|
761
823
|
}
|
|
@@ -771,6 +833,14 @@ function _generate$1(generator, node, options = {}) {
|
|
|
771
833
|
generator.push(strValue);
|
|
772
834
|
}
|
|
773
835
|
break;
|
|
836
|
+
case "JSONUnaryExpression":
|
|
837
|
+
case "JSONIdentifier":
|
|
838
|
+
case "JSONTemplateLiteral":
|
|
839
|
+
case "JSONBinaryExpression":
|
|
840
|
+
if (parent.type === "JSONArrayExpression") {
|
|
841
|
+
pushExpressionValue(node2);
|
|
842
|
+
}
|
|
843
|
+
break;
|
|
774
844
|
}
|
|
775
845
|
},
|
|
776
846
|
leaveNode(node2, parent) {
|
|
@@ -816,48 +886,18 @@ function _generate$1(generator, node, options = {}) {
|
|
|
816
886
|
return codeMaps;
|
|
817
887
|
}
|
|
818
888
|
|
|
819
|
-
|
|
820
|
-
type = "plain",
|
|
821
|
-
filename = "vue-i18n-loader.js",
|
|
822
|
-
inSourceMap = void 0,
|
|
823
|
-
locale = "",
|
|
824
|
-
isGlobal = false,
|
|
825
|
-
sourceMap = false,
|
|
826
|
-
env = "development",
|
|
827
|
-
forceStringify = false,
|
|
828
|
-
onError = void 0,
|
|
829
|
-
onWarn = void 0,
|
|
830
|
-
strictMessage = true,
|
|
831
|
-
escapeHtml = false,
|
|
832
|
-
allowDynamic = false,
|
|
833
|
-
jit = false
|
|
834
|
-
}) {
|
|
889
|
+
function generate$1(targetSource, options) {
|
|
835
890
|
let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
836
|
-
const
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
filename,
|
|
845
|
-
forceStringify,
|
|
846
|
-
onError,
|
|
847
|
-
onWarn,
|
|
848
|
-
strictMessage,
|
|
849
|
-
escapeHtml,
|
|
850
|
-
allowDynamic,
|
|
851
|
-
jit
|
|
852
|
-
};
|
|
853
|
-
if (options.filename && /.[c|m]?ts$/.test(options.filename)) {
|
|
854
|
-
const transformed = await transform(value, { loader: "ts" });
|
|
855
|
-
if (transformed && transformed.code) {
|
|
856
|
-
value = transformed.code;
|
|
857
|
-
options.source = transformed.code;
|
|
858
|
-
}
|
|
891
|
+
const _options = Object.assign({}, DEFAULT_OPTIONS, options, { source: value });
|
|
892
|
+
const transformed = transform(_options.filename ?? "", value, { lang: "ts" });
|
|
893
|
+
if (transformed.errors.length > 0) {
|
|
894
|
+
throw createOxcParseError(transformed.errors, value, _options, "utf-8");
|
|
895
|
+
}
|
|
896
|
+
if (transformed && transformed.code) {
|
|
897
|
+
value = transformed.code;
|
|
898
|
+
_options.source = transformed.code;
|
|
859
899
|
}
|
|
860
|
-
return generate$3(value,
|
|
900
|
+
return generate$3(value, _options);
|
|
861
901
|
}
|
|
862
902
|
|
|
863
903
|
function generate(targetSource, {
|
|
@@ -876,6 +916,9 @@ function generate(targetSource, {
|
|
|
876
916
|
jit = false
|
|
877
917
|
}) {
|
|
878
918
|
let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
919
|
+
if (value.charCodeAt(0) === 65279) {
|
|
920
|
+
value = value.slice(1);
|
|
921
|
+
}
|
|
879
922
|
const options = {
|
|
880
923
|
type,
|
|
881
924
|
source: value,
|
|
@@ -891,9 +934,31 @@ function generate(targetSource, {
|
|
|
891
934
|
escapeHtml,
|
|
892
935
|
jit
|
|
893
936
|
};
|
|
894
|
-
let ast
|
|
937
|
+
let ast;
|
|
938
|
+
try {
|
|
939
|
+
ast = parseYAML(value, { filePath: filename });
|
|
940
|
+
} catch (err) {
|
|
941
|
+
throw createResourceParseError(err, options, 1);
|
|
942
|
+
}
|
|
943
|
+
const rootDocument = getRootDocument(ast, options);
|
|
944
|
+
const content = rootDocument?.content;
|
|
945
|
+
const root = content?.type === "YAMLWithMeta" ? content.value : content;
|
|
946
|
+
if (root?.type !== "YAMLMapping") {
|
|
947
|
+
const isAlias = root?.type === "YAMLAlias";
|
|
948
|
+
const rootValue = rootDocument == null || isAlias ? null : getStaticYAMLValue(rootDocument);
|
|
949
|
+
if (isAlias || rootValue !== null) {
|
|
950
|
+
const actual = isAlias ? "an alias" : describeValue(rootValue);
|
|
951
|
+
throw createResourceError(
|
|
952
|
+
`You need to define an object as the locale messages at the top level, but got ${actual}`,
|
|
953
|
+
options
|
|
954
|
+
);
|
|
955
|
+
}
|
|
956
|
+
value = JSON.stringify({});
|
|
957
|
+
ast = parseYAML(value, { filePath: filename });
|
|
958
|
+
options.source = void 0;
|
|
959
|
+
}
|
|
895
960
|
if (!locale && type === "sfc" && onlyLocales?.length) {
|
|
896
|
-
const messages = getStaticYAMLValue(ast);
|
|
961
|
+
const messages = getStaticYAMLValue(getRootDocument(ast, options));
|
|
897
962
|
value = JSON.stringify(
|
|
898
963
|
excludeLocales({
|
|
899
964
|
messages,
|
|
@@ -918,6 +983,16 @@ function generate(targetSource, {
|
|
|
918
983
|
map: newMap != null ? newMap : void 0
|
|
919
984
|
};
|
|
920
985
|
}
|
|
986
|
+
function getRootDocument(ast, options) {
|
|
987
|
+
const documents = ast.body.filter((doc) => doc.content != null);
|
|
988
|
+
if (documents.length > 1) {
|
|
989
|
+
throw createResourceError(
|
|
990
|
+
`You need to define the locale messages in a single YAML document, but got ${documents.length} documents`,
|
|
991
|
+
options
|
|
992
|
+
);
|
|
993
|
+
}
|
|
994
|
+
return documents[0] ?? null;
|
|
995
|
+
}
|
|
921
996
|
function _generate(generator, node, options = {}) {
|
|
922
997
|
const propsCountStack = [];
|
|
923
998
|
const pathStack = [];
|
|
@@ -978,7 +1053,7 @@ function _generate(generator, node, options = {}) {
|
|
|
978
1053
|
const name = node2.key.value;
|
|
979
1054
|
const value = node2.value.value;
|
|
980
1055
|
const strName = JSON.stringify(name);
|
|
981
|
-
const strValue =
|
|
1056
|
+
const strValue = stringifyValue(value);
|
|
982
1057
|
name && pathStack.push(name.toString());
|
|
983
1058
|
generator.push(`${strName}: `);
|
|
984
1059
|
if (isString(value)) {
|
|
@@ -997,7 +1072,7 @@ function _generate(generator, node, options = {}) {
|
|
|
997
1072
|
case "YAMLScalar":
|
|
998
1073
|
if (parent.type === "YAMLSequence") {
|
|
999
1074
|
const value = node2.value;
|
|
1000
|
-
const strValue =
|
|
1075
|
+
const strValue = stringifyValue(value);
|
|
1001
1076
|
if (isString(value)) {
|
|
1002
1077
|
generator.push(codegenFn(value), node2, value);
|
|
1003
1078
|
} else if (forceStringify) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlify/bundle-utils",
|
|
3
3
|
"description": "Bundle utilities for Intlify project",
|
|
4
|
-
"version": "12.0.0-alpha.
|
|
4
|
+
"version": "12.0.0-alpha.4",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -20,21 +20,20 @@
|
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@intlify/message-compiler": "next",
|
|
22
22
|
"@intlify/shared": "next",
|
|
23
|
-
"acorn": "^8.8.2",
|
|
24
|
-
"esbuild": "^0.25.1",
|
|
25
23
|
"escodegen": "^2.1.0",
|
|
26
|
-
"estree-walker": "^2.0.2",
|
|
27
24
|
"jsonc-eslint-parser": "^2.3.0",
|
|
25
|
+
"oxc-parser": "^0.131.0",
|
|
26
|
+
"oxc-transform": "^0.96.0",
|
|
27
|
+
"oxc-walker": "^0.7.0",
|
|
28
28
|
"source-map-js": "^1.0.1",
|
|
29
29
|
"yaml-eslint-parser": "^1.2.2"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/escodegen": "^0.0.10",
|
|
33
|
-
"
|
|
34
|
-
"unbuild": "^3.5.0"
|
|
33
|
+
"unbuild": "^3.6.1"
|
|
35
34
|
},
|
|
36
35
|
"engines": {
|
|
37
|
-
"node": ">=
|
|
36
|
+
"node": ">= 22.13"
|
|
38
37
|
},
|
|
39
38
|
"files": [
|
|
40
39
|
"lib"
|