ext-api.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.injectsToAutoImports = exports.uniUTSExtApiReplace = void 0;
  7. const shared_1 = require("@vue/shared");
  8. const uni_shared_1 = require("@dcloudio/uni-shared");
  9. const vite_1 = __importDefault(require("unplugin-auto-import/vite"));
  10. const uni_modules_1 = require("../../../uni_modules");
  11. const url_1 = require("../../utils/url");
  12. const escape = (str) => str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
  13. const parseUniExtApisOnce = (0, uni_shared_1.once)(uni_modules_1.parseUniExtApis);
  14. function uniUTSExtApiReplace() {
  15. const injects = parseUniExtApisOnce(true, process.env.UNI_UTS_PLATFORM, 'javascript');
  16. const injectApis = Object.keys(injects);
  17. const firstPass = new RegExp(`(?:${injectApis.map(escape).join('|')})`, 'g');
  18. return {
  19. name: 'uni:uts-ext-api-replace',
  20. configResolved(config) {
  21. const index = config.plugins.findIndex((p) => p.name === 'uts');
  22. if (index > -1) {
  23. const injects = parseUniExtApisOnce(true, process.env.UNI_UTS_PLATFORM, 'javascript');
  24. if (Object.keys(injects).length) {
  25. // @ts-expect-error
  26. config.plugins.splice(index, 0, (0, vite_1.default)({
  27. include: [/\.[u]?ts$/, /\.[u]?vue/],
  28. exclude: [/[\\/]\.git[\\/]/],
  29. imports: injectsToAutoImports(parseUniExtApisOnce(true, process.env.UNI_UTS_PLATFORM, 'javascript')),
  30. dts: false,
  31. }));
  32. }
  33. }
  34. },
  35. transform(code, id) {
  36. if (!injectApis.length) {
  37. return;
  38. }
  39. if (!(0, url_1.isJsFile)(id)) {
  40. return;
  41. }
  42. if (code.search(firstPass) === -1) {
  43. return;
  44. }
  45. injectApis.forEach((api) => {
  46. code = code.replaceAll(api, api.replace('.', '_'));
  47. });
  48. return {
  49. code,
  50. map: { mappings: '' },
  51. };
  52. },
  53. };
  54. }
  55. exports.uniUTSExtApiReplace = uniUTSExtApiReplace;
  56. /**
  57. * { 'uni.getBatteryInfo': ['@/uni_modules/uni-getbatteryinfo/utssdk/web/index.uts','getBatteryInfo'] }
  58. * { '@/uni_modules/uni-getbatteryinfo/utssdk/web/index.ts': [['getBatteryInfo', 'uni_getBatteryInfo']] }
  59. * @param injects
  60. */
  61. function injectsToAutoImports(injects) {
  62. const autoImports = {};
  63. Object.keys(injects).forEach((api) => {
  64. const options = injects[api];
  65. if ((0, shared_1.isArray)(options) && options.length >= 2) {
  66. const source = options[0];
  67. const name = options[1];
  68. if (!autoImports[source]) {
  69. autoImports[source] = [];
  70. }
  71. autoImports[source].push([name, api.replace('.', '_')]);
  72. }
  73. });
  74. return Object.keys(autoImports).map((source) => {
  75. return {
  76. from: source,
  77. imports: autoImports[source],
  78. };
  79. });
  80. }
  81. exports.injectsToAutoImports = injectsToAutoImports;