@intlify/bundle-utils 5.0.0 → 5.1.0

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