版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1 год назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. }