@intlify/bundle-utils 7.2.0 → 7.4.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 CHANGED
@@ -11,6 +11,7 @@ const escodegen = require('escodegen');
11
11
  const estreeWalker = require('estree-walker');
12
12
  const module$1 = require('node:module');
13
13
 
14
+ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
14
15
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
15
16
 
16
17
  const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
@@ -261,6 +262,18 @@ function generateResourceAst(msg, options = {}, path) {
261
262
  const code = !occured ? `${shared.friendlyJSONstringify(ast)}` : `\`${_msg}\``;
262
263
  return { code, ast, map, errors };
263
264
  }
265
+ function excludeLocales({
266
+ messages,
267
+ onlyLocales
268
+ }) {
269
+ const _messages = { ...messages };
270
+ Object.keys(_messages).forEach((locale) => {
271
+ if (!onlyLocales.includes(locale)) {
272
+ delete _messages[locale];
273
+ }
274
+ });
275
+ return _messages;
276
+ }
264
277
 
265
278
  function generateLegacyCode({ isGlobal }, generator) {
266
279
  const componentNamespace = `Component.options`;
@@ -280,6 +293,7 @@ function generate$2(targetSource, {
280
293
  type = "plain",
281
294
  legacy = false,
282
295
  bridge = false,
296
+ onlyLocales = [],
283
297
  exportESM = false,
284
298
  filename = "vue-i18n-loader.json",
285
299
  inSourceMap = void 0,
@@ -294,8 +308,7 @@ function generate$2(targetSource, {
294
308
  useClassComponent = false,
295
309
  jit = false
296
310
  }, injector) {
297
- const target = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
298
- const value = target;
311
+ let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
299
312
  const options = {
300
313
  type,
301
314
  bridge,
@@ -315,7 +328,17 @@ function generate$2(targetSource, {
315
328
  jit
316
329
  };
317
330
  const generator = createCodeGenerator(options);
318
- const ast = jsoncEslintParser.parseJSON(value, { filePath: filename });
331
+ let ast = jsoncEslintParser.parseJSON(value, { filePath: filename });
332
+ if (!locale && type === "sfc" && onlyLocales?.length) {
333
+ const messages = jsoncEslintParser.getStaticJSONValue(ast);
334
+ value = JSON.stringify(
335
+ excludeLocales({
336
+ messages,
337
+ onlyLocales
338
+ })
339
+ );
340
+ ast = jsoncEslintParser.parseJSON(value, { filePath: filename });
341
+ }
319
342
  if (legacy && type === "sfc") {
320
343
  const gen = () => shared.friendlyJSONstringify(jsoncEslintParser.getStaticJSONValue(ast));
321
344
  const code2 = generateLegacyCode(options, gen);
@@ -540,6 +563,7 @@ function generate$1(targetSource, {
540
563
  type = "plain",
541
564
  legacy = false,
542
565
  bridge = false,
566
+ onlyLocales = [],
543
567
  exportESM = false,
544
568
  useClassComponent = false,
545
569
  filename = "vue-i18n-loader.yaml",
@@ -554,7 +578,7 @@ function generate$1(targetSource, {
554
578
  escapeHtml = false,
555
579
  jit = false
556
580
  }, injector) {
557
- const value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
581
+ let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
558
582
  const options = {
559
583
  type,
560
584
  bridge,
@@ -574,7 +598,17 @@ function generate$1(targetSource, {
574
598
  jit
575
599
  };
576
600
  const generator = createCodeGenerator(options);
577
- const ast = yamlEslintParser.parseYAML(value, { filePath: filename });
601
+ let ast = yamlEslintParser.parseYAML(value, { filePath: filename });
602
+ if (!locale && type === "sfc" && onlyLocales?.length) {
603
+ const messages = yamlEslintParser.getStaticYAMLValue(ast);
604
+ value = JSON.stringify(
605
+ excludeLocales({
606
+ messages,
607
+ onlyLocales
608
+ })
609
+ );
610
+ ast = yamlEslintParser.parseYAML(value, { filePath: filename });
611
+ }
578
612
  if (legacy && type === "sfc") {
579
613
  const gen = () => shared.friendlyJSONstringify(yamlEslintParser.getStaticYAMLValue(ast));
580
614
  const code2 = generateLegacyCode(options, gen);
@@ -1149,7 +1183,7 @@ function getValue(node) {
1149
1183
  return node.type === "Literal" ? node.value : node.type === "TemplateLiteral" ? node.quasis.map((quasi) => quasi.value.cooked).join("") : void 0;
1150
1184
  }
1151
1185
 
1152
- const _require = module__default.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));
1186
+ const _require = module__default.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
1153
1187
  function checkInstallPackage(pkg, debug) {
1154
1188
  let installedVueI18n = false;
1155
1189
  try {
@@ -0,0 +1,82 @@
1
+ import { JSONProgram } from 'jsonc-eslint-parser/lib/parser/ast';
2
+ import { RawSourceMap } from 'source-map-js';
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
+ onlyLocales?: string[];
22
+ source?: string;
23
+ sourceMap?: boolean;
24
+ filename?: string;
25
+ inSourceMap?: RawSourceMap;
26
+ isGlobal?: boolean;
27
+ locale?: string;
28
+ env?: DevEnv;
29
+ forceStringify?: boolean;
30
+ useClassComponent?: boolean;
31
+ allowDynamic?: boolean;
32
+ strictMessage?: boolean;
33
+ escapeHtml?: boolean;
34
+ jit?: boolean;
35
+ minify?: boolean;
36
+ onWarn?: (msg: string) => void;
37
+ onError?: (msg: string, extra?: {
38
+ source: string;
39
+ path: string;
40
+ code?: CompileError['code'];
41
+ domain?: CompileError['domain'];
42
+ location?: CompileError['location'];
43
+ }) => void;
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 / AST generator for i18n json/json5 resource
54
+ */
55
+
56
+ declare function generate$2(targetSource: string | Buffer, { type, legacy, bridge, onlyLocales, exportESM, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, useClassComponent, jit }: CodeGenOptions, injector?: () => string): CodeGenResult<JSONProgram>;
57
+
58
+ /**
59
+ * Code generator for i18n yaml resource
60
+ */
61
+
62
+ /**
63
+ * @internal
64
+ */
65
+ declare function generate$1(targetSource: string | Buffer, { type, legacy, bridge, onlyLocales, exportESM, useClassComponent, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, jit }: CodeGenOptions, injector?: () => string): CodeGenResult<YAMLProgram>;
66
+
67
+ /**
68
+ * Code generator for i18n js resource
69
+ */
70
+
71
+ /**
72
+ * @internal
73
+ */
74
+ declare function generate(targetSource: string | Buffer, { type, bridge, exportESM, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, useClassComponent, allowDynamic, jit }: CodeGenOptions, injector?: () => string): CodeGenResult<Node>;
75
+
76
+ type InstalledPackage = 'vue-i18n' | 'petite-vue-i18n';
77
+ declare function checkInstallPackage(pkg: string, debug: Function): InstalledPackage;
78
+ declare function checkVueI18nBridgeInstallPackage(debug: Function): boolean;
79
+ type VueI18nVersion = '8' | '9' | 'unknown' | '';
80
+ declare function getVueI18nVersion(debug: Function): VueI18nVersion;
81
+
82
+ export { type CodeGenOptions, type CodeGenResult, type DevEnv, type InstalledPackage, checkInstallPackage, checkVueI18nBridgeInstallPackage, generate$2 as generateJSON, generate as generateJavaScript, generate$1 as generateYAML, getVueI18nVersion };
@@ -0,0 +1,82 @@
1
+ import { JSONProgram } from 'jsonc-eslint-parser/lib/parser/ast';
2
+ import { RawSourceMap } from 'source-map-js';
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
+ onlyLocales?: string[];
22
+ source?: string;
23
+ sourceMap?: boolean;
24
+ filename?: string;
25
+ inSourceMap?: RawSourceMap;
26
+ isGlobal?: boolean;
27
+ locale?: string;
28
+ env?: DevEnv;
29
+ forceStringify?: boolean;
30
+ useClassComponent?: boolean;
31
+ allowDynamic?: boolean;
32
+ strictMessage?: boolean;
33
+ escapeHtml?: boolean;
34
+ jit?: boolean;
35
+ minify?: boolean;
36
+ onWarn?: (msg: string) => void;
37
+ onError?: (msg: string, extra?: {
38
+ source: string;
39
+ path: string;
40
+ code?: CompileError['code'];
41
+ domain?: CompileError['domain'];
42
+ location?: CompileError['location'];
43
+ }) => void;
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 / AST generator for i18n json/json5 resource
54
+ */
55
+
56
+ declare function generate$2(targetSource: string | Buffer, { type, legacy, bridge, onlyLocales, exportESM, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, useClassComponent, jit }: CodeGenOptions, injector?: () => string): CodeGenResult<JSONProgram>;
57
+
58
+ /**
59
+ * Code generator for i18n yaml resource
60
+ */
61
+
62
+ /**
63
+ * @internal
64
+ */
65
+ declare function generate$1(targetSource: string | Buffer, { type, legacy, bridge, onlyLocales, exportESM, useClassComponent, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, jit }: CodeGenOptions, injector?: () => string): CodeGenResult<YAMLProgram>;
66
+
67
+ /**
68
+ * Code generator for i18n js resource
69
+ */
70
+
71
+ /**
72
+ * @internal
73
+ */
74
+ declare function generate(targetSource: string | Buffer, { type, bridge, exportESM, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, useClassComponent, allowDynamic, jit }: CodeGenOptions, injector?: () => string): CodeGenResult<Node>;
75
+
76
+ type InstalledPackage = 'vue-i18n' | 'petite-vue-i18n';
77
+ declare function checkInstallPackage(pkg: string, debug: Function): InstalledPackage;
78
+ declare function checkVueI18nBridgeInstallPackage(debug: Function): boolean;
79
+ type VueI18nVersion = '8' | '9' | 'unknown' | '';
80
+ declare function getVueI18nVersion(debug: Function): VueI18nVersion;
81
+
82
+ export { type CodeGenOptions, type CodeGenResult, type DevEnv, type InstalledPackage, checkInstallPackage, checkVueI18nBridgeInstallPackage, generate$2 as generateJSON, generate as generateJavaScript, generate$1 as generateYAML, getVueI18nVersion };
package/lib/index.d.ts CHANGED
@@ -18,6 +18,7 @@ interface CodeGenOptions {
18
18
  legacy?: boolean;
19
19
  bridge?: boolean;
20
20
  exportESM?: boolean;
21
+ onlyLocales?: string[];
21
22
  source?: string;
22
23
  sourceMap?: boolean;
23
24
  filename?: string;
@@ -52,7 +53,7 @@ interface CodeGenResult<ASTNode, CodeGenError extends Error = Error> {
52
53
  * Code / AST generator for i18n json/json5 resource
53
54
  */
54
55
 
55
- declare function generate$2(targetSource: string | Buffer, { type, legacy, bridge, exportESM, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, useClassComponent, jit }: CodeGenOptions, injector?: () => string): CodeGenResult<JSONProgram>;
56
+ declare function generate$2(targetSource: string | Buffer, { type, legacy, bridge, onlyLocales, exportESM, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, useClassComponent, jit }: CodeGenOptions, injector?: () => string): CodeGenResult<JSONProgram>;
56
57
 
57
58
  /**
58
59
  * Code generator for i18n yaml resource
@@ -61,7 +62,7 @@ declare function generate$2(targetSource: string | Buffer, { type, legacy, bridg
61
62
  /**
62
63
  * @internal
63
64
  */
64
- declare function generate$1(targetSource: string | Buffer, { type, legacy, bridge, exportESM, useClassComponent, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, jit }: CodeGenOptions, injector?: () => string): CodeGenResult<YAMLProgram>;
65
+ declare function generate$1(targetSource: string | Buffer, { type, legacy, bridge, onlyLocales, exportESM, useClassComponent, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml, jit }: CodeGenOptions, injector?: () => string): CodeGenResult<YAMLProgram>;
65
66
 
66
67
  /**
67
68
  * Code generator for i18n js resource
@@ -78,4 +79,4 @@ declare function checkVueI18nBridgeInstallPackage(debug: Function): boolean;
78
79
  type VueI18nVersion = '8' | '9' | 'unknown' | '';
79
80
  declare function getVueI18nVersion(debug: Function): VueI18nVersion;
80
81
 
81
- export { CodeGenOptions, CodeGenResult, DevEnv, InstalledPackage, checkInstallPackage, checkVueI18nBridgeInstallPackage, generate$2 as generateJSON, generate as generateJavaScript, generate$1 as generateYAML, getVueI18nVersion };
82
+ export { type CodeGenOptions, type CodeGenResult, type DevEnv, type InstalledPackage, checkInstallPackage, checkVueI18nBridgeInstallPackage, generate$2 as generateJSON, generate as generateJavaScript, generate$1 as generateYAML, getVueI18nVersion };
package/lib/index.mjs CHANGED
@@ -1,9 +1,9 @@
1
- import { parseJSON, traverseNodes, getStaticJSONValue } from 'jsonc-eslint-parser';
1
+ import { parseJSON, getStaticJSONValue, traverseNodes } from 'jsonc-eslint-parser';
2
2
  import { isBoolean, format, escapeHtml, friendlyJSONstringify, isString, isNumber } from '@intlify/shared';
3
3
  import { SourceMapGenerator, SourceMapConsumer } from 'source-map-js';
4
4
  import { LOCATION_STUB, detectHtmlTag, baseCompile } from '@intlify/message-compiler';
5
5
  import MagicString from 'magic-string';
6
- import { parseYAML, traverseNodes as traverseNodes$1, getStaticYAMLValue } from 'yaml-eslint-parser';
6
+ import { parseYAML, getStaticYAMLValue, traverseNodes as traverseNodes$1 } from 'yaml-eslint-parser';
7
7
  import { parse } from 'acorn';
8
8
  import { generate as generate$3 } from 'escodegen';
9
9
  import { walk } from 'estree-walker';
@@ -254,6 +254,18 @@ function generateResourceAst(msg, options = {}, path) {
254
254
  const code = !occured ? `${friendlyJSONstringify(ast)}` : `\`${_msg}\``;
255
255
  return { code, ast, map, errors };
256
256
  }
257
+ function excludeLocales({
258
+ messages,
259
+ onlyLocales
260
+ }) {
261
+ const _messages = { ...messages };
262
+ Object.keys(_messages).forEach((locale) => {
263
+ if (!onlyLocales.includes(locale)) {
264
+ delete _messages[locale];
265
+ }
266
+ });
267
+ return _messages;
268
+ }
257
269
 
258
270
  function generateLegacyCode({ isGlobal }, generator) {
259
271
  const componentNamespace = `Component.options`;
@@ -273,6 +285,7 @@ function generate$2(targetSource, {
273
285
  type = "plain",
274
286
  legacy = false,
275
287
  bridge = false,
288
+ onlyLocales = [],
276
289
  exportESM = false,
277
290
  filename = "vue-i18n-loader.json",
278
291
  inSourceMap = void 0,
@@ -287,8 +300,7 @@ function generate$2(targetSource, {
287
300
  useClassComponent = false,
288
301
  jit = false
289
302
  }, injector) {
290
- const target = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
291
- const value = target;
303
+ let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
292
304
  const options = {
293
305
  type,
294
306
  bridge,
@@ -308,7 +320,17 @@ function generate$2(targetSource, {
308
320
  jit
309
321
  };
310
322
  const generator = createCodeGenerator(options);
311
- const ast = parseJSON(value, { filePath: filename });
323
+ let ast = parseJSON(value, { filePath: filename });
324
+ if (!locale && type === "sfc" && onlyLocales?.length) {
325
+ const messages = getStaticJSONValue(ast);
326
+ value = JSON.stringify(
327
+ excludeLocales({
328
+ messages,
329
+ onlyLocales
330
+ })
331
+ );
332
+ ast = parseJSON(value, { filePath: filename });
333
+ }
312
334
  if (legacy && type === "sfc") {
313
335
  const gen = () => friendlyJSONstringify(getStaticJSONValue(ast));
314
336
  const code2 = generateLegacyCode(options, gen);
@@ -533,6 +555,7 @@ function generate$1(targetSource, {
533
555
  type = "plain",
534
556
  legacy = false,
535
557
  bridge = false,
558
+ onlyLocales = [],
536
559
  exportESM = false,
537
560
  useClassComponent = false,
538
561
  filename = "vue-i18n-loader.yaml",
@@ -547,7 +570,7 @@ function generate$1(targetSource, {
547
570
  escapeHtml = false,
548
571
  jit = false
549
572
  }, injector) {
550
- const value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
573
+ let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
551
574
  const options = {
552
575
  type,
553
576
  bridge,
@@ -567,7 +590,17 @@ function generate$1(targetSource, {
567
590
  jit
568
591
  };
569
592
  const generator = createCodeGenerator(options);
570
- const ast = parseYAML(value, { filePath: filename });
593
+ let ast = parseYAML(value, { filePath: filename });
594
+ if (!locale && type === "sfc" && onlyLocales?.length) {
595
+ const messages = getStaticYAMLValue(ast);
596
+ value = JSON.stringify(
597
+ excludeLocales({
598
+ messages,
599
+ onlyLocales
600
+ })
601
+ );
602
+ ast = parseYAML(value, { filePath: filename });
603
+ }
571
604
  if (legacy && type === "sfc") {
572
605
  const gen = () => friendlyJSONstringify(getStaticYAMLValue(ast));
573
606
  const code2 = generateLegacyCode(options, gen);
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": "7.2.0",
4
+ "version": "7.4.0",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -18,8 +18,8 @@
18
18
  }
19
19
  },
20
20
  "dependencies": {
21
- "@intlify/message-compiler": "^9.3.0",
22
- "@intlify/shared": "^9.3.0",
21
+ "@intlify/message-compiler": "^9.4.0",
22
+ "@intlify/shared": "^9.4.0",
23
23
  "acorn": "^8.8.2",
24
24
  "escodegen": "^2.0.0",
25
25
  "estree-walker": "^2.0.2",
@@ -30,9 +30,9 @@
30
30
  "yaml-eslint-parser": "^1.2.2"
31
31
  },
32
32
  "devDependencies": {
33
- "@types/escodegen": "^0.0.7",
33
+ "@types/escodegen": "^0.0.8",
34
34
  "@types/estree": "^1.0.0",
35
- "unbuild": "^1.2.1"
35
+ "unbuild": "^2.0.0"
36
36
  },
37
37
  "engines": {
38
38
  "node": ">= 14.16"