index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.compile = exports.getBaseTransformPreset = void 0;
  4. const shared_1 = require("@vue/shared");
  5. const compiler_core_1 = require("@vue/compiler-core");
  6. const uni_shared_1 = require("@dcloudio/uni-shared");
  7. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  8. require("./runtimeHelpers");
  9. const codegen_1 = require("./codegen");
  10. const transform_1 = require("./transform");
  11. const vIf_1 = require("./transforms/vIf");
  12. const vFor_1 = require("./transforms/vFor");
  13. const vModel_1 = require("./transforms/vModel");
  14. const vShow_1 = require("./transforms/vShow");
  15. const vText_1 = require("./transforms/vText");
  16. const transformInterpolation_1 = require("./transforms/transformInterpolation");
  17. const transformText_1 = require("./transforms/transformText");
  18. const vOnWithModifier_1 = require("./transforms/vOnWithModifier");
  19. const vBind_1 = require("./transforms/vBind");
  20. const transformSlotOutlet_1 = require("./transforms/transformSlotOutlet");
  21. const transformObjectExpression_1 = require("./transforms/transformObjectExpression");
  22. const transformExpression_1 = require("./transforms/transformExpression");
  23. const transformElements_1 = require("./transforms/transformElements");
  24. const transformStyle_1 = require("./transforms/transformStyle");
  25. const vHtml_1 = require("./transforms/vHtml");
  26. const vMemo_1 = require("./transforms/vMemo");
  27. const vOnce_1 = require("./transforms/vOnce");
  28. const source_map_js_1 = require("source-map-js");
  29. function getBaseTransformPreset(prefixIdentifiers) {
  30. return [
  31. [
  32. vOnce_1.transformOnce,
  33. vIf_1.transformIf,
  34. vMemo_1.transformMemo,
  35. vFor_1.transformFor,
  36. // order is important
  37. compiler_core_1.trackVForSlotScopes,
  38. vHtml_1.transformVHtml,
  39. transformExpression_1.transformExpression,
  40. transformSlotOutlet_1.transformSlotOutlet,
  41. compiler_core_1.transformElement,
  42. compiler_core_1.trackSlotScopes,
  43. transformText_1.transformText,
  44. uni_cli_shared_1.transformTapToClick,
  45. transformInterpolation_1.transformInterpolation,
  46. transformObjectExpression_1.transformObjectExpression,
  47. transformElements_1.transformElements,
  48. transformStyle_1.transformStyle,
  49. ],
  50. {
  51. on: vOnWithModifier_1.transformOn,
  52. bind: vBind_1.transformBind,
  53. model: vModel_1.transformModel,
  54. show: vShow_1.transformShow,
  55. text: vText_1.transformVText,
  56. },
  57. ];
  58. }
  59. exports.getBaseTransformPreset = getBaseTransformPreset;
  60. function compile(template, options) {
  61. options.rootDir = options.rootDir || '';
  62. options.targetLanguage = options.targetLanguage || 'kotlin';
  63. options.prefixIdentifiers =
  64. 'prefixIdentifiers' in options
  65. ? options.prefixIdentifiers
  66. : options.mode === 'module';
  67. wrapOptionsLog(template, options);
  68. const isNativeTag = options?.isNativeTag ||
  69. function (tag) {
  70. return ((0, uni_shared_1.isAppUVueNativeTag)(tag) ||
  71. !!options.parseUTSComponent?.(tag, options.targetLanguage));
  72. };
  73. const ast = (0, compiler_core_1.baseParse)(template, {
  74. comments: false,
  75. isNativeTag,
  76. onError: options.onError,
  77. });
  78. const [nodeTransforms, directiveTransforms] = getBaseTransformPreset(options.prefixIdentifiers);
  79. (0, transform_1.transform)(ast, (0, shared_1.extend)({}, options, {
  80. nodeTransforms: [
  81. ...nodeTransforms,
  82. ...(0, uni_cli_shared_1.getBaseNodeTransforms)('/'),
  83. ...(options.nodeTransforms || []), // user transforms
  84. ],
  85. directiveTransforms: (0, shared_1.extend)({}, directiveTransforms, options.directiveTransforms || {} // user transforms
  86. ),
  87. }));
  88. const result = (0, codegen_1.generate)(ast, options);
  89. // inMap should be the map produced by ./parse.ts which is a simple line-only
  90. // mapping. If it is present, we need to adjust the final map and errors to
  91. // reflect the original line numbers.
  92. if (options.inMap) {
  93. if (options.sourceMap) {
  94. result.map = mapLines(options.inMap, result.map);
  95. }
  96. // if (result.errors.length) {
  97. // patchErrors(errors, source, inMap)
  98. // }
  99. }
  100. return result;
  101. }
  102. exports.compile = compile;
  103. function mapLines(oldMap, newMap) {
  104. if (!oldMap)
  105. return newMap;
  106. if (!newMap)
  107. return oldMap;
  108. const oldMapConsumer = new source_map_js_1.SourceMapConsumer(oldMap);
  109. const newMapConsumer = new source_map_js_1.SourceMapConsumer(newMap);
  110. const mergedMapGenerator = new source_map_js_1.SourceMapGenerator();
  111. newMapConsumer.eachMapping((m) => {
  112. if (m.originalLine == null) {
  113. return;
  114. }
  115. const origPosInOldMap = oldMapConsumer.originalPositionFor({
  116. line: m.originalLine,
  117. column: m.originalColumn,
  118. });
  119. if (origPosInOldMap.source == null) {
  120. return;
  121. }
  122. mergedMapGenerator.addMapping({
  123. generated: {
  124. line: m.generatedLine,
  125. column: m.generatedColumn,
  126. },
  127. original: {
  128. line: origPosInOldMap.line,
  129. // use current column, since the oldMap produced by @vue/compiler-sfc
  130. // does not
  131. column: m.originalColumn,
  132. },
  133. source: origPosInOldMap.source,
  134. name: origPosInOldMap.name,
  135. });
  136. });
  137. // source-map's type definition is incomplete
  138. const generator = mergedMapGenerator;
  139. oldMapConsumer.sources.forEach((sourceFile) => {
  140. generator._sources.add(sourceFile);
  141. const sourceContent = oldMapConsumer.sourceContentFor(sourceFile);
  142. if (sourceContent != null) {
  143. mergedMapGenerator.setSourceContent(sourceFile, sourceContent);
  144. }
  145. });
  146. generator._sourceRoot = oldMap.sourceRoot;
  147. generator._file = oldMap.file;
  148. return generator.toJSON();
  149. }
  150. function wrapOptionsLog(source, options) {
  151. const { onWarn, onError, inMap } = options;
  152. if (inMap && inMap.sourcesContent?.length) {
  153. if (onWarn || onError) {
  154. const originalSource = inMap.sourcesContent[0];
  155. const offset = originalSource.indexOf(source);
  156. const lineOffset = originalSource.slice(0, offset).split(/\r?\n/).length - 1;
  157. if (onWarn) {
  158. options.onWarn = (err) => {
  159. patchError(err, lineOffset, offset);
  160. onWarn(err);
  161. };
  162. }
  163. if (onError) {
  164. options.onError = (err) => {
  165. patchError(err, lineOffset, offset);
  166. onError(err);
  167. };
  168. }
  169. }
  170. }
  171. }
  172. function patchError(err, lineOffset, offset) {
  173. if (err.loc) {
  174. err.loc.start.line += lineOffset;
  175. err.loc.start.offset += offset;
  176. if (err.loc.end !== err.loc.start) {
  177. err.loc.end.line += lineOffset;
  178. err.loc.end.offset += offset;
  179. }
  180. }
  181. }