easycom.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.uniEasycomPlugin = void 0;
  7. const path_1 = __importDefault(require("path"));
  8. const pluginutils_1 = require("@rollup/pluginutils");
  9. const shared_1 = require("@vue/shared");
  10. const uni_shared_1 = require("@dcloudio/uni-shared");
  11. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  12. const H5_COMPONENTS_PATH = '@dcloudio/uni-h5';
  13. const xBaseComponents = ['slider', 'switch'];
  14. const baseComponents = [
  15. 'audio',
  16. 'button',
  17. 'canvas',
  18. 'checkbox',
  19. 'checkbox-group',
  20. 'editor',
  21. 'form',
  22. 'icon',
  23. 'image',
  24. 'input',
  25. 'label',
  26. 'movable-area',
  27. 'movable-view',
  28. 'navigator',
  29. 'picker-view',
  30. 'picker-view-column',
  31. 'progress',
  32. 'radio',
  33. 'radio-group',
  34. 'resize-sensor',
  35. 'rich-text',
  36. 'scroll-view',
  37. 'slider',
  38. 'swiper',
  39. 'swiper-item',
  40. 'switch',
  41. 'text',
  42. 'textarea',
  43. 'view',
  44. 'list-view',
  45. 'list-item',
  46. 'sticky-section',
  47. 'sticky-header',
  48. ];
  49. function uniEasycomPlugin(options) {
  50. const filter = (0, pluginutils_1.createFilter)(options.include, options.exclude);
  51. let needCombineBuiltInCss = false;
  52. return {
  53. name: 'uni:h5-easycom',
  54. configResolved(config) {
  55. needCombineBuiltInCss = (0, uni_cli_shared_1.isCombineBuiltInCss)(config);
  56. },
  57. transform(code, id) {
  58. if (!filter(id)) {
  59. return;
  60. }
  61. const { filename } = (0, uni_cli_shared_1.parseVueRequest)(id);
  62. if (!uni_cli_shared_1.EXTNAME_VUE_TEMPLATE.includes(path_1.default.extname(filename))) {
  63. return;
  64. }
  65. if (!code.includes('_resolveComponent')) {
  66. return;
  67. }
  68. let i = 0;
  69. const importDeclarations = [];
  70. code = code.replace(/_resolveComponent\("(.+?)"(, true)?\)/g, (str, name) => {
  71. if (name && !name.startsWith('_')) {
  72. if ((0, uni_shared_1.isBuiltInComponent)(name)) {
  73. name = name.replace(uni_shared_1.COMPONENT_PREFIX, '');
  74. const local = `__syscom_${i++}`;
  75. if (needCombineBuiltInCss) {
  76. // 发行模式下,应该将内置组件css输出到入口css中
  77. resolveBuiltInCssImport(name).forEach((cssImport) => uni_cli_shared_1.buildInCssSet.add(cssImport));
  78. return (0, uni_cli_shared_1.addImportDeclaration)(importDeclarations, local, H5_COMPONENTS_PATH, (0, shared_1.capitalize)((0, shared_1.camelize)(name)));
  79. }
  80. return addBuiltInImportDeclaration(importDeclarations, local, name);
  81. }
  82. const source = (0, uni_cli_shared_1.matchEasycom)(name);
  83. if (source) {
  84. // 处理easycom组件优先级
  85. return (0, uni_cli_shared_1.genResolveEasycomCode)(importDeclarations, str, (0, uni_cli_shared_1.addImportDeclaration)(importDeclarations, `__easycom_${i++}`, source));
  86. }
  87. }
  88. return str;
  89. });
  90. if (importDeclarations.length) {
  91. code = importDeclarations.join('') + code;
  92. }
  93. return {
  94. code,
  95. map: null,
  96. };
  97. },
  98. };
  99. }
  100. exports.uniEasycomPlugin = uniEasycomPlugin;
  101. function resolveBuiltInCssImport(name) {
  102. const cssImports = [];
  103. if (baseComponents.includes(name)) {
  104. const isX = process.env.UNI_APP_X === 'true';
  105. if (isX && xBaseComponents.includes(name)) {
  106. cssImports.push(uni_cli_shared_1.X_BASE_COMPONENTS_STYLE_PATH + name + '.css');
  107. }
  108. else {
  109. cssImports.push(uni_cli_shared_1.BASE_COMPONENTS_STYLE_PATH + name + '.css');
  110. }
  111. }
  112. else {
  113. cssImports.push(uni_cli_shared_1.H5_COMPONENTS_STYLE_PATH + name + '.css');
  114. }
  115. const deps = uni_cli_shared_1.COMPONENT_DEPS_CSS[name];
  116. deps && deps.forEach((dep) => cssImports.push(dep));
  117. return cssImports;
  118. }
  119. function addBuiltInImportDeclaration(importDeclarations, local, name) {
  120. resolveBuiltInCssImport(name).forEach((cssImport) => importDeclarations.push(`import '${cssImport}';`));
  121. return (0, uni_cli_shared_1.addImportDeclaration)(importDeclarations, local, H5_COMPONENTS_PATH, (0, shared_1.capitalize)((0, shared_1.camelize)(name)));
  122. }