@intlify/bundle-utils 12.0.0-alpha.1 → 12.0.0-alpha.3
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 +69 -101
- package/lib/index.d.cts +16 -4
- package/lib/index.d.mts +16 -4
- package/lib/index.d.ts +16 -4
- package/lib/index.mjs +68 -101
- package/package.json +6 -9
package/lib/index.cjs
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const module$1 = require('node:module');
|
|
4
|
-
const core = require('@babel/core');
|
|
5
4
|
const shared = require('@intlify/shared');
|
|
6
|
-
const acorn = require('acorn');
|
|
7
5
|
const escodegen = require('escodegen');
|
|
8
|
-
const
|
|
6
|
+
const oxcParser = require('oxc-parser');
|
|
7
|
+
const oxcWalker = require('oxc-walker');
|
|
9
8
|
const messageCompiler = require('@intlify/message-compiler');
|
|
10
9
|
const sourceMapJs = require('source-map-js');
|
|
11
10
|
const jsoncEslintParser = require('jsonc-eslint-parser');
|
|
11
|
+
const oxcTransform = require('oxc-transform');
|
|
12
12
|
const yamlEslintParser = require('yaml-eslint-parser');
|
|
13
13
|
|
|
14
14
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
@@ -16,7 +16,7 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
|
|
|
16
16
|
|
|
17
17
|
const module__default = /*#__PURE__*/_interopDefaultCompat(module$1);
|
|
18
18
|
|
|
19
|
-
const _require = module__default.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
|
|
19
|
+
const _require = module__default.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
|
|
20
20
|
function checkInstallPackage(pkg, debug) {
|
|
21
21
|
let installedVueI18n = false;
|
|
22
22
|
try {
|
|
@@ -210,39 +210,46 @@ function mapLinesColumns(resMap, codeMaps, inSourceMap) {
|
|
|
210
210
|
if (inMapFirstItem) {
|
|
211
211
|
return;
|
|
212
212
|
}
|
|
213
|
+
if (m.originalLine == null || m.originalColumn == null) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
213
216
|
inMapFirstItem = m;
|
|
214
217
|
});
|
|
215
218
|
}
|
|
216
219
|
resMapConsumer.eachMapping((res) => {
|
|
217
|
-
|
|
220
|
+
const resOriginalLine = res.originalLine;
|
|
221
|
+
const resOriginalColumn = res.originalColumn;
|
|
222
|
+
const resName = res.name;
|
|
223
|
+
if (resOriginalLine == null || resOriginalColumn == null || resName == null) {
|
|
218
224
|
return;
|
|
219
225
|
}
|
|
220
|
-
const map = codeMaps.get(
|
|
226
|
+
const map = codeMaps.get(resName);
|
|
221
227
|
if (!map) {
|
|
222
228
|
return;
|
|
223
229
|
}
|
|
224
230
|
let inMapOrigin = null;
|
|
225
231
|
if (inMapConsumer) {
|
|
226
232
|
inMapOrigin = inMapConsumer.originalPositionFor({
|
|
227
|
-
line:
|
|
228
|
-
column:
|
|
233
|
+
line: resOriginalLine,
|
|
234
|
+
column: resOriginalColumn - 1
|
|
229
235
|
});
|
|
230
236
|
if (inMapOrigin.source == null) {
|
|
231
237
|
inMapOrigin = null;
|
|
232
238
|
return;
|
|
233
239
|
}
|
|
234
240
|
}
|
|
241
|
+
const inMapFirst = inMapFirstItem;
|
|
235
242
|
const mapConsumer = new sourceMapJs.SourceMapConsumer(map);
|
|
236
243
|
mapConsumer.eachMapping((m) => {
|
|
237
244
|
mergedMapGenerator.addMapping({
|
|
238
245
|
original: {
|
|
239
|
-
line:
|
|
240
|
-
column:
|
|
246
|
+
line: inMapFirst ? inMapFirst.originalLine + resOriginalLine - 2 : resOriginalLine,
|
|
247
|
+
column: inMapFirst ? inMapFirst.originalColumn + resOriginalColumn : resOriginalColumn
|
|
241
248
|
},
|
|
242
249
|
generated: {
|
|
243
|
-
line:
|
|
250
|
+
line: inMapFirst ? inMapFirst.generatedLine + resOriginalLine - 2 : resOriginalLine,
|
|
244
251
|
// map column with message format compilation code map
|
|
245
|
-
column:
|
|
252
|
+
column: inMapFirst ? inMapFirst.generatedColumn + resOriginalColumn + m.generatedColumn : resOriginalColumn + m.generatedColumn
|
|
246
253
|
},
|
|
247
254
|
source: inMapOrigin ? inMapOrigin.source : res.source,
|
|
248
255
|
name: m.name
|
|
@@ -324,65 +331,32 @@ function excludeLocales({
|
|
|
324
331
|
|
|
325
332
|
class DynamicResourceError extends Error {
|
|
326
333
|
}
|
|
327
|
-
|
|
328
|
-
type
|
|
329
|
-
filename
|
|
330
|
-
inSourceMap
|
|
331
|
-
locale
|
|
332
|
-
isGlobal
|
|
333
|
-
sourceMap
|
|
334
|
-
env
|
|
335
|
-
forceStringify
|
|
336
|
-
onError
|
|
337
|
-
onWarn
|
|
338
|
-
strictMessage
|
|
339
|
-
escapeHtml
|
|
340
|
-
allowDynamic
|
|
341
|
-
jit
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
sourceMap,
|
|
349
|
-
locale,
|
|
350
|
-
isGlobal,
|
|
351
|
-
inSourceMap,
|
|
352
|
-
env,
|
|
353
|
-
filename,
|
|
354
|
-
forceStringify,
|
|
355
|
-
onError,
|
|
356
|
-
onWarn,
|
|
357
|
-
strictMessage,
|
|
358
|
-
escapeHtml,
|
|
359
|
-
jit
|
|
360
|
-
};
|
|
361
|
-
const generator = createCodeGenerator(options);
|
|
362
|
-
if (options.filename && /.[c|m]?ts$/.test(options.filename)) {
|
|
363
|
-
const transformed = core.transform(value, {
|
|
364
|
-
filename: options.filename,
|
|
365
|
-
sourceMaps: options.sourceMap,
|
|
366
|
-
babelrc: false,
|
|
367
|
-
configFile: false,
|
|
368
|
-
browserslistConfigFile: false,
|
|
369
|
-
comments: false,
|
|
370
|
-
envName: "development",
|
|
371
|
-
presets: ["@babel/preset-typescript"]
|
|
372
|
-
});
|
|
373
|
-
if (transformed && transformed.code) {
|
|
374
|
-
value = transformed.code;
|
|
375
|
-
options.source = transformed.code;
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
const ast = acorn.parse(value, {
|
|
379
|
-
ecmaVersion: "latest",
|
|
334
|
+
const DEFAULT_OPTIONS = {
|
|
335
|
+
type: "plain",
|
|
336
|
+
filename: "vue-i18n-loader.js",
|
|
337
|
+
inSourceMap: void 0,
|
|
338
|
+
locale: "",
|
|
339
|
+
isGlobal: false,
|
|
340
|
+
sourceMap: false,
|
|
341
|
+
env: "development",
|
|
342
|
+
forceStringify: false,
|
|
343
|
+
onError: void 0,
|
|
344
|
+
onWarn: void 0,
|
|
345
|
+
strictMessage: true,
|
|
346
|
+
escapeHtml: false,
|
|
347
|
+
allowDynamic: false,
|
|
348
|
+
jit: false
|
|
349
|
+
};
|
|
350
|
+
function generate$3(targetSource, options) {
|
|
351
|
+
const value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
352
|
+
const _options = Object.assign({}, DEFAULT_OPTIONS, options, { source: value });
|
|
353
|
+
const generator = createCodeGenerator(_options);
|
|
354
|
+
const { program: ast } = oxcParser.parseSync(_options.filename ?? "", value, {
|
|
380
355
|
sourceType: "module",
|
|
381
|
-
|
|
382
|
-
allowImportExportEverywhere: true
|
|
356
|
+
lang: "js"
|
|
383
357
|
});
|
|
384
358
|
const exportResult = scanAst(ast);
|
|
385
|
-
if (!allowDynamic) {
|
|
359
|
+
if (!_options.allowDynamic) {
|
|
386
360
|
if (!exportResult) {
|
|
387
361
|
throw new Error(`You need to define an object as the locale message with 'export default'.`);
|
|
388
362
|
}
|
|
@@ -399,13 +373,13 @@ function generate$2(targetSource, {
|
|
|
399
373
|
return {
|
|
400
374
|
ast,
|
|
401
375
|
code: value,
|
|
402
|
-
map: inSourceMap
|
|
376
|
+
map: _options.inSourceMap
|
|
403
377
|
};
|
|
404
378
|
}
|
|
405
379
|
}
|
|
406
|
-
const codeMaps = _generate$2(generator, ast,
|
|
380
|
+
const codeMaps = _generate$2(generator, ast, _options);
|
|
407
381
|
const { code, map } = generator.context();
|
|
408
|
-
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
382
|
+
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, _options.inSourceMap) || null : null;
|
|
409
383
|
return {
|
|
410
384
|
ast,
|
|
411
385
|
code,
|
|
@@ -417,8 +391,7 @@ function scanAst(ast) {
|
|
|
417
391
|
throw new Error("Invalid AST: does not have Program node");
|
|
418
392
|
}
|
|
419
393
|
for (const node of ast.body) {
|
|
420
|
-
if (node.type !== "ExportDefaultDeclaration")
|
|
421
|
-
continue;
|
|
394
|
+
if (node.type !== "ExportDefaultDeclaration") continue;
|
|
422
395
|
switch (node.declaration.type) {
|
|
423
396
|
case "ObjectExpression":
|
|
424
397
|
return "object";
|
|
@@ -446,17 +419,10 @@ function _generate$2(generator, node, options = {}) {
|
|
|
446
419
|
}
|
|
447
420
|
const componentNamespace = "_Component";
|
|
448
421
|
const variableDeclarations = [];
|
|
449
|
-
|
|
450
|
-
/**
|
|
451
|
-
* NOTE:
|
|
452
|
-
* force cast to Node of `estree-walker@3.x`,
|
|
453
|
-
* because `estree-walker@3.x` is not dual packages,
|
|
454
|
-
* so it's support only esm only ...
|
|
455
|
-
*/
|
|
422
|
+
oxcWalker.walk(node, {
|
|
456
423
|
// @ts-ignore
|
|
457
|
-
enter(node2,
|
|
458
|
-
if (
|
|
459
|
-
this.skip();
|
|
424
|
+
enter(node2, parent) {
|
|
425
|
+
if (parent?.type != null) this.skip();
|
|
460
426
|
switch (node2.type) {
|
|
461
427
|
case "ExportDefaultDeclaration":
|
|
462
428
|
this.skip();
|
|
@@ -481,7 +447,7 @@ function _generate$2(generator, node, options = {}) {
|
|
|
481
447
|
Variable declarations are not optimized - found ${variableDeclarations.join(", ")}`
|
|
482
448
|
);
|
|
483
449
|
}
|
|
484
|
-
|
|
450
|
+
oxcWalker.walk(node, {
|
|
485
451
|
/**
|
|
486
452
|
* NOTE:
|
|
487
453
|
* force cast to Node of `estree-walker@3.x`,
|
|
@@ -536,10 +502,8 @@ Variable declarations are not optimized - found ${variableDeclarations.join(", "
|
|
|
536
502
|
itemsCountStack.push(node2.elements.length);
|
|
537
503
|
break;
|
|
538
504
|
case "Property":
|
|
539
|
-
if (parent?.type !== "ObjectExpression")
|
|
540
|
-
|
|
541
|
-
if (node2.key.type !== "Literal" && node2.key.type !== "Identifier")
|
|
542
|
-
break;
|
|
505
|
+
if (parent?.type !== "ObjectExpression") break;
|
|
506
|
+
if (node2.key.type !== "Literal" && node2.key.type !== "Identifier") break;
|
|
543
507
|
const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
|
|
544
508
|
const strName = JSON.stringify(name);
|
|
545
509
|
if (isJSONablePrimitiveLiteral(node2.value)) {
|
|
@@ -602,12 +566,6 @@ Variable declarations are not optimized - found ${variableDeclarations.join(", "
|
|
|
602
566
|
break;
|
|
603
567
|
}
|
|
604
568
|
},
|
|
605
|
-
/**
|
|
606
|
-
* NOTE:
|
|
607
|
-
* force cast to Node of `estree-walker@3.x`,
|
|
608
|
-
* because `estree-walker@3.x` is not dual packages,
|
|
609
|
-
* so it's support only esm only ...
|
|
610
|
-
*/
|
|
611
569
|
// @ts-ignore
|
|
612
570
|
leave(node2, parent) {
|
|
613
571
|
switch (node2.type) {
|
|
@@ -657,7 +615,7 @@ function getValue(node) {
|
|
|
657
615
|
return node.type === "Literal" ? node.value : node.type === "TemplateLiteral" ? node.quasis.map((quasi) => quasi.value.cooked).join("") : void 0;
|
|
658
616
|
}
|
|
659
617
|
|
|
660
|
-
function generate$
|
|
618
|
+
function generate$2(targetSource, {
|
|
661
619
|
type = "plain",
|
|
662
620
|
onlyLocales = [],
|
|
663
621
|
filename = "vue-i18n-loader.json",
|
|
@@ -788,8 +746,7 @@ function _generate$1(generator, node, options = {}) {
|
|
|
788
746
|
break;
|
|
789
747
|
}
|
|
790
748
|
case "JSONLiteral":
|
|
791
|
-
if (parent.type !== "JSONArrayExpression")
|
|
792
|
-
break;
|
|
749
|
+
if (parent.type !== "JSONArrayExpression") break;
|
|
793
750
|
const value = node2.value;
|
|
794
751
|
const strValue = JSON.stringify(value);
|
|
795
752
|
if (shared.isString(value)) {
|
|
@@ -845,6 +802,17 @@ function _generate$1(generator, node, options = {}) {
|
|
|
845
802
|
return codeMaps;
|
|
846
803
|
}
|
|
847
804
|
|
|
805
|
+
function generate$1(targetSource, options) {
|
|
806
|
+
let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
807
|
+
const _options = Object.assign({}, DEFAULT_OPTIONS, options, { source: value });
|
|
808
|
+
const transformed = oxcTransform.transform(_options.filename ?? "", value, { lang: "ts" });
|
|
809
|
+
if (transformed && transformed.code) {
|
|
810
|
+
value = transformed.code;
|
|
811
|
+
_options.source = transformed.code;
|
|
812
|
+
}
|
|
813
|
+
return generate$3(value, _options);
|
|
814
|
+
}
|
|
815
|
+
|
|
848
816
|
function generate(targetSource, {
|
|
849
817
|
type = "plain",
|
|
850
818
|
onlyLocales = [],
|
|
@@ -958,8 +926,7 @@ function _generate(generator, node, options = {}) {
|
|
|
958
926
|
itemsCountStack.push(node2.entries.length);
|
|
959
927
|
break;
|
|
960
928
|
case "YAMLPair":
|
|
961
|
-
if (node2.key?.type !== "YAMLScalar")
|
|
962
|
-
break;
|
|
929
|
+
if (node2.key?.type !== "YAMLScalar") break;
|
|
963
930
|
if (node2.value?.type === "YAMLScalar") {
|
|
964
931
|
const name = node2.key.value;
|
|
965
932
|
const value = node2.value.value;
|
|
@@ -1042,7 +1009,8 @@ function _generate(generator, node, options = {}) {
|
|
|
1042
1009
|
|
|
1043
1010
|
exports.DynamicResourceError = DynamicResourceError;
|
|
1044
1011
|
exports.checkInstallPackage = checkInstallPackage;
|
|
1045
|
-
exports.generateJSON = generate$
|
|
1046
|
-
exports.generateJavaScript = generate$
|
|
1012
|
+
exports.generateJSON = generate$2;
|
|
1013
|
+
exports.generateJavaScript = generate$3;
|
|
1014
|
+
exports.generateTypescript = generate$1;
|
|
1047
1015
|
exports.generateYAML = generate;
|
|
1048
1016
|
exports.getVueI18nVersion = getVueI18nVersion;
|
package/lib/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { RawSourceMap } from 'source-map-js';
|
|
2
2
|
import { CompileError } from '@intlify/message-compiler';
|
|
3
|
-
import { Node } from '
|
|
3
|
+
import { Node } from 'oxc-parser';
|
|
4
4
|
import { JSONProgram } from 'jsonc-eslint-parser/lib/parser/ast';
|
|
5
5
|
import { YAMLProgram } from 'yaml-eslint-parser/lib/ast';
|
|
6
6
|
|
|
@@ -61,13 +61,24 @@ declare class DynamicResourceError extends Error {
|
|
|
61
61
|
/**
|
|
62
62
|
* @internal
|
|
63
63
|
*/
|
|
64
|
-
declare function generate$
|
|
64
|
+
declare function generate$3(targetSource: string | Buffer, options: CodeGenOptions): CodeGenResult<Node>;
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* Code / AST generator for i18n json/json5 resource
|
|
68
68
|
*/
|
|
69
69
|
|
|
70
|
-
declare function generate$
|
|
70
|
+
declare function generate$2(targetSource: string | Buffer, { type, onlyLocales, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, jit }: CodeGenOptions): CodeGenResult<JSONProgram>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Code generator for i18n ts resource
|
|
74
|
+
*
|
|
75
|
+
* A wrapper for the js generator which transforms ts to js before generating
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
declare function generate$1(targetSource: string | Buffer, options: CodeGenOptions): CodeGenResult<Node>;
|
|
71
82
|
|
|
72
83
|
/**
|
|
73
84
|
* Code generator for i18n yaml resource
|
|
@@ -78,4 +89,5 @@ declare function generate$1(targetSource: string | Buffer, { type, onlyLocales,
|
|
|
78
89
|
*/
|
|
79
90
|
declare function generate(targetSource: string | Buffer, { type, onlyLocales, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, jit }: CodeGenOptions): CodeGenResult<YAMLProgram>;
|
|
80
91
|
|
|
81
|
-
export {
|
|
92
|
+
export { DynamicResourceError, checkInstallPackage, generate$2 as generateJSON, generate$3 as generateJavaScript, generate$1 as generateTypescript, generate as generateYAML, getVueI18nVersion };
|
|
93
|
+
export type { CodeGenOptions, CodeGenResult, DevEnv, InstalledPackage };
|
package/lib/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { RawSourceMap } from 'source-map-js';
|
|
2
2
|
import { CompileError } from '@intlify/message-compiler';
|
|
3
|
-
import { Node } from '
|
|
3
|
+
import { Node } from 'oxc-parser';
|
|
4
4
|
import { JSONProgram } from 'jsonc-eslint-parser/lib/parser/ast';
|
|
5
5
|
import { YAMLProgram } from 'yaml-eslint-parser/lib/ast';
|
|
6
6
|
|
|
@@ -61,13 +61,24 @@ declare class DynamicResourceError extends Error {
|
|
|
61
61
|
/**
|
|
62
62
|
* @internal
|
|
63
63
|
*/
|
|
64
|
-
declare function generate$
|
|
64
|
+
declare function generate$3(targetSource: string | Buffer, options: CodeGenOptions): CodeGenResult<Node>;
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* Code / AST generator for i18n json/json5 resource
|
|
68
68
|
*/
|
|
69
69
|
|
|
70
|
-
declare function generate$
|
|
70
|
+
declare function generate$2(targetSource: string | Buffer, { type, onlyLocales, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, jit }: CodeGenOptions): CodeGenResult<JSONProgram>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Code generator for i18n ts resource
|
|
74
|
+
*
|
|
75
|
+
* A wrapper for the js generator which transforms ts to js before generating
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
declare function generate$1(targetSource: string | Buffer, options: CodeGenOptions): CodeGenResult<Node>;
|
|
71
82
|
|
|
72
83
|
/**
|
|
73
84
|
* Code generator for i18n yaml resource
|
|
@@ -78,4 +89,5 @@ declare function generate$1(targetSource: string | Buffer, { type, onlyLocales,
|
|
|
78
89
|
*/
|
|
79
90
|
declare function generate(targetSource: string | Buffer, { type, onlyLocales, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, jit }: CodeGenOptions): CodeGenResult<YAMLProgram>;
|
|
80
91
|
|
|
81
|
-
export {
|
|
92
|
+
export { DynamicResourceError, checkInstallPackage, generate$2 as generateJSON, generate$3 as generateJavaScript, generate$1 as generateTypescript, generate as generateYAML, getVueI18nVersion };
|
|
93
|
+
export type { CodeGenOptions, CodeGenResult, DevEnv, InstalledPackage };
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { RawSourceMap } from 'source-map-js';
|
|
2
2
|
import { CompileError } from '@intlify/message-compiler';
|
|
3
|
-
import { Node } from '
|
|
3
|
+
import { Node } from 'oxc-parser';
|
|
4
4
|
import { JSONProgram } from 'jsonc-eslint-parser/lib/parser/ast';
|
|
5
5
|
import { YAMLProgram } from 'yaml-eslint-parser/lib/ast';
|
|
6
6
|
|
|
@@ -61,13 +61,24 @@ declare class DynamicResourceError extends Error {
|
|
|
61
61
|
/**
|
|
62
62
|
* @internal
|
|
63
63
|
*/
|
|
64
|
-
declare function generate$
|
|
64
|
+
declare function generate$3(targetSource: string | Buffer, options: CodeGenOptions): CodeGenResult<Node>;
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* Code / AST generator for i18n json/json5 resource
|
|
68
68
|
*/
|
|
69
69
|
|
|
70
|
-
declare function generate$
|
|
70
|
+
declare function generate$2(targetSource: string | Buffer, { type, onlyLocales, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, jit }: CodeGenOptions): CodeGenResult<JSONProgram>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Code generator for i18n ts resource
|
|
74
|
+
*
|
|
75
|
+
* A wrapper for the js generator which transforms ts to js before generating
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
declare function generate$1(targetSource: string | Buffer, options: CodeGenOptions): CodeGenResult<Node>;
|
|
71
82
|
|
|
72
83
|
/**
|
|
73
84
|
* Code generator for i18n yaml resource
|
|
@@ -78,4 +89,5 @@ declare function generate$1(targetSource: string | Buffer, { type, onlyLocales,
|
|
|
78
89
|
*/
|
|
79
90
|
declare function generate(targetSource: string | Buffer, { type, onlyLocales, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, jit }: CodeGenOptions): CodeGenResult<YAMLProgram>;
|
|
80
91
|
|
|
81
|
-
export {
|
|
92
|
+
export { DynamicResourceError, checkInstallPackage, generate$2 as generateJSON, generate$3 as generateJavaScript, generate$1 as generateTypescript, generate as generateYAML, getVueI18nVersion };
|
|
93
|
+
export type { CodeGenOptions, CodeGenResult, DevEnv, InstalledPackage };
|
package/lib/index.mjs
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import module from 'node:module';
|
|
2
|
-
import { transform } from '@babel/core';
|
|
1
|
+
import module$1 from 'node:module';
|
|
3
2
|
import { isBoolean, format, escapeHtml, friendlyJSONstringify, isString, isNumber } from '@intlify/shared';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { walk } from '
|
|
3
|
+
import { generate as generate$4 } from 'escodegen';
|
|
4
|
+
import { parseSync } from 'oxc-parser';
|
|
5
|
+
import { walk } from 'oxc-walker';
|
|
7
6
|
import { LOCATION_STUB, detectHtmlTag, baseCompile } from '@intlify/message-compiler';
|
|
8
7
|
import { SourceMapGenerator, SourceMapConsumer } from 'source-map-js';
|
|
9
8
|
import { parseJSON, getStaticJSONValue, traverseNodes } from 'jsonc-eslint-parser';
|
|
9
|
+
import { transform } from 'oxc-transform';
|
|
10
10
|
import { parseYAML, getStaticYAMLValue, traverseNodes as traverseNodes$1 } from 'yaml-eslint-parser';
|
|
11
11
|
|
|
12
|
-
const _require = module.createRequire(import.meta.url);
|
|
12
|
+
const _require = module$1.createRequire(import.meta.url);
|
|
13
13
|
function checkInstallPackage(pkg, debug) {
|
|
14
14
|
let installedVueI18n = false;
|
|
15
15
|
try {
|
|
@@ -203,39 +203,46 @@ function mapLinesColumns(resMap, codeMaps, inSourceMap) {
|
|
|
203
203
|
if (inMapFirstItem) {
|
|
204
204
|
return;
|
|
205
205
|
}
|
|
206
|
+
if (m.originalLine == null || m.originalColumn == null) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
206
209
|
inMapFirstItem = m;
|
|
207
210
|
});
|
|
208
211
|
}
|
|
209
212
|
resMapConsumer.eachMapping((res) => {
|
|
210
|
-
|
|
213
|
+
const resOriginalLine = res.originalLine;
|
|
214
|
+
const resOriginalColumn = res.originalColumn;
|
|
215
|
+
const resName = res.name;
|
|
216
|
+
if (resOriginalLine == null || resOriginalColumn == null || resName == null) {
|
|
211
217
|
return;
|
|
212
218
|
}
|
|
213
|
-
const map = codeMaps.get(
|
|
219
|
+
const map = codeMaps.get(resName);
|
|
214
220
|
if (!map) {
|
|
215
221
|
return;
|
|
216
222
|
}
|
|
217
223
|
let inMapOrigin = null;
|
|
218
224
|
if (inMapConsumer) {
|
|
219
225
|
inMapOrigin = inMapConsumer.originalPositionFor({
|
|
220
|
-
line:
|
|
221
|
-
column:
|
|
226
|
+
line: resOriginalLine,
|
|
227
|
+
column: resOriginalColumn - 1
|
|
222
228
|
});
|
|
223
229
|
if (inMapOrigin.source == null) {
|
|
224
230
|
inMapOrigin = null;
|
|
225
231
|
return;
|
|
226
232
|
}
|
|
227
233
|
}
|
|
234
|
+
const inMapFirst = inMapFirstItem;
|
|
228
235
|
const mapConsumer = new SourceMapConsumer(map);
|
|
229
236
|
mapConsumer.eachMapping((m) => {
|
|
230
237
|
mergedMapGenerator.addMapping({
|
|
231
238
|
original: {
|
|
232
|
-
line:
|
|
233
|
-
column:
|
|
239
|
+
line: inMapFirst ? inMapFirst.originalLine + resOriginalLine - 2 : resOriginalLine,
|
|
240
|
+
column: inMapFirst ? inMapFirst.originalColumn + resOriginalColumn : resOriginalColumn
|
|
234
241
|
},
|
|
235
242
|
generated: {
|
|
236
|
-
line:
|
|
243
|
+
line: inMapFirst ? inMapFirst.generatedLine + resOriginalLine - 2 : resOriginalLine,
|
|
237
244
|
// map column with message format compilation code map
|
|
238
|
-
column:
|
|
245
|
+
column: inMapFirst ? inMapFirst.generatedColumn + resOriginalColumn + m.generatedColumn : resOriginalColumn + m.generatedColumn
|
|
239
246
|
},
|
|
240
247
|
source: inMapOrigin ? inMapOrigin.source : res.source,
|
|
241
248
|
name: m.name
|
|
@@ -317,65 +324,32 @@ function excludeLocales({
|
|
|
317
324
|
|
|
318
325
|
class DynamicResourceError extends Error {
|
|
319
326
|
}
|
|
320
|
-
|
|
321
|
-
type
|
|
322
|
-
filename
|
|
323
|
-
inSourceMap
|
|
324
|
-
locale
|
|
325
|
-
isGlobal
|
|
326
|
-
sourceMap
|
|
327
|
-
env
|
|
328
|
-
forceStringify
|
|
329
|
-
onError
|
|
330
|
-
onWarn
|
|
331
|
-
strictMessage
|
|
332
|
-
escapeHtml
|
|
333
|
-
allowDynamic
|
|
334
|
-
jit
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
const
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
sourceMap,
|
|
342
|
-
locale,
|
|
343
|
-
isGlobal,
|
|
344
|
-
inSourceMap,
|
|
345
|
-
env,
|
|
346
|
-
filename,
|
|
347
|
-
forceStringify,
|
|
348
|
-
onError,
|
|
349
|
-
onWarn,
|
|
350
|
-
strictMessage,
|
|
351
|
-
escapeHtml,
|
|
352
|
-
jit
|
|
353
|
-
};
|
|
354
|
-
const generator = createCodeGenerator(options);
|
|
355
|
-
if (options.filename && /.[c|m]?ts$/.test(options.filename)) {
|
|
356
|
-
const transformed = transform(value, {
|
|
357
|
-
filename: options.filename,
|
|
358
|
-
sourceMaps: options.sourceMap,
|
|
359
|
-
babelrc: false,
|
|
360
|
-
configFile: false,
|
|
361
|
-
browserslistConfigFile: false,
|
|
362
|
-
comments: false,
|
|
363
|
-
envName: "development",
|
|
364
|
-
presets: ["@babel/preset-typescript"]
|
|
365
|
-
});
|
|
366
|
-
if (transformed && transformed.code) {
|
|
367
|
-
value = transformed.code;
|
|
368
|
-
options.source = transformed.code;
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
const ast = parse(value, {
|
|
372
|
-
ecmaVersion: "latest",
|
|
327
|
+
const DEFAULT_OPTIONS = {
|
|
328
|
+
type: "plain",
|
|
329
|
+
filename: "vue-i18n-loader.js",
|
|
330
|
+
inSourceMap: void 0,
|
|
331
|
+
locale: "",
|
|
332
|
+
isGlobal: false,
|
|
333
|
+
sourceMap: false,
|
|
334
|
+
env: "development",
|
|
335
|
+
forceStringify: false,
|
|
336
|
+
onError: void 0,
|
|
337
|
+
onWarn: void 0,
|
|
338
|
+
strictMessage: true,
|
|
339
|
+
escapeHtml: false,
|
|
340
|
+
allowDynamic: false,
|
|
341
|
+
jit: false
|
|
342
|
+
};
|
|
343
|
+
function generate$3(targetSource, options) {
|
|
344
|
+
const value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
345
|
+
const _options = Object.assign({}, DEFAULT_OPTIONS, options, { source: value });
|
|
346
|
+
const generator = createCodeGenerator(_options);
|
|
347
|
+
const { program: ast } = parseSync(_options.filename ?? "", value, {
|
|
373
348
|
sourceType: "module",
|
|
374
|
-
|
|
375
|
-
allowImportExportEverywhere: true
|
|
349
|
+
lang: "js"
|
|
376
350
|
});
|
|
377
351
|
const exportResult = scanAst(ast);
|
|
378
|
-
if (!allowDynamic) {
|
|
352
|
+
if (!_options.allowDynamic) {
|
|
379
353
|
if (!exportResult) {
|
|
380
354
|
throw new Error(`You need to define an object as the locale message with 'export default'.`);
|
|
381
355
|
}
|
|
@@ -392,13 +366,13 @@ function generate$2(targetSource, {
|
|
|
392
366
|
return {
|
|
393
367
|
ast,
|
|
394
368
|
code: value,
|
|
395
|
-
map: inSourceMap
|
|
369
|
+
map: _options.inSourceMap
|
|
396
370
|
};
|
|
397
371
|
}
|
|
398
372
|
}
|
|
399
|
-
const codeMaps = _generate$2(generator, ast,
|
|
373
|
+
const codeMaps = _generate$2(generator, ast, _options);
|
|
400
374
|
const { code, map } = generator.context();
|
|
401
|
-
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
375
|
+
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, _options.inSourceMap) || null : null;
|
|
402
376
|
return {
|
|
403
377
|
ast,
|
|
404
378
|
code,
|
|
@@ -410,8 +384,7 @@ function scanAst(ast) {
|
|
|
410
384
|
throw new Error("Invalid AST: does not have Program node");
|
|
411
385
|
}
|
|
412
386
|
for (const node of ast.body) {
|
|
413
|
-
if (node.type !== "ExportDefaultDeclaration")
|
|
414
|
-
continue;
|
|
387
|
+
if (node.type !== "ExportDefaultDeclaration") continue;
|
|
415
388
|
switch (node.declaration.type) {
|
|
416
389
|
case "ObjectExpression":
|
|
417
390
|
return "object";
|
|
@@ -440,16 +413,9 @@ function _generate$2(generator, node, options = {}) {
|
|
|
440
413
|
const componentNamespace = "_Component";
|
|
441
414
|
const variableDeclarations = [];
|
|
442
415
|
walk(node, {
|
|
443
|
-
/**
|
|
444
|
-
* NOTE:
|
|
445
|
-
* force cast to Node of `estree-walker@3.x`,
|
|
446
|
-
* because `estree-walker@3.x` is not dual packages,
|
|
447
|
-
* so it's support only esm only ...
|
|
448
|
-
*/
|
|
449
416
|
// @ts-ignore
|
|
450
|
-
enter(node2,
|
|
451
|
-
if (
|
|
452
|
-
this.skip();
|
|
417
|
+
enter(node2, parent) {
|
|
418
|
+
if (parent?.type != null) this.skip();
|
|
453
419
|
switch (node2.type) {
|
|
454
420
|
case "ExportDefaultDeclaration":
|
|
455
421
|
this.skip();
|
|
@@ -529,10 +495,8 @@ Variable declarations are not optimized - found ${variableDeclarations.join(", "
|
|
|
529
495
|
itemsCountStack.push(node2.elements.length);
|
|
530
496
|
break;
|
|
531
497
|
case "Property":
|
|
532
|
-
if (parent?.type !== "ObjectExpression")
|
|
533
|
-
|
|
534
|
-
if (node2.key.type !== "Literal" && node2.key.type !== "Identifier")
|
|
535
|
-
break;
|
|
498
|
+
if (parent?.type !== "ObjectExpression") break;
|
|
499
|
+
if (node2.key.type !== "Literal" && node2.key.type !== "Identifier") break;
|
|
536
500
|
const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
|
|
537
501
|
const strName = JSON.stringify(name);
|
|
538
502
|
if (isJSONablePrimitiveLiteral(node2.value)) {
|
|
@@ -555,7 +519,7 @@ Variable declarations are not optimized - found ${variableDeclarations.join(", "
|
|
|
555
519
|
} else if (node2.value.type === "FunctionExpression" || node2.value.type === "ArrowFunctionExpression") {
|
|
556
520
|
generator.push(`${strName}: `);
|
|
557
521
|
pathStack.push(name);
|
|
558
|
-
const code = generate$
|
|
522
|
+
const code = generate$4(node2.value, {
|
|
559
523
|
format: { compact: true }
|
|
560
524
|
});
|
|
561
525
|
generator.push(code, node2.value, code);
|
|
@@ -595,12 +559,6 @@ Variable declarations are not optimized - found ${variableDeclarations.join(", "
|
|
|
595
559
|
break;
|
|
596
560
|
}
|
|
597
561
|
},
|
|
598
|
-
/**
|
|
599
|
-
* NOTE:
|
|
600
|
-
* force cast to Node of `estree-walker@3.x`,
|
|
601
|
-
* because `estree-walker@3.x` is not dual packages,
|
|
602
|
-
* so it's support only esm only ...
|
|
603
|
-
*/
|
|
604
562
|
// @ts-ignore
|
|
605
563
|
leave(node2, parent) {
|
|
606
564
|
switch (node2.type) {
|
|
@@ -650,7 +608,7 @@ function getValue(node) {
|
|
|
650
608
|
return node.type === "Literal" ? node.value : node.type === "TemplateLiteral" ? node.quasis.map((quasi) => quasi.value.cooked).join("") : void 0;
|
|
651
609
|
}
|
|
652
610
|
|
|
653
|
-
function generate$
|
|
611
|
+
function generate$2(targetSource, {
|
|
654
612
|
type = "plain",
|
|
655
613
|
onlyLocales = [],
|
|
656
614
|
filename = "vue-i18n-loader.json",
|
|
@@ -781,8 +739,7 @@ function _generate$1(generator, node, options = {}) {
|
|
|
781
739
|
break;
|
|
782
740
|
}
|
|
783
741
|
case "JSONLiteral":
|
|
784
|
-
if (parent.type !== "JSONArrayExpression")
|
|
785
|
-
break;
|
|
742
|
+
if (parent.type !== "JSONArrayExpression") break;
|
|
786
743
|
const value = node2.value;
|
|
787
744
|
const strValue = JSON.stringify(value);
|
|
788
745
|
if (isString(value)) {
|
|
@@ -838,6 +795,17 @@ function _generate$1(generator, node, options = {}) {
|
|
|
838
795
|
return codeMaps;
|
|
839
796
|
}
|
|
840
797
|
|
|
798
|
+
function generate$1(targetSource, options) {
|
|
799
|
+
let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
800
|
+
const _options = Object.assign({}, DEFAULT_OPTIONS, options, { source: value });
|
|
801
|
+
const transformed = transform(_options.filename ?? "", value, { lang: "ts" });
|
|
802
|
+
if (transformed && transformed.code) {
|
|
803
|
+
value = transformed.code;
|
|
804
|
+
_options.source = transformed.code;
|
|
805
|
+
}
|
|
806
|
+
return generate$3(value, _options);
|
|
807
|
+
}
|
|
808
|
+
|
|
841
809
|
function generate(targetSource, {
|
|
842
810
|
type = "plain",
|
|
843
811
|
onlyLocales = [],
|
|
@@ -951,8 +919,7 @@ function _generate(generator, node, options = {}) {
|
|
|
951
919
|
itemsCountStack.push(node2.entries.length);
|
|
952
920
|
break;
|
|
953
921
|
case "YAMLPair":
|
|
954
|
-
if (node2.key?.type !== "YAMLScalar")
|
|
955
|
-
break;
|
|
922
|
+
if (node2.key?.type !== "YAMLScalar") break;
|
|
956
923
|
if (node2.value?.type === "YAMLScalar") {
|
|
957
924
|
const name = node2.key.value;
|
|
958
925
|
const value = node2.value.value;
|
|
@@ -1033,4 +1000,4 @@ function _generate(generator, node, options = {}) {
|
|
|
1033
1000
|
return codeMaps;
|
|
1034
1001
|
}
|
|
1035
1002
|
|
|
1036
|
-
export { DynamicResourceError, checkInstallPackage, generate$
|
|
1003
|
+
export { DynamicResourceError, checkInstallPackage, generate$2 as generateJSON, generate$3 as generateJavaScript, generate$1 as generateTypescript, generate as generateYAML, getVueI18nVersion };
|
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": "12.0.0-alpha.
|
|
4
|
+
"version": "12.0.0-alpha.3",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -18,25 +18,22 @@
|
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@babel/core": "^7.26.10",
|
|
22
|
-
"@babel/preset-typescript": "^7.26.0",
|
|
23
21
|
"@intlify/message-compiler": "next",
|
|
24
22
|
"@intlify/shared": "next",
|
|
25
|
-
"acorn": "^8.8.2",
|
|
26
23
|
"escodegen": "^2.1.0",
|
|
27
|
-
"estree-walker": "^2.0.2",
|
|
28
24
|
"jsonc-eslint-parser": "^2.3.0",
|
|
25
|
+
"oxc-parser": "^0.130.0",
|
|
26
|
+
"oxc-transform": "^0.96.0",
|
|
27
|
+
"oxc-walker": "^0.7.0",
|
|
29
28
|
"source-map-js": "^1.0.1",
|
|
30
29
|
"yaml-eslint-parser": "^1.2.2"
|
|
31
30
|
},
|
|
32
31
|
"devDependencies": {
|
|
33
|
-
"@types/babel__core": "^7.20.5",
|
|
34
32
|
"@types/escodegen": "^0.0.10",
|
|
35
|
-
"
|
|
36
|
-
"unbuild": "^2.0.0"
|
|
33
|
+
"unbuild": "^3.6.1"
|
|
37
34
|
},
|
|
38
35
|
"engines": {
|
|
39
|
-
"node": ">=
|
|
36
|
+
"node": ">= 22.13"
|
|
40
37
|
},
|
|
41
38
|
"files": [
|
|
42
39
|
"lib"
|