@intlify/bundle-utils 6.0.1 → 7.0.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 +92 -77
- package/lib/index.d.ts +6 -11
- package/lib/index.mjs +85 -70
- package/package.json +4 -4
package/lib/index.cjs
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
const jsoncEslintParser = require('jsonc-eslint-parser');
|
|
4
4
|
const shared = require('@intlify/shared');
|
|
5
5
|
const module$1 = require('node:module');
|
|
6
|
-
const
|
|
6
|
+
const sourceMapJs = require('source-map-js');
|
|
7
7
|
const MagicString = require('magic-string');
|
|
8
8
|
const yamlEslintParser = require('yaml-eslint-parser');
|
|
9
9
|
const acorn = require('acorn');
|
|
@@ -22,7 +22,7 @@ function createCodeGenerator(options = {
|
|
|
22
22
|
env: "development",
|
|
23
23
|
forceStringify: false
|
|
24
24
|
}) {
|
|
25
|
-
const { sourceMap
|
|
25
|
+
const { sourceMap, source, filename } = options;
|
|
26
26
|
const _context = Object.assign(
|
|
27
27
|
{
|
|
28
28
|
code: "",
|
|
@@ -76,8 +76,8 @@ function createCodeGenerator(options = {
|
|
|
76
76
|
}
|
|
77
77
|
});
|
|
78
78
|
}
|
|
79
|
-
if (sourceMap
|
|
80
|
-
_context.map = new
|
|
79
|
+
if (sourceMap && source) {
|
|
80
|
+
_context.map = new sourceMapJs.SourceMapGenerator();
|
|
81
81
|
_context.map.setSourceContent(filename, source);
|
|
82
82
|
}
|
|
83
83
|
return {
|
|
@@ -112,7 +112,7 @@ const ON_ERROR_NOOP = () => {
|
|
|
112
112
|
function parsePath(path) {
|
|
113
113
|
return path ? path.join(".") : "";
|
|
114
114
|
}
|
|
115
|
-
function generateMessageFunction(msg, options, path) {
|
|
115
|
+
function generateMessageFunction(msg, options = {}, path) {
|
|
116
116
|
const env = options.env != null ? options.env : "development";
|
|
117
117
|
const strictMessage = shared.isBoolean(options.strictMessage) ? options.strictMessage : true;
|
|
118
118
|
const escapeHtml = !!options.escapeHtml;
|
|
@@ -153,9 +153,9 @@ function mapLinesColumns(resMap, codeMaps, inSourceMap) {
|
|
|
153
153
|
if (!resMap) {
|
|
154
154
|
return null;
|
|
155
155
|
}
|
|
156
|
-
const resMapConsumer = new
|
|
157
|
-
const inMapConsumer = inSourceMap ? new
|
|
158
|
-
const mergedMapGenerator = new
|
|
156
|
+
const resMapConsumer = new sourceMapJs.SourceMapConsumer(resMap);
|
|
157
|
+
const inMapConsumer = inSourceMap ? new sourceMapJs.SourceMapConsumer(inSourceMap) : null;
|
|
158
|
+
const mergedMapGenerator = new sourceMapJs.SourceMapGenerator();
|
|
159
159
|
let inMapFirstItem = null;
|
|
160
160
|
if (inMapConsumer) {
|
|
161
161
|
inMapConsumer.eachMapping((m) => {
|
|
@@ -184,7 +184,7 @@ function mapLinesColumns(resMap, codeMaps, inSourceMap) {
|
|
|
184
184
|
return;
|
|
185
185
|
}
|
|
186
186
|
}
|
|
187
|
-
const mapConsumer = new
|
|
187
|
+
const mapConsumer = new sourceMapJs.SourceMapConsumer(map);
|
|
188
188
|
mapConsumer.eachMapping((m) => {
|
|
189
189
|
mergedMapGenerator.addMapping({
|
|
190
190
|
original: {
|
|
@@ -215,6 +215,49 @@ function mapLinesColumns(resMap, codeMaps, inSourceMap) {
|
|
|
215
215
|
generator._file = inSourceMap ? inSourceMap.file : resMap.file;
|
|
216
216
|
return generator.toJSON();
|
|
217
217
|
}
|
|
218
|
+
function generateResourceAst(msg, options = {}, path) {
|
|
219
|
+
const env = options.env != null ? options.env : "development";
|
|
220
|
+
const strictMessage = shared.isBoolean(options.strictMessage) ? options.strictMessage : true;
|
|
221
|
+
const escapeHtml = !!options.escapeHtml;
|
|
222
|
+
const onError = options.onError || ON_ERROR_NOOP;
|
|
223
|
+
const errors = [];
|
|
224
|
+
let detecteHtmlInMsg = false;
|
|
225
|
+
if (detectHtmlTag(msg)) {
|
|
226
|
+
detecteHtmlInMsg = true;
|
|
227
|
+
if (strictMessage) {
|
|
228
|
+
const errMsg = shared.format(DETECT_MESSAGE, { msg });
|
|
229
|
+
onError(shared.format(errMsg), {
|
|
230
|
+
source: msg,
|
|
231
|
+
path: parsePath(path)
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const _msg = detecteHtmlInMsg && escapeHtml ? shared.escapeHtml(msg) : msg;
|
|
236
|
+
const newOptions = Object.assign(
|
|
237
|
+
{ location: env === "development" },
|
|
238
|
+
options
|
|
239
|
+
);
|
|
240
|
+
if (newOptions.jit != null) {
|
|
241
|
+
newOptions.jit = true;
|
|
242
|
+
}
|
|
243
|
+
newOptions.onError = (err) => {
|
|
244
|
+
if (onError) {
|
|
245
|
+
const extra = {
|
|
246
|
+
source: msg,
|
|
247
|
+
path: parsePath(path),
|
|
248
|
+
code: err.code,
|
|
249
|
+
domain: err.domain,
|
|
250
|
+
location: err.location
|
|
251
|
+
};
|
|
252
|
+
onError(err.message, extra);
|
|
253
|
+
errors.push(err);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
const { ast, map } = baseCompile(_msg, newOptions);
|
|
257
|
+
const occured = errors.length > 0;
|
|
258
|
+
const code = !occured ? `${shared.friendlyJSONstringify(ast)}` : `\`${_msg}\``;
|
|
259
|
+
return { code, ast, map, errors };
|
|
260
|
+
}
|
|
218
261
|
|
|
219
262
|
function generateLegacyCode({ isGlobal }, generator) {
|
|
220
263
|
const componentNamespace = `Component.options`;
|
|
@@ -245,7 +288,8 @@ function generate$2(targetSource, {
|
|
|
245
288
|
onError = void 0,
|
|
246
289
|
strictMessage = true,
|
|
247
290
|
escapeHtml = false,
|
|
248
|
-
useClassComponent = false
|
|
291
|
+
useClassComponent = false,
|
|
292
|
+
jit = false
|
|
249
293
|
}, injector) {
|
|
250
294
|
const target = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
251
295
|
const value = target;
|
|
@@ -264,7 +308,8 @@ function generate$2(targetSource, {
|
|
|
264
308
|
onError,
|
|
265
309
|
strictMessage,
|
|
266
310
|
escapeHtml,
|
|
267
|
-
useClassComponent
|
|
311
|
+
useClassComponent,
|
|
312
|
+
jit
|
|
268
313
|
};
|
|
269
314
|
const generator = createCodeGenerator(options);
|
|
270
315
|
const ast = jsoncEslintParser.parseJSON(value, { filePath: filename });
|
|
@@ -282,16 +327,16 @@ function generate$2(targetSource, {
|
|
|
282
327
|
})
|
|
283
328
|
};
|
|
284
329
|
}
|
|
285
|
-
const codeMaps =
|
|
330
|
+
const codeMaps = _generate$2(generator, ast, options, injector);
|
|
286
331
|
const { code, map } = generator.context();
|
|
287
|
-
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
332
|
+
const newMap = map && !jit ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
288
333
|
return {
|
|
289
334
|
ast,
|
|
290
335
|
code,
|
|
291
336
|
map: newMap != null ? newMap : void 0
|
|
292
337
|
};
|
|
293
338
|
}
|
|
294
|
-
function
|
|
339
|
+
function _generate$2(generator, node, options = {}, injector) {
|
|
295
340
|
const propsCountStack = [];
|
|
296
341
|
const pathStack = [];
|
|
297
342
|
const itemsCountStack = [];
|
|
@@ -304,8 +349,10 @@ function generateNode$2(generator, node, options, injector) {
|
|
|
304
349
|
sourceMap,
|
|
305
350
|
isGlobal,
|
|
306
351
|
locale,
|
|
307
|
-
useClassComponent
|
|
352
|
+
useClassComponent,
|
|
353
|
+
jit
|
|
308
354
|
} = options;
|
|
355
|
+
const codegenFn = jit ? generateResourceAst : generateMessageFunction;
|
|
309
356
|
const componentNamespace = "_Component";
|
|
310
357
|
jsoncEslintParser.traverseNodes(node, {
|
|
311
358
|
enterNode(node2, parent) {
|
|
@@ -348,11 +395,7 @@ function generateNode$2(generator, node, options, injector) {
|
|
|
348
395
|
if (shared.isString(value)) {
|
|
349
396
|
generator.push(`${JSON.stringify(name)}: `);
|
|
350
397
|
pathStack.push(name.toString());
|
|
351
|
-
const { code, map } =
|
|
352
|
-
value,
|
|
353
|
-
options,
|
|
354
|
-
pathStack
|
|
355
|
-
);
|
|
398
|
+
const { code, map } = codegenFn(value, options, pathStack);
|
|
356
399
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
357
400
|
generator.push(`${code}`, node2.value, value);
|
|
358
401
|
} else {
|
|
@@ -360,11 +403,7 @@ function generateNode$2(generator, node, options, injector) {
|
|
|
360
403
|
const strValue = JSON.stringify(value);
|
|
361
404
|
generator.push(`${JSON.stringify(name)}: `);
|
|
362
405
|
pathStack.push(name.toString());
|
|
363
|
-
const { code, map } =
|
|
364
|
-
strValue,
|
|
365
|
-
options,
|
|
366
|
-
pathStack
|
|
367
|
-
);
|
|
406
|
+
const { code, map } = codegenFn(strValue, options, pathStack);
|
|
368
407
|
sourceMap && map != null && codeMaps.set(strValue, map);
|
|
369
408
|
generator.push(`${code}`, node2.value, strValue);
|
|
370
409
|
} else {
|
|
@@ -401,21 +440,13 @@ function generateNode$2(generator, node, options, injector) {
|
|
|
401
440
|
if (node2.type === "JSONLiteral") {
|
|
402
441
|
const value = node2.value;
|
|
403
442
|
if (shared.isString(value)) {
|
|
404
|
-
const { code, map } =
|
|
405
|
-
value,
|
|
406
|
-
options,
|
|
407
|
-
pathStack
|
|
408
|
-
);
|
|
443
|
+
const { code, map } = codegenFn(value, options, pathStack);
|
|
409
444
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
410
445
|
generator.push(`${code}`, node2, value);
|
|
411
446
|
} else {
|
|
412
447
|
if (forceStringify) {
|
|
413
448
|
const strValue = JSON.stringify(value);
|
|
414
|
-
const { code, map } =
|
|
415
|
-
strValue,
|
|
416
|
-
options,
|
|
417
|
-
pathStack
|
|
418
|
-
);
|
|
449
|
+
const { code, map } = codegenFn(strValue, options, pathStack);
|
|
419
450
|
sourceMap && map != null && codeMaps.set(strValue, map);
|
|
420
451
|
generator.push(`${code}`, node2, strValue);
|
|
421
452
|
} else {
|
|
@@ -517,7 +548,8 @@ function generate$1(targetSource, {
|
|
|
517
548
|
forceStringify = false,
|
|
518
549
|
onError = void 0,
|
|
519
550
|
strictMessage = true,
|
|
520
|
-
escapeHtml = false
|
|
551
|
+
escapeHtml = false,
|
|
552
|
+
jit = false
|
|
521
553
|
}, injector) {
|
|
522
554
|
const value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
523
555
|
const options = {
|
|
@@ -535,7 +567,8 @@ function generate$1(targetSource, {
|
|
|
535
567
|
onError,
|
|
536
568
|
strictMessage,
|
|
537
569
|
escapeHtml,
|
|
538
|
-
useClassComponent
|
|
570
|
+
useClassComponent,
|
|
571
|
+
jit
|
|
539
572
|
};
|
|
540
573
|
const generator = createCodeGenerator(options);
|
|
541
574
|
const ast = yamlEslintParser.parseYAML(value, { filePath: filename });
|
|
@@ -553,7 +586,7 @@ function generate$1(targetSource, {
|
|
|
553
586
|
})
|
|
554
587
|
};
|
|
555
588
|
}
|
|
556
|
-
const codeMaps =
|
|
589
|
+
const codeMaps = _generate$1(generator, ast, options, injector);
|
|
557
590
|
const { code, map } = generator.context();
|
|
558
591
|
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
559
592
|
return {
|
|
@@ -562,7 +595,7 @@ function generate$1(targetSource, {
|
|
|
562
595
|
map: newMap != null ? newMap : void 0
|
|
563
596
|
};
|
|
564
597
|
}
|
|
565
|
-
function
|
|
598
|
+
function _generate$1(generator, node, options = {}, injector) {
|
|
566
599
|
const propsCountStack = [];
|
|
567
600
|
const pathStack = [];
|
|
568
601
|
const itemsCountStack = [];
|
|
@@ -575,8 +608,10 @@ function generateNode$1(generator, node, options, injector) {
|
|
|
575
608
|
sourceMap,
|
|
576
609
|
isGlobal,
|
|
577
610
|
locale,
|
|
578
|
-
useClassComponent
|
|
611
|
+
useClassComponent,
|
|
612
|
+
jit
|
|
579
613
|
} = options;
|
|
614
|
+
const codegenFn = jit ? generateResourceAst : generateMessageFunction;
|
|
580
615
|
const componentNamespace = "_Component";
|
|
581
616
|
yamlEslintParser.traverseNodes(node, {
|
|
582
617
|
enterNode(node2, parent) {
|
|
@@ -619,11 +654,7 @@ function generateNode$1(generator, node, options, injector) {
|
|
|
619
654
|
if (shared.isString(value)) {
|
|
620
655
|
generator.push(`${JSON.stringify(name)}: `);
|
|
621
656
|
name && pathStack.push(name.toString());
|
|
622
|
-
const { code, map } =
|
|
623
|
-
value,
|
|
624
|
-
options,
|
|
625
|
-
pathStack
|
|
626
|
-
);
|
|
657
|
+
const { code, map } = codegenFn(value, options, pathStack);
|
|
627
658
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
628
659
|
generator.push(`${code}`, node2.value, value);
|
|
629
660
|
} else {
|
|
@@ -631,11 +662,7 @@ function generateNode$1(generator, node, options, injector) {
|
|
|
631
662
|
const strValue = JSON.stringify(value);
|
|
632
663
|
generator.push(`${JSON.stringify(name)}: `);
|
|
633
664
|
name && pathStack.push(name.toString());
|
|
634
|
-
const { code, map } =
|
|
635
|
-
strValue,
|
|
636
|
-
options,
|
|
637
|
-
pathStack
|
|
638
|
-
);
|
|
665
|
+
const { code, map } = codegenFn(strValue, options, pathStack);
|
|
639
666
|
sourceMap && map != null && codeMaps.set(strValue, map);
|
|
640
667
|
generator.push(`${code}`, node2.value, strValue);
|
|
641
668
|
} else {
|
|
@@ -672,21 +699,13 @@ function generateNode$1(generator, node, options, injector) {
|
|
|
672
699
|
if (node2.type === "YAMLScalar") {
|
|
673
700
|
const value = node2.value;
|
|
674
701
|
if (shared.isString(value)) {
|
|
675
|
-
const { code, map } =
|
|
676
|
-
value,
|
|
677
|
-
options,
|
|
678
|
-
pathStack
|
|
679
|
-
);
|
|
702
|
+
const { code, map } = codegenFn(value, options, pathStack);
|
|
680
703
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
681
704
|
generator.push(`${code}`, node2, value);
|
|
682
705
|
} else {
|
|
683
706
|
if (forceStringify) {
|
|
684
707
|
const strValue = JSON.stringify(value);
|
|
685
|
-
const { code, map } =
|
|
686
|
-
strValue,
|
|
687
|
-
options,
|
|
688
|
-
pathStack
|
|
689
|
-
);
|
|
708
|
+
const { code, map } = codegenFn(strValue, options, pathStack);
|
|
690
709
|
sourceMap && map != null && codeMaps.set(strValue, map);
|
|
691
710
|
generator.push(`${code}`, node2, strValue);
|
|
692
711
|
} else {
|
|
@@ -788,7 +807,8 @@ function generate(targetSource, {
|
|
|
788
807
|
strictMessage = true,
|
|
789
808
|
escapeHtml = false,
|
|
790
809
|
useClassComponent = false,
|
|
791
|
-
allowDynamic = false
|
|
810
|
+
allowDynamic = false,
|
|
811
|
+
jit = false
|
|
792
812
|
}, injector) {
|
|
793
813
|
const target = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
794
814
|
const value = target;
|
|
@@ -807,7 +827,8 @@ function generate(targetSource, {
|
|
|
807
827
|
onError,
|
|
808
828
|
strictMessage,
|
|
809
829
|
escapeHtml,
|
|
810
|
-
useClassComponent
|
|
830
|
+
useClassComponent,
|
|
831
|
+
jit
|
|
811
832
|
};
|
|
812
833
|
const generator = createCodeGenerator(options);
|
|
813
834
|
const ast = acorn.parse(value, {
|
|
@@ -837,7 +858,7 @@ function generate(targetSource, {
|
|
|
837
858
|
};
|
|
838
859
|
}
|
|
839
860
|
}
|
|
840
|
-
const codeMaps =
|
|
861
|
+
const codeMaps = _generate(generator, ast, options, injector);
|
|
841
862
|
const { code, map } = generator.context();
|
|
842
863
|
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
843
864
|
return {
|
|
@@ -867,7 +888,7 @@ function scanAst(ast) {
|
|
|
867
888
|
}
|
|
868
889
|
return ret;
|
|
869
890
|
}
|
|
870
|
-
function
|
|
891
|
+
function _generate(generator, node, options = {}, injector) {
|
|
871
892
|
const propsCountStack = [];
|
|
872
893
|
const pathStack = [];
|
|
873
894
|
const itemsCountStack = [];
|
|
@@ -881,8 +902,10 @@ function generateNode(generator, node, options, injector) {
|
|
|
881
902
|
sourceMap,
|
|
882
903
|
isGlobal,
|
|
883
904
|
locale,
|
|
884
|
-
useClassComponent
|
|
905
|
+
useClassComponent,
|
|
906
|
+
jit
|
|
885
907
|
} = options;
|
|
908
|
+
const codegenFn = jit ? generateResourceAst : generateMessageFunction;
|
|
886
909
|
const componentNamespace = "_Component";
|
|
887
910
|
estreeWalker.walk(node, {
|
|
888
911
|
/**
|
|
@@ -934,11 +957,7 @@ function generateNode(generator, node, options, injector) {
|
|
|
934
957
|
const value = getValue(node2.value);
|
|
935
958
|
generator.push(`${JSON.stringify(name)}: `);
|
|
936
959
|
pathStack.push(name);
|
|
937
|
-
const { code, map } =
|
|
938
|
-
value,
|
|
939
|
-
options,
|
|
940
|
-
pathStack
|
|
941
|
-
);
|
|
960
|
+
const { code, map } = codegenFn(value, options, pathStack);
|
|
942
961
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
943
962
|
generator.push(`${code}`, node2.value, value);
|
|
944
963
|
skipStack.push(false);
|
|
@@ -948,7 +967,7 @@ function generateNode(generator, node, options, injector) {
|
|
|
948
967
|
const strValue = JSON.stringify(value);
|
|
949
968
|
generator.push(`${JSON.stringify(name)}: `);
|
|
950
969
|
pathStack.push(name);
|
|
951
|
-
const { code, map } =
|
|
970
|
+
const { code, map } = codegenFn(
|
|
952
971
|
strValue,
|
|
953
972
|
options,
|
|
954
973
|
pathStack
|
|
@@ -1002,18 +1021,14 @@ function generateNode(generator, node, options, injector) {
|
|
|
1002
1021
|
if (isJSONablePrimitiveLiteral(node2)) {
|
|
1003
1022
|
if (node2.type === "Literal" && shared.isString(node2.value) || node2.type === "TemplateLiteral") {
|
|
1004
1023
|
const value = getValue(node2);
|
|
1005
|
-
const { code, map } =
|
|
1006
|
-
value,
|
|
1007
|
-
options,
|
|
1008
|
-
pathStack
|
|
1009
|
-
);
|
|
1024
|
+
const { code, map } = codegenFn(value, options, pathStack);
|
|
1010
1025
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
1011
1026
|
generator.push(`${code}`, node2, value);
|
|
1012
1027
|
} else {
|
|
1013
1028
|
const value = getValue(node2);
|
|
1014
1029
|
if (forceStringify) {
|
|
1015
1030
|
const strValue = JSON.stringify(value);
|
|
1016
|
-
const { code, map } =
|
|
1031
|
+
const { code, map } = codegenFn(
|
|
1017
1032
|
strValue,
|
|
1018
1033
|
options,
|
|
1019
1034
|
pathStack
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { JSONProgram } from 'jsonc-eslint-parser/lib/parser/ast';
|
|
2
|
-
import { RawSourceMap } from 'source-map';
|
|
2
|
+
import { RawSourceMap } from 'source-map-js';
|
|
3
3
|
import { CompileError } from '@intlify/message-compiler';
|
|
4
4
|
import { YAMLProgram } from 'yaml-eslint-parser/lib/ast';
|
|
5
5
|
import { Node } from 'estree';
|
|
@@ -30,6 +30,7 @@ interface CodeGenOptions {
|
|
|
30
30
|
allowDynamic?: boolean;
|
|
31
31
|
strictMessage?: boolean;
|
|
32
32
|
escapeHtml?: boolean;
|
|
33
|
+
jit?: boolean;
|
|
33
34
|
onWarn?: (msg: string) => void;
|
|
34
35
|
onError?: (msg: string, extra?: {
|
|
35
36
|
source: string;
|
|
@@ -39,9 +40,6 @@ interface CodeGenOptions {
|
|
|
39
40
|
location?: CompileError['location'];
|
|
40
41
|
}) => void;
|
|
41
42
|
}
|
|
42
|
-
/**
|
|
43
|
-
* @internal
|
|
44
|
-
*/
|
|
45
43
|
interface CodeGenResult<ASTNode, CodeGenError extends Error = Error> {
|
|
46
44
|
code: string;
|
|
47
45
|
ast: ASTNode;
|
|
@@ -50,13 +48,10 @@ interface CodeGenResult<ASTNode, CodeGenError extends Error = Error> {
|
|
|
50
48
|
}
|
|
51
49
|
|
|
52
50
|
/**
|
|
53
|
-
* Code generator for i18n json/json5 resource
|
|
51
|
+
* Code / AST generator for i18n json/json5 resource
|
|
54
52
|
*/
|
|
55
53
|
|
|
56
|
-
|
|
57
|
-
* @internal
|
|
58
|
-
*/
|
|
59
|
-
declare function generate$2(targetSource: string | Buffer, { type, legacy, bridge, exportESM, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, useClassComponent }: CodeGenOptions, injector?: () => string): CodeGenResult<JSONProgram>;
|
|
54
|
+
declare function generate$2(targetSource: string | Buffer, { type, legacy, bridge, exportESM, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, useClassComponent, jit }: CodeGenOptions, injector?: () => string): CodeGenResult<JSONProgram>;
|
|
60
55
|
|
|
61
56
|
/**
|
|
62
57
|
* Code generator for i18n yaml resource
|
|
@@ -65,7 +60,7 @@ declare function generate$2(targetSource: string | Buffer, { type, legacy, bridg
|
|
|
65
60
|
/**
|
|
66
61
|
* @internal
|
|
67
62
|
*/
|
|
68
|
-
declare function generate$1(targetSource: string | Buffer, { type, legacy, bridge, exportESM, useClassComponent, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml }: CodeGenOptions, injector?: () => string): CodeGenResult<YAMLProgram>;
|
|
63
|
+
declare function generate$1(targetSource: string | Buffer, { type, legacy, bridge, exportESM, useClassComponent, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, jit }: CodeGenOptions, injector?: () => string): CodeGenResult<YAMLProgram>;
|
|
69
64
|
|
|
70
65
|
/**
|
|
71
66
|
* Code generator for i18n js resource
|
|
@@ -74,7 +69,7 @@ declare function generate$1(targetSource: string | Buffer, { type, legacy, bridg
|
|
|
74
69
|
/**
|
|
75
70
|
* @internal
|
|
76
71
|
*/
|
|
77
|
-
declare function generate(targetSource: string | Buffer, { type, bridge, exportESM, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, useClassComponent, allowDynamic }: CodeGenOptions, injector?: () => string): CodeGenResult<Node>;
|
|
72
|
+
declare function generate(targetSource: string | Buffer, { type, bridge, exportESM, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, useClassComponent, allowDynamic, jit }: CodeGenOptions, injector?: () => string): CodeGenResult<Node>;
|
|
78
73
|
|
|
79
74
|
type InstalledPackage = 'vue-i18n' | 'petite-vue-i18n';
|
|
80
75
|
declare function checkInstallPackage(pkg: string, debug: Function): InstalledPackage;
|
package/lib/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { parseJSON, traverseNodes, getStaticJSONValue } from 'jsonc-eslint-parser';
|
|
2
2
|
import { isBoolean, format, escapeHtml, friendlyJSONstringify, isString, isNumber } from '@intlify/shared';
|
|
3
3
|
import module from 'node:module';
|
|
4
|
-
import { SourceMapGenerator, SourceMapConsumer } from 'source-map';
|
|
4
|
+
import { SourceMapGenerator, SourceMapConsumer } from 'source-map-js';
|
|
5
5
|
import MagicString from 'magic-string';
|
|
6
6
|
import { parseYAML, traverseNodes as traverseNodes$1, getStaticYAMLValue } from 'yaml-eslint-parser';
|
|
7
7
|
import { parse } from 'acorn';
|
|
@@ -110,7 +110,7 @@ const ON_ERROR_NOOP = () => {
|
|
|
110
110
|
function parsePath(path) {
|
|
111
111
|
return path ? path.join(".") : "";
|
|
112
112
|
}
|
|
113
|
-
function generateMessageFunction(msg, options, path) {
|
|
113
|
+
function generateMessageFunction(msg, options = {}, path) {
|
|
114
114
|
const env = options.env != null ? options.env : "development";
|
|
115
115
|
const strictMessage = isBoolean(options.strictMessage) ? options.strictMessage : true;
|
|
116
116
|
const escapeHtml$1 = !!options.escapeHtml;
|
|
@@ -213,6 +213,49 @@ function mapLinesColumns(resMap, codeMaps, inSourceMap) {
|
|
|
213
213
|
generator._file = inSourceMap ? inSourceMap.file : resMap.file;
|
|
214
214
|
return generator.toJSON();
|
|
215
215
|
}
|
|
216
|
+
function generateResourceAst(msg, options = {}, path) {
|
|
217
|
+
const env = options.env != null ? options.env : "development";
|
|
218
|
+
const strictMessage = isBoolean(options.strictMessage) ? options.strictMessage : true;
|
|
219
|
+
const escapeHtml$1 = !!options.escapeHtml;
|
|
220
|
+
const onError = options.onError || ON_ERROR_NOOP;
|
|
221
|
+
const errors = [];
|
|
222
|
+
let detecteHtmlInMsg = false;
|
|
223
|
+
if (detectHtmlTag(msg)) {
|
|
224
|
+
detecteHtmlInMsg = true;
|
|
225
|
+
if (strictMessage) {
|
|
226
|
+
const errMsg = format(DETECT_MESSAGE, { msg });
|
|
227
|
+
onError(format(errMsg), {
|
|
228
|
+
source: msg,
|
|
229
|
+
path: parsePath(path)
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
const _msg = detecteHtmlInMsg && escapeHtml$1 ? escapeHtml(msg) : msg;
|
|
234
|
+
const newOptions = Object.assign(
|
|
235
|
+
{ location: env === "development" },
|
|
236
|
+
options
|
|
237
|
+
);
|
|
238
|
+
if (newOptions.jit != null) {
|
|
239
|
+
newOptions.jit = true;
|
|
240
|
+
}
|
|
241
|
+
newOptions.onError = (err) => {
|
|
242
|
+
if (onError) {
|
|
243
|
+
const extra = {
|
|
244
|
+
source: msg,
|
|
245
|
+
path: parsePath(path),
|
|
246
|
+
code: err.code,
|
|
247
|
+
domain: err.domain,
|
|
248
|
+
location: err.location
|
|
249
|
+
};
|
|
250
|
+
onError(err.message, extra);
|
|
251
|
+
errors.push(err);
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
const { ast, map } = baseCompile(_msg, newOptions);
|
|
255
|
+
const occured = errors.length > 0;
|
|
256
|
+
const code = !occured ? `${friendlyJSONstringify(ast)}` : `\`${_msg}\``;
|
|
257
|
+
return { code, ast, map, errors };
|
|
258
|
+
}
|
|
216
259
|
|
|
217
260
|
function generateLegacyCode({ isGlobal }, generator) {
|
|
218
261
|
const componentNamespace = `Component.options`;
|
|
@@ -243,7 +286,8 @@ function generate$2(targetSource, {
|
|
|
243
286
|
onError = void 0,
|
|
244
287
|
strictMessage = true,
|
|
245
288
|
escapeHtml = false,
|
|
246
|
-
useClassComponent = false
|
|
289
|
+
useClassComponent = false,
|
|
290
|
+
jit = false
|
|
247
291
|
}, injector) {
|
|
248
292
|
const target = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
249
293
|
const value = target;
|
|
@@ -262,7 +306,8 @@ function generate$2(targetSource, {
|
|
|
262
306
|
onError,
|
|
263
307
|
strictMessage,
|
|
264
308
|
escapeHtml,
|
|
265
|
-
useClassComponent
|
|
309
|
+
useClassComponent,
|
|
310
|
+
jit
|
|
266
311
|
};
|
|
267
312
|
const generator = createCodeGenerator(options);
|
|
268
313
|
const ast = parseJSON(value, { filePath: filename });
|
|
@@ -280,16 +325,16 @@ function generate$2(targetSource, {
|
|
|
280
325
|
})
|
|
281
326
|
};
|
|
282
327
|
}
|
|
283
|
-
const codeMaps =
|
|
328
|
+
const codeMaps = _generate$2(generator, ast, options, injector);
|
|
284
329
|
const { code, map } = generator.context();
|
|
285
|
-
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
330
|
+
const newMap = map && !jit ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
286
331
|
return {
|
|
287
332
|
ast,
|
|
288
333
|
code,
|
|
289
334
|
map: newMap != null ? newMap : void 0
|
|
290
335
|
};
|
|
291
336
|
}
|
|
292
|
-
function
|
|
337
|
+
function _generate$2(generator, node, options = {}, injector) {
|
|
293
338
|
const propsCountStack = [];
|
|
294
339
|
const pathStack = [];
|
|
295
340
|
const itemsCountStack = [];
|
|
@@ -302,8 +347,10 @@ function generateNode$2(generator, node, options, injector) {
|
|
|
302
347
|
sourceMap,
|
|
303
348
|
isGlobal,
|
|
304
349
|
locale,
|
|
305
|
-
useClassComponent
|
|
350
|
+
useClassComponent,
|
|
351
|
+
jit
|
|
306
352
|
} = options;
|
|
353
|
+
const codegenFn = jit ? generateResourceAst : generateMessageFunction;
|
|
307
354
|
const componentNamespace = "_Component";
|
|
308
355
|
traverseNodes(node, {
|
|
309
356
|
enterNode(node2, parent) {
|
|
@@ -346,11 +393,7 @@ function generateNode$2(generator, node, options, injector) {
|
|
|
346
393
|
if (isString(value)) {
|
|
347
394
|
generator.push(`${JSON.stringify(name)}: `);
|
|
348
395
|
pathStack.push(name.toString());
|
|
349
|
-
const { code, map } =
|
|
350
|
-
value,
|
|
351
|
-
options,
|
|
352
|
-
pathStack
|
|
353
|
-
);
|
|
396
|
+
const { code, map } = codegenFn(value, options, pathStack);
|
|
354
397
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
355
398
|
generator.push(`${code}`, node2.value, value);
|
|
356
399
|
} else {
|
|
@@ -358,11 +401,7 @@ function generateNode$2(generator, node, options, injector) {
|
|
|
358
401
|
const strValue = JSON.stringify(value);
|
|
359
402
|
generator.push(`${JSON.stringify(name)}: `);
|
|
360
403
|
pathStack.push(name.toString());
|
|
361
|
-
const { code, map } =
|
|
362
|
-
strValue,
|
|
363
|
-
options,
|
|
364
|
-
pathStack
|
|
365
|
-
);
|
|
404
|
+
const { code, map } = codegenFn(strValue, options, pathStack);
|
|
366
405
|
sourceMap && map != null && codeMaps.set(strValue, map);
|
|
367
406
|
generator.push(`${code}`, node2.value, strValue);
|
|
368
407
|
} else {
|
|
@@ -399,21 +438,13 @@ function generateNode$2(generator, node, options, injector) {
|
|
|
399
438
|
if (node2.type === "JSONLiteral") {
|
|
400
439
|
const value = node2.value;
|
|
401
440
|
if (isString(value)) {
|
|
402
|
-
const { code, map } =
|
|
403
|
-
value,
|
|
404
|
-
options,
|
|
405
|
-
pathStack
|
|
406
|
-
);
|
|
441
|
+
const { code, map } = codegenFn(value, options, pathStack);
|
|
407
442
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
408
443
|
generator.push(`${code}`, node2, value);
|
|
409
444
|
} else {
|
|
410
445
|
if (forceStringify) {
|
|
411
446
|
const strValue = JSON.stringify(value);
|
|
412
|
-
const { code, map } =
|
|
413
|
-
strValue,
|
|
414
|
-
options,
|
|
415
|
-
pathStack
|
|
416
|
-
);
|
|
447
|
+
const { code, map } = codegenFn(strValue, options, pathStack);
|
|
417
448
|
sourceMap && map != null && codeMaps.set(strValue, map);
|
|
418
449
|
generator.push(`${code}`, node2, strValue);
|
|
419
450
|
} else {
|
|
@@ -515,7 +546,8 @@ function generate$1(targetSource, {
|
|
|
515
546
|
forceStringify = false,
|
|
516
547
|
onError = void 0,
|
|
517
548
|
strictMessage = true,
|
|
518
|
-
escapeHtml = false
|
|
549
|
+
escapeHtml = false,
|
|
550
|
+
jit = false
|
|
519
551
|
}, injector) {
|
|
520
552
|
const value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
521
553
|
const options = {
|
|
@@ -533,7 +565,8 @@ function generate$1(targetSource, {
|
|
|
533
565
|
onError,
|
|
534
566
|
strictMessage,
|
|
535
567
|
escapeHtml,
|
|
536
|
-
useClassComponent
|
|
568
|
+
useClassComponent,
|
|
569
|
+
jit
|
|
537
570
|
};
|
|
538
571
|
const generator = createCodeGenerator(options);
|
|
539
572
|
const ast = parseYAML(value, { filePath: filename });
|
|
@@ -551,7 +584,7 @@ function generate$1(targetSource, {
|
|
|
551
584
|
})
|
|
552
585
|
};
|
|
553
586
|
}
|
|
554
|
-
const codeMaps =
|
|
587
|
+
const codeMaps = _generate$1(generator, ast, options, injector);
|
|
555
588
|
const { code, map } = generator.context();
|
|
556
589
|
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
557
590
|
return {
|
|
@@ -560,7 +593,7 @@ function generate$1(targetSource, {
|
|
|
560
593
|
map: newMap != null ? newMap : void 0
|
|
561
594
|
};
|
|
562
595
|
}
|
|
563
|
-
function
|
|
596
|
+
function _generate$1(generator, node, options = {}, injector) {
|
|
564
597
|
const propsCountStack = [];
|
|
565
598
|
const pathStack = [];
|
|
566
599
|
const itemsCountStack = [];
|
|
@@ -573,8 +606,10 @@ function generateNode$1(generator, node, options, injector) {
|
|
|
573
606
|
sourceMap,
|
|
574
607
|
isGlobal,
|
|
575
608
|
locale,
|
|
576
|
-
useClassComponent
|
|
609
|
+
useClassComponent,
|
|
610
|
+
jit
|
|
577
611
|
} = options;
|
|
612
|
+
const codegenFn = jit ? generateResourceAst : generateMessageFunction;
|
|
578
613
|
const componentNamespace = "_Component";
|
|
579
614
|
traverseNodes$1(node, {
|
|
580
615
|
enterNode(node2, parent) {
|
|
@@ -617,11 +652,7 @@ function generateNode$1(generator, node, options, injector) {
|
|
|
617
652
|
if (isString(value)) {
|
|
618
653
|
generator.push(`${JSON.stringify(name)}: `);
|
|
619
654
|
name && pathStack.push(name.toString());
|
|
620
|
-
const { code, map } =
|
|
621
|
-
value,
|
|
622
|
-
options,
|
|
623
|
-
pathStack
|
|
624
|
-
);
|
|
655
|
+
const { code, map } = codegenFn(value, options, pathStack);
|
|
625
656
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
626
657
|
generator.push(`${code}`, node2.value, value);
|
|
627
658
|
} else {
|
|
@@ -629,11 +660,7 @@ function generateNode$1(generator, node, options, injector) {
|
|
|
629
660
|
const strValue = JSON.stringify(value);
|
|
630
661
|
generator.push(`${JSON.stringify(name)}: `);
|
|
631
662
|
name && pathStack.push(name.toString());
|
|
632
|
-
const { code, map } =
|
|
633
|
-
strValue,
|
|
634
|
-
options,
|
|
635
|
-
pathStack
|
|
636
|
-
);
|
|
663
|
+
const { code, map } = codegenFn(strValue, options, pathStack);
|
|
637
664
|
sourceMap && map != null && codeMaps.set(strValue, map);
|
|
638
665
|
generator.push(`${code}`, node2.value, strValue);
|
|
639
666
|
} else {
|
|
@@ -670,21 +697,13 @@ function generateNode$1(generator, node, options, injector) {
|
|
|
670
697
|
if (node2.type === "YAMLScalar") {
|
|
671
698
|
const value = node2.value;
|
|
672
699
|
if (isString(value)) {
|
|
673
|
-
const { code, map } =
|
|
674
|
-
value,
|
|
675
|
-
options,
|
|
676
|
-
pathStack
|
|
677
|
-
);
|
|
700
|
+
const { code, map } = codegenFn(value, options, pathStack);
|
|
678
701
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
679
702
|
generator.push(`${code}`, node2, value);
|
|
680
703
|
} else {
|
|
681
704
|
if (forceStringify) {
|
|
682
705
|
const strValue = JSON.stringify(value);
|
|
683
|
-
const { code, map } =
|
|
684
|
-
strValue,
|
|
685
|
-
options,
|
|
686
|
-
pathStack
|
|
687
|
-
);
|
|
706
|
+
const { code, map } = codegenFn(strValue, options, pathStack);
|
|
688
707
|
sourceMap && map != null && codeMaps.set(strValue, map);
|
|
689
708
|
generator.push(`${code}`, node2, strValue);
|
|
690
709
|
} else {
|
|
@@ -786,7 +805,8 @@ function generate(targetSource, {
|
|
|
786
805
|
strictMessage = true,
|
|
787
806
|
escapeHtml = false,
|
|
788
807
|
useClassComponent = false,
|
|
789
|
-
allowDynamic = false
|
|
808
|
+
allowDynamic = false,
|
|
809
|
+
jit = false
|
|
790
810
|
}, injector) {
|
|
791
811
|
const target = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
792
812
|
const value = target;
|
|
@@ -805,7 +825,8 @@ function generate(targetSource, {
|
|
|
805
825
|
onError,
|
|
806
826
|
strictMessage,
|
|
807
827
|
escapeHtml,
|
|
808
|
-
useClassComponent
|
|
828
|
+
useClassComponent,
|
|
829
|
+
jit
|
|
809
830
|
};
|
|
810
831
|
const generator = createCodeGenerator(options);
|
|
811
832
|
const ast = parse(value, {
|
|
@@ -835,7 +856,7 @@ function generate(targetSource, {
|
|
|
835
856
|
};
|
|
836
857
|
}
|
|
837
858
|
}
|
|
838
|
-
const codeMaps =
|
|
859
|
+
const codeMaps = _generate(generator, ast, options, injector);
|
|
839
860
|
const { code, map } = generator.context();
|
|
840
861
|
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
841
862
|
return {
|
|
@@ -865,7 +886,7 @@ function scanAst(ast) {
|
|
|
865
886
|
}
|
|
866
887
|
return ret;
|
|
867
888
|
}
|
|
868
|
-
function
|
|
889
|
+
function _generate(generator, node, options = {}, injector) {
|
|
869
890
|
const propsCountStack = [];
|
|
870
891
|
const pathStack = [];
|
|
871
892
|
const itemsCountStack = [];
|
|
@@ -879,8 +900,10 @@ function generateNode(generator, node, options, injector) {
|
|
|
879
900
|
sourceMap,
|
|
880
901
|
isGlobal,
|
|
881
902
|
locale,
|
|
882
|
-
useClassComponent
|
|
903
|
+
useClassComponent,
|
|
904
|
+
jit
|
|
883
905
|
} = options;
|
|
906
|
+
const codegenFn = jit ? generateResourceAst : generateMessageFunction;
|
|
884
907
|
const componentNamespace = "_Component";
|
|
885
908
|
walk(node, {
|
|
886
909
|
/**
|
|
@@ -932,11 +955,7 @@ function generateNode(generator, node, options, injector) {
|
|
|
932
955
|
const value = getValue(node2.value);
|
|
933
956
|
generator.push(`${JSON.stringify(name)}: `);
|
|
934
957
|
pathStack.push(name);
|
|
935
|
-
const { code, map } =
|
|
936
|
-
value,
|
|
937
|
-
options,
|
|
938
|
-
pathStack
|
|
939
|
-
);
|
|
958
|
+
const { code, map } = codegenFn(value, options, pathStack);
|
|
940
959
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
941
960
|
generator.push(`${code}`, node2.value, value);
|
|
942
961
|
skipStack.push(false);
|
|
@@ -946,7 +965,7 @@ function generateNode(generator, node, options, injector) {
|
|
|
946
965
|
const strValue = JSON.stringify(value);
|
|
947
966
|
generator.push(`${JSON.stringify(name)}: `);
|
|
948
967
|
pathStack.push(name);
|
|
949
|
-
const { code, map } =
|
|
968
|
+
const { code, map } = codegenFn(
|
|
950
969
|
strValue,
|
|
951
970
|
options,
|
|
952
971
|
pathStack
|
|
@@ -1000,18 +1019,14 @@ function generateNode(generator, node, options, injector) {
|
|
|
1000
1019
|
if (isJSONablePrimitiveLiteral(node2)) {
|
|
1001
1020
|
if (node2.type === "Literal" && isString(node2.value) || node2.type === "TemplateLiteral") {
|
|
1002
1021
|
const value = getValue(node2);
|
|
1003
|
-
const { code, map } =
|
|
1004
|
-
value,
|
|
1005
|
-
options,
|
|
1006
|
-
pathStack
|
|
1007
|
-
);
|
|
1022
|
+
const { code, map } = codegenFn(value, options, pathStack);
|
|
1008
1023
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
1009
1024
|
generator.push(`${code}`, node2, value);
|
|
1010
1025
|
} else {
|
|
1011
1026
|
const value = getValue(node2);
|
|
1012
1027
|
if (forceStringify) {
|
|
1013
1028
|
const strValue = JSON.stringify(value);
|
|
1014
|
-
const { code, map } =
|
|
1029
|
+
const { code, map } = codegenFn(
|
|
1015
1030
|
strValue,
|
|
1016
1031
|
options,
|
|
1017
1032
|
pathStack
|
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": "
|
|
4
|
+
"version": "7.0.1",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -18,15 +18,15 @@
|
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@intlify/message-compiler": "9.3.0-beta.
|
|
22
|
-
"@intlify/shared": "9.3.0-beta.
|
|
21
|
+
"@intlify/message-compiler": "9.3.0-beta.20",
|
|
22
|
+
"@intlify/shared": "9.3.0-beta.20",
|
|
23
23
|
"acorn": "^8.8.2",
|
|
24
24
|
"escodegen": "^2.0.0",
|
|
25
25
|
"estree-walker": "^2.0.2",
|
|
26
26
|
"jsonc-eslint-parser": "^1.0.1",
|
|
27
27
|
"magic-string": "^0.30.0",
|
|
28
28
|
"mlly": "^1.2.0",
|
|
29
|
-
"source-map": "0.
|
|
29
|
+
"source-map-js": "^1.0.1",
|
|
30
30
|
"yaml-eslint-parser": "^0.3.2"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|