@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/json.js DELETED
@@ -1,283 +0,0 @@
1
- "use strict";
2
- /**
3
- * Code generator for i18n json/json5 resource
4
- */
5
- var __importDefault = (this && this.__importDefault) || function (mod) {
6
- return (mod && mod.__esModule) ? mod : { "default": mod };
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.generate = void 0;
10
- const jsonc_eslint_parser_1 = require("jsonc-eslint-parser");
11
- const shared_1 = require("@intlify/shared");
12
- const codegen_1 = require("./codegen");
13
- const legacy_1 = require("./legacy");
14
- const magic_string_1 = __importDefault(require("magic-string"));
15
- /**
16
- * @internal
17
- */
18
- function generate(targetSource, { type = 'plain', legacy = false, bridge = false, exportESM = false, filename = 'vue-i18n-loader.json', inSourceMap = undefined, locale = '', isGlobal = false, sourceMap = false, env = 'development', forceStringify = false, onError = undefined, strictMessage = true, escapeHtml = false, useClassComponent = false }, injector) {
19
- const target = Buffer.isBuffer(targetSource)
20
- ? targetSource.toString()
21
- : targetSource;
22
- // const value = JSON.stringify(JSON.parse(target))
23
- // .replace(/\u2028/g, '\\u2028') // line separator
24
- // .replace(/\u2029/g, '\\u2029') // paragraph separator
25
- const value = target;
26
- const options = {
27
- type,
28
- bridge,
29
- exportESM,
30
- source: value,
31
- sourceMap,
32
- locale,
33
- isGlobal,
34
- inSourceMap,
35
- env,
36
- filename,
37
- forceStringify,
38
- onError,
39
- strictMessage,
40
- escapeHtml,
41
- useClassComponent
42
- };
43
- const generator = (0, codegen_1.createCodeGenerator)(options);
44
- const ast = (0, jsonc_eslint_parser_1.parseJSON)(value, { filePath: filename });
45
- // for vue 2.x
46
- if (legacy && type === 'sfc') {
47
- const gen = () => (0, shared_1.friendlyJSONstringify)((0, jsonc_eslint_parser_1.getStaticJSONValue)(ast));
48
- const code = (0, legacy_1.generateLegacyCode)(options, gen);
49
- const s = new magic_string_1.default(code);
50
- return {
51
- ast,
52
- code: s.toString(),
53
- map: s.generateMap({
54
- file: filename,
55
- source: value,
56
- includeContent: true
57
- })
58
- };
59
- }
60
- const codeMaps = generateNode(generator, ast, options, injector);
61
- const { code, map } = generator.context();
62
- // if (map) {
63
- // const s = new SourceMapConsumer((map as any).toJSON())
64
- // s.eachMapping(m => {
65
- // console.log('sourcemap json', m)
66
- // })
67
- // }
68
- // prettier-ignore
69
- const newMap = map
70
- ? (0, codegen_1.mapLinesColumns)(map.toJSON(), codeMaps, inSourceMap) || null // eslint-disable-line @typescript-eslint/no-explicit-any
71
- : null;
72
- return {
73
- ast,
74
- code,
75
- map: newMap != null ? newMap : undefined
76
- };
77
- }
78
- exports.generate = generate;
79
- function generateNode(generator, node, options, injector) {
80
- const propsCountStack = [];
81
- const pathStack = [];
82
- const itemsCountStack = [];
83
- const { forceStringify } = generator.context();
84
- const codeMaps = new Map();
85
- const { type, bridge, exportESM, sourceMap, isGlobal, locale, useClassComponent } = options;
86
- const componentNamespace = '_Component';
87
- (0, jsonc_eslint_parser_1.traverseNodes)(node, {
88
- enterNode(node, parent) {
89
- switch (node.type) {
90
- case 'Program':
91
- if (type === 'plain') {
92
- generator.push(`const resource = `);
93
- }
94
- else if (type === 'sfc') {
95
- // for 'sfc'
96
- const variableName = type === 'sfc' ? (!isGlobal ? '__i18n' : '__i18nGlobal') : '';
97
- const localeName = type === 'sfc' ? (locale != null ? locale : `""`) : '';
98
- const exportSyntax = bridge
99
- ? exportESM
100
- ? `export default`
101
- : `module.exports =`
102
- : `export default`;
103
- generator.push(`${exportSyntax} function (Component) {`);
104
- generator.indent();
105
- // prettier-ignore
106
- const componentVariable = bridge
107
- ? `Component.options || Component`
108
- : useClassComponent
109
- ? `Component.__o || Component`
110
- : `Component`;
111
- // prettier-ignore
112
- generator.pushline(`const ${componentNamespace} = ${componentVariable}`);
113
- generator.pushline(`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`);
114
- generator.push(`${componentNamespace}.${variableName}.push({`);
115
- generator.indent();
116
- generator.pushline(`"locale": ${JSON.stringify(localeName)},`);
117
- generator.push(`"resource": `);
118
- }
119
- break;
120
- case 'JSONObjectExpression':
121
- generator.push(`{`);
122
- generator.indent();
123
- propsCountStack.push(node.properties.length);
124
- if (parent.type === 'JSONArrayExpression') {
125
- const lastIndex = itemsCountStack.length - 1;
126
- const currentCount = parent.elements.length - itemsCountStack[lastIndex];
127
- pathStack.push(currentCount.toString());
128
- itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
129
- }
130
- break;
131
- case 'JSONProperty':
132
- if (node.value.type === 'JSONLiteral' &&
133
- (node.key.type === 'JSONLiteral' ||
134
- node.key.type === 'JSONIdentifier')) {
135
- const name = node.key.type === 'JSONLiteral' ? node.key.value : node.key.name;
136
- const value = node.value.value;
137
- if ((0, shared_1.isString)(value)) {
138
- generator.push(`${JSON.stringify(name)}: `);
139
- pathStack.push(name.toString());
140
- const { code, map } = (0, codegen_1.generateMessageFunction)(value, options, pathStack);
141
- sourceMap && map != null && codeMaps.set(value, map);
142
- generator.push(`${code}`, node.value, value);
143
- }
144
- else {
145
- if (forceStringify) {
146
- const strValue = JSON.stringify(value);
147
- generator.push(`${JSON.stringify(name)}: `);
148
- pathStack.push(name.toString());
149
- const { code, map } = (0, codegen_1.generateMessageFunction)(strValue, options, pathStack);
150
- sourceMap && map != null && codeMaps.set(strValue, map);
151
- generator.push(`${code}`, node.value, strValue);
152
- }
153
- else {
154
- generator.push(`${JSON.stringify(name)}: ${JSON.stringify(value)}`);
155
- pathStack.push(name.toString());
156
- }
157
- }
158
- }
159
- else if ((node.value.type === 'JSONObjectExpression' ||
160
- node.value.type === 'JSONArrayExpression') &&
161
- (node.key.type === 'JSONLiteral' ||
162
- node.key.type === 'JSONIdentifier')) {
163
- const name = node.key.type === 'JSONLiteral' ? node.key.value : node.key.name;
164
- generator.push(`${JSON.stringify(name)}: `);
165
- pathStack.push(name.toString());
166
- }
167
- const lastIndex = propsCountStack.length - 1;
168
- propsCountStack[lastIndex] = --propsCountStack[lastIndex];
169
- break;
170
- case 'JSONArrayExpression':
171
- generator.push(`[`);
172
- generator.indent();
173
- if (parent.type === 'JSONArrayExpression') {
174
- const lastIndex = itemsCountStack.length - 1;
175
- const currentCount = parent.elements.length - itemsCountStack[lastIndex];
176
- pathStack.push(currentCount.toString());
177
- itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
178
- }
179
- itemsCountStack.push(node.elements.length);
180
- break;
181
- case 'JSONLiteral':
182
- if (parent.type === 'JSONArrayExpression') {
183
- const lastIndex = itemsCountStack.length - 1;
184
- const currentCount = parent.elements.length - itemsCountStack[lastIndex];
185
- pathStack.push(currentCount.toString());
186
- if (node.type === 'JSONLiteral') {
187
- const value = node.value;
188
- if ((0, shared_1.isString)(value)) {
189
- const { code, map } = (0, codegen_1.generateMessageFunction)(value, options, pathStack);
190
- sourceMap && map != null && codeMaps.set(value, map);
191
- generator.push(`${code}`, node, value);
192
- }
193
- else {
194
- if (forceStringify) {
195
- const strValue = JSON.stringify(value);
196
- const { code, map } = (0, codegen_1.generateMessageFunction)(strValue, options, pathStack);
197
- sourceMap && map != null && codeMaps.set(strValue, map);
198
- generator.push(`${code}`, node, strValue);
199
- }
200
- else {
201
- generator.push(`${JSON.stringify(value)}`);
202
- }
203
- }
204
- }
205
- itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
206
- }
207
- break;
208
- default:
209
- break;
210
- }
211
- },
212
- leaveNode(node, parent) {
213
- switch (node.type) {
214
- case 'Program':
215
- if (type === 'sfc') {
216
- generator.deindent();
217
- generator.push(`})`);
218
- if (bridge && injector) {
219
- generator.newline();
220
- generator.pushline(`${componentNamespace}.__i18nBridge = ${componentNamespace}.__i18nBridge || []`);
221
- generator.pushline(`${componentNamespace}.__i18nBridge.push('${injector()}')`);
222
- generator.pushline(`delete ${componentNamespace}._Ctor`);
223
- }
224
- generator.deindent();
225
- generator.pushline(`}`);
226
- }
227
- else if (type === 'plain') {
228
- generator.push(`\n`);
229
- generator.push('export default resource');
230
- }
231
- break;
232
- case 'JSONObjectExpression':
233
- if (propsCountStack[propsCountStack.length - 1] === 0) {
234
- pathStack.pop();
235
- propsCountStack.pop();
236
- }
237
- generator.deindent();
238
- generator.push(`}`);
239
- if (parent.type === 'JSONArrayExpression') {
240
- if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
241
- pathStack.pop();
242
- generator.pushline(`,`);
243
- }
244
- }
245
- break;
246
- case 'JSONProperty':
247
- if (propsCountStack[propsCountStack.length - 1] !== 0) {
248
- pathStack.pop();
249
- generator.pushline(`,`);
250
- }
251
- break;
252
- case 'JSONArrayExpression':
253
- if (itemsCountStack[itemsCountStack.length - 1] === 0) {
254
- pathStack.pop();
255
- itemsCountStack.pop();
256
- }
257
- generator.deindent();
258
- generator.push(`]`);
259
- if (parent.type === 'JSONArrayExpression') {
260
- if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
261
- pathStack.pop();
262
- generator.pushline(`,`);
263
- }
264
- }
265
- break;
266
- case 'JSONLiteral':
267
- if (parent.type === 'JSONArrayExpression') {
268
- if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
269
- pathStack.pop();
270
- generator.pushline(`,`);
271
- }
272
- else {
273
- generator.pushline(`,`);
274
- }
275
- }
276
- break;
277
- default:
278
- break;
279
- }
280
- }
281
- });
282
- return codeMaps;
283
- }
package/lib/legacy.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import type { CodeGenOptions } from './codegen';
2
- export declare function generateLegacyCode({ isGlobal }: CodeGenOptions, generator: () => string): string;
package/lib/legacy.js DELETED
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateLegacyCode = void 0;
4
- const shared_1 = require("@intlify/shared");
5
- function generateLegacyCode({ isGlobal }, generator) {
6
- // prettier-ignore
7
- const componentNamespace = `Component.options`;
8
- const variableName = !isGlobal ? '__i18n' : '__i18nGlobal';
9
- const exportSyntax = 'export default';
10
- const code = `${exportSyntax} function (Component) {
11
- ${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []
12
- ${componentNamespace}.${variableName}.push(${(0, shared_1.friendlyJSONstringify)(generator())})
13
- delete ${componentNamespace}._Ctor
14
- }`;
15
- return code;
16
- }
17
- exports.generateLegacyCode = generateLegacyCode;
package/lib/yaml.d.ts DELETED
@@ -1,10 +0,0 @@
1
- /**
2
- * Code generator for i18n yaml resource
3
- */
4
- /// <reference types="node" />
5
- import type { YAMLProgram } from 'yaml-eslint-parser/lib/ast';
6
- import type { CodeGenOptions, CodeGenResult } from './codegen';
7
- /**
8
- * @internal
9
- */
10
- export declare function generate(targetSource: string | Buffer, { type, legacy, bridge, exportESM, useClassComponent, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, strictMessage, escapeHtml }: CodeGenOptions, injector?: () => string): CodeGenResult<YAMLProgram>;
package/lib/yaml.js DELETED
@@ -1,274 +0,0 @@
1
- "use strict";
2
- /**
3
- * Code generator for i18n yaml resource
4
- */
5
- var __importDefault = (this && this.__importDefault) || function (mod) {
6
- return (mod && mod.__esModule) ? mod : { "default": mod };
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.generate = void 0;
10
- const shared_1 = require("@intlify/shared");
11
- const codegen_1 = require("./codegen");
12
- const yaml_eslint_parser_1 = require("yaml-eslint-parser");
13
- const legacy_1 = require("./legacy");
14
- const magic_string_1 = __importDefault(require("magic-string"));
15
- /**
16
- * @internal
17
- */
18
- function generate(targetSource, { type = 'plain', legacy = false, bridge = false, exportESM = false, useClassComponent = false, filename = 'vue-i18n-loader.yaml', inSourceMap = undefined, locale = '', isGlobal = false, sourceMap = false, env = 'development', forceStringify = false, onError = undefined, strictMessage = true, escapeHtml = false }, injector) {
19
- const value = Buffer.isBuffer(targetSource)
20
- ? targetSource.toString()
21
- : targetSource;
22
- const options = {
23
- type,
24
- bridge,
25
- exportESM,
26
- source: value,
27
- sourceMap,
28
- locale,
29
- isGlobal,
30
- inSourceMap,
31
- env,
32
- filename,
33
- forceStringify,
34
- onError,
35
- strictMessage,
36
- escapeHtml,
37
- useClassComponent
38
- };
39
- const generator = (0, codegen_1.createCodeGenerator)(options);
40
- const ast = (0, yaml_eslint_parser_1.parseYAML)(value, { filePath: filename });
41
- // for vue 2.x
42
- if (legacy && type === 'sfc') {
43
- const gen = () => (0, shared_1.friendlyJSONstringify)((0, yaml_eslint_parser_1.getStaticYAMLValue)(ast));
44
- const code = (0, legacy_1.generateLegacyCode)(options, gen);
45
- const s = new magic_string_1.default(code);
46
- return {
47
- ast,
48
- code: s.toString(),
49
- map: s.generateMap({
50
- file: filename,
51
- source: value,
52
- includeContent: true
53
- })
54
- };
55
- }
56
- const codeMaps = generateNode(generator, ast, options, injector);
57
- const { code, map } = generator.context();
58
- // prettier-ignore
59
- const newMap = map
60
- ? (0, codegen_1.mapLinesColumns)(map.toJSON(), codeMaps, inSourceMap) || null // eslint-disable-line @typescript-eslint/no-explicit-any
61
- : null;
62
- return {
63
- ast,
64
- code,
65
- map: newMap != null ? newMap : undefined
66
- };
67
- }
68
- exports.generate = generate;
69
- function generateNode(generator, node, options, injector) {
70
- const propsCountStack = [];
71
- const pathStack = [];
72
- const itemsCountStack = [];
73
- const { forceStringify } = generator.context();
74
- const codeMaps = new Map();
75
- const { type, bridge, exportESM, sourceMap, isGlobal, locale, useClassComponent } = options;
76
- const componentNamespace = '_Component';
77
- (0, yaml_eslint_parser_1.traverseNodes)(node, {
78
- enterNode(node, parent) {
79
- switch (node.type) {
80
- case 'Program':
81
- if (type === 'plain') {
82
- generator.push(`const resource = `);
83
- }
84
- else if (type === 'sfc') {
85
- const variableName = type === 'sfc' ? (!isGlobal ? '__i18n' : '__i18nGlobal') : '';
86
- const localeName = type === 'sfc' ? (locale != null ? locale : `""`) : '';
87
- const exportSyntax = bridge
88
- ? exportESM
89
- ? `export default`
90
- : `module.exports =`
91
- : `export default`;
92
- generator.push(`${exportSyntax} function (Component) {`);
93
- generator.indent();
94
- // prettier-ignore
95
- const componentVariable = bridge
96
- ? `Component.options || Component`
97
- : useClassComponent
98
- ? `Component.__o || Component`
99
- : `Component`;
100
- // prettier-ignore
101
- generator.pushline(`const ${componentNamespace} = ${componentVariable}`);
102
- generator.pushline(`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`);
103
- generator.push(`${componentNamespace}.${variableName}.push({`);
104
- generator.indent();
105
- generator.pushline(`"locale": ${JSON.stringify(localeName)},`);
106
- generator.push(`"resource": `);
107
- }
108
- break;
109
- case 'YAMLMapping':
110
- generator.push(`{`);
111
- generator.indent();
112
- propsCountStack.push(node.pairs.length);
113
- if (parent.type === 'YAMLSequence') {
114
- const lastIndex = itemsCountStack.length - 1;
115
- const currentCount = parent.entries.length - itemsCountStack[lastIndex];
116
- pathStack.push(currentCount.toString());
117
- itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
118
- }
119
- break;
120
- case 'YAMLPair':
121
- if (node.value &&
122
- node.value.type === 'YAMLScalar' &&
123
- node.key &&
124
- node.key.type === 'YAMLScalar') {
125
- const name = node.key.value;
126
- const value = node.value.value;
127
- if ((0, shared_1.isString)(value)) {
128
- generator.push(`${JSON.stringify(name)}: `);
129
- name && pathStack.push(name.toString());
130
- const { code, map } = (0, codegen_1.generateMessageFunction)(value, options, pathStack);
131
- sourceMap && map != null && codeMaps.set(value, map);
132
- generator.push(`${code}`, node.value, value);
133
- }
134
- else {
135
- if (forceStringify) {
136
- const strValue = JSON.stringify(value);
137
- generator.push(`${JSON.stringify(name)}: `);
138
- name && pathStack.push(name.toString());
139
- const { code, map } = (0, codegen_1.generateMessageFunction)(strValue, options, pathStack);
140
- sourceMap && map != null && codeMaps.set(strValue, map);
141
- generator.push(`${code}`, node.value, strValue);
142
- }
143
- else {
144
- generator.push(`${JSON.stringify(name)}: ${JSON.stringify(value)}`);
145
- name && pathStack.push(name.toString());
146
- }
147
- }
148
- }
149
- else if (node.value &&
150
- (node.value.type === 'YAMLMapping' ||
151
- node.value.type === 'YAMLSequence') &&
152
- node.key &&
153
- node.key.type === 'YAMLScalar') {
154
- const name = node.key.value;
155
- generator.push(`${JSON.stringify(name)}: `);
156
- name && pathStack.push(name.toString());
157
- }
158
- const lastIndex = propsCountStack.length - 1;
159
- propsCountStack[lastIndex] = --propsCountStack[lastIndex];
160
- break;
161
- case 'YAMLSequence':
162
- generator.push(`[`);
163
- generator.indent();
164
- if (parent.type === 'YAMLSequence') {
165
- const lastIndex = itemsCountStack.length - 1;
166
- const currentCount = parent.entries.length - itemsCountStack[lastIndex];
167
- pathStack.push(currentCount.toString());
168
- itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
169
- }
170
- itemsCountStack.push(node.entries.length);
171
- break;
172
- case 'YAMLScalar':
173
- if (parent.type === 'YAMLSequence') {
174
- const lastIndex = itemsCountStack.length - 1;
175
- const currentCount = parent.entries.length - itemsCountStack[lastIndex];
176
- pathStack.push(currentCount.toString());
177
- if (node.type === 'YAMLScalar') {
178
- const value = node.value;
179
- if ((0, shared_1.isString)(value)) {
180
- const { code, map } = (0, codegen_1.generateMessageFunction)(value, options, pathStack);
181
- sourceMap && map != null && codeMaps.set(value, map);
182
- generator.push(`${code}`, node, value);
183
- }
184
- else {
185
- if (forceStringify) {
186
- const strValue = JSON.stringify(value);
187
- const { code, map } = (0, codegen_1.generateMessageFunction)(strValue, options, pathStack);
188
- sourceMap && map != null && codeMaps.set(strValue, map);
189
- generator.push(`${code}`, node, strValue);
190
- }
191
- else {
192
- generator.push(`${JSON.stringify(value)}`);
193
- }
194
- }
195
- }
196
- itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
197
- }
198
- break;
199
- default:
200
- break;
201
- }
202
- },
203
- leaveNode(node, parent) {
204
- switch (node.type) {
205
- case 'Program':
206
- if (type === 'sfc') {
207
- generator.deindent();
208
- generator.push(`})`);
209
- if (bridge && injector) {
210
- generator.newline();
211
- generator.pushline(`${componentNamespace}.__i18nBridge = ${componentNamespace}.__i18nBridge || []`);
212
- generator.pushline(`${componentNamespace}.__i18nBridge.push('${injector()}')`);
213
- generator.pushline(`delete ${componentNamespace}._Ctor`);
214
- }
215
- generator.deindent();
216
- generator.push(`}`);
217
- }
218
- else if (type === 'plain') {
219
- generator.push(`\n`);
220
- generator.push('export default resource');
221
- }
222
- break;
223
- case 'YAMLMapping':
224
- if (propsCountStack[propsCountStack.length - 1] === 0) {
225
- pathStack.pop();
226
- propsCountStack.pop();
227
- }
228
- generator.deindent();
229
- generator.push(`}`);
230
- if (parent.type === 'YAMLSequence') {
231
- if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
232
- pathStack.pop();
233
- generator.pushline(`,`);
234
- }
235
- }
236
- break;
237
- case 'YAMLPair':
238
- if (propsCountStack[propsCountStack.length - 1] !== 0) {
239
- pathStack.pop();
240
- generator.pushline(`,`);
241
- }
242
- break;
243
- case 'YAMLSequence':
244
- if (itemsCountStack[itemsCountStack.length - 1] === 0) {
245
- pathStack.pop();
246
- itemsCountStack.pop();
247
- }
248
- generator.deindent();
249
- generator.push(`]`);
250
- if (parent.type === 'YAMLSequence') {
251
- if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
252
- pathStack.pop();
253
- generator.pushline(`,`);
254
- }
255
- }
256
- break;
257
- case 'YAMLScalar':
258
- if (parent.type === 'YAMLSequence') {
259
- if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
260
- pathStack.pop();
261
- generator.pushline(`,`);
262
- }
263
- else {
264
- generator.pushline(`,`);
265
- }
266
- }
267
- break;
268
- default:
269
- break;
270
- }
271
- }
272
- });
273
- return codeMaps;
274
- }