@intlify/bundle-utils 5.5.0 → 6.0.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.cjs +236 -75
- package/lib/index.d.ts +85 -5
- package/lib/index.mjs +238 -86
- package/package.json +11 -10
- package/index.mjs +0 -1
- package/lib/codegen.d.ts +0 -82
- package/lib/codegen.js +0 -226
- package/lib/deps.d.ts +0 -7
- package/lib/deps.js +0 -68
- package/lib/index.js +0 -13
- package/lib/js.d.ts +0 -10
- package/lib/js.js +0 -417
- package/lib/json.d.ts +0 -10
- package/lib/json.js +0 -283
- package/lib/legacy.d.ts +0 -2
- package/lib/legacy.js +0 -17
- package/lib/yaml.d.ts +0 -10
- package/lib/yaml.js +0 -274
package/lib/index.cjs
CHANGED
|
@@ -2,13 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
const jsoncEslintParser = require('jsonc-eslint-parser');
|
|
4
4
|
const shared = require('@intlify/shared');
|
|
5
|
-
const
|
|
5
|
+
const module$1 = require('node:module');
|
|
6
6
|
const sourceMap = require('source-map');
|
|
7
|
+
const MagicString = require('magic-string');
|
|
7
8
|
const yamlEslintParser = require('yaml-eslint-parser');
|
|
8
9
|
const acorn = require('acorn');
|
|
10
|
+
const escodegen = require('escodegen');
|
|
9
11
|
const estreeWalker = require('estree-walker');
|
|
10
|
-
const esquery = require('esquery');
|
|
11
12
|
|
|
13
|
+
const require$1 = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));
|
|
14
|
+
const {
|
|
15
|
+
baseCompile,
|
|
16
|
+
detectHtmlTag,
|
|
17
|
+
LocationStub
|
|
18
|
+
} = require$1("@intlify/message-compiler");
|
|
12
19
|
function createCodeGenerator(options = {
|
|
13
20
|
filename: "bundle.json",
|
|
14
21
|
sourceMap: false,
|
|
@@ -31,7 +38,7 @@ function createCodeGenerator(options = {
|
|
|
31
38
|
function push(code, node, name) {
|
|
32
39
|
_context.code += code;
|
|
33
40
|
if (_context.map && node) {
|
|
34
|
-
if (node.loc && node.loc !==
|
|
41
|
+
if (node.loc && node.loc !== LocationStub) {
|
|
35
42
|
addMapping(node.loc.start, name);
|
|
36
43
|
}
|
|
37
44
|
advancePositionWithSource(_context, code);
|
|
@@ -99,16 +106,36 @@ function advancePositionWithSource(pos, source, numberOfCharacters = source.leng
|
|
|
99
106
|
pos.column = lastNewLinePos === -1 ? pos.column + numberOfCharacters : numberOfCharacters - lastNewLinePos;
|
|
100
107
|
return pos;
|
|
101
108
|
}
|
|
109
|
+
const DETECT_MESSAGE = `Detected HTML in '{msg}' message.`;
|
|
110
|
+
const ON_ERROR_NOOP = () => {
|
|
111
|
+
};
|
|
112
|
+
function parsePath(path) {
|
|
113
|
+
return path ? path.join(".") : "";
|
|
114
|
+
}
|
|
102
115
|
function generateMessageFunction(msg, options, path) {
|
|
103
116
|
const env = options.env != null ? options.env : "development";
|
|
104
|
-
const
|
|
117
|
+
const strictMessage = shared.isBoolean(options.strictMessage) ? options.strictMessage : true;
|
|
118
|
+
const escapeHtml = !!options.escapeHtml;
|
|
119
|
+
const onError = options.onError || ON_ERROR_NOOP;
|
|
105
120
|
const errors = [];
|
|
121
|
+
let detecteHtmlInMsg = false;
|
|
122
|
+
if (detectHtmlTag(msg)) {
|
|
123
|
+
detecteHtmlInMsg = true;
|
|
124
|
+
if (strictMessage) {
|
|
125
|
+
const errMsg = shared.format(DETECT_MESSAGE, { msg });
|
|
126
|
+
onError(shared.format(errMsg), {
|
|
127
|
+
source: msg,
|
|
128
|
+
path: parsePath(path)
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const _msg = detecteHtmlInMsg && escapeHtml ? shared.escapeHtml(msg) : msg;
|
|
106
133
|
const newOptions = Object.assign({ mode: "arrow" }, options);
|
|
107
134
|
newOptions.onError = (err) => {
|
|
108
135
|
if (onError) {
|
|
109
136
|
const extra = {
|
|
110
137
|
source: msg,
|
|
111
|
-
path: path
|
|
138
|
+
path: parsePath(path),
|
|
112
139
|
code: err.code,
|
|
113
140
|
domain: err.domain,
|
|
114
141
|
location: err.location
|
|
@@ -117,10 +144,10 @@ function generateMessageFunction(msg, options, path) {
|
|
|
117
144
|
errors.push(err);
|
|
118
145
|
}
|
|
119
146
|
};
|
|
120
|
-
const { code, ast, map } =
|
|
147
|
+
const { code, ast, map } = baseCompile(_msg, newOptions);
|
|
121
148
|
const occured = errors.length > 0;
|
|
122
|
-
const genCode = !occured ? env === "development" ? `(()=>{const fn=${code};fn.source=${JSON.stringify(msg)};return fn;})()` : `${code}` :
|
|
123
|
-
return { code: genCode, ast, map };
|
|
149
|
+
const genCode = !occured ? env === "development" ? `(()=>{const fn=${code};fn.source=${JSON.stringify(msg)};return fn;})()` : `${code}` : _msg;
|
|
150
|
+
return { code: genCode, ast, map, errors };
|
|
124
151
|
}
|
|
125
152
|
function mapLinesColumns(resMap, codeMaps, inSourceMap) {
|
|
126
153
|
if (!resMap) {
|
|
@@ -189,8 +216,23 @@ function mapLinesColumns(resMap, codeMaps, inSourceMap) {
|
|
|
189
216
|
return generator.toJSON();
|
|
190
217
|
}
|
|
191
218
|
|
|
219
|
+
function generateLegacyCode({ isGlobal }, generator) {
|
|
220
|
+
const componentNamespace = `Component.options`;
|
|
221
|
+
const variableName = !isGlobal ? "__i18n" : "__i18nGlobal";
|
|
222
|
+
const exportSyntax = "export default";
|
|
223
|
+
const code = `${exportSyntax} function (Component) {
|
|
224
|
+
${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []
|
|
225
|
+
${componentNamespace}.${variableName}.push(${shared.friendlyJSONstringify(
|
|
226
|
+
generator()
|
|
227
|
+
)})
|
|
228
|
+
delete ${componentNamespace}._Ctor
|
|
229
|
+
}`;
|
|
230
|
+
return code;
|
|
231
|
+
}
|
|
232
|
+
|
|
192
233
|
function generate$2(targetSource, {
|
|
193
234
|
type = "plain",
|
|
235
|
+
legacy = false,
|
|
194
236
|
bridge = false,
|
|
195
237
|
exportESM = false,
|
|
196
238
|
filename = "vue-i18n-loader.json",
|
|
@@ -201,6 +243,8 @@ function generate$2(targetSource, {
|
|
|
201
243
|
env = "development",
|
|
202
244
|
forceStringify = false,
|
|
203
245
|
onError = void 0,
|
|
246
|
+
strictMessage = true,
|
|
247
|
+
escapeHtml = false,
|
|
204
248
|
useClassComponent = false
|
|
205
249
|
}, injector) {
|
|
206
250
|
const target = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
@@ -218,10 +262,26 @@ function generate$2(targetSource, {
|
|
|
218
262
|
filename,
|
|
219
263
|
forceStringify,
|
|
220
264
|
onError,
|
|
265
|
+
strictMessage,
|
|
266
|
+
escapeHtml,
|
|
221
267
|
useClassComponent
|
|
222
268
|
};
|
|
223
269
|
const generator = createCodeGenerator(options);
|
|
224
270
|
const ast = jsoncEslintParser.parseJSON(value, { filePath: filename });
|
|
271
|
+
if (legacy && type === "sfc") {
|
|
272
|
+
const gen = () => shared.friendlyJSONstringify(jsoncEslintParser.getStaticJSONValue(ast));
|
|
273
|
+
const code2 = generateLegacyCode(options, gen);
|
|
274
|
+
const s = new MagicString(code2);
|
|
275
|
+
return {
|
|
276
|
+
ast,
|
|
277
|
+
code: s.toString(),
|
|
278
|
+
map: s.generateMap({
|
|
279
|
+
file: filename,
|
|
280
|
+
source: value,
|
|
281
|
+
includeContent: true
|
|
282
|
+
})
|
|
283
|
+
};
|
|
284
|
+
}
|
|
225
285
|
const codeMaps = generateNode$2(generator, ast, options, injector);
|
|
226
286
|
const { code, map } = generator.context();
|
|
227
287
|
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
@@ -246,19 +306,21 @@ function generateNode$2(generator, node, options, injector) {
|
|
|
246
306
|
locale,
|
|
247
307
|
useClassComponent
|
|
248
308
|
} = options;
|
|
249
|
-
const componentNamespace =
|
|
309
|
+
const componentNamespace = "_Component";
|
|
250
310
|
jsoncEslintParser.traverseNodes(node, {
|
|
251
311
|
enterNode(node2, parent) {
|
|
252
312
|
switch (node2.type) {
|
|
253
313
|
case "Program":
|
|
254
314
|
if (type === "plain") {
|
|
255
|
-
generator.push(`
|
|
315
|
+
generator.push(`const resource = `);
|
|
256
316
|
} else if (type === "sfc") {
|
|
257
317
|
const variableName = type === "sfc" ? !isGlobal ? "__i18n" : "__i18nGlobal" : "";
|
|
258
318
|
const localeName = type === "sfc" ? locale != null ? locale : `""` : "";
|
|
259
319
|
const exportSyntax = bridge ? exportESM ? `export default` : `module.exports =` : `export default`;
|
|
260
320
|
generator.push(`${exportSyntax} function (Component) {`);
|
|
261
321
|
generator.indent();
|
|
322
|
+
const componentVariable = bridge ? `Component.options || Component` : useClassComponent ? `Component.__o || Component` : `Component`;
|
|
323
|
+
generator.pushline(`const ${componentNamespace} = ${componentVariable}`);
|
|
262
324
|
generator.pushline(
|
|
263
325
|
`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
|
|
264
326
|
);
|
|
@@ -384,6 +446,10 @@ function generateNode$2(generator, node, options, injector) {
|
|
|
384
446
|
}
|
|
385
447
|
generator.deindent();
|
|
386
448
|
generator.pushline(`}`);
|
|
449
|
+
} else if (type === "plain") {
|
|
450
|
+
generator.push(`
|
|
451
|
+
`);
|
|
452
|
+
generator.push("export default resource");
|
|
387
453
|
}
|
|
388
454
|
break;
|
|
389
455
|
case "JSONObjectExpression":
|
|
@@ -438,6 +504,7 @@ function generateNode$2(generator, node, options, injector) {
|
|
|
438
504
|
|
|
439
505
|
function generate$1(targetSource, {
|
|
440
506
|
type = "plain",
|
|
507
|
+
legacy = false,
|
|
441
508
|
bridge = false,
|
|
442
509
|
exportESM = false,
|
|
443
510
|
useClassComponent = false,
|
|
@@ -448,7 +515,9 @@ function generate$1(targetSource, {
|
|
|
448
515
|
sourceMap = false,
|
|
449
516
|
env = "development",
|
|
450
517
|
forceStringify = false,
|
|
451
|
-
onError = void 0
|
|
518
|
+
onError = void 0,
|
|
519
|
+
strictMessage = true,
|
|
520
|
+
escapeHtml = false
|
|
452
521
|
}, injector) {
|
|
453
522
|
const value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
454
523
|
const options = {
|
|
@@ -464,10 +533,26 @@ function generate$1(targetSource, {
|
|
|
464
533
|
filename,
|
|
465
534
|
forceStringify,
|
|
466
535
|
onError,
|
|
536
|
+
strictMessage,
|
|
537
|
+
escapeHtml,
|
|
467
538
|
useClassComponent
|
|
468
539
|
};
|
|
469
540
|
const generator = createCodeGenerator(options);
|
|
470
541
|
const ast = yamlEslintParser.parseYAML(value, { filePath: filename });
|
|
542
|
+
if (legacy && type === "sfc") {
|
|
543
|
+
const gen = () => shared.friendlyJSONstringify(yamlEslintParser.getStaticYAMLValue(ast));
|
|
544
|
+
const code2 = generateLegacyCode(options, gen);
|
|
545
|
+
const s = new MagicString(code2);
|
|
546
|
+
return {
|
|
547
|
+
ast,
|
|
548
|
+
code: s.toString(),
|
|
549
|
+
map: s.generateMap({
|
|
550
|
+
file: filename,
|
|
551
|
+
source: value,
|
|
552
|
+
includeContent: true
|
|
553
|
+
})
|
|
554
|
+
};
|
|
555
|
+
}
|
|
471
556
|
const codeMaps = generateNode$1(generator, ast, options, injector);
|
|
472
557
|
const { code, map } = generator.context();
|
|
473
558
|
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
@@ -492,19 +577,21 @@ function generateNode$1(generator, node, options, injector) {
|
|
|
492
577
|
locale,
|
|
493
578
|
useClassComponent
|
|
494
579
|
} = options;
|
|
495
|
-
const componentNamespace =
|
|
580
|
+
const componentNamespace = "_Component";
|
|
496
581
|
yamlEslintParser.traverseNodes(node, {
|
|
497
582
|
enterNode(node2, parent) {
|
|
498
583
|
switch (node2.type) {
|
|
499
584
|
case "Program":
|
|
500
585
|
if (type === "plain") {
|
|
501
|
-
generator.push(`
|
|
586
|
+
generator.push(`const resource = `);
|
|
502
587
|
} else if (type === "sfc") {
|
|
503
588
|
const variableName = type === "sfc" ? !isGlobal ? "__i18n" : "__i18nGlobal" : "";
|
|
504
589
|
const localeName = type === "sfc" ? locale != null ? locale : `""` : "";
|
|
505
590
|
const exportSyntax = bridge ? exportESM ? `export default` : `module.exports =` : `export default`;
|
|
506
591
|
generator.push(`${exportSyntax} function (Component) {`);
|
|
507
592
|
generator.indent();
|
|
593
|
+
const componentVariable = bridge ? `Component.options || Component` : useClassComponent ? `Component.__o || Component` : `Component`;
|
|
594
|
+
generator.pushline(`const ${componentNamespace} = ${componentVariable}`);
|
|
508
595
|
generator.pushline(
|
|
509
596
|
`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
|
|
510
597
|
);
|
|
@@ -630,6 +717,10 @@ function generateNode$1(generator, node, options, injector) {
|
|
|
630
717
|
}
|
|
631
718
|
generator.deindent();
|
|
632
719
|
generator.push(`}`);
|
|
720
|
+
} else if (type === "plain") {
|
|
721
|
+
generator.push(`
|
|
722
|
+
`);
|
|
723
|
+
generator.push("export default resource");
|
|
633
724
|
}
|
|
634
725
|
break;
|
|
635
726
|
case "YAMLMapping":
|
|
@@ -694,7 +785,10 @@ function generate(targetSource, {
|
|
|
694
785
|
env = "development",
|
|
695
786
|
forceStringify = false,
|
|
696
787
|
onError = void 0,
|
|
697
|
-
|
|
788
|
+
strictMessage = true,
|
|
789
|
+
escapeHtml = false,
|
|
790
|
+
useClassComponent = false,
|
|
791
|
+
allowDynamic = false
|
|
698
792
|
}, injector) {
|
|
699
793
|
const target = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
700
794
|
const value = target;
|
|
@@ -711,6 +805,8 @@ function generate(targetSource, {
|
|
|
711
805
|
filename,
|
|
712
806
|
forceStringify,
|
|
713
807
|
onError,
|
|
808
|
+
strictMessage,
|
|
809
|
+
escapeHtml,
|
|
714
810
|
useClassComponent
|
|
715
811
|
};
|
|
716
812
|
const generator = createCodeGenerator(options);
|
|
@@ -720,14 +816,26 @@ function generate(targetSource, {
|
|
|
720
816
|
sourceFile: filename,
|
|
721
817
|
allowImportExportEverywhere: true
|
|
722
818
|
});
|
|
723
|
-
const
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
819
|
+
const exportResult = scanAst(ast);
|
|
820
|
+
if (!allowDynamic) {
|
|
821
|
+
if (!exportResult || exportResult !== "object") {
|
|
822
|
+
throw new Error(
|
|
823
|
+
`You need to define an object as the locale message with 'export default'.`
|
|
824
|
+
);
|
|
825
|
+
}
|
|
826
|
+
} else {
|
|
827
|
+
if (!exportResult) {
|
|
828
|
+
throw new Error(
|
|
829
|
+
`You need to define 'export default' that will return the locale messages.`
|
|
830
|
+
);
|
|
831
|
+
}
|
|
832
|
+
if (exportResult !== "object") {
|
|
833
|
+
return {
|
|
834
|
+
ast,
|
|
835
|
+
code: value,
|
|
836
|
+
map: inSourceMap
|
|
837
|
+
};
|
|
838
|
+
}
|
|
731
839
|
}
|
|
732
840
|
const codeMaps = generateNode(generator, ast, options, injector);
|
|
733
841
|
const { code, map } = generator.context();
|
|
@@ -738,6 +846,27 @@ function generate(targetSource, {
|
|
|
738
846
|
map: newMap != null ? newMap : void 0
|
|
739
847
|
};
|
|
740
848
|
}
|
|
849
|
+
function scanAst(ast) {
|
|
850
|
+
if (ast.type !== "Program") {
|
|
851
|
+
throw new Error("Invalid AST: does not have Program node");
|
|
852
|
+
}
|
|
853
|
+
let ret = false;
|
|
854
|
+
for (const node of ast.body) {
|
|
855
|
+
if (node.type === "ExportDefaultDeclaration") {
|
|
856
|
+
if (node.declaration.type === "ObjectExpression") {
|
|
857
|
+
ret = "object";
|
|
858
|
+
break;
|
|
859
|
+
} else if (node.declaration.type === "FunctionDeclaration") {
|
|
860
|
+
ret = "function";
|
|
861
|
+
break;
|
|
862
|
+
} else if (node.declaration.type === "ArrowFunctionExpression") {
|
|
863
|
+
ret = "arrow-function";
|
|
864
|
+
break;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
return ret;
|
|
869
|
+
}
|
|
741
870
|
function generateNode(generator, node, options, injector) {
|
|
742
871
|
const propsCountStack = [];
|
|
743
872
|
const pathStack = [];
|
|
@@ -754,19 +883,28 @@ function generateNode(generator, node, options, injector) {
|
|
|
754
883
|
locale,
|
|
755
884
|
useClassComponent
|
|
756
885
|
} = options;
|
|
757
|
-
const componentNamespace =
|
|
886
|
+
const componentNamespace = "_Component";
|
|
758
887
|
estreeWalker.walk(node, {
|
|
888
|
+
/**
|
|
889
|
+
* NOTE:
|
|
890
|
+
* force cast to Node of `estree-walker@3.x`,
|
|
891
|
+
* because `estree-walker@3.x` is not dual packages,
|
|
892
|
+
* so it's support only esm only ...
|
|
893
|
+
*/
|
|
894
|
+
// @ts-ignore
|
|
759
895
|
enter(node2, parent) {
|
|
760
896
|
switch (node2.type) {
|
|
761
897
|
case "Program":
|
|
762
898
|
if (type === "plain") {
|
|
763
|
-
generator.push(`
|
|
899
|
+
generator.push(`const resource = `);
|
|
764
900
|
} else if (type === "sfc") {
|
|
765
901
|
const variableName = type === "sfc" ? !isGlobal ? "__i18n" : "__i18nGlobal" : "";
|
|
766
902
|
const localeName = type === "sfc" ? locale != null ? locale : `""` : "";
|
|
767
903
|
const exportSyntax = bridge ? exportESM ? `export default` : `module.exports =` : `export default`;
|
|
768
904
|
generator.push(`${exportSyntax} function (Component) {`);
|
|
769
905
|
generator.indent();
|
|
906
|
+
const componentVariable = bridge ? `Component.options || Component` : useClassComponent ? `Component.__o || Component` : `Component`;
|
|
907
|
+
generator.pushline(`const ${componentNamespace} = ${componentVariable}`);
|
|
770
908
|
generator.pushline(
|
|
771
909
|
`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
|
|
772
910
|
);
|
|
@@ -781,76 +919,85 @@ function generateNode(generator, node, options, injector) {
|
|
|
781
919
|
generator.indent();
|
|
782
920
|
propsCountStack.push(node2.properties.length);
|
|
783
921
|
if (parent != null && parent.type === "ArrayExpression") {
|
|
784
|
-
const
|
|
785
|
-
const currentCount = parent.elements.length - itemsCountStack[
|
|
922
|
+
const lastIndex = itemsCountStack.length - 1;
|
|
923
|
+
const currentCount = parent.elements.length - itemsCountStack[lastIndex];
|
|
786
924
|
pathStack.push(currentCount.toString());
|
|
787
|
-
itemsCountStack[
|
|
925
|
+
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
|
|
788
926
|
}
|
|
789
927
|
break;
|
|
790
928
|
case "Property":
|
|
791
|
-
if (
|
|
792
|
-
if (
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
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);
|
|
929
|
+
if (parent != null && parent.type === "ObjectExpression") {
|
|
930
|
+
if (node2 != null) {
|
|
931
|
+
if (isJSONablePrimitiveLiteral(node2.value) && (node2.key.type === "Literal" || node2.key.type === "Identifier")) {
|
|
932
|
+
const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
|
|
933
|
+
if (node2.value.type === "Literal" && shared.isString(node2.value.value) || node2.value.type === "TemplateLiteral") {
|
|
934
|
+
const value = getValue(node2.value);
|
|
810
935
|
generator.push(`${JSON.stringify(name)}: `);
|
|
811
936
|
pathStack.push(name);
|
|
812
937
|
const { code, map } = generateMessageFunction(
|
|
813
|
-
|
|
938
|
+
value,
|
|
814
939
|
options,
|
|
815
940
|
pathStack
|
|
816
941
|
);
|
|
817
|
-
sourceMap && map != null && codeMaps.set(
|
|
818
|
-
generator.push(`${code}`, node2.value,
|
|
942
|
+
sourceMap && map != null && codeMaps.set(value, map);
|
|
943
|
+
generator.push(`${code}`, node2.value, value);
|
|
944
|
+
skipStack.push(false);
|
|
819
945
|
} else {
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
946
|
+
const value = getValue(node2.value);
|
|
947
|
+
if (forceStringify) {
|
|
948
|
+
const strValue = JSON.stringify(value);
|
|
949
|
+
generator.push(`${JSON.stringify(name)}: `);
|
|
950
|
+
pathStack.push(name);
|
|
951
|
+
const { code, map } = generateMessageFunction(
|
|
952
|
+
strValue,
|
|
953
|
+
options,
|
|
954
|
+
pathStack
|
|
955
|
+
);
|
|
956
|
+
sourceMap && map != null && codeMaps.set(strValue, map);
|
|
957
|
+
generator.push(`${code}`, node2.value, strValue);
|
|
958
|
+
} else {
|
|
959
|
+
generator.push(
|
|
960
|
+
`${JSON.stringify(name)}: ${JSON.stringify(value)}`
|
|
961
|
+
);
|
|
962
|
+
pathStack.push(name);
|
|
963
|
+
}
|
|
964
|
+
skipStack.push(false);
|
|
824
965
|
}
|
|
966
|
+
} else if ((node2.value.type === "FunctionExpression" || node2.value.type === "ArrowFunctionExpression") && (node2.key.type === "Literal" || node2.key.type === "Identifier")) {
|
|
967
|
+
const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
|
|
968
|
+
generator.push(`${JSON.stringify(name)}: `);
|
|
969
|
+
pathStack.push(name);
|
|
970
|
+
const code = escodegen.generate(node2.value);
|
|
971
|
+
generator.push(`${code}`, node2.value, code);
|
|
825
972
|
skipStack.push(false);
|
|
973
|
+
} else if ((node2.value.type === "ObjectExpression" || node2.value.type === "ArrayExpression") && (node2.key.type === "Literal" || node2.key.type === "Identifier")) {
|
|
974
|
+
const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
|
|
975
|
+
generator.push(`${JSON.stringify(name)}: `);
|
|
976
|
+
pathStack.push(name);
|
|
977
|
+
} else {
|
|
978
|
+
skipStack.push(true);
|
|
826
979
|
}
|
|
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
980
|
}
|
|
981
|
+
const lastIndex = propsCountStack.length - 1;
|
|
982
|
+
propsCountStack[lastIndex] = --propsCountStack[lastIndex];
|
|
834
983
|
}
|
|
835
|
-
const lastIndex = propsCountStack.length - 1;
|
|
836
|
-
propsCountStack[lastIndex] = --propsCountStack[lastIndex];
|
|
837
984
|
break;
|
|
838
985
|
case "ArrayExpression":
|
|
839
986
|
generator.push(`[`);
|
|
840
987
|
generator.indent();
|
|
841
988
|
if (parent != null && parent.type === "ArrayExpression") {
|
|
842
|
-
const
|
|
843
|
-
const currentCount = parent.elements.length - itemsCountStack[
|
|
989
|
+
const lastIndex = itemsCountStack.length - 1;
|
|
990
|
+
const currentCount = parent.elements.length - itemsCountStack[lastIndex];
|
|
844
991
|
pathStack.push(currentCount.toString());
|
|
845
|
-
itemsCountStack[
|
|
992
|
+
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
|
|
846
993
|
}
|
|
847
994
|
itemsCountStack.push(node2.elements.length);
|
|
848
995
|
break;
|
|
849
996
|
default:
|
|
850
997
|
if (node2 != null && parent != null) {
|
|
851
998
|
if (parent.type === "ArrayExpression") {
|
|
852
|
-
const
|
|
853
|
-
const currentCount = parent.elements.length - itemsCountStack[
|
|
999
|
+
const lastIndex = itemsCountStack.length - 1;
|
|
1000
|
+
const currentCount = parent.elements.length - itemsCountStack[lastIndex];
|
|
854
1001
|
pathStack.push(currentCount.toString());
|
|
855
1002
|
if (isJSONablePrimitiveLiteral(node2)) {
|
|
856
1003
|
if (node2.type === "Literal" && shared.isString(node2.value) || node2.type === "TemplateLiteral") {
|
|
@@ -881,12 +1028,19 @@ function generateNode(generator, node, options, injector) {
|
|
|
881
1028
|
} else {
|
|
882
1029
|
skipStack.push(true);
|
|
883
1030
|
}
|
|
884
|
-
itemsCountStack[
|
|
1031
|
+
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
|
|
885
1032
|
}
|
|
886
1033
|
}
|
|
887
1034
|
break;
|
|
888
1035
|
}
|
|
889
1036
|
},
|
|
1037
|
+
/**
|
|
1038
|
+
* NOTE:
|
|
1039
|
+
* force cast to Node of `estree-walker@3.x`,
|
|
1040
|
+
* because `estree-walker@3.x` is not dual packages,
|
|
1041
|
+
* so it's support only esm only ...
|
|
1042
|
+
*/
|
|
1043
|
+
// @ts-ignore
|
|
890
1044
|
leave(node2, parent) {
|
|
891
1045
|
switch (node2.type) {
|
|
892
1046
|
case "Program":
|
|
@@ -905,6 +1059,10 @@ function generateNode(generator, node, options, injector) {
|
|
|
905
1059
|
}
|
|
906
1060
|
generator.deindent();
|
|
907
1061
|
generator.pushline(`}`);
|
|
1062
|
+
} else if (type === "plain") {
|
|
1063
|
+
generator.push(`
|
|
1064
|
+
`);
|
|
1065
|
+
generator.push("export default resource");
|
|
908
1066
|
}
|
|
909
1067
|
break;
|
|
910
1068
|
case "ObjectExpression":
|
|
@@ -922,10 +1080,12 @@ function generateNode(generator, node, options, injector) {
|
|
|
922
1080
|
}
|
|
923
1081
|
break;
|
|
924
1082
|
case "Property":
|
|
925
|
-
if (
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
1083
|
+
if (parent != null && parent.type === "ObjectExpression") {
|
|
1084
|
+
if (propsCountStack[propsCountStack.length - 1] !== 0) {
|
|
1085
|
+
pathStack.pop();
|
|
1086
|
+
if (!skipStack.pop()) {
|
|
1087
|
+
generator.pushline(`,`);
|
|
1088
|
+
}
|
|
929
1089
|
}
|
|
930
1090
|
}
|
|
931
1091
|
break;
|
|
@@ -971,17 +1131,18 @@ function getValue(node) {
|
|
|
971
1131
|
return node.type === "Literal" ? node.value : node.type === "TemplateLiteral" ? node.quasis.map((quasi) => quasi.value.cooked).join("") : void 0;
|
|
972
1132
|
}
|
|
973
1133
|
|
|
1134
|
+
const _require = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));
|
|
974
1135
|
function checkInstallPackage(pkg, debug) {
|
|
975
1136
|
let installedVueI18n = false;
|
|
976
1137
|
try {
|
|
977
|
-
debug(`vue-i18n load path: ${
|
|
1138
|
+
debug(`vue-i18n load path: ${_require.resolve("vue-i18n")}`);
|
|
978
1139
|
installedVueI18n = true;
|
|
979
1140
|
} catch (e) {
|
|
980
1141
|
debug(`cannot find 'vue-i18n'`, e);
|
|
981
1142
|
}
|
|
982
1143
|
let installedPetiteVueI18n = false;
|
|
983
1144
|
try {
|
|
984
|
-
debug(`petite-vue-i18n load path: ${
|
|
1145
|
+
debug(`petite-vue-i18n load path: ${_require.resolve("petite-vue-i18n")}`);
|
|
985
1146
|
installedPetiteVueI18n = true;
|
|
986
1147
|
} catch (e) {
|
|
987
1148
|
debug(`cannot find 'petite-vue-i18n'`, e);
|
|
@@ -999,7 +1160,7 @@ function checkInstallPackage(pkg, debug) {
|
|
|
999
1160
|
function checkVueI18nBridgeInstallPackage(debug) {
|
|
1000
1161
|
let ret = false;
|
|
1001
1162
|
try {
|
|
1002
|
-
debug(`vue-i18n-bridge load path: ${
|
|
1163
|
+
debug(`vue-i18n-bridge load path: ${_require.resolve("vue-i18n-bridge")}`);
|
|
1003
1164
|
ret = true;
|
|
1004
1165
|
} catch (e) {
|
|
1005
1166
|
debug(`cannot find 'vue-i18n-bridge'`, e);
|
|
@@ -1021,7 +1182,7 @@ function getVueI18nVersion(debug) {
|
|
|
1021
1182
|
}
|
|
1022
1183
|
function loadModule(moduleName, debug) {
|
|
1023
1184
|
try {
|
|
1024
|
-
return
|
|
1185
|
+
return _require(moduleName);
|
|
1025
1186
|
} catch (e) {
|
|
1026
1187
|
debug(`cannot load '${moduleName}'`, e);
|
|
1027
1188
|
return null;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,85 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { JSONProgram } from 'jsonc-eslint-parser/lib/parser/ast';
|
|
2
|
+
import { RawSourceMap } from 'source-map';
|
|
3
|
+
import { CompileError } from '@intlify/message-compiler';
|
|
4
|
+
import { YAMLProgram } from 'yaml-eslint-parser/lib/ast';
|
|
5
|
+
import { Node } from 'estree';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Compilation dev environments
|
|
9
|
+
*
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
type DevEnv = 'development' | 'production';
|
|
13
|
+
/**
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
interface CodeGenOptions {
|
|
17
|
+
type?: 'plain' | 'sfc' | 'bare';
|
|
18
|
+
legacy?: boolean;
|
|
19
|
+
bridge?: boolean;
|
|
20
|
+
exportESM?: boolean;
|
|
21
|
+
source?: string;
|
|
22
|
+
sourceMap?: boolean;
|
|
23
|
+
filename?: string;
|
|
24
|
+
inSourceMap?: RawSourceMap;
|
|
25
|
+
isGlobal?: boolean;
|
|
26
|
+
locale?: string;
|
|
27
|
+
env?: DevEnv;
|
|
28
|
+
forceStringify?: boolean;
|
|
29
|
+
useClassComponent?: boolean;
|
|
30
|
+
allowDynamic?: boolean;
|
|
31
|
+
strictMessage?: boolean;
|
|
32
|
+
escapeHtml?: boolean;
|
|
33
|
+
onWarn?: (msg: string) => void;
|
|
34
|
+
onError?: (msg: string, extra?: {
|
|
35
|
+
source: string;
|
|
36
|
+
path: string;
|
|
37
|
+
code?: CompileError['code'];
|
|
38
|
+
domain?: CompileError['domain'];
|
|
39
|
+
location?: CompileError['location'];
|
|
40
|
+
}) => void;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* @internal
|
|
44
|
+
*/
|
|
45
|
+
interface CodeGenResult<ASTNode, CodeGenError extends Error = Error> {
|
|
46
|
+
code: string;
|
|
47
|
+
ast: ASTNode;
|
|
48
|
+
errors?: CodeGenError[];
|
|
49
|
+
map?: RawSourceMap;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Code generator for i18n json/json5 resource
|
|
54
|
+
*/
|
|
55
|
+
|
|
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>;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Code generator for i18n yaml resource
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
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>;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Code generator for i18n js resource
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @internal
|
|
76
|
+
*/
|
|
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>;
|
|
78
|
+
|
|
79
|
+
type InstalledPackage = 'vue-i18n' | 'petite-vue-i18n';
|
|
80
|
+
declare function checkInstallPackage(pkg: string, debug: Function): InstalledPackage;
|
|
81
|
+
declare function checkVueI18nBridgeInstallPackage(debug: Function): boolean;
|
|
82
|
+
type VueI18nVersion = '8' | '9' | 'unknown' | '';
|
|
83
|
+
declare function getVueI18nVersion(debug: Function): VueI18nVersion;
|
|
84
|
+
|
|
85
|
+
export { CodeGenOptions, CodeGenResult, DevEnv, InstalledPackage, checkInstallPackage, checkVueI18nBridgeInstallPackage, generate$2 as generateJSON, generate as generateJavaScript, generate$1 as generateYAML, getVueI18nVersion };
|