@intlify/bundle-utils 3.0.0 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/codegen.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ResourceNode } from '@intlify/message-compiler';
1
+ import { CompileError, ResourceNode } from '@intlify/message-compiler';
2
2
  import { SourceMapGenerator } from 'source-map';
3
3
  import type { RawSourceMap } from 'source-map';
4
4
  /**
@@ -24,6 +24,7 @@ export interface SourceLocationable {
24
24
  export interface CodeGenOptions {
25
25
  type?: 'plain' | 'sfc' | 'bare';
26
26
  bridge?: boolean;
27
+ exportESM?: boolean;
27
28
  source?: string;
28
29
  sourceMap?: boolean;
29
30
  filename?: string;
@@ -34,7 +35,12 @@ export interface CodeGenOptions {
34
35
  forceStringify?: boolean;
35
36
  useClassComponent?: boolean;
36
37
  onWarn?: (msg: string) => void;
37
- onError?: (msg: string) => void;
38
+ onError?: (msg: string, extra?: {
39
+ source: string;
40
+ code?: CompileError['code'];
41
+ domain?: CompileError['domain'];
42
+ location?: CompileError['location'];
43
+ }) => void;
38
44
  }
39
45
  export interface CodeGenContext {
40
46
  source?: string;
package/lib/codegen.js CHANGED
@@ -96,13 +96,23 @@ function advancePositionWithSource(pos, source, numberOfCharacters = source.leng
96
96
  }
97
97
  function generateMessageFunction(msg, options) {
98
98
  const env = options.env != null ? options.env : 'development';
99
- let occured = false;
100
- const newOptions = Object.assign(options, { mode: 'arrow' });
99
+ const onError = options.onError;
100
+ const errors = [];
101
+ const newOptions = Object.assign({ mode: 'arrow' }, options);
101
102
  newOptions.onError = (err) => {
102
- options.onError && options.onError(err.message);
103
- occured = true;
103
+ if (onError) {
104
+ const extra = {
105
+ source: msg,
106
+ code: err.code,
107
+ domain: err.domain,
108
+ location: err.location
109
+ };
110
+ onError(err.message, extra);
111
+ errors.push(err);
112
+ }
104
113
  };
105
- const { code, ast, map } = message_compiler_1.baseCompile(msg, newOptions);
114
+ const { code, ast, map } = (0, message_compiler_1.baseCompile)(msg, newOptions);
115
+ const occured = errors.length > 0;
106
116
  const genCode = !occured
107
117
  ? env === 'development'
108
118
  ? `(()=>{const fn=${code};fn.source=${JSON.stringify(msg)};return fn;})()`
@@ -0,0 +1,2 @@
1
+ export declare class CompileError extends SyntaxError {
2
+ }
package/lib/errors.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CompileError = void 0;
4
+ class CompileError extends SyntaxError {
5
+ }
6
+ exports.CompileError = CompileError;
package/lib/json.d.ts CHANGED
@@ -7,4 +7,4 @@ import type { CodeGenOptions, CodeGenResult } from './codegen';
7
7
  /**
8
8
  * @internal
9
9
  */
10
- export declare function generate(targetSource: string | Buffer, { type, bridge, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, useClassComponent }: CodeGenOptions, injector?: () => string): CodeGenResult<JSONProgram>;
10
+ export declare function generate(targetSource: string | Buffer, { type, bridge, exportESM, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, useClassComponent }: CodeGenOptions, injector?: () => string): CodeGenResult<JSONProgram>;
package/lib/json.js CHANGED
@@ -10,7 +10,7 @@ const codegen_1 = require("./codegen");
10
10
  /**
11
11
  * @internal
12
12
  */
13
- function generate(targetSource, { type = 'plain', bridge = false, filename = 'vue-i18n-loader.json', inSourceMap = undefined, locale = '', isGlobal = false, sourceMap = false, env = 'development', forceStringify = false, useClassComponent = false }, injector) {
13
+ function generate(targetSource, { type = 'plain', bridge = false, exportESM = false, filename = 'vue-i18n-loader.json', inSourceMap = undefined, locale = '', isGlobal = false, sourceMap = false, env = 'development', forceStringify = false, onError = undefined, useClassComponent = false }, injector) {
14
14
  const target = Buffer.isBuffer(targetSource)
15
15
  ? targetSource.toString()
16
16
  : targetSource;
@@ -21,6 +21,7 @@ function generate(targetSource, { type = 'plain', bridge = false, filename = 'vu
21
21
  const options = {
22
22
  type,
23
23
  bridge,
24
+ exportESM,
24
25
  source: value,
25
26
  sourceMap,
26
27
  locale,
@@ -29,10 +30,11 @@ function generate(targetSource, { type = 'plain', bridge = false, filename = 'vu
29
30
  env,
30
31
  filename,
31
32
  forceStringify,
33
+ onError,
32
34
  useClassComponent
33
35
  };
34
- const generator = codegen_1.createCodeGenerator(options);
35
- const ast = jsonc_eslint_parser_1.parseJSON(value, { filePath: filename });
36
+ const generator = (0, codegen_1.createCodeGenerator)(options);
37
+ const ast = (0, jsonc_eslint_parser_1.parseJSON)(value, { filePath: filename });
36
38
  const codeMaps = generateNode(generator, ast, options, injector);
37
39
  const { code, map } = generator.context();
38
40
  // if (map) {
@@ -43,7 +45,7 @@ function generate(targetSource, { type = 'plain', bridge = false, filename = 'vu
43
45
  // }
44
46
  // prettier-ignore
45
47
  const newMap = map
46
- ? codegen_1.mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null // eslint-disable-line @typescript-eslint/no-explicit-any
48
+ ? (0, codegen_1.mapLinesColumns)(map.toJSON(), codeMaps, inSourceMap) || null // eslint-disable-line @typescript-eslint/no-explicit-any
47
49
  : null;
48
50
  return {
49
51
  ast,
@@ -57,13 +59,14 @@ function generateNode(generator, node, options, injector) {
57
59
  const itemsCountStack = [];
58
60
  const { forceStringify } = generator.context();
59
61
  const codeMaps = new Map();
60
- const { type, bridge, sourceMap, isGlobal, locale, useClassComponent } = options;
62
+ const { type, bridge, exportESM, sourceMap, isGlobal, locale, useClassComponent } = options;
63
+ // prettier-ignore
61
64
  const componentNamespace = bridge
62
65
  ? `Component.options`
63
66
  : useClassComponent
64
67
  ? `Component.__o`
65
68
  : `Component`;
66
- jsonc_eslint_parser_1.traverseNodes(node, {
69
+ (0, jsonc_eslint_parser_1.traverseNodes)(node, {
67
70
  enterNode(node, parent) {
68
71
  switch (node.type) {
69
72
  case 'Program':
@@ -74,7 +77,11 @@ function generateNode(generator, node, options, injector) {
74
77
  // for 'sfc'
75
78
  const variableName = type === 'sfc' ? (!isGlobal ? '__i18n' : '__i18nGlobal') : '';
76
79
  const localeName = type === 'sfc' ? (locale != null ? locale : `""`) : '';
77
- const exportSyntax = bridge ? `module.exports =` : `export default`;
80
+ const exportSyntax = bridge
81
+ ? exportESM
82
+ ? `export default`
83
+ : `module.exports =`
84
+ : `export default`;
78
85
  generator.push(`${exportSyntax} function (Component) {`);
79
86
  generator.indent();
80
87
  generator.pushline(`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`);
@@ -99,9 +106,9 @@ function generateNode(generator, node, options, injector) {
99
106
  node.key.type === 'JSONIdentifier')) {
100
107
  const name = node.key.type === 'JSONLiteral' ? node.key.value : node.key.name;
101
108
  const value = node.value.value;
102
- if (shared_1.isString(value)) {
109
+ if ((0, shared_1.isString)(value)) {
103
110
  generator.push(`${JSON.stringify(name)}: `);
104
- const { code, map } = codegen_1.generateMessageFunction(value, options);
111
+ const { code, map } = (0, codegen_1.generateMessageFunction)(value, options);
105
112
  sourceMap && map != null && codeMaps.set(value, map);
106
113
  generator.push(`${code}`, node.value, value);
107
114
  }
@@ -109,7 +116,7 @@ function generateNode(generator, node, options, injector) {
109
116
  if (forceStringify) {
110
117
  const strValue = JSON.stringify(value);
111
118
  generator.push(`${JSON.stringify(name)}: `);
112
- const { code, map } = codegen_1.generateMessageFunction(strValue, options);
119
+ const { code, map } = (0, codegen_1.generateMessageFunction)(strValue, options);
113
120
  sourceMap && map != null && codeMaps.set(strValue, map);
114
121
  generator.push(`${code}`, node.value, strValue);
115
122
  }
@@ -137,15 +144,15 @@ function generateNode(generator, node, options, injector) {
137
144
  if (parent.type === 'JSONArrayExpression') {
138
145
  if (node.type === 'JSONLiteral') {
139
146
  const value = node.value;
140
- if (shared_1.isString(value)) {
141
- const { code, map } = codegen_1.generateMessageFunction(value, options);
147
+ if ((0, shared_1.isString)(value)) {
148
+ const { code, map } = (0, codegen_1.generateMessageFunction)(value, options);
142
149
  sourceMap && map != null && codeMaps.set(value, map);
143
150
  generator.push(`${code}`, node, value);
144
151
  }
145
152
  else {
146
153
  if (forceStringify) {
147
154
  const strValue = JSON.stringify(value);
148
- const { code, map } = codegen_1.generateMessageFunction(strValue, options);
155
+ const { code, map } = (0, codegen_1.generateMessageFunction)(strValue, options);
149
156
  sourceMap && map != null && codeMaps.set(strValue, map);
150
157
  generator.push(`${code}`, node, strValue);
151
158
  }
package/lib/yaml.d.ts CHANGED
@@ -7,4 +7,4 @@ import type { CodeGenOptions, CodeGenResult } from './codegen';
7
7
  /**
8
8
  * @internal
9
9
  */
10
- export declare function generate(targetSource: string | Buffer, { type, bridge, useClassComponent, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify }: CodeGenOptions, injector?: () => string): CodeGenResult<YAMLProgram>;
10
+ export declare function generate(targetSource: string | Buffer, { type, bridge, exportESM, useClassComponent, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError }: CodeGenOptions, injector?: () => string): CodeGenResult<YAMLProgram>;
package/lib/yaml.js CHANGED
@@ -10,13 +10,14 @@ const yaml_eslint_parser_1 = require("yaml-eslint-parser");
10
10
  /**
11
11
  * @internal
12
12
  */
13
- function generate(targetSource, { type = 'plain', bridge = false, useClassComponent = false, filename = 'vue-i18n-loader.yaml', inSourceMap = undefined, locale = '', isGlobal = false, sourceMap = false, env = 'development', forceStringify = false }, injector) {
13
+ function generate(targetSource, { type = 'plain', bridge = false, exportESM = false, useClassComponent = false, filename = 'vue-i18n-loader.yaml', inSourceMap = undefined, locale = '', isGlobal = false, sourceMap = false, env = 'development', forceStringify = false, onError = undefined }, injector) {
14
14
  const value = Buffer.isBuffer(targetSource)
15
15
  ? targetSource.toString()
16
16
  : targetSource;
17
17
  const options = {
18
18
  type,
19
19
  bridge,
20
+ exportESM,
20
21
  source: value,
21
22
  sourceMap,
22
23
  locale,
@@ -25,15 +26,16 @@ function generate(targetSource, { type = 'plain', bridge = false, useClassCompon
25
26
  env,
26
27
  filename,
27
28
  forceStringify,
29
+ onError,
28
30
  useClassComponent
29
31
  };
30
- const generator = codegen_1.createCodeGenerator(options);
31
- const ast = yaml_eslint_parser_1.parseYAML(value, { filePath: filename });
32
+ const generator = (0, codegen_1.createCodeGenerator)(options);
33
+ const ast = (0, yaml_eslint_parser_1.parseYAML)(value, { filePath: filename });
32
34
  const codeMaps = generateNode(generator, ast, options, injector);
33
35
  const { code, map } = generator.context();
34
36
  // prettier-ignore
35
37
  const newMap = map
36
- ? codegen_1.mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null // eslint-disable-line @typescript-eslint/no-explicit-any
38
+ ? (0, codegen_1.mapLinesColumns)(map.toJSON(), codeMaps, inSourceMap) || null // eslint-disable-line @typescript-eslint/no-explicit-any
37
39
  : null;
38
40
  return {
39
41
  ast,
@@ -47,13 +49,14 @@ function generateNode(generator, node, options, injector) {
47
49
  const itemsCountStack = [];
48
50
  const { forceStringify } = generator.context();
49
51
  const codeMaps = new Map();
50
- const { type, bridge, sourceMap, isGlobal, locale, useClassComponent } = options;
52
+ const { type, bridge, exportESM, sourceMap, isGlobal, locale, useClassComponent } = options;
53
+ // prettier-ignore
51
54
  const componentNamespace = bridge
52
55
  ? `Component.options`
53
56
  : useClassComponent
54
57
  ? `Component.__o`
55
58
  : `Component`;
56
- yaml_eslint_parser_1.traverseNodes(node, {
59
+ (0, yaml_eslint_parser_1.traverseNodes)(node, {
57
60
  enterNode(node, parent) {
58
61
  switch (node.type) {
59
62
  case 'Program':
@@ -63,7 +66,11 @@ function generateNode(generator, node, options, injector) {
63
66
  else if (type === 'sfc') {
64
67
  const variableName = type === 'sfc' ? (!isGlobal ? '__i18n' : '__i18nGlobal') : '';
65
68
  const localeName = type === 'sfc' ? (locale != null ? locale : `""`) : '';
66
- const exportSyntax = bridge ? `module.exports =` : `export default`;
69
+ const exportSyntax = bridge
70
+ ? exportESM
71
+ ? `export default`
72
+ : `module.exports =`
73
+ : `export default`;
67
74
  generator.push(`${exportSyntax} function (Component) {`);
68
75
  generator.indent();
69
76
  generator.pushline(`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`);
@@ -89,9 +96,9 @@ function generateNode(generator, node, options, injector) {
89
96
  node.key.type === 'YAMLScalar') {
90
97
  const name = node.key.value;
91
98
  const value = node.value.value;
92
- if (shared_1.isString(value)) {
99
+ if ((0, shared_1.isString)(value)) {
93
100
  generator.push(`${JSON.stringify(name)}: `);
94
- const { code, map } = codegen_1.generateMessageFunction(value, options);
101
+ const { code, map } = (0, codegen_1.generateMessageFunction)(value, options);
95
102
  sourceMap && map != null && codeMaps.set(value, map);
96
103
  generator.push(`${code}`, node.value, value);
97
104
  }
@@ -99,7 +106,7 @@ function generateNode(generator, node, options, injector) {
99
106
  if (forceStringify) {
100
107
  const strValue = JSON.stringify(value);
101
108
  generator.push(`${JSON.stringify(name)}: `);
102
- const { code, map } = codegen_1.generateMessageFunction(strValue, options);
109
+ const { code, map } = (0, codegen_1.generateMessageFunction)(strValue, options);
103
110
  sourceMap && map != null && codeMaps.set(strValue, map);
104
111
  generator.push(`${code}`, node.value, strValue);
105
112
  }
@@ -128,15 +135,15 @@ function generateNode(generator, node, options, injector) {
128
135
  if (parent.type === 'YAMLSequence') {
129
136
  if (node.type === 'YAMLScalar') {
130
137
  const value = node.value;
131
- if (shared_1.isString(value)) {
132
- const { code, map } = codegen_1.generateMessageFunction(value, options);
138
+ if ((0, shared_1.isString)(value)) {
139
+ const { code, map } = (0, codegen_1.generateMessageFunction)(value, options);
133
140
  sourceMap && map != null && codeMaps.set(value, map);
134
141
  generator.push(`${code}`, node, value);
135
142
  }
136
143
  else {
137
144
  if (forceStringify) {
138
145
  const strValue = JSON.stringify(value);
139
- const { code, map } = codegen_1.generateMessageFunction(strValue, options);
146
+ const { code, map } = (0, codegen_1.generateMessageFunction)(strValue, options);
140
147
  sourceMap && map != null && codeMaps.set(strValue, map);
141
148
  generator.push(`${code}`, node, strValue);
142
149
  }
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": "3.0.0",
4
+ "version": "3.1.1",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -9,12 +9,11 @@
9
9
  "bugs": {
10
10
  "url": "https://github.com/intlify/bundle-tools/issues"
11
11
  },
12
- "peerDependencies": {},
13
12
  "peerDependenciesMeta": {
14
- "vue-i18n": {
13
+ "petite-vue-i18n": {
15
14
  "optional": true
16
15
  },
17
- "petite-vue-i18n": {
16
+ "vue-i18n": {
18
17
  "optional": true
19
18
  }
20
19
  },
@@ -22,10 +21,9 @@
22
21
  "@intlify/message-compiler": "next",
23
22
  "@intlify/shared": "next",
24
23
  "jsonc-eslint-parser": "^1.0.1",
25
- "source-map": "^0.6.1",
24
+ "source-map": "0.6.1",
26
25
  "yaml-eslint-parser": "^0.3.2"
27
26
  },
28
- "devDependencies": {},
29
27
  "engines": {
30
28
  "node": ">= 12"
31
29
  },
@@ -42,11 +40,11 @@
42
40
  },
43
41
  "scripts": {
44
42
  "build": "tsc -p .",
45
- "clean": "npm-run-all clean:*",
43
+ "clean": "npm-run-all \"clean:*\"",
46
44
  "clean:lib": "rm -rf ./lib",
47
45
  "watch": "tsc -p . --watch",
48
- "changelog": "esmo ../../scripts/changelog.ts",
49
- "release": "esmo ../../scripts/release.ts"
46
+ "changelog": "jiti ../../scripts/changelog.ts",
47
+ "release": "jiti ../../scripts/release.ts"
50
48
  },
51
49
  "types": "lib/index.d.ts"
52
50
  }
package/CHANGELOG.md DELETED
@@ -1,102 +0,0 @@
1
-
2
- ## @intlify/bundle-utils@3.0.0 (2022-04-05)
3
-
4
- #### :star: Features
5
- * [#101](https://github.com/intlify/bundle-tools/pull/101) feat(bundle-utils): pinned intlify related module next version ([@kazupon](https://github.com/kazupon))
6
-
7
- #### Committers: 1
8
- - kazuya kawaguchi ([@kazupon](https://github.com/kazupon))
9
-
10
-
11
- ## @intlify/bundle-utils@2.2.2 (2022-04-05)
12
-
13
- #### :zap: Improvement Features
14
- * [#100](https://github.com/intlify/bundle-tools/pull/100) fix(bundle-utils): pinned latest stable version ([@kazupon](https://github.com/kazupon))
15
-
16
- #### Committers: 1
17
- - kazuya kawaguchi ([@kazupon](https://github.com/kazupon))
18
-
19
-
20
- ## @intlify/bundle-utils@2.2.1 (2022-03-22)
21
-
22
- #### :bug: Bug Fixes
23
- * [#98](https://github.com/intlify/bundle-tools/pull/98) fix(bundle-utils): fix nested array code generation ([@kazupon](https://github.com/kazupon))
24
-
25
- #### Committers: 1
26
- - kazuya kawaguchi ([@kazupon](https://github.com/kazupon))
27
-
28
-
29
- ## @intlify/bundle-utils@2.2.1 (2022-03-22)
30
-
31
- #### :bug: Bug Fixes
32
- * [#98](https://github.com/intlify/bundle-tools/pull/98) fix(bundle-utils): fix nested array code generation ([@kazupon](https://github.com/kazupon))
33
-
34
- #### Committers: 1
35
- - kazuya kawaguchi ([@kazupon](https://github.com/kazupon))
36
-
37
-
38
- ## @intlify/bundle-utils@2.2.0 (2021-12-16)
39
-
40
- #### :zap: Improvement Features
41
- * [#75](https://github.com/intlify/bundle-tools/pull/75) Add vue-class-components support via loader option ([@SniperJoe](https://github.com/SniperJoe))
42
-
43
- #### Committers: 1
44
- - [@SniperJoe](https://github.com/SniperJoe)
45
-
46
-
47
- ## @intlify/bundle-utils@2.1.0 (2021-11-10)
48
-
49
- #### :zap: Improvement Features
50
- * [#67](https://github.com/intlify/bundle-tools/pull/67) fix: add vite and vue-i18n as peerDependencies ([@AlexandreBonaventure](https://github.com/AlexandreBonaventure))
51
-
52
- #### Committers: 1
53
- - Alexandre Bonaventure Geissmann ([@AlexandreBonaventure](https://github.com/AlexandreBonaventure))
54
-
55
-
56
- ## @intlify/bundle-utils@2.0.0 (2021-10-24)
57
-
58
- #### :star: Features
59
- * [#60](https://github.com/intlify/bundle-tools/pull/60) feat: change to intlify beta related deps ([@kazupon](https://github.com/kazupon))
60
-
61
- #### Committers: 1
62
- - kazuya kawaguchi ([@kazupon](https://github.com/kazupon))
63
-
64
-
65
- ## @intlify/bundle-utils@1.0.0 (2021-10-24)
66
-
67
- #### :star: Features
68
- * [#56](https://github.com/intlify/bundle-tools/pull/56) feat: bump to major version ([@kazupon](https://github.com/kazupon))
69
-
70
- #### Committers: 1
71
- - kazuya kawaguchi ([@kazupon](https://github.com/kazupon))
72
-
73
-
74
- ## @intlify/bundle-utils@0.3.0 (2021-09-29)
75
-
76
- #### :star: Features
77
- * [#47](https://github.com/intlify/bundle-tools/pull/47) feat: support vue i18n bridge ([@kazupon](https://github.com/kazupon))
78
-
79
- #### Committers: 1
80
- - kazuya kawaguchi ([@kazupon](https://github.com/kazupon))
81
-
82
-
83
- ## @intlify/bundle-utils@0.2.0 (2021-07-26)
84
-
85
- #### :star: Features
86
- * [#36](https://github.com/intlify/bundle-tools/pull/36) add install package checking ([@kazupon](https://github.com/kazupon))
87
-
88
- #### Committers: 1
89
- - kazuya kawaguchi ([@kazupon](https://github.com/kazupon))
90
-
91
-
92
- ## @intlify/bundle-utils@0.1.0 (2021-07-12)
93
-
94
- #### :star: Features
95
- * [#26](https://github.com/intlify/bundle-tools/pull/26) feat: bundle-utils basic implementation ([@kazupon](https://github.com/kazupon))
96
-
97
- #### Committers: 1
98
- - kazuya kawaguchi ([@kazupon](https://github.com/kazupon))
99
-
100
- ## v0.0.0 (2021-07-13)
101
-
102
- :tada: Initial release.