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

1039 строки
36 KiB

  1. import { parseJSON, traverseNodes } from 'jsonc-eslint-parser';
  2. import { isString, isNumber, isBoolean } from '@intlify/shared';
  3. import { baseCompile, LocationStub } from '@intlify/message-compiler';
  4. import { SourceMapGenerator, SourceMapConsumer } from 'source-map';
  5. import { parseYAML, traverseNodes as traverseNodes$1 } from 'yaml-eslint-parser';
  6. import { parse } from 'acorn';
  7. import { walk } from 'estree-walker';
  8. import esquery from 'esquery';
  9. // -- Unbuild CommonJS Shims --
  10. import __cjs_url__ from 'url';
  11. import __cjs_path__ from 'path';
  12. import __cjs_mod__ from 'module';
  13. const __filename = __cjs_url__.fileURLToPath(import.meta.url);
  14. const __dirname = __cjs_path__.dirname(__filename);
  15. const require = __cjs_mod__.createRequire(import.meta.url);
  16. function createCodeGenerator(options = {
  17. filename: "bundle.json",
  18. sourceMap: false,
  19. env: "development",
  20. forceStringify: false
  21. }) {
  22. const { sourceMap, source, filename } = options;
  23. const _context = Object.assign(
  24. {
  25. code: "",
  26. column: 1,
  27. line: 1,
  28. offset: 0,
  29. map: void 0,
  30. indentLevel: 0
  31. },
  32. options
  33. );
  34. const context = () => _context;
  35. function push(code, node, name) {
  36. _context.code += code;
  37. if (_context.map && node) {
  38. if (node.loc && node.loc !== LocationStub) {
  39. addMapping(node.loc.start, name);
  40. }
  41. advancePositionWithSource(_context, code);
  42. }
  43. }
  44. function _newline(n) {
  45. push("\n" + ` `.repeat(n));
  46. }
  47. function indent(withNewLine = true) {
  48. const level = ++_context.indentLevel;
  49. withNewLine && _newline(level);
  50. }
  51. function deindent(withNewLine = true) {
  52. const level = --_context.indentLevel;
  53. withNewLine && _newline(level);
  54. }
  55. function newline() {
  56. _newline(_context.indentLevel);
  57. }
  58. function pushline(code, node, name) {
  59. push(code, node, name);
  60. newline();
  61. }
  62. function addMapping(loc, name) {
  63. _context.map.addMapping({
  64. name,
  65. source: _context.filename,
  66. original: {
  67. line: loc.line,
  68. column: loc.column - 1
  69. },
  70. generated: {
  71. line: _context.line,
  72. column: _context.column - 1
  73. }
  74. });
  75. }
  76. if (sourceMap && source) {
  77. _context.map = new SourceMapGenerator();
  78. _context.map.setSourceContent(filename, source);
  79. }
  80. return {
  81. context,
  82. push,
  83. indent,
  84. deindent,
  85. newline,
  86. pushline
  87. };
  88. }
  89. function advancePositionWithSource(pos, source, numberOfCharacters = source.length) {
  90. if (pos.offset == null) {
  91. return pos;
  92. }
  93. let linesCount = 0;
  94. let lastNewLinePos = -1;
  95. for (let i = 0; i < numberOfCharacters; i++) {
  96. if (source.charCodeAt(i) === 10) {
  97. linesCount++;
  98. lastNewLinePos = i;
  99. }
  100. }
  101. pos.offset += numberOfCharacters;
  102. pos.line += linesCount;
  103. pos.column = lastNewLinePos === -1 ? pos.column + numberOfCharacters : numberOfCharacters - lastNewLinePos;
  104. return pos;
  105. }
  106. function generateMessageFunction(msg, options, path) {
  107. const env = options.env != null ? options.env : "development";
  108. const onError = options.onError;
  109. const errors = [];
  110. const newOptions = Object.assign({ mode: "arrow" }, options);
  111. newOptions.onError = (err) => {
  112. if (onError) {
  113. const extra = {
  114. source: msg,
  115. path: path ? path.join(".") : "",
  116. code: err.code,
  117. domain: err.domain,
  118. location: err.location
  119. };
  120. onError(err.message, extra);
  121. errors.push(err);
  122. }
  123. };
  124. const { code, ast, map } = baseCompile(msg, newOptions);
  125. const occured = errors.length > 0;
  126. const genCode = !occured ? env === "development" ? `(()=>{const fn=${code};fn.source=${JSON.stringify(msg)};return fn;})()` : `${code}` : msg;
  127. return { code: genCode, ast, map };
  128. }
  129. function mapLinesColumns(resMap, codeMaps, inSourceMap) {
  130. if (!resMap) {
  131. return null;
  132. }
  133. const resMapConsumer = new SourceMapConsumer(resMap);
  134. const inMapConsumer = inSourceMap ? new SourceMapConsumer(inSourceMap) : null;
  135. const mergedMapGenerator = new SourceMapGenerator();
  136. let inMapFirstItem = null;
  137. if (inMapConsumer) {
  138. inMapConsumer.eachMapping((m) => {
  139. if (inMapFirstItem) {
  140. return;
  141. }
  142. inMapFirstItem = m;
  143. });
  144. }
  145. resMapConsumer.eachMapping((res) => {
  146. if (res.originalLine == null) {
  147. return;
  148. }
  149. const map = codeMaps.get(res.name);
  150. if (!map) {
  151. return;
  152. }
  153. let inMapOrigin = null;
  154. if (inMapConsumer) {
  155. inMapOrigin = inMapConsumer.originalPositionFor({
  156. line: res.originalLine,
  157. column: res.originalColumn - 1
  158. });
  159. if (inMapOrigin.source == null) {
  160. inMapOrigin = null;
  161. return;
  162. }
  163. }
  164. const mapConsumer = new SourceMapConsumer(map);
  165. mapConsumer.eachMapping((m) => {
  166. mergedMapGenerator.addMapping({
  167. original: {
  168. line: inMapFirstItem ? inMapFirstItem.originalLine + res.originalLine - 2 : res.originalLine,
  169. column: inMapFirstItem ? inMapFirstItem.originalColumn + res.originalColumn : res.originalColumn
  170. },
  171. generated: {
  172. line: inMapFirstItem ? inMapFirstItem.generatedLine + res.originalLine - 2 : res.originalLine,
  173. // map column with message format compilation code map
  174. column: inMapFirstItem ? inMapFirstItem.generatedColumn + res.originalColumn + m.generatedColumn : res.originalColumn + m.generatedColumn
  175. },
  176. source: inMapOrigin ? inMapOrigin.source : res.source,
  177. name: m.name
  178. // message format compilation code
  179. });
  180. });
  181. });
  182. const generator = mergedMapGenerator;
  183. const targetConsumer = inMapConsumer || resMapConsumer;
  184. targetConsumer.sources.forEach((sourceFile) => {
  185. generator._sources.add(sourceFile);
  186. const sourceContent = targetConsumer.sourceContentFor(sourceFile);
  187. if (sourceContent != null) {
  188. mergedMapGenerator.setSourceContent(sourceFile, sourceContent);
  189. }
  190. });
  191. generator._sourceRoot = inSourceMap ? inSourceMap.sourceRoot : resMap.sourceRoot;
  192. generator._file = inSourceMap ? inSourceMap.file : resMap.file;
  193. return generator.toJSON();
  194. }
  195. function generate$2(targetSource, {
  196. type = "plain",
  197. bridge = false,
  198. exportESM = false,
  199. filename = "vue-i18n-loader.json",
  200. inSourceMap = void 0,
  201. locale = "",
  202. isGlobal = false,
  203. sourceMap = false,
  204. env = "development",
  205. forceStringify = false,
  206. onError = void 0,
  207. useClassComponent = false
  208. }, injector) {
  209. const target = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
  210. const value = target;
  211. const options = {
  212. type,
  213. bridge,
  214. exportESM,
  215. source: value,
  216. sourceMap,
  217. locale,
  218. isGlobal,
  219. inSourceMap,
  220. env,
  221. filename,
  222. forceStringify,
  223. onError,
  224. useClassComponent
  225. };
  226. const generator = createCodeGenerator(options);
  227. const ast = parseJSON(value, { filePath: filename });
  228. const codeMaps = generateNode$2(generator, ast, options, injector);
  229. const { code, map } = generator.context();
  230. const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
  231. return {
  232. ast,
  233. code,
  234. map: newMap != null ? newMap : void 0
  235. };
  236. }
  237. function generateNode$2(generator, node, options, injector) {
  238. const propsCountStack = [];
  239. const pathStack = [];
  240. const itemsCountStack = [];
  241. const { forceStringify } = generator.context();
  242. const codeMaps = /* @__PURE__ */ new Map();
  243. const {
  244. type,
  245. bridge,
  246. exportESM,
  247. sourceMap,
  248. isGlobal,
  249. locale,
  250. useClassComponent
  251. } = options;
  252. const componentNamespace = bridge ? `Component.options` : useClassComponent ? `Component.__o` : `Component`;
  253. traverseNodes(node, {
  254. enterNode(node2, parent) {
  255. switch (node2.type) {
  256. case "Program":
  257. if (type === "plain") {
  258. generator.push(`export default `);
  259. } else if (type === "sfc") {
  260. const variableName = type === "sfc" ? !isGlobal ? "__i18n" : "__i18nGlobal" : "";
  261. const localeName = type === "sfc" ? locale != null ? locale : `""` : "";
  262. const exportSyntax = bridge ? exportESM ? `export default` : `module.exports =` : `export default`;
  263. generator.push(`${exportSyntax} function (Component) {`);
  264. generator.indent();
  265. generator.pushline(
  266. `${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
  267. );
  268. generator.push(`${componentNamespace}.${variableName}.push({`);
  269. generator.indent();
  270. generator.pushline(`"locale": ${JSON.stringify(localeName)},`);
  271. generator.push(`"resource": `);
  272. }
  273. break;
  274. case "JSONObjectExpression":
  275. generator.push(`{`);
  276. generator.indent();
  277. propsCountStack.push(node2.properties.length);
  278. if (parent.type === "JSONArrayExpression") {
  279. const lastIndex2 = itemsCountStack.length - 1;
  280. const currentCount = parent.elements.length - itemsCountStack[lastIndex2];
  281. pathStack.push(currentCount.toString());
  282. itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
  283. }
  284. break;
  285. case "JSONProperty":
  286. if (node2.value.type === "JSONLiteral" && (node2.key.type === "JSONLiteral" || node2.key.type === "JSONIdentifier")) {
  287. const name = node2.key.type === "JSONLiteral" ? node2.key.value : node2.key.name;
  288. const value = node2.value.value;
  289. if (isString(value)) {
  290. generator.push(`${JSON.stringify(name)}: `);
  291. pathStack.push(name.toString());
  292. const { code, map } = generateMessageFunction(
  293. value,
  294. options,
  295. pathStack
  296. );
  297. sourceMap && map != null && codeMaps.set(value, map);
  298. generator.push(`${code}`, node2.value, value);
  299. } else {
  300. if (forceStringify) {
  301. const strValue = JSON.stringify(value);
  302. generator.push(`${JSON.stringify(name)}: `);
  303. pathStack.push(name.toString());
  304. const { code, map } = generateMessageFunction(
  305. strValue,
  306. options,
  307. pathStack
  308. );
  309. sourceMap && map != null && codeMaps.set(strValue, map);
  310. generator.push(`${code}`, node2.value, strValue);
  311. } else {
  312. generator.push(
  313. `${JSON.stringify(name)}: ${JSON.stringify(value)}`
  314. );
  315. pathStack.push(name.toString());
  316. }
  317. }
  318. } else if ((node2.value.type === "JSONObjectExpression" || node2.value.type === "JSONArrayExpression") && (node2.key.type === "JSONLiteral" || node2.key.type === "JSONIdentifier")) {
  319. const name = node2.key.type === "JSONLiteral" ? node2.key.value : node2.key.name;
  320. generator.push(`${JSON.stringify(name)}: `);
  321. pathStack.push(name.toString());
  322. }
  323. const lastIndex = propsCountStack.length - 1;
  324. propsCountStack[lastIndex] = --propsCountStack[lastIndex];
  325. break;
  326. case "JSONArrayExpression":
  327. generator.push(`[`);
  328. generator.indent();
  329. if (parent.type === "JSONArrayExpression") {
  330. const lastIndex2 = itemsCountStack.length - 1;
  331. const currentCount = parent.elements.length - itemsCountStack[lastIndex2];
  332. pathStack.push(currentCount.toString());
  333. itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
  334. }
  335. itemsCountStack.push(node2.elements.length);
  336. break;
  337. case "JSONLiteral":
  338. if (parent.type === "JSONArrayExpression") {
  339. const lastIndex2 = itemsCountStack.length - 1;
  340. const currentCount = parent.elements.length - itemsCountStack[lastIndex2];
  341. pathStack.push(currentCount.toString());
  342. if (node2.type === "JSONLiteral") {
  343. const value = node2.value;
  344. if (isString(value)) {
  345. const { code, map } = generateMessageFunction(
  346. value,
  347. options,
  348. pathStack
  349. );
  350. sourceMap && map != null && codeMaps.set(value, map);
  351. generator.push(`${code}`, node2, value);
  352. } else {
  353. if (forceStringify) {
  354. const strValue = JSON.stringify(value);
  355. const { code, map } = generateMessageFunction(
  356. strValue,
  357. options,
  358. pathStack
  359. );
  360. sourceMap && map != null && codeMaps.set(strValue, map);
  361. generator.push(`${code}`, node2, strValue);
  362. } else {
  363. generator.push(`${JSON.stringify(value)}`);
  364. }
  365. }
  366. }
  367. itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
  368. }
  369. break;
  370. }
  371. },
  372. leaveNode(node2, parent) {
  373. switch (node2.type) {
  374. case "Program":
  375. if (type === "sfc") {
  376. generator.deindent();
  377. generator.push(`})`);
  378. if (bridge && injector) {
  379. generator.newline();
  380. generator.pushline(
  381. `${componentNamespace}.__i18nBridge = ${componentNamespace}.__i18nBridge || []`
  382. );
  383. generator.pushline(
  384. `${componentNamespace}.__i18nBridge.push('${injector()}')`
  385. );
  386. generator.pushline(`delete ${componentNamespace}._Ctor`);
  387. }
  388. generator.deindent();
  389. generator.pushline(`}`);
  390. }
  391. break;
  392. case "JSONObjectExpression":
  393. if (propsCountStack[propsCountStack.length - 1] === 0) {
  394. pathStack.pop();
  395. propsCountStack.pop();
  396. }
  397. generator.deindent();
  398. generator.push(`}`);
  399. if (parent.type === "JSONArrayExpression") {
  400. if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
  401. pathStack.pop();
  402. generator.pushline(`,`);
  403. }
  404. }
  405. break;
  406. case "JSONProperty":
  407. if (propsCountStack[propsCountStack.length - 1] !== 0) {
  408. pathStack.pop();
  409. generator.pushline(`,`);
  410. }
  411. break;
  412. case "JSONArrayExpression":
  413. if (itemsCountStack[itemsCountStack.length - 1] === 0) {
  414. pathStack.pop();
  415. itemsCountStack.pop();
  416. }
  417. generator.deindent();
  418. generator.push(`]`);
  419. if (parent.type === "JSONArrayExpression") {
  420. if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
  421. pathStack.pop();
  422. generator.pushline(`,`);
  423. }
  424. }
  425. break;
  426. case "JSONLiteral":
  427. if (parent.type === "JSONArrayExpression") {
  428. if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
  429. pathStack.pop();
  430. generator.pushline(`,`);
  431. } else {
  432. generator.pushline(`,`);
  433. }
  434. }
  435. break;
  436. }
  437. }
  438. });
  439. return codeMaps;
  440. }
  441. function generate$1(targetSource, {
  442. type = "plain",
  443. bridge = false,
  444. exportESM = false,
  445. useClassComponent = false,
  446. filename = "vue-i18n-loader.yaml",
  447. inSourceMap = void 0,
  448. locale = "",
  449. isGlobal = false,
  450. sourceMap = false,
  451. env = "development",
  452. forceStringify = false,
  453. onError = void 0
  454. }, injector) {
  455. const value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
  456. const options = {
  457. type,
  458. bridge,
  459. exportESM,
  460. source: value,
  461. sourceMap,
  462. locale,
  463. isGlobal,
  464. inSourceMap,
  465. env,
  466. filename,
  467. forceStringify,
  468. onError,
  469. useClassComponent
  470. };
  471. const generator = createCodeGenerator(options);
  472. const ast = parseYAML(value, { filePath: filename });
  473. const codeMaps = generateNode$1(generator, ast, options, injector);
  474. const { code, map } = generator.context();
  475. const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
  476. return {
  477. ast,
  478. code,
  479. map: newMap != null ? newMap : void 0
  480. };
  481. }
  482. function generateNode$1(generator, node, options, injector) {
  483. const propsCountStack = [];
  484. const pathStack = [];
  485. const itemsCountStack = [];
  486. const { forceStringify } = generator.context();
  487. const codeMaps = /* @__PURE__ */ new Map();
  488. const {
  489. type,
  490. bridge,
  491. exportESM,
  492. sourceMap,
  493. isGlobal,
  494. locale,
  495. useClassComponent
  496. } = options;
  497. const componentNamespace = bridge ? `Component.options` : useClassComponent ? `Component.__o` : `Component`;
  498. traverseNodes$1(node, {
  499. enterNode(node2, parent) {
  500. switch (node2.type) {
  501. case "Program":
  502. if (type === "plain") {
  503. generator.push(`export default `);
  504. } else if (type === "sfc") {
  505. const variableName = type === "sfc" ? !isGlobal ? "__i18n" : "__i18nGlobal" : "";
  506. const localeName = type === "sfc" ? locale != null ? locale : `""` : "";
  507. const exportSyntax = bridge ? exportESM ? `export default` : `module.exports =` : `export default`;
  508. generator.push(`${exportSyntax} function (Component) {`);
  509. generator.indent();
  510. generator.pushline(
  511. `${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
  512. );
  513. generator.push(`${componentNamespace}.${variableName}.push({`);
  514. generator.indent();
  515. generator.pushline(`"locale": ${JSON.stringify(localeName)},`);
  516. generator.push(`"resource": `);
  517. }
  518. break;
  519. case "YAMLMapping":
  520. generator.push(`{`);
  521. generator.indent();
  522. propsCountStack.push(node2.pairs.length);
  523. if (parent.type === "YAMLSequence") {
  524. const lastIndex2 = itemsCountStack.length - 1;
  525. const currentCount = parent.entries.length - itemsCountStack[lastIndex2];
  526. pathStack.push(currentCount.toString());
  527. itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
  528. }
  529. break;
  530. case "YAMLPair":
  531. if (node2.value && node2.value.type === "YAMLScalar" && node2.key && node2.key.type === "YAMLScalar") {
  532. const name = node2.key.value;
  533. const value = node2.value.value;
  534. if (isString(value)) {
  535. generator.push(`${JSON.stringify(name)}: `);
  536. name && pathStack.push(name.toString());
  537. const { code, map } = generateMessageFunction(
  538. value,
  539. options,
  540. pathStack
  541. );
  542. sourceMap && map != null && codeMaps.set(value, map);
  543. generator.push(`${code}`, node2.value, value);
  544. } else {
  545. if (forceStringify) {
  546. const strValue = JSON.stringify(value);
  547. generator.push(`${JSON.stringify(name)}: `);
  548. name && pathStack.push(name.toString());
  549. const { code, map } = generateMessageFunction(
  550. strValue,
  551. options,
  552. pathStack
  553. );
  554. sourceMap && map != null && codeMaps.set(strValue, map);
  555. generator.push(`${code}`, node2.value, strValue);
  556. } else {
  557. generator.push(
  558. `${JSON.stringify(name)}: ${JSON.stringify(value)}`
  559. );
  560. name && pathStack.push(name.toString());
  561. }
  562. }
  563. } else if (node2.value && (node2.value.type === "YAMLMapping" || node2.value.type === "YAMLSequence") && node2.key && node2.key.type === "YAMLScalar") {
  564. const name = node2.key.value;
  565. generator.push(`${JSON.stringify(name)}: `);
  566. name && pathStack.push(name.toString());
  567. }
  568. const lastIndex = propsCountStack.length - 1;
  569. propsCountStack[lastIndex] = --propsCountStack[lastIndex];
  570. break;
  571. case "YAMLSequence":
  572. generator.push(`[`);
  573. generator.indent();
  574. if (parent.type === "YAMLSequence") {
  575. const lastIndex2 = itemsCountStack.length - 1;
  576. const currentCount = parent.entries.length - itemsCountStack[lastIndex2];
  577. pathStack.push(currentCount.toString());
  578. itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
  579. }
  580. itemsCountStack.push(node2.entries.length);
  581. break;
  582. case "YAMLScalar":
  583. if (parent.type === "YAMLSequence") {
  584. const lastIndex2 = itemsCountStack.length - 1;
  585. const currentCount = parent.entries.length - itemsCountStack[lastIndex2];
  586. pathStack.push(currentCount.toString());
  587. if (node2.type === "YAMLScalar") {
  588. const value = node2.value;
  589. if (isString(value)) {
  590. const { code, map } = generateMessageFunction(
  591. value,
  592. options,
  593. pathStack
  594. );
  595. sourceMap && map != null && codeMaps.set(value, map);
  596. generator.push(`${code}`, node2, value);
  597. } else {
  598. if (forceStringify) {
  599. const strValue = JSON.stringify(value);
  600. const { code, map } = generateMessageFunction(
  601. strValue,
  602. options,
  603. pathStack
  604. );
  605. sourceMap && map != null && codeMaps.set(strValue, map);
  606. generator.push(`${code}`, node2, strValue);
  607. } else {
  608. generator.push(`${JSON.stringify(value)}`);
  609. }
  610. }
  611. }
  612. itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
  613. }
  614. break;
  615. }
  616. },
  617. leaveNode(node2, parent) {
  618. switch (node2.type) {
  619. case "Program":
  620. if (type === "sfc") {
  621. generator.deindent();
  622. generator.push(`})`);
  623. if (bridge && injector) {
  624. generator.newline();
  625. generator.pushline(
  626. `${componentNamespace}.__i18nBridge = ${componentNamespace}.__i18nBridge || []`
  627. );
  628. generator.pushline(
  629. `${componentNamespace}.__i18nBridge.push('${injector()}')`
  630. );
  631. generator.pushline(`delete ${componentNamespace}._Ctor`);
  632. }
  633. generator.deindent();
  634. generator.push(`}`);
  635. }
  636. break;
  637. case "YAMLMapping":
  638. if (propsCountStack[propsCountStack.length - 1] === 0) {
  639. pathStack.pop();
  640. propsCountStack.pop();
  641. }
  642. generator.deindent();
  643. generator.push(`}`);
  644. if (parent.type === "YAMLSequence") {
  645. if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
  646. pathStack.pop();
  647. generator.pushline(`,`);
  648. }
  649. }
  650. break;
  651. case "YAMLPair":
  652. if (propsCountStack[propsCountStack.length - 1] !== 0) {
  653. pathStack.pop();
  654. generator.pushline(`,`);
  655. }
  656. break;
  657. case "YAMLSequence":
  658. if (itemsCountStack[itemsCountStack.length - 1] === 0) {
  659. pathStack.pop();
  660. itemsCountStack.pop();
  661. }
  662. generator.deindent();
  663. generator.push(`]`);
  664. if (parent.type === "YAMLSequence") {
  665. if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
  666. pathStack.pop();
  667. generator.pushline(`,`);
  668. }
  669. }
  670. break;
  671. case "YAMLScalar":
  672. if (parent.type === "YAMLSequence") {
  673. if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
  674. pathStack.pop();
  675. generator.pushline(`,`);
  676. } else {
  677. generator.pushline(`,`);
  678. }
  679. }
  680. break;
  681. }
  682. }
  683. });
  684. return codeMaps;
  685. }
  686. function generate(targetSource, {
  687. type = "plain",
  688. bridge = false,
  689. exportESM = false,
  690. filename = "vue-i18n-loader.js",
  691. inSourceMap = void 0,
  692. locale = "",
  693. isGlobal = false,
  694. sourceMap = false,
  695. env = "development",
  696. forceStringify = false,
  697. onError = void 0,
  698. useClassComponent = false
  699. }, injector) {
  700. const target = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
  701. const value = target;
  702. const options = {
  703. type,
  704. bridge,
  705. exportESM,
  706. source: value,
  707. sourceMap,
  708. locale,
  709. isGlobal,
  710. inSourceMap,
  711. env,
  712. filename,
  713. forceStringify,
  714. onError,
  715. useClassComponent
  716. };
  717. const generator = createCodeGenerator(options);
  718. const ast = parse(value, {
  719. ecmaVersion: "latest",
  720. sourceType: "module",
  721. sourceFile: filename,
  722. allowImportExportEverywhere: true
  723. });
  724. const selectedWithExportDefault = esquery(
  725. ast,
  726. "Program:has(ExportDefaultDeclaration, [declaration=ObjectExpression])"
  727. );
  728. if (!selectedWithExportDefault.length) {
  729. throw new Error(
  730. `You need to define an object as the locale message with "export default'.`
  731. );
  732. }
  733. const codeMaps = generateNode(generator, ast, options, injector);
  734. const { code, map } = generator.context();
  735. const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
  736. return {
  737. ast,
  738. code,
  739. map: newMap != null ? newMap : void 0
  740. };
  741. }
  742. function generateNode(generator, node, options, injector) {
  743. const propsCountStack = [];
  744. const pathStack = [];
  745. const itemsCountStack = [];
  746. const skipStack = [];
  747. const { forceStringify } = generator.context();
  748. const codeMaps = /* @__PURE__ */ new Map();
  749. const {
  750. type,
  751. bridge,
  752. exportESM,
  753. sourceMap,
  754. isGlobal,
  755. locale,
  756. useClassComponent
  757. } = options;
  758. const componentNamespace = bridge ? `Component.options` : useClassComponent ? `Component.__o` : `Component`;
  759. walk(node, {
  760. enter(node2, parent) {
  761. switch (node2.type) {
  762. case "Program":
  763. if (type === "plain") {
  764. generator.push(`export default `);
  765. } else if (type === "sfc") {
  766. const variableName = type === "sfc" ? !isGlobal ? "__i18n" : "__i18nGlobal" : "";
  767. const localeName = type === "sfc" ? locale != null ? locale : `""` : "";
  768. const exportSyntax = bridge ? exportESM ? `export default` : `module.exports =` : `export default`;
  769. generator.push(`${exportSyntax} function (Component) {`);
  770. generator.indent();
  771. generator.pushline(
  772. `${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
  773. );
  774. generator.push(`${componentNamespace}.${variableName}.push({`);
  775. generator.indent();
  776. generator.pushline(`"locale": ${JSON.stringify(localeName)},`);
  777. generator.push(`"resource": `);
  778. }
  779. break;
  780. case "ObjectExpression":
  781. generator.push(`{`);
  782. generator.indent();
  783. propsCountStack.push(node2.properties.length);
  784. if (parent != null && parent.type === "ArrayExpression") {
  785. const lastIndex2 = itemsCountStack.length - 1;
  786. const currentCount = parent.elements.length - itemsCountStack[lastIndex2];
  787. pathStack.push(currentCount.toString());
  788. itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
  789. }
  790. break;
  791. case "Property":
  792. if (node2 != null) {
  793. if (isJSONablePrimitiveLiteral(node2.value) && (node2.key.type === "Literal" || node2.key.type === "Identifier")) {
  794. const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
  795. if (node2.value.type === "Literal" && isString(node2.value.value) || node2.value.type === "TemplateLiteral") {
  796. const value = getValue(node2.value);
  797. generator.push(`${JSON.stringify(name)}: `);
  798. pathStack.push(name);
  799. const { code, map } = generateMessageFunction(
  800. value,
  801. options,
  802. pathStack
  803. );
  804. sourceMap && map != null && codeMaps.set(value, map);
  805. generator.push(`${code}`, node2.value, value);
  806. skipStack.push(false);
  807. } else {
  808. const value = getValue(node2.value);
  809. if (forceStringify) {
  810. const strValue = JSON.stringify(value);
  811. generator.push(`${JSON.stringify(name)}: `);
  812. pathStack.push(name);
  813. const { code, map } = generateMessageFunction(
  814. strValue,
  815. options,
  816. pathStack
  817. );
  818. sourceMap && map != null && codeMaps.set(strValue, map);
  819. generator.push(`${code}`, node2.value, strValue);
  820. } else {
  821. generator.push(
  822. `${JSON.stringify(name)}: ${JSON.stringify(value)}`
  823. );
  824. pathStack.push(name);
  825. }
  826. skipStack.push(false);
  827. }
  828. } else if ((node2.value.type === "ObjectExpression" || node2.value.type === "ArrayExpression") && (node2.key.type === "Literal" || node2.key.type === "Identifier")) {
  829. const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
  830. generator.push(`${JSON.stringify(name)}: `);
  831. pathStack.push(name);
  832. } else {
  833. skipStack.push(true);
  834. }
  835. }
  836. const lastIndex = propsCountStack.length - 1;
  837. propsCountStack[lastIndex] = --propsCountStack[lastIndex];
  838. break;
  839. case "ArrayExpression":
  840. generator.push(`[`);
  841. generator.indent();
  842. if (parent != null && parent.type === "ArrayExpression") {
  843. const lastIndex2 = itemsCountStack.length - 1;
  844. const currentCount = parent.elements.length - itemsCountStack[lastIndex2];
  845. pathStack.push(currentCount.toString());
  846. itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
  847. }
  848. itemsCountStack.push(node2.elements.length);
  849. break;
  850. default:
  851. if (node2 != null && parent != null) {
  852. if (parent.type === "ArrayExpression") {
  853. const lastIndex2 = itemsCountStack.length - 1;
  854. const currentCount = parent.elements.length - itemsCountStack[lastIndex2];
  855. pathStack.push(currentCount.toString());
  856. if (isJSONablePrimitiveLiteral(node2)) {
  857. if (node2.type === "Literal" && isString(node2.value) || node2.type === "TemplateLiteral") {
  858. const value = getValue(node2);
  859. const { code, map } = generateMessageFunction(
  860. value,
  861. options,
  862. pathStack
  863. );
  864. sourceMap && map != null && codeMaps.set(value, map);
  865. generator.push(`${code}`, node2, value);
  866. } else {
  867. const value = getValue(node2);
  868. if (forceStringify) {
  869. const strValue = JSON.stringify(value);
  870. const { code, map } = generateMessageFunction(
  871. strValue,
  872. options,
  873. pathStack
  874. );
  875. sourceMap && map != null && codeMaps.set(strValue, map);
  876. generator.push(`${code}`, node2, strValue);
  877. } else {
  878. generator.push(`${JSON.stringify(value)}`);
  879. }
  880. }
  881. skipStack.push(false);
  882. } else {
  883. skipStack.push(true);
  884. }
  885. itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
  886. }
  887. }
  888. break;
  889. }
  890. },
  891. leave(node2, parent) {
  892. switch (node2.type) {
  893. case "Program":
  894. if (type === "sfc") {
  895. generator.deindent();
  896. generator.push(`})`);
  897. if (bridge && injector) {
  898. generator.newline();
  899. generator.pushline(
  900. `${componentNamespace}.__i18nBridge = ${componentNamespace}.__i18nBridge || []`
  901. );
  902. generator.pushline(
  903. `${componentNamespace}.__i18nBridge.push('${injector()}')`
  904. );
  905. generator.pushline(`delete ${componentNamespace}._Ctor`);
  906. }
  907. generator.deindent();
  908. generator.pushline(`}`);
  909. }
  910. break;
  911. case "ObjectExpression":
  912. if (propsCountStack[propsCountStack.length - 1] === 0) {
  913. pathStack.pop();
  914. propsCountStack.pop();
  915. }
  916. generator.deindent();
  917. generator.push(`}`);
  918. if (parent != null && parent.type === "ArrayExpression") {
  919. if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
  920. pathStack.pop();
  921. generator.pushline(`,`);
  922. }
  923. }
  924. break;
  925. case "Property":
  926. if (propsCountStack[propsCountStack.length - 1] !== 0) {
  927. pathStack.pop();
  928. if (!skipStack.pop()) {
  929. generator.pushline(`,`);
  930. }
  931. }
  932. break;
  933. case "ArrayExpression":
  934. if (itemsCountStack[itemsCountStack.length - 1] === 0) {
  935. pathStack.pop();
  936. itemsCountStack.pop();
  937. }
  938. generator.deindent();
  939. generator.push(`]`);
  940. if (parent != null && parent.type === "ArrayExpression") {
  941. if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
  942. pathStack.pop();
  943. if (!skipStack.pop()) {
  944. generator.pushline(`,`);
  945. }
  946. }
  947. }
  948. break;
  949. case "Literal":
  950. if (parent != null && parent.type === "ArrayExpression") {
  951. if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
  952. pathStack.pop();
  953. if (!skipStack.pop()) {
  954. generator.pushline(`,`);
  955. }
  956. } else {
  957. if (!skipStack.pop()) {
  958. generator.pushline(`,`);
  959. }
  960. }
  961. }
  962. break;
  963. }
  964. }
  965. });
  966. return codeMaps;
  967. }
  968. function isJSONablePrimitiveLiteral(node) {
  969. return node.type === "Literal" && (isString(node.value) || isNumber(node.value) || isBoolean(node.value) || node.value === null) || node.type === "TemplateLiteral";
  970. }
  971. function getValue(node) {
  972. return node.type === "Literal" ? node.value : node.type === "TemplateLiteral" ? node.quasis.map((quasi) => quasi.value.cooked).join("") : void 0;
  973. }
  974. function checkInstallPackage(pkg, debug) {
  975. let installedVueI18n = false;
  976. try {
  977. debug(`vue-i18n load path: ${require.resolve("vue-i18n")}`);
  978. installedVueI18n = true;
  979. } catch (e) {
  980. debug(`cannot find 'vue-i18n'`, e);
  981. }
  982. let installedPetiteVueI18n = false;
  983. try {
  984. debug(`petite-vue-i18n load path: ${require.resolve("petite-vue-i18n")}`);
  985. installedPetiteVueI18n = true;
  986. } catch (e) {
  987. debug(`cannot find 'petite-vue-i18n'`, e);
  988. }
  989. if (installedVueI18n) {
  990. return "vue-i18n";
  991. }
  992. if (installedPetiteVueI18n) {
  993. return "petite-vue-i18n";
  994. }
  995. throw new Error(
  996. `${pkg} requires 'vue-i18n' or 'petite-vue-i18n' to be present in the dependency tree.`
  997. );
  998. }
  999. function checkVueI18nBridgeInstallPackage(debug) {
  1000. let ret = false;
  1001. try {
  1002. debug(`vue-i18n-bridge load path: ${require.resolve("vue-i18n-bridge")}`);
  1003. ret = true;
  1004. } catch (e) {
  1005. debug(`cannot find 'vue-i18n-bridge'`, e);
  1006. }
  1007. return ret;
  1008. }
  1009. function getVueI18nVersion(debug) {
  1010. const VueI18n = loadModule("vue-i18n", debug);
  1011. if (VueI18n == null) {
  1012. return "";
  1013. }
  1014. if (VueI18n.version && VueI18n.version.startsWith("8.")) {
  1015. return "8";
  1016. }
  1017. if (VueI18n.VERSION && VueI18n.VERSION.startsWith("9.")) {
  1018. return "9";
  1019. }
  1020. return "unknown";
  1021. }
  1022. function loadModule(moduleName, debug) {
  1023. try {
  1024. return require(moduleName);
  1025. } catch (e) {
  1026. debug(`cannot load '${moduleName}'`, e);
  1027. return null;
  1028. }
  1029. }
  1030. export { checkInstallPackage, checkVueI18nBridgeInstallPackage, generate$2 as generateJSON, generate as generateJavaScript, generate$1 as generateYAML, getVueI18nVersion };