@intlify/bundle-utils 5.0.1 → 5.1.1

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 ADDED
@@ -0,0 +1,1036 @@
1
+ 'use strict';
2
+
3
+ const jsoncEslintParser = require('jsonc-eslint-parser');
4
+ const shared = require('@intlify/shared');
5
+ const messageCompiler = require('@intlify/message-compiler');
6
+ const sourceMap = require('source-map');
7
+ const yamlEslintParser = require('yaml-eslint-parser');
8
+ const acorn = require('acorn');
9
+ const estreeWalker = require('estree-walker');
10
+ const esquery = require('esquery');
11
+
12
+ function createCodeGenerator(options = {
13
+ filename: "bundle.json",
14
+ sourceMap: false,
15
+ env: "development",
16
+ forceStringify: false
17
+ }) {
18
+ const { sourceMap: sourceMap$1, source, filename } = options;
19
+ const _context = Object.assign(
20
+ {
21
+ code: "",
22
+ column: 1,
23
+ line: 1,
24
+ offset: 0,
25
+ map: void 0,
26
+ indentLevel: 0
27
+ },
28
+ options
29
+ );
30
+ const context = () => _context;
31
+ function push(code, node, name) {
32
+ _context.code += code;
33
+ if (_context.map && node) {
34
+ if (node.loc && node.loc !== messageCompiler.LocationStub) {
35
+ addMapping(node.loc.start, name);
36
+ }
37
+ advancePositionWithSource(_context, code);
38
+ }
39
+ }
40
+ function _newline(n) {
41
+ push("\n" + ` `.repeat(n));
42
+ }
43
+ function indent(withNewLine = true) {
44
+ const level = ++_context.indentLevel;
45
+ withNewLine && _newline(level);
46
+ }
47
+ function deindent(withNewLine = true) {
48
+ const level = --_context.indentLevel;
49
+ withNewLine && _newline(level);
50
+ }
51
+ function newline() {
52
+ _newline(_context.indentLevel);
53
+ }
54
+ function pushline(code, node, name) {
55
+ push(code, node, name);
56
+ newline();
57
+ }
58
+ function addMapping(loc, name) {
59
+ _context.map.addMapping({
60
+ name,
61
+ source: _context.filename,
62
+ original: {
63
+ line: loc.line,
64
+ column: loc.column - 1
65
+ },
66
+ generated: {
67
+ line: _context.line,
68
+ column: _context.column - 1
69
+ }
70
+ });
71
+ }
72
+ if (sourceMap$1 && source) {
73
+ _context.map = new sourceMap.SourceMapGenerator();
74
+ _context.map.setSourceContent(filename, source);
75
+ }
76
+ return {
77
+ context,
78
+ push,
79
+ indent,
80
+ deindent,
81
+ newline,
82
+ pushline
83
+ };
84
+ }
85
+ function advancePositionWithSource(pos, source, numberOfCharacters = source.length) {
86
+ if (pos.offset == null) {
87
+ return pos;
88
+ }
89
+ let linesCount = 0;
90
+ let lastNewLinePos = -1;
91
+ for (let i = 0; i < numberOfCharacters; i++) {
92
+ if (source.charCodeAt(i) === 10) {
93
+ linesCount++;
94
+ lastNewLinePos = i;
95
+ }
96
+ }
97
+ pos.offset += numberOfCharacters;
98
+ pos.line += linesCount;
99
+ pos.column = lastNewLinePos === -1 ? pos.column + numberOfCharacters : numberOfCharacters - lastNewLinePos;
100
+ return pos;
101
+ }
102
+ function generateMessageFunction(msg, options, path) {
103
+ const env = options.env != null ? options.env : "development";
104
+ const onError = options.onError;
105
+ const errors = [];
106
+ const newOptions = Object.assign({ mode: "arrow" }, options);
107
+ newOptions.onError = (err) => {
108
+ if (onError) {
109
+ const extra = {
110
+ source: msg,
111
+ path: path ? path.join(".") : "",
112
+ code: err.code,
113
+ domain: err.domain,
114
+ location: err.location
115
+ };
116
+ onError(err.message, extra);
117
+ errors.push(err);
118
+ }
119
+ };
120
+ const { code, ast, map } = messageCompiler.baseCompile(msg, newOptions);
121
+ const occured = errors.length > 0;
122
+ const genCode = !occured ? env === "development" ? `(()=>{const fn=${code};fn.source=${JSON.stringify(msg)};return fn;})()` : `${code}` : msg;
123
+ return { code: genCode, ast, map };
124
+ }
125
+ function mapLinesColumns(resMap, codeMaps, inSourceMap) {
126
+ if (!resMap) {
127
+ return null;
128
+ }
129
+ const resMapConsumer = new sourceMap.SourceMapConsumer(resMap);
130
+ const inMapConsumer = inSourceMap ? new sourceMap.SourceMapConsumer(inSourceMap) : null;
131
+ const mergedMapGenerator = new sourceMap.SourceMapGenerator();
132
+ let inMapFirstItem = null;
133
+ if (inMapConsumer) {
134
+ inMapConsumer.eachMapping((m) => {
135
+ if (inMapFirstItem) {
136
+ return;
137
+ }
138
+ inMapFirstItem = m;
139
+ });
140
+ }
141
+ resMapConsumer.eachMapping((res) => {
142
+ if (res.originalLine == null) {
143
+ return;
144
+ }
145
+ const map = codeMaps.get(res.name);
146
+ if (!map) {
147
+ return;
148
+ }
149
+ let inMapOrigin = null;
150
+ if (inMapConsumer) {
151
+ inMapOrigin = inMapConsumer.originalPositionFor({
152
+ line: res.originalLine,
153
+ column: res.originalColumn - 1
154
+ });
155
+ if (inMapOrigin.source == null) {
156
+ inMapOrigin = null;
157
+ return;
158
+ }
159
+ }
160
+ const mapConsumer = new sourceMap.SourceMapConsumer(map);
161
+ mapConsumer.eachMapping((m) => {
162
+ mergedMapGenerator.addMapping({
163
+ original: {
164
+ line: inMapFirstItem ? inMapFirstItem.originalLine + res.originalLine - 2 : res.originalLine,
165
+ column: inMapFirstItem ? inMapFirstItem.originalColumn + res.originalColumn : res.originalColumn
166
+ },
167
+ generated: {
168
+ line: inMapFirstItem ? inMapFirstItem.generatedLine + res.originalLine - 2 : res.originalLine,
169
+ // map column with message format compilation code map
170
+ column: inMapFirstItem ? inMapFirstItem.generatedColumn + res.originalColumn + m.generatedColumn : res.originalColumn + m.generatedColumn
171
+ },
172
+ source: inMapOrigin ? inMapOrigin.source : res.source,
173
+ name: m.name
174
+ // message format compilation code
175
+ });
176
+ });
177
+ });
178
+ const generator = mergedMapGenerator;
179
+ const targetConsumer = inMapConsumer || resMapConsumer;
180
+ targetConsumer.sources.forEach((sourceFile) => {
181
+ generator._sources.add(sourceFile);
182
+ const sourceContent = targetConsumer.sourceContentFor(sourceFile);
183
+ if (sourceContent != null) {
184
+ mergedMapGenerator.setSourceContent(sourceFile, sourceContent);
185
+ }
186
+ });
187
+ generator._sourceRoot = inSourceMap ? inSourceMap.sourceRoot : resMap.sourceRoot;
188
+ generator._file = inSourceMap ? inSourceMap.file : resMap.file;
189
+ return generator.toJSON();
190
+ }
191
+
192
+ function generate$2(targetSource, {
193
+ type = "plain",
194
+ bridge = false,
195
+ exportESM = false,
196
+ filename = "vue-i18n-loader.json",
197
+ inSourceMap = void 0,
198
+ locale = "",
199
+ isGlobal = false,
200
+ sourceMap = false,
201
+ env = "development",
202
+ forceStringify = false,
203
+ onError = void 0,
204
+ useClassComponent = false
205
+ }, injector) {
206
+ const target = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
207
+ const value = target;
208
+ const options = {
209
+ type,
210
+ bridge,
211
+ exportESM,
212
+ source: value,
213
+ sourceMap,
214
+ locale,
215
+ isGlobal,
216
+ inSourceMap,
217
+ env,
218
+ filename,
219
+ forceStringify,
220
+ onError,
221
+ useClassComponent
222
+ };
223
+ const generator = createCodeGenerator(options);
224
+ const ast = jsoncEslintParser.parseJSON(value, { filePath: filename });
225
+ const codeMaps = generateNode$2(generator, ast, options, injector);
226
+ const { code, map } = generator.context();
227
+ const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
228
+ return {
229
+ ast,
230
+ code,
231
+ map: newMap != null ? newMap : void 0
232
+ };
233
+ }
234
+ function generateNode$2(generator, node, options, injector) {
235
+ const propsCountStack = [];
236
+ const pathStack = [];
237
+ const itemsCountStack = [];
238
+ const { forceStringify } = generator.context();
239
+ const codeMaps = /* @__PURE__ */ new Map();
240
+ const {
241
+ type,
242
+ bridge,
243
+ exportESM,
244
+ sourceMap,
245
+ isGlobal,
246
+ locale,
247
+ useClassComponent
248
+ } = options;
249
+ const componentNamespace = bridge ? `Component.options` : useClassComponent ? `Component.__o` : `Component`;
250
+ jsoncEslintParser.traverseNodes(node, {
251
+ enterNode(node2, parent) {
252
+ switch (node2.type) {
253
+ case "Program":
254
+ if (type === "plain") {
255
+ generator.push(`export default `);
256
+ } else if (type === "sfc") {
257
+ const variableName = type === "sfc" ? !isGlobal ? "__i18n" : "__i18nGlobal" : "";
258
+ const localeName = type === "sfc" ? locale != null ? locale : `""` : "";
259
+ const exportSyntax = bridge ? exportESM ? `export default` : `module.exports =` : `export default`;
260
+ generator.push(`${exportSyntax} function (Component) {`);
261
+ generator.indent();
262
+ generator.pushline(
263
+ `${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
264
+ );
265
+ generator.push(`${componentNamespace}.${variableName}.push({`);
266
+ generator.indent();
267
+ generator.pushline(`"locale": ${JSON.stringify(localeName)},`);
268
+ generator.push(`"resource": `);
269
+ }
270
+ break;
271
+ case "JSONObjectExpression":
272
+ generator.push(`{`);
273
+ generator.indent();
274
+ propsCountStack.push(node2.properties.length);
275
+ if (parent.type === "JSONArrayExpression") {
276
+ const lastIndex2 = itemsCountStack.length - 1;
277
+ const currentCount = parent.elements.length - itemsCountStack[lastIndex2];
278
+ pathStack.push(currentCount.toString());
279
+ itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
280
+ }
281
+ break;
282
+ case "JSONProperty":
283
+ if (node2.value.type === "JSONLiteral" && (node2.key.type === "JSONLiteral" || node2.key.type === "JSONIdentifier")) {
284
+ const name = node2.key.type === "JSONLiteral" ? node2.key.value : node2.key.name;
285
+ const value = node2.value.value;
286
+ if (shared.isString(value)) {
287
+ generator.push(`${JSON.stringify(name)}: `);
288
+ pathStack.push(name.toString());
289
+ const { code, map } = generateMessageFunction(
290
+ value,
291
+ options,
292
+ pathStack
293
+ );
294
+ sourceMap && map != null && codeMaps.set(value, map);
295
+ generator.push(`${code}`, node2.value, value);
296
+ } else {
297
+ if (forceStringify) {
298
+ const strValue = JSON.stringify(value);
299
+ generator.push(`${JSON.stringify(name)}: `);
300
+ pathStack.push(name.toString());
301
+ const { code, map } = generateMessageFunction(
302
+ strValue,
303
+ options,
304
+ pathStack
305
+ );
306
+ sourceMap && map != null && codeMaps.set(strValue, map);
307
+ generator.push(`${code}`, node2.value, strValue);
308
+ } else {
309
+ generator.push(
310
+ `${JSON.stringify(name)}: ${JSON.stringify(value)}`
311
+ );
312
+ pathStack.push(name.toString());
313
+ }
314
+ }
315
+ } else if ((node2.value.type === "JSONObjectExpression" || node2.value.type === "JSONArrayExpression") && (node2.key.type === "JSONLiteral" || node2.key.type === "JSONIdentifier")) {
316
+ const name = node2.key.type === "JSONLiteral" ? node2.key.value : node2.key.name;
317
+ generator.push(`${JSON.stringify(name)}: `);
318
+ pathStack.push(name.toString());
319
+ }
320
+ const lastIndex = propsCountStack.length - 1;
321
+ propsCountStack[lastIndex] = --propsCountStack[lastIndex];
322
+ break;
323
+ case "JSONArrayExpression":
324
+ generator.push(`[`);
325
+ generator.indent();
326
+ if (parent.type === "JSONArrayExpression") {
327
+ const lastIndex2 = itemsCountStack.length - 1;
328
+ const currentCount = parent.elements.length - itemsCountStack[lastIndex2];
329
+ pathStack.push(currentCount.toString());
330
+ itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
331
+ }
332
+ itemsCountStack.push(node2.elements.length);
333
+ break;
334
+ case "JSONLiteral":
335
+ if (parent.type === "JSONArrayExpression") {
336
+ const lastIndex2 = itemsCountStack.length - 1;
337
+ const currentCount = parent.elements.length - itemsCountStack[lastIndex2];
338
+ pathStack.push(currentCount.toString());
339
+ if (node2.type === "JSONLiteral") {
340
+ const value = node2.value;
341
+ if (shared.isString(value)) {
342
+ const { code, map } = generateMessageFunction(
343
+ value,
344
+ options,
345
+ pathStack
346
+ );
347
+ sourceMap && map != null && codeMaps.set(value, map);
348
+ generator.push(`${code}`, node2, value);
349
+ } else {
350
+ if (forceStringify) {
351
+ const strValue = JSON.stringify(value);
352
+ const { code, map } = generateMessageFunction(
353
+ strValue,
354
+ options,
355
+ pathStack
356
+ );
357
+ sourceMap && map != null && codeMaps.set(strValue, map);
358
+ generator.push(`${code}`, node2, strValue);
359
+ } else {
360
+ generator.push(`${JSON.stringify(value)}`);
361
+ }
362
+ }
363
+ }
364
+ itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
365
+ }
366
+ break;
367
+ }
368
+ },
369
+ leaveNode(node2, parent) {
370
+ switch (node2.type) {
371
+ case "Program":
372
+ if (type === "sfc") {
373
+ generator.deindent();
374
+ generator.push(`})`);
375
+ if (bridge && injector) {
376
+ generator.newline();
377
+ generator.pushline(
378
+ `${componentNamespace}.__i18nBridge = ${componentNamespace}.__i18nBridge || []`
379
+ );
380
+ generator.pushline(
381
+ `${componentNamespace}.__i18nBridge.push('${injector()}')`
382
+ );
383
+ generator.pushline(`delete ${componentNamespace}._Ctor`);
384
+ }
385
+ generator.deindent();
386
+ generator.pushline(`}`);
387
+ }
388
+ break;
389
+ case "JSONObjectExpression":
390
+ if (propsCountStack[propsCountStack.length - 1] === 0) {
391
+ pathStack.pop();
392
+ propsCountStack.pop();
393
+ }
394
+ generator.deindent();
395
+ generator.push(`}`);
396
+ if (parent.type === "JSONArrayExpression") {
397
+ if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
398
+ pathStack.pop();
399
+ generator.pushline(`,`);
400
+ }
401
+ }
402
+ break;
403
+ case "JSONProperty":
404
+ if (propsCountStack[propsCountStack.length - 1] !== 0) {
405
+ pathStack.pop();
406
+ generator.pushline(`,`);
407
+ }
408
+ break;
409
+ case "JSONArrayExpression":
410
+ if (itemsCountStack[itemsCountStack.length - 1] === 0) {
411
+ pathStack.pop();
412
+ itemsCountStack.pop();
413
+ }
414
+ generator.deindent();
415
+ generator.push(`]`);
416
+ if (parent.type === "JSONArrayExpression") {
417
+ if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
418
+ pathStack.pop();
419
+ generator.pushline(`,`);
420
+ }
421
+ }
422
+ break;
423
+ case "JSONLiteral":
424
+ if (parent.type === "JSONArrayExpression") {
425
+ if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
426
+ pathStack.pop();
427
+ generator.pushline(`,`);
428
+ } else {
429
+ generator.pushline(`,`);
430
+ }
431
+ }
432
+ break;
433
+ }
434
+ }
435
+ });
436
+ return codeMaps;
437
+ }
438
+
439
+ function generate$1(targetSource, {
440
+ type = "plain",
441
+ bridge = false,
442
+ exportESM = false,
443
+ useClassComponent = false,
444
+ filename = "vue-i18n-loader.yaml",
445
+ inSourceMap = void 0,
446
+ locale = "",
447
+ isGlobal = false,
448
+ sourceMap = false,
449
+ env = "development",
450
+ forceStringify = false,
451
+ onError = void 0
452
+ }, injector) {
453
+ const value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
454
+ const options = {
455
+ type,
456
+ bridge,
457
+ exportESM,
458
+ source: value,
459
+ sourceMap,
460
+ locale,
461
+ isGlobal,
462
+ inSourceMap,
463
+ env,
464
+ filename,
465
+ forceStringify,
466
+ onError,
467
+ useClassComponent
468
+ };
469
+ const generator = createCodeGenerator(options);
470
+ const ast = yamlEslintParser.parseYAML(value, { filePath: filename });
471
+ const codeMaps = generateNode$1(generator, ast, options, injector);
472
+ const { code, map } = generator.context();
473
+ const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
474
+ return {
475
+ ast,
476
+ code,
477
+ map: newMap != null ? newMap : void 0
478
+ };
479
+ }
480
+ function generateNode$1(generator, node, options, injector) {
481
+ const propsCountStack = [];
482
+ const pathStack = [];
483
+ const itemsCountStack = [];
484
+ const { forceStringify } = generator.context();
485
+ const codeMaps = /* @__PURE__ */ new Map();
486
+ const {
487
+ type,
488
+ bridge,
489
+ exportESM,
490
+ sourceMap,
491
+ isGlobal,
492
+ locale,
493
+ useClassComponent
494
+ } = options;
495
+ const componentNamespace = bridge ? `Component.options` : useClassComponent ? `Component.__o` : `Component`;
496
+ yamlEslintParser.traverseNodes(node, {
497
+ enterNode(node2, parent) {
498
+ switch (node2.type) {
499
+ case "Program":
500
+ if (type === "plain") {
501
+ generator.push(`export default `);
502
+ } else if (type === "sfc") {
503
+ const variableName = type === "sfc" ? !isGlobal ? "__i18n" : "__i18nGlobal" : "";
504
+ const localeName = type === "sfc" ? locale != null ? locale : `""` : "";
505
+ const exportSyntax = bridge ? exportESM ? `export default` : `module.exports =` : `export default`;
506
+ generator.push(`${exportSyntax} function (Component) {`);
507
+ generator.indent();
508
+ generator.pushline(
509
+ `${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
510
+ );
511
+ generator.push(`${componentNamespace}.${variableName}.push({`);
512
+ generator.indent();
513
+ generator.pushline(`"locale": ${JSON.stringify(localeName)},`);
514
+ generator.push(`"resource": `);
515
+ }
516
+ break;
517
+ case "YAMLMapping":
518
+ generator.push(`{`);
519
+ generator.indent();
520
+ propsCountStack.push(node2.pairs.length);
521
+ if (parent.type === "YAMLSequence") {
522
+ const lastIndex2 = itemsCountStack.length - 1;
523
+ const currentCount = parent.entries.length - itemsCountStack[lastIndex2];
524
+ pathStack.push(currentCount.toString());
525
+ itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
526
+ }
527
+ break;
528
+ case "YAMLPair":
529
+ if (node2.value && node2.value.type === "YAMLScalar" && node2.key && node2.key.type === "YAMLScalar") {
530
+ const name = node2.key.value;
531
+ const value = node2.value.value;
532
+ if (shared.isString(value)) {
533
+ generator.push(`${JSON.stringify(name)}: `);
534
+ name && pathStack.push(name.toString());
535
+ const { code, map } = generateMessageFunction(
536
+ value,
537
+ options,
538
+ pathStack
539
+ );
540
+ sourceMap && map != null && codeMaps.set(value, map);
541
+ generator.push(`${code}`, node2.value, value);
542
+ } else {
543
+ if (forceStringify) {
544
+ const strValue = JSON.stringify(value);
545
+ generator.push(`${JSON.stringify(name)}: `);
546
+ name && pathStack.push(name.toString());
547
+ const { code, map } = generateMessageFunction(
548
+ strValue,
549
+ options,
550
+ pathStack
551
+ );
552
+ sourceMap && map != null && codeMaps.set(strValue, map);
553
+ generator.push(`${code}`, node2.value, strValue);
554
+ } else {
555
+ generator.push(
556
+ `${JSON.stringify(name)}: ${JSON.stringify(value)}`
557
+ );
558
+ name && pathStack.push(name.toString());
559
+ }
560
+ }
561
+ } else if (node2.value && (node2.value.type === "YAMLMapping" || node2.value.type === "YAMLSequence") && node2.key && node2.key.type === "YAMLScalar") {
562
+ const name = node2.key.value;
563
+ generator.push(`${JSON.stringify(name)}: `);
564
+ name && pathStack.push(name.toString());
565
+ }
566
+ const lastIndex = propsCountStack.length - 1;
567
+ propsCountStack[lastIndex] = --propsCountStack[lastIndex];
568
+ break;
569
+ case "YAMLSequence":
570
+ generator.push(`[`);
571
+ generator.indent();
572
+ if (parent.type === "YAMLSequence") {
573
+ const lastIndex2 = itemsCountStack.length - 1;
574
+ const currentCount = parent.entries.length - itemsCountStack[lastIndex2];
575
+ pathStack.push(currentCount.toString());
576
+ itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
577
+ }
578
+ itemsCountStack.push(node2.entries.length);
579
+ break;
580
+ case "YAMLScalar":
581
+ if (parent.type === "YAMLSequence") {
582
+ const lastIndex2 = itemsCountStack.length - 1;
583
+ const currentCount = parent.entries.length - itemsCountStack[lastIndex2];
584
+ pathStack.push(currentCount.toString());
585
+ if (node2.type === "YAMLScalar") {
586
+ const value = node2.value;
587
+ if (shared.isString(value)) {
588
+ const { code, map } = generateMessageFunction(
589
+ value,
590
+ options,
591
+ pathStack
592
+ );
593
+ sourceMap && map != null && codeMaps.set(value, map);
594
+ generator.push(`${code}`, node2, value);
595
+ } else {
596
+ if (forceStringify) {
597
+ const strValue = JSON.stringify(value);
598
+ const { code, map } = generateMessageFunction(
599
+ strValue,
600
+ options,
601
+ pathStack
602
+ );
603
+ sourceMap && map != null && codeMaps.set(strValue, map);
604
+ generator.push(`${code}`, node2, strValue);
605
+ } else {
606
+ generator.push(`${JSON.stringify(value)}`);
607
+ }
608
+ }
609
+ }
610
+ itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
611
+ }
612
+ break;
613
+ }
614
+ },
615
+ leaveNode(node2, parent) {
616
+ switch (node2.type) {
617
+ case "Program":
618
+ if (type === "sfc") {
619
+ generator.deindent();
620
+ generator.push(`})`);
621
+ if (bridge && injector) {
622
+ generator.newline();
623
+ generator.pushline(
624
+ `${componentNamespace}.__i18nBridge = ${componentNamespace}.__i18nBridge || []`
625
+ );
626
+ generator.pushline(
627
+ `${componentNamespace}.__i18nBridge.push('${injector()}')`
628
+ );
629
+ generator.pushline(`delete ${componentNamespace}._Ctor`);
630
+ }
631
+ generator.deindent();
632
+ generator.push(`}`);
633
+ }
634
+ break;
635
+ case "YAMLMapping":
636
+ if (propsCountStack[propsCountStack.length - 1] === 0) {
637
+ pathStack.pop();
638
+ propsCountStack.pop();
639
+ }
640
+ generator.deindent();
641
+ generator.push(`}`);
642
+ if (parent.type === "YAMLSequence") {
643
+ if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
644
+ pathStack.pop();
645
+ generator.pushline(`,`);
646
+ }
647
+ }
648
+ break;
649
+ case "YAMLPair":
650
+ if (propsCountStack[propsCountStack.length - 1] !== 0) {
651
+ pathStack.pop();
652
+ generator.pushline(`,`);
653
+ }
654
+ break;
655
+ case "YAMLSequence":
656
+ if (itemsCountStack[itemsCountStack.length - 1] === 0) {
657
+ pathStack.pop();
658
+ itemsCountStack.pop();
659
+ }
660
+ generator.deindent();
661
+ generator.push(`]`);
662
+ if (parent.type === "YAMLSequence") {
663
+ if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
664
+ pathStack.pop();
665
+ generator.pushline(`,`);
666
+ }
667
+ }
668
+ break;
669
+ case "YAMLScalar":
670
+ if (parent.type === "YAMLSequence") {
671
+ if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
672
+ pathStack.pop();
673
+ generator.pushline(`,`);
674
+ } else {
675
+ generator.pushline(`,`);
676
+ }
677
+ }
678
+ break;
679
+ }
680
+ }
681
+ });
682
+ return codeMaps;
683
+ }
684
+
685
+ function generate(targetSource, {
686
+ type = "plain",
687
+ bridge = false,
688
+ exportESM = false,
689
+ filename = "vue-i18n-loader.js",
690
+ inSourceMap = void 0,
691
+ locale = "",
692
+ isGlobal = false,
693
+ sourceMap = false,
694
+ env = "development",
695
+ forceStringify = false,
696
+ onError = void 0,
697
+ useClassComponent = false
698
+ }, injector) {
699
+ const target = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
700
+ const value = target;
701
+ const options = {
702
+ type,
703
+ bridge,
704
+ exportESM,
705
+ source: value,
706
+ sourceMap,
707
+ locale,
708
+ isGlobal,
709
+ inSourceMap,
710
+ env,
711
+ filename,
712
+ forceStringify,
713
+ onError,
714
+ useClassComponent
715
+ };
716
+ const generator = createCodeGenerator(options);
717
+ const ast = acorn.parse(value, {
718
+ ecmaVersion: "latest",
719
+ sourceType: "module",
720
+ sourceFile: filename,
721
+ allowImportExportEverywhere: true
722
+ });
723
+ const selectedWithExportDefault = esquery(
724
+ ast,
725
+ "Program:has(ExportDefaultDeclaration, [declaration=ObjectExpression])"
726
+ );
727
+ if (!selectedWithExportDefault.length) {
728
+ throw new Error(
729
+ `You need to define an object as the locale message with "export default'.`
730
+ );
731
+ }
732
+ const codeMaps = generateNode(generator, ast, options, injector);
733
+ const { code, map } = generator.context();
734
+ const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
735
+ return {
736
+ ast,
737
+ code,
738
+ map: newMap != null ? newMap : void 0
739
+ };
740
+ }
741
+ function generateNode(generator, node, options, injector) {
742
+ const propsCountStack = [];
743
+ const pathStack = [];
744
+ const itemsCountStack = [];
745
+ const skipStack = [];
746
+ const { forceStringify } = generator.context();
747
+ const codeMaps = /* @__PURE__ */ new Map();
748
+ const {
749
+ type,
750
+ bridge,
751
+ exportESM,
752
+ sourceMap,
753
+ isGlobal,
754
+ locale,
755
+ useClassComponent
756
+ } = options;
757
+ const componentNamespace = bridge ? `Component.options` : useClassComponent ? `Component.__o` : `Component`;
758
+ estreeWalker.walk(node, {
759
+ enter(node2, parent) {
760
+ switch (node2.type) {
761
+ case "Program":
762
+ if (type === "plain") {
763
+ generator.push(`export default `);
764
+ } else if (type === "sfc") {
765
+ const variableName = type === "sfc" ? !isGlobal ? "__i18n" : "__i18nGlobal" : "";
766
+ const localeName = type === "sfc" ? locale != null ? locale : `""` : "";
767
+ const exportSyntax = bridge ? exportESM ? `export default` : `module.exports =` : `export default`;
768
+ generator.push(`${exportSyntax} function (Component) {`);
769
+ generator.indent();
770
+ generator.pushline(
771
+ `${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
772
+ );
773
+ generator.push(`${componentNamespace}.${variableName}.push({`);
774
+ generator.indent();
775
+ generator.pushline(`"locale": ${JSON.stringify(localeName)},`);
776
+ generator.push(`"resource": `);
777
+ }
778
+ break;
779
+ case "ObjectExpression":
780
+ generator.push(`{`);
781
+ generator.indent();
782
+ propsCountStack.push(node2.properties.length);
783
+ if (parent != null && parent.type === "ArrayExpression") {
784
+ const lastIndex2 = itemsCountStack.length - 1;
785
+ const currentCount = parent.elements.length - itemsCountStack[lastIndex2];
786
+ pathStack.push(currentCount.toString());
787
+ itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
788
+ }
789
+ break;
790
+ case "Property":
791
+ if (node2 != null) {
792
+ if (isJSONablePrimitiveLiteral(node2.value) && (node2.key.type === "Literal" || node2.key.type === "Identifier")) {
793
+ const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
794
+ if (node2.value.type === "Literal" && shared.isString(node2.value.value) || node2.value.type === "TemplateLiteral") {
795
+ const value = getValue(node2.value);
796
+ generator.push(`${JSON.stringify(name)}: `);
797
+ pathStack.push(name);
798
+ const { code, map } = generateMessageFunction(
799
+ value,
800
+ options,
801
+ pathStack
802
+ );
803
+ sourceMap && map != null && codeMaps.set(value, map);
804
+ generator.push(`${code}`, node2.value, value);
805
+ skipStack.push(false);
806
+ } else {
807
+ const value = getValue(node2.value);
808
+ if (forceStringify) {
809
+ const strValue = JSON.stringify(value);
810
+ generator.push(`${JSON.stringify(name)}: `);
811
+ pathStack.push(name);
812
+ const { code, map } = generateMessageFunction(
813
+ strValue,
814
+ options,
815
+ pathStack
816
+ );
817
+ sourceMap && map != null && codeMaps.set(strValue, map);
818
+ generator.push(`${code}`, node2.value, strValue);
819
+ } else {
820
+ generator.push(
821
+ `${JSON.stringify(name)}: ${JSON.stringify(value)}`
822
+ );
823
+ pathStack.push(name);
824
+ }
825
+ skipStack.push(false);
826
+ }
827
+ } else if ((node2.value.type === "ObjectExpression" || node2.value.type === "ArrayExpression") && (node2.key.type === "Literal" || node2.key.type === "Identifier")) {
828
+ const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
829
+ generator.push(`${JSON.stringify(name)}: `);
830
+ pathStack.push(name);
831
+ } else {
832
+ skipStack.push(true);
833
+ }
834
+ }
835
+ const lastIndex = propsCountStack.length - 1;
836
+ propsCountStack[lastIndex] = --propsCountStack[lastIndex];
837
+ break;
838
+ case "ArrayExpression":
839
+ generator.push(`[`);
840
+ generator.indent();
841
+ if (parent != null && parent.type === "ArrayExpression") {
842
+ const lastIndex2 = itemsCountStack.length - 1;
843
+ const currentCount = parent.elements.length - itemsCountStack[lastIndex2];
844
+ pathStack.push(currentCount.toString());
845
+ itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
846
+ }
847
+ itemsCountStack.push(node2.elements.length);
848
+ break;
849
+ default:
850
+ if (node2 != null && parent != null) {
851
+ if (parent.type === "ArrayExpression") {
852
+ const lastIndex2 = itemsCountStack.length - 1;
853
+ const currentCount = parent.elements.length - itemsCountStack[lastIndex2];
854
+ pathStack.push(currentCount.toString());
855
+ if (isJSONablePrimitiveLiteral(node2)) {
856
+ if (node2.type === "Literal" && shared.isString(node2.value) || node2.type === "TemplateLiteral") {
857
+ const value = getValue(node2);
858
+ const { code, map } = generateMessageFunction(
859
+ value,
860
+ options,
861
+ pathStack
862
+ );
863
+ sourceMap && map != null && codeMaps.set(value, map);
864
+ generator.push(`${code}`, node2, value);
865
+ } else {
866
+ const value = getValue(node2);
867
+ if (forceStringify) {
868
+ const strValue = JSON.stringify(value);
869
+ const { code, map } = generateMessageFunction(
870
+ strValue,
871
+ options,
872
+ pathStack
873
+ );
874
+ sourceMap && map != null && codeMaps.set(strValue, map);
875
+ generator.push(`${code}`, node2, strValue);
876
+ } else {
877
+ generator.push(`${JSON.stringify(value)}`);
878
+ }
879
+ }
880
+ skipStack.push(false);
881
+ } else {
882
+ skipStack.push(true);
883
+ }
884
+ itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
885
+ }
886
+ }
887
+ break;
888
+ }
889
+ },
890
+ leave(node2, parent) {
891
+ switch (node2.type) {
892
+ case "Program":
893
+ if (type === "sfc") {
894
+ generator.deindent();
895
+ generator.push(`})`);
896
+ if (bridge && injector) {
897
+ generator.newline();
898
+ generator.pushline(
899
+ `${componentNamespace}.__i18nBridge = ${componentNamespace}.__i18nBridge || []`
900
+ );
901
+ generator.pushline(
902
+ `${componentNamespace}.__i18nBridge.push('${injector()}')`
903
+ );
904
+ generator.pushline(`delete ${componentNamespace}._Ctor`);
905
+ }
906
+ generator.deindent();
907
+ generator.pushline(`}`);
908
+ }
909
+ break;
910
+ case "ObjectExpression":
911
+ if (propsCountStack[propsCountStack.length - 1] === 0) {
912
+ pathStack.pop();
913
+ propsCountStack.pop();
914
+ }
915
+ generator.deindent();
916
+ generator.push(`}`);
917
+ if (parent != null && parent.type === "ArrayExpression") {
918
+ if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
919
+ pathStack.pop();
920
+ generator.pushline(`,`);
921
+ }
922
+ }
923
+ break;
924
+ case "Property":
925
+ if (propsCountStack[propsCountStack.length - 1] !== 0) {
926
+ pathStack.pop();
927
+ if (!skipStack.pop()) {
928
+ generator.pushline(`,`);
929
+ }
930
+ }
931
+ break;
932
+ case "ArrayExpression":
933
+ if (itemsCountStack[itemsCountStack.length - 1] === 0) {
934
+ pathStack.pop();
935
+ itemsCountStack.pop();
936
+ }
937
+ generator.deindent();
938
+ generator.push(`]`);
939
+ if (parent != null && parent.type === "ArrayExpression") {
940
+ if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
941
+ pathStack.pop();
942
+ if (!skipStack.pop()) {
943
+ generator.pushline(`,`);
944
+ }
945
+ }
946
+ }
947
+ break;
948
+ case "Literal":
949
+ if (parent != null && parent.type === "ArrayExpression") {
950
+ if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
951
+ pathStack.pop();
952
+ if (!skipStack.pop()) {
953
+ generator.pushline(`,`);
954
+ }
955
+ } else {
956
+ if (!skipStack.pop()) {
957
+ generator.pushline(`,`);
958
+ }
959
+ }
960
+ }
961
+ break;
962
+ }
963
+ }
964
+ });
965
+ return codeMaps;
966
+ }
967
+ function isJSONablePrimitiveLiteral(node) {
968
+ return node.type === "Literal" && (shared.isString(node.value) || shared.isNumber(node.value) || shared.isBoolean(node.value) || node.value === null) || node.type === "TemplateLiteral";
969
+ }
970
+ function getValue(node) {
971
+ return node.type === "Literal" ? node.value : node.type === "TemplateLiteral" ? node.quasis.map((quasi) => quasi.value.cooked).join("") : void 0;
972
+ }
973
+
974
+ function checkInstallPackage(pkg, debug) {
975
+ let installedVueI18n = false;
976
+ try {
977
+ debug(`vue-i18n load path: ${require.resolve("vue-i18n")}`);
978
+ installedVueI18n = true;
979
+ } catch (e) {
980
+ debug(`cannot find 'vue-i18n'`, e);
981
+ }
982
+ let installedPetiteVueI18n = false;
983
+ try {
984
+ debug(`petite-vue-i18n load path: ${require.resolve("petite-vue-i18n")}`);
985
+ installedPetiteVueI18n = true;
986
+ } catch (e) {
987
+ debug(`cannot find 'petite-vue-i18n'`, e);
988
+ }
989
+ if (installedVueI18n) {
990
+ return "vue-i18n";
991
+ }
992
+ if (installedPetiteVueI18n) {
993
+ return "petite-vue-i18n";
994
+ }
995
+ throw new Error(
996
+ `${pkg} requires 'vue-i18n' or 'petite-vue-i18n' to be present in the dependency tree.`
997
+ );
998
+ }
999
+ function checkVueI18nBridgeInstallPackage(debug) {
1000
+ let ret = false;
1001
+ try {
1002
+ debug(`vue-i18n-bridge load path: ${require.resolve("vue-i18n-bridge")}`);
1003
+ ret = true;
1004
+ } catch (e) {
1005
+ debug(`cannot find 'vue-i18n-bridge'`, e);
1006
+ }
1007
+ return ret;
1008
+ }
1009
+ function getVueI18nVersion(debug) {
1010
+ const VueI18n = loadModule("vue-i18n", debug);
1011
+ if (VueI18n == null) {
1012
+ return "";
1013
+ }
1014
+ if (VueI18n.version && VueI18n.version.startsWith("8.")) {
1015
+ return "8";
1016
+ }
1017
+ if (VueI18n.VERSION && VueI18n.VERSION.startsWith("9.")) {
1018
+ return "9";
1019
+ }
1020
+ return "unknown";
1021
+ }
1022
+ function loadModule(moduleName, debug) {
1023
+ try {
1024
+ return require(moduleName);
1025
+ } catch (e) {
1026
+ debug(`cannot load '${moduleName}'`, e);
1027
+ return null;
1028
+ }
1029
+ }
1030
+
1031
+ exports.checkInstallPackage = checkInstallPackage;
1032
+ exports.checkVueI18nBridgeInstallPackage = checkVueI18nBridgeInstallPackage;
1033
+ exports.generateJSON = generate$2;
1034
+ exports.generateJavaScript = generate;
1035
+ exports.generateYAML = generate$1;
1036
+ exports.getVueI18nVersion = getVueI18nVersion;