@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.mjs
CHANGED
|
@@ -1,21 +1,19 @@
|
|
|
1
|
-
import { parseJSON, traverseNodes } from 'jsonc-eslint-parser';
|
|
2
|
-
import { isString, isNumber
|
|
3
|
-
import
|
|
1
|
+
import { parseJSON, traverseNodes, getStaticJSONValue } from 'jsonc-eslint-parser';
|
|
2
|
+
import { isBoolean, format, escapeHtml, friendlyJSONstringify, isString, isNumber } from '@intlify/shared';
|
|
3
|
+
import module from 'node:module';
|
|
4
4
|
import { SourceMapGenerator, SourceMapConsumer } from 'source-map';
|
|
5
|
-
import
|
|
5
|
+
import MagicString from 'magic-string';
|
|
6
|
+
import { parseYAML, traverseNodes as traverseNodes$1, getStaticYAMLValue } from 'yaml-eslint-parser';
|
|
6
7
|
import { parse } from 'acorn';
|
|
8
|
+
import { generate as generate$3 } from 'escodegen';
|
|
7
9
|
import { walk } from 'estree-walker';
|
|
8
|
-
import esquery from 'esquery';
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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);
|
|
11
|
+
const require = module.createRequire(import.meta.url);
|
|
12
|
+
const {
|
|
13
|
+
baseCompile,
|
|
14
|
+
detectHtmlTag,
|
|
15
|
+
LocationStub
|
|
16
|
+
} = require("@intlify/message-compiler");
|
|
19
17
|
function createCodeGenerator(options = {
|
|
20
18
|
filename: "bundle.json",
|
|
21
19
|
sourceMap: false,
|
|
@@ -106,16 +104,36 @@ function advancePositionWithSource(pos, source, numberOfCharacters = source.leng
|
|
|
106
104
|
pos.column = lastNewLinePos === -1 ? pos.column + numberOfCharacters : numberOfCharacters - lastNewLinePos;
|
|
107
105
|
return pos;
|
|
108
106
|
}
|
|
107
|
+
const DETECT_MESSAGE = `Detected HTML in '{msg}' message.`;
|
|
108
|
+
const ON_ERROR_NOOP = () => {
|
|
109
|
+
};
|
|
110
|
+
function parsePath(path) {
|
|
111
|
+
return path ? path.join(".") : "";
|
|
112
|
+
}
|
|
109
113
|
function generateMessageFunction(msg, options, path) {
|
|
110
114
|
const env = options.env != null ? options.env : "development";
|
|
111
|
-
const
|
|
115
|
+
const strictMessage = isBoolean(options.strictMessage) ? options.strictMessage : true;
|
|
116
|
+
const escapeHtml$1 = !!options.escapeHtml;
|
|
117
|
+
const onError = options.onError || ON_ERROR_NOOP;
|
|
112
118
|
const errors = [];
|
|
119
|
+
let detecteHtmlInMsg = false;
|
|
120
|
+
if (detectHtmlTag(msg)) {
|
|
121
|
+
detecteHtmlInMsg = true;
|
|
122
|
+
if (strictMessage) {
|
|
123
|
+
const errMsg = format(DETECT_MESSAGE, { msg });
|
|
124
|
+
onError(format(errMsg), {
|
|
125
|
+
source: msg,
|
|
126
|
+
path: parsePath(path)
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const _msg = detecteHtmlInMsg && escapeHtml$1 ? escapeHtml(msg) : msg;
|
|
113
131
|
const newOptions = Object.assign({ mode: "arrow" }, options);
|
|
114
132
|
newOptions.onError = (err) => {
|
|
115
133
|
if (onError) {
|
|
116
134
|
const extra = {
|
|
117
135
|
source: msg,
|
|
118
|
-
path: path
|
|
136
|
+
path: parsePath(path),
|
|
119
137
|
code: err.code,
|
|
120
138
|
domain: err.domain,
|
|
121
139
|
location: err.location
|
|
@@ -124,10 +142,10 @@ function generateMessageFunction(msg, options, path) {
|
|
|
124
142
|
errors.push(err);
|
|
125
143
|
}
|
|
126
144
|
};
|
|
127
|
-
const { code, ast, map } = baseCompile(
|
|
145
|
+
const { code, ast, map } = baseCompile(_msg, newOptions);
|
|
128
146
|
const occured = errors.length > 0;
|
|
129
|
-
const genCode = !occured ? env === "development" ? `(()=>{const fn=${code};fn.source=${JSON.stringify(msg)};return fn;})()` : `${code}` :
|
|
130
|
-
return { code: genCode, ast, map };
|
|
147
|
+
const genCode = !occured ? env === "development" ? `(()=>{const fn=${code};fn.source=${JSON.stringify(msg)};return fn;})()` : `${code}` : _msg;
|
|
148
|
+
return { code: genCode, ast, map, errors };
|
|
131
149
|
}
|
|
132
150
|
function mapLinesColumns(resMap, codeMaps, inSourceMap) {
|
|
133
151
|
if (!resMap) {
|
|
@@ -196,8 +214,23 @@ function mapLinesColumns(resMap, codeMaps, inSourceMap) {
|
|
|
196
214
|
return generator.toJSON();
|
|
197
215
|
}
|
|
198
216
|
|
|
217
|
+
function generateLegacyCode({ isGlobal }, generator) {
|
|
218
|
+
const componentNamespace = `Component.options`;
|
|
219
|
+
const variableName = !isGlobal ? "__i18n" : "__i18nGlobal";
|
|
220
|
+
const exportSyntax = "export default";
|
|
221
|
+
const code = `${exportSyntax} function (Component) {
|
|
222
|
+
${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []
|
|
223
|
+
${componentNamespace}.${variableName}.push(${friendlyJSONstringify(
|
|
224
|
+
generator()
|
|
225
|
+
)})
|
|
226
|
+
delete ${componentNamespace}._Ctor
|
|
227
|
+
}`;
|
|
228
|
+
return code;
|
|
229
|
+
}
|
|
230
|
+
|
|
199
231
|
function generate$2(targetSource, {
|
|
200
232
|
type = "plain",
|
|
233
|
+
legacy = false,
|
|
201
234
|
bridge = false,
|
|
202
235
|
exportESM = false,
|
|
203
236
|
filename = "vue-i18n-loader.json",
|
|
@@ -208,6 +241,8 @@ function generate$2(targetSource, {
|
|
|
208
241
|
env = "development",
|
|
209
242
|
forceStringify = false,
|
|
210
243
|
onError = void 0,
|
|
244
|
+
strictMessage = true,
|
|
245
|
+
escapeHtml = false,
|
|
211
246
|
useClassComponent = false
|
|
212
247
|
}, injector) {
|
|
213
248
|
const target = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
@@ -225,10 +260,26 @@ function generate$2(targetSource, {
|
|
|
225
260
|
filename,
|
|
226
261
|
forceStringify,
|
|
227
262
|
onError,
|
|
263
|
+
strictMessage,
|
|
264
|
+
escapeHtml,
|
|
228
265
|
useClassComponent
|
|
229
266
|
};
|
|
230
267
|
const generator = createCodeGenerator(options);
|
|
231
268
|
const ast = parseJSON(value, { filePath: filename });
|
|
269
|
+
if (legacy && type === "sfc") {
|
|
270
|
+
const gen = () => friendlyJSONstringify(getStaticJSONValue(ast));
|
|
271
|
+
const code2 = generateLegacyCode(options, gen);
|
|
272
|
+
const s = new MagicString(code2);
|
|
273
|
+
return {
|
|
274
|
+
ast,
|
|
275
|
+
code: s.toString(),
|
|
276
|
+
map: s.generateMap({
|
|
277
|
+
file: filename,
|
|
278
|
+
source: value,
|
|
279
|
+
includeContent: true
|
|
280
|
+
})
|
|
281
|
+
};
|
|
282
|
+
}
|
|
232
283
|
const codeMaps = generateNode$2(generator, ast, options, injector);
|
|
233
284
|
const { code, map } = generator.context();
|
|
234
285
|
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
@@ -253,19 +304,21 @@ function generateNode$2(generator, node, options, injector) {
|
|
|
253
304
|
locale,
|
|
254
305
|
useClassComponent
|
|
255
306
|
} = options;
|
|
256
|
-
const componentNamespace =
|
|
307
|
+
const componentNamespace = "_Component";
|
|
257
308
|
traverseNodes(node, {
|
|
258
309
|
enterNode(node2, parent) {
|
|
259
310
|
switch (node2.type) {
|
|
260
311
|
case "Program":
|
|
261
312
|
if (type === "plain") {
|
|
262
|
-
generator.push(`
|
|
313
|
+
generator.push(`const resource = `);
|
|
263
314
|
} else if (type === "sfc") {
|
|
264
315
|
const variableName = type === "sfc" ? !isGlobal ? "__i18n" : "__i18nGlobal" : "";
|
|
265
316
|
const localeName = type === "sfc" ? locale != null ? locale : `""` : "";
|
|
266
317
|
const exportSyntax = bridge ? exportESM ? `export default` : `module.exports =` : `export default`;
|
|
267
318
|
generator.push(`${exportSyntax} function (Component) {`);
|
|
268
319
|
generator.indent();
|
|
320
|
+
const componentVariable = bridge ? `Component.options || Component` : useClassComponent ? `Component.__o || Component` : `Component`;
|
|
321
|
+
generator.pushline(`const ${componentNamespace} = ${componentVariable}`);
|
|
269
322
|
generator.pushline(
|
|
270
323
|
`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
|
|
271
324
|
);
|
|
@@ -391,6 +444,10 @@ function generateNode$2(generator, node, options, injector) {
|
|
|
391
444
|
}
|
|
392
445
|
generator.deindent();
|
|
393
446
|
generator.pushline(`}`);
|
|
447
|
+
} else if (type === "plain") {
|
|
448
|
+
generator.push(`
|
|
449
|
+
`);
|
|
450
|
+
generator.push("export default resource");
|
|
394
451
|
}
|
|
395
452
|
break;
|
|
396
453
|
case "JSONObjectExpression":
|
|
@@ -445,6 +502,7 @@ function generateNode$2(generator, node, options, injector) {
|
|
|
445
502
|
|
|
446
503
|
function generate$1(targetSource, {
|
|
447
504
|
type = "plain",
|
|
505
|
+
legacy = false,
|
|
448
506
|
bridge = false,
|
|
449
507
|
exportESM = false,
|
|
450
508
|
useClassComponent = false,
|
|
@@ -455,7 +513,9 @@ function generate$1(targetSource, {
|
|
|
455
513
|
sourceMap = false,
|
|
456
514
|
env = "development",
|
|
457
515
|
forceStringify = false,
|
|
458
|
-
onError = void 0
|
|
516
|
+
onError = void 0,
|
|
517
|
+
strictMessage = true,
|
|
518
|
+
escapeHtml = false
|
|
459
519
|
}, injector) {
|
|
460
520
|
const value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
461
521
|
const options = {
|
|
@@ -471,10 +531,26 @@ function generate$1(targetSource, {
|
|
|
471
531
|
filename,
|
|
472
532
|
forceStringify,
|
|
473
533
|
onError,
|
|
534
|
+
strictMessage,
|
|
535
|
+
escapeHtml,
|
|
474
536
|
useClassComponent
|
|
475
537
|
};
|
|
476
538
|
const generator = createCodeGenerator(options);
|
|
477
539
|
const ast = parseYAML(value, { filePath: filename });
|
|
540
|
+
if (legacy && type === "sfc") {
|
|
541
|
+
const gen = () => friendlyJSONstringify(getStaticYAMLValue(ast));
|
|
542
|
+
const code2 = generateLegacyCode(options, gen);
|
|
543
|
+
const s = new MagicString(code2);
|
|
544
|
+
return {
|
|
545
|
+
ast,
|
|
546
|
+
code: s.toString(),
|
|
547
|
+
map: s.generateMap({
|
|
548
|
+
file: filename,
|
|
549
|
+
source: value,
|
|
550
|
+
includeContent: true
|
|
551
|
+
})
|
|
552
|
+
};
|
|
553
|
+
}
|
|
478
554
|
const codeMaps = generateNode$1(generator, ast, options, injector);
|
|
479
555
|
const { code, map } = generator.context();
|
|
480
556
|
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
@@ -499,19 +575,21 @@ function generateNode$1(generator, node, options, injector) {
|
|
|
499
575
|
locale,
|
|
500
576
|
useClassComponent
|
|
501
577
|
} = options;
|
|
502
|
-
const componentNamespace =
|
|
578
|
+
const componentNamespace = "_Component";
|
|
503
579
|
traverseNodes$1(node, {
|
|
504
580
|
enterNode(node2, parent) {
|
|
505
581
|
switch (node2.type) {
|
|
506
582
|
case "Program":
|
|
507
583
|
if (type === "plain") {
|
|
508
|
-
generator.push(`
|
|
584
|
+
generator.push(`const resource = `);
|
|
509
585
|
} else if (type === "sfc") {
|
|
510
586
|
const variableName = type === "sfc" ? !isGlobal ? "__i18n" : "__i18nGlobal" : "";
|
|
511
587
|
const localeName = type === "sfc" ? locale != null ? locale : `""` : "";
|
|
512
588
|
const exportSyntax = bridge ? exportESM ? `export default` : `module.exports =` : `export default`;
|
|
513
589
|
generator.push(`${exportSyntax} function (Component) {`);
|
|
514
590
|
generator.indent();
|
|
591
|
+
const componentVariable = bridge ? `Component.options || Component` : useClassComponent ? `Component.__o || Component` : `Component`;
|
|
592
|
+
generator.pushline(`const ${componentNamespace} = ${componentVariable}`);
|
|
515
593
|
generator.pushline(
|
|
516
594
|
`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
|
|
517
595
|
);
|
|
@@ -637,6 +715,10 @@ function generateNode$1(generator, node, options, injector) {
|
|
|
637
715
|
}
|
|
638
716
|
generator.deindent();
|
|
639
717
|
generator.push(`}`);
|
|
718
|
+
} else if (type === "plain") {
|
|
719
|
+
generator.push(`
|
|
720
|
+
`);
|
|
721
|
+
generator.push("export default resource");
|
|
640
722
|
}
|
|
641
723
|
break;
|
|
642
724
|
case "YAMLMapping":
|
|
@@ -701,7 +783,10 @@ function generate(targetSource, {
|
|
|
701
783
|
env = "development",
|
|
702
784
|
forceStringify = false,
|
|
703
785
|
onError = void 0,
|
|
704
|
-
|
|
786
|
+
strictMessage = true,
|
|
787
|
+
escapeHtml = false,
|
|
788
|
+
useClassComponent = false,
|
|
789
|
+
allowDynamic = false
|
|
705
790
|
}, injector) {
|
|
706
791
|
const target = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
707
792
|
const value = target;
|
|
@@ -718,6 +803,8 @@ function generate(targetSource, {
|
|
|
718
803
|
filename,
|
|
719
804
|
forceStringify,
|
|
720
805
|
onError,
|
|
806
|
+
strictMessage,
|
|
807
|
+
escapeHtml,
|
|
721
808
|
useClassComponent
|
|
722
809
|
};
|
|
723
810
|
const generator = createCodeGenerator(options);
|
|
@@ -727,14 +814,26 @@ function generate(targetSource, {
|
|
|
727
814
|
sourceFile: filename,
|
|
728
815
|
allowImportExportEverywhere: true
|
|
729
816
|
});
|
|
730
|
-
const
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
817
|
+
const exportResult = scanAst(ast);
|
|
818
|
+
if (!allowDynamic) {
|
|
819
|
+
if (!exportResult || exportResult !== "object") {
|
|
820
|
+
throw new Error(
|
|
821
|
+
`You need to define an object as the locale message with 'export default'.`
|
|
822
|
+
);
|
|
823
|
+
}
|
|
824
|
+
} else {
|
|
825
|
+
if (!exportResult) {
|
|
826
|
+
throw new Error(
|
|
827
|
+
`You need to define 'export default' that will return the locale messages.`
|
|
828
|
+
);
|
|
829
|
+
}
|
|
830
|
+
if (exportResult !== "object") {
|
|
831
|
+
return {
|
|
832
|
+
ast,
|
|
833
|
+
code: value,
|
|
834
|
+
map: inSourceMap
|
|
835
|
+
};
|
|
836
|
+
}
|
|
738
837
|
}
|
|
739
838
|
const codeMaps = generateNode(generator, ast, options, injector);
|
|
740
839
|
const { code, map } = generator.context();
|
|
@@ -745,6 +844,27 @@ function generate(targetSource, {
|
|
|
745
844
|
map: newMap != null ? newMap : void 0
|
|
746
845
|
};
|
|
747
846
|
}
|
|
847
|
+
function scanAst(ast) {
|
|
848
|
+
if (ast.type !== "Program") {
|
|
849
|
+
throw new Error("Invalid AST: does not have Program node");
|
|
850
|
+
}
|
|
851
|
+
let ret = false;
|
|
852
|
+
for (const node of ast.body) {
|
|
853
|
+
if (node.type === "ExportDefaultDeclaration") {
|
|
854
|
+
if (node.declaration.type === "ObjectExpression") {
|
|
855
|
+
ret = "object";
|
|
856
|
+
break;
|
|
857
|
+
} else if (node.declaration.type === "FunctionDeclaration") {
|
|
858
|
+
ret = "function";
|
|
859
|
+
break;
|
|
860
|
+
} else if (node.declaration.type === "ArrowFunctionExpression") {
|
|
861
|
+
ret = "arrow-function";
|
|
862
|
+
break;
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
return ret;
|
|
867
|
+
}
|
|
748
868
|
function generateNode(generator, node, options, injector) {
|
|
749
869
|
const propsCountStack = [];
|
|
750
870
|
const pathStack = [];
|
|
@@ -761,19 +881,28 @@ function generateNode(generator, node, options, injector) {
|
|
|
761
881
|
locale,
|
|
762
882
|
useClassComponent
|
|
763
883
|
} = options;
|
|
764
|
-
const componentNamespace =
|
|
884
|
+
const componentNamespace = "_Component";
|
|
765
885
|
walk(node, {
|
|
886
|
+
/**
|
|
887
|
+
* NOTE:
|
|
888
|
+
* force cast to Node of `estree-walker@3.x`,
|
|
889
|
+
* because `estree-walker@3.x` is not dual packages,
|
|
890
|
+
* so it's support only esm only ...
|
|
891
|
+
*/
|
|
892
|
+
// @ts-ignore
|
|
766
893
|
enter(node2, parent) {
|
|
767
894
|
switch (node2.type) {
|
|
768
895
|
case "Program":
|
|
769
896
|
if (type === "plain") {
|
|
770
|
-
generator.push(`
|
|
897
|
+
generator.push(`const resource = `);
|
|
771
898
|
} else if (type === "sfc") {
|
|
772
899
|
const variableName = type === "sfc" ? !isGlobal ? "__i18n" : "__i18nGlobal" : "";
|
|
773
900
|
const localeName = type === "sfc" ? locale != null ? locale : `""` : "";
|
|
774
901
|
const exportSyntax = bridge ? exportESM ? `export default` : `module.exports =` : `export default`;
|
|
775
902
|
generator.push(`${exportSyntax} function (Component) {`);
|
|
776
903
|
generator.indent();
|
|
904
|
+
const componentVariable = bridge ? `Component.options || Component` : useClassComponent ? `Component.__o || Component` : `Component`;
|
|
905
|
+
generator.pushline(`const ${componentNamespace} = ${componentVariable}`);
|
|
777
906
|
generator.pushline(
|
|
778
907
|
`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
|
|
779
908
|
);
|
|
@@ -788,76 +917,85 @@ function generateNode(generator, node, options, injector) {
|
|
|
788
917
|
generator.indent();
|
|
789
918
|
propsCountStack.push(node2.properties.length);
|
|
790
919
|
if (parent != null && parent.type === "ArrayExpression") {
|
|
791
|
-
const
|
|
792
|
-
const currentCount = parent.elements.length - itemsCountStack[
|
|
920
|
+
const lastIndex = itemsCountStack.length - 1;
|
|
921
|
+
const currentCount = parent.elements.length - itemsCountStack[lastIndex];
|
|
793
922
|
pathStack.push(currentCount.toString());
|
|
794
|
-
itemsCountStack[
|
|
923
|
+
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
|
|
795
924
|
}
|
|
796
925
|
break;
|
|
797
926
|
case "Property":
|
|
798
|
-
if (
|
|
799
|
-
if (
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
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);
|
|
927
|
+
if (parent != null && parent.type === "ObjectExpression") {
|
|
928
|
+
if (node2 != null) {
|
|
929
|
+
if (isJSONablePrimitiveLiteral(node2.value) && (node2.key.type === "Literal" || node2.key.type === "Identifier")) {
|
|
930
|
+
const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
|
|
931
|
+
if (node2.value.type === "Literal" && isString(node2.value.value) || node2.value.type === "TemplateLiteral") {
|
|
932
|
+
const value = getValue(node2.value);
|
|
817
933
|
generator.push(`${JSON.stringify(name)}: `);
|
|
818
934
|
pathStack.push(name);
|
|
819
935
|
const { code, map } = generateMessageFunction(
|
|
820
|
-
|
|
936
|
+
value,
|
|
821
937
|
options,
|
|
822
938
|
pathStack
|
|
823
939
|
);
|
|
824
|
-
sourceMap && map != null && codeMaps.set(
|
|
825
|
-
generator.push(`${code}`, node2.value,
|
|
940
|
+
sourceMap && map != null && codeMaps.set(value, map);
|
|
941
|
+
generator.push(`${code}`, node2.value, value);
|
|
942
|
+
skipStack.push(false);
|
|
826
943
|
} else {
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
944
|
+
const value = getValue(node2.value);
|
|
945
|
+
if (forceStringify) {
|
|
946
|
+
const strValue = JSON.stringify(value);
|
|
947
|
+
generator.push(`${JSON.stringify(name)}: `);
|
|
948
|
+
pathStack.push(name);
|
|
949
|
+
const { code, map } = generateMessageFunction(
|
|
950
|
+
strValue,
|
|
951
|
+
options,
|
|
952
|
+
pathStack
|
|
953
|
+
);
|
|
954
|
+
sourceMap && map != null && codeMaps.set(strValue, map);
|
|
955
|
+
generator.push(`${code}`, node2.value, strValue);
|
|
956
|
+
} else {
|
|
957
|
+
generator.push(
|
|
958
|
+
`${JSON.stringify(name)}: ${JSON.stringify(value)}`
|
|
959
|
+
);
|
|
960
|
+
pathStack.push(name);
|
|
961
|
+
}
|
|
962
|
+
skipStack.push(false);
|
|
831
963
|
}
|
|
964
|
+
} else if ((node2.value.type === "FunctionExpression" || node2.value.type === "ArrowFunctionExpression") && (node2.key.type === "Literal" || node2.key.type === "Identifier")) {
|
|
965
|
+
const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
|
|
966
|
+
generator.push(`${JSON.stringify(name)}: `);
|
|
967
|
+
pathStack.push(name);
|
|
968
|
+
const code = generate$3(node2.value);
|
|
969
|
+
generator.push(`${code}`, node2.value, code);
|
|
832
970
|
skipStack.push(false);
|
|
971
|
+
} else if ((node2.value.type === "ObjectExpression" || node2.value.type === "ArrayExpression") && (node2.key.type === "Literal" || node2.key.type === "Identifier")) {
|
|
972
|
+
const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
|
|
973
|
+
generator.push(`${JSON.stringify(name)}: `);
|
|
974
|
+
pathStack.push(name);
|
|
975
|
+
} else {
|
|
976
|
+
skipStack.push(true);
|
|
833
977
|
}
|
|
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
978
|
}
|
|
979
|
+
const lastIndex = propsCountStack.length - 1;
|
|
980
|
+
propsCountStack[lastIndex] = --propsCountStack[lastIndex];
|
|
841
981
|
}
|
|
842
|
-
const lastIndex = propsCountStack.length - 1;
|
|
843
|
-
propsCountStack[lastIndex] = --propsCountStack[lastIndex];
|
|
844
982
|
break;
|
|
845
983
|
case "ArrayExpression":
|
|
846
984
|
generator.push(`[`);
|
|
847
985
|
generator.indent();
|
|
848
986
|
if (parent != null && parent.type === "ArrayExpression") {
|
|
849
|
-
const
|
|
850
|
-
const currentCount = parent.elements.length - itemsCountStack[
|
|
987
|
+
const lastIndex = itemsCountStack.length - 1;
|
|
988
|
+
const currentCount = parent.elements.length - itemsCountStack[lastIndex];
|
|
851
989
|
pathStack.push(currentCount.toString());
|
|
852
|
-
itemsCountStack[
|
|
990
|
+
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
|
|
853
991
|
}
|
|
854
992
|
itemsCountStack.push(node2.elements.length);
|
|
855
993
|
break;
|
|
856
994
|
default:
|
|
857
995
|
if (node2 != null && parent != null) {
|
|
858
996
|
if (parent.type === "ArrayExpression") {
|
|
859
|
-
const
|
|
860
|
-
const currentCount = parent.elements.length - itemsCountStack[
|
|
997
|
+
const lastIndex = itemsCountStack.length - 1;
|
|
998
|
+
const currentCount = parent.elements.length - itemsCountStack[lastIndex];
|
|
861
999
|
pathStack.push(currentCount.toString());
|
|
862
1000
|
if (isJSONablePrimitiveLiteral(node2)) {
|
|
863
1001
|
if (node2.type === "Literal" && isString(node2.value) || node2.type === "TemplateLiteral") {
|
|
@@ -888,12 +1026,19 @@ function generateNode(generator, node, options, injector) {
|
|
|
888
1026
|
} else {
|
|
889
1027
|
skipStack.push(true);
|
|
890
1028
|
}
|
|
891
|
-
itemsCountStack[
|
|
1029
|
+
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
|
|
892
1030
|
}
|
|
893
1031
|
}
|
|
894
1032
|
break;
|
|
895
1033
|
}
|
|
896
1034
|
},
|
|
1035
|
+
/**
|
|
1036
|
+
* NOTE:
|
|
1037
|
+
* force cast to Node of `estree-walker@3.x`,
|
|
1038
|
+
* because `estree-walker@3.x` is not dual packages,
|
|
1039
|
+
* so it's support only esm only ...
|
|
1040
|
+
*/
|
|
1041
|
+
// @ts-ignore
|
|
897
1042
|
leave(node2, parent) {
|
|
898
1043
|
switch (node2.type) {
|
|
899
1044
|
case "Program":
|
|
@@ -912,6 +1057,10 @@ function generateNode(generator, node, options, injector) {
|
|
|
912
1057
|
}
|
|
913
1058
|
generator.deindent();
|
|
914
1059
|
generator.pushline(`}`);
|
|
1060
|
+
} else if (type === "plain") {
|
|
1061
|
+
generator.push(`
|
|
1062
|
+
`);
|
|
1063
|
+
generator.push("export default resource");
|
|
915
1064
|
}
|
|
916
1065
|
break;
|
|
917
1066
|
case "ObjectExpression":
|
|
@@ -929,10 +1078,12 @@ function generateNode(generator, node, options, injector) {
|
|
|
929
1078
|
}
|
|
930
1079
|
break;
|
|
931
1080
|
case "Property":
|
|
932
|
-
if (
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
1081
|
+
if (parent != null && parent.type === "ObjectExpression") {
|
|
1082
|
+
if (propsCountStack[propsCountStack.length - 1] !== 0) {
|
|
1083
|
+
pathStack.pop();
|
|
1084
|
+
if (!skipStack.pop()) {
|
|
1085
|
+
generator.pushline(`,`);
|
|
1086
|
+
}
|
|
936
1087
|
}
|
|
937
1088
|
}
|
|
938
1089
|
break;
|
|
@@ -978,17 +1129,18 @@ function getValue(node) {
|
|
|
978
1129
|
return node.type === "Literal" ? node.value : node.type === "TemplateLiteral" ? node.quasis.map((quasi) => quasi.value.cooked).join("") : void 0;
|
|
979
1130
|
}
|
|
980
1131
|
|
|
1132
|
+
const _require = module.createRequire(import.meta.url);
|
|
981
1133
|
function checkInstallPackage(pkg, debug) {
|
|
982
1134
|
let installedVueI18n = false;
|
|
983
1135
|
try {
|
|
984
|
-
debug(`vue-i18n load path: ${
|
|
1136
|
+
debug(`vue-i18n load path: ${_require.resolve("vue-i18n")}`);
|
|
985
1137
|
installedVueI18n = true;
|
|
986
1138
|
} catch (e) {
|
|
987
1139
|
debug(`cannot find 'vue-i18n'`, e);
|
|
988
1140
|
}
|
|
989
1141
|
let installedPetiteVueI18n = false;
|
|
990
1142
|
try {
|
|
991
|
-
debug(`petite-vue-i18n load path: ${
|
|
1143
|
+
debug(`petite-vue-i18n load path: ${_require.resolve("petite-vue-i18n")}`);
|
|
992
1144
|
installedPetiteVueI18n = true;
|
|
993
1145
|
} catch (e) {
|
|
994
1146
|
debug(`cannot find 'petite-vue-i18n'`, e);
|
|
@@ -1006,7 +1158,7 @@ function checkInstallPackage(pkg, debug) {
|
|
|
1006
1158
|
function checkVueI18nBridgeInstallPackage(debug) {
|
|
1007
1159
|
let ret = false;
|
|
1008
1160
|
try {
|
|
1009
|
-
debug(`vue-i18n-bridge load path: ${
|
|
1161
|
+
debug(`vue-i18n-bridge load path: ${_require.resolve("vue-i18n-bridge")}`);
|
|
1010
1162
|
ret = true;
|
|
1011
1163
|
} catch (e) {
|
|
1012
1164
|
debug(`cannot find 'vue-i18n-bridge'`, e);
|
|
@@ -1028,7 +1180,7 @@ function getVueI18nVersion(debug) {
|
|
|
1028
1180
|
}
|
|
1029
1181
|
function loadModule(moduleName, debug) {
|
|
1030
1182
|
try {
|
|
1031
|
-
return
|
|
1183
|
+
return _require(moduleName);
|
|
1032
1184
|
} catch (e) {
|
|
1033
1185
|
debug(`cannot load '${moduleName}'`, e);
|
|
1034
1186
|
return null;
|
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": "6.0.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -25,15 +25,17 @@
|
|
|
25
25
|
"estree-walker": "^2.0.2",
|
|
26
26
|
"jsonc-eslint-parser": "^1.0.1",
|
|
27
27
|
"magic-string": "^0.30.0",
|
|
28
|
+
"mlly": "^1.2.0",
|
|
28
29
|
"source-map": "0.6.1",
|
|
29
30
|
"yaml-eslint-parser": "^0.3.2"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
33
|
"@types/escodegen": "^0.0.7",
|
|
33
|
-
"@types/estree": "^1.0.0"
|
|
34
|
+
"@types/estree": "^1.0.0",
|
|
35
|
+
"unbuild": "^1.1.2"
|
|
34
36
|
},
|
|
35
37
|
"engines": {
|
|
36
|
-
"node": ">=
|
|
38
|
+
"node": ">= 14.16"
|
|
37
39
|
},
|
|
38
40
|
"files": [
|
|
39
41
|
"lib",
|
|
@@ -41,14 +43,14 @@
|
|
|
41
43
|
],
|
|
42
44
|
"homepage": "https://github.com/intlify/bundle-tools/blob/main/packages/bundle-utils/README.md",
|
|
43
45
|
"license": "MIT",
|
|
44
|
-
"main": "lib/index.
|
|
45
|
-
"module": "
|
|
46
|
+
"main": "lib/index.cjs",
|
|
47
|
+
"module": "lib/index.mjs",
|
|
46
48
|
"types": "lib/index.d.ts",
|
|
47
49
|
"exports": {
|
|
48
50
|
".": {
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"
|
|
51
|
+
"types": "./lib/index.d.ts",
|
|
52
|
+
"import": "./lib/index.mjs",
|
|
53
|
+
"require": "./lib/index.cjs"
|
|
52
54
|
},
|
|
53
55
|
"./lib/*": "./lib/*",
|
|
54
56
|
"./package.json": "./package.json"
|
|
@@ -59,10 +61,9 @@
|
|
|
59
61
|
"directory": "packages/bundle-utils"
|
|
60
62
|
},
|
|
61
63
|
"scripts": {
|
|
62
|
-
"build": "
|
|
64
|
+
"build": "unbuild",
|
|
63
65
|
"clean": "npm-run-all \"clean:*\"",
|
|
64
66
|
"clean:lib": "rm -rf ./lib",
|
|
65
|
-
"watch": "tsc -p . --watch",
|
|
66
67
|
"changelog": "jiti ../../scripts/changelog.ts",
|
|
67
68
|
"release": "jiti ../../scripts/release.ts"
|
|
68
69
|
}
|
package/index.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './lib/index.js'
|