| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.parseInjects = exports.parseUniExtApis = exports.getUniExtApiProviders = void 0;
- // 重要:此文件编译后的js,需同步至 vue2 编译器中 uni-cli-shared/lib/uts/uni_modules.js
- const path_1 = __importDefault(require("path"));
- const fs_extra_1 = __importDefault(require("fs-extra"));
- const extApiProviders = [];
- function getUniExtApiProviders() {
- return extApiProviders;
- }
- exports.getUniExtApiProviders = getUniExtApiProviders;
- function parseUniExtApis(vite = true, platform, language = 'javascript') {
- if (!process.env.UNI_INPUT_DIR) {
- return {};
- }
- const uniModulesDir = path_1.default.resolve(process.env.UNI_INPUT_DIR, 'uni_modules');
- if (!fs_extra_1.default.existsSync(uniModulesDir)) {
- return {};
- }
- const injects = {};
- extApiProviders.length = 0;
- fs_extra_1.default.readdirSync(uniModulesDir).forEach((uniModuleDir) => {
- // 必须以 uni- 开头
- if (!uniModuleDir.startsWith('uni-')) {
- return;
- }
- const uniModuleRootDir = path_1.default.resolve(uniModulesDir, uniModuleDir);
- const pkgPath = path_1.default.resolve(uniModuleRootDir, 'package.json');
- if (!fs_extra_1.default.existsSync(pkgPath)) {
- return;
- }
- try {
- let exports;
- const pkg = JSON.parse(fs_extra_1.default.readFileSync(pkgPath, 'utf8'));
- if (pkg && pkg.uni_modules && pkg.uni_modules['uni-ext-api']) {
- exports = pkg.uni_modules['uni-ext-api'];
- }
- if (exports) {
- const provider = exports.provider;
- if (provider && provider.service) {
- provider.plugin = uniModuleDir;
- extApiProviders.push(provider);
- }
- const curInjects = parseInjects(vite, platform, language, `@/uni_modules/${uniModuleDir}`, uniModuleRootDir, exports);
- Object.assign(injects, curInjects);
- }
- }
- catch (e) { }
- });
- return injects;
- }
- exports.parseUniExtApis = parseUniExtApis;
- /**
- * uni:'getBatteryInfo'
- * import getBatteryInfo from '..'
- *
- * uni:['getBatteryInfo']
- * import { getBatteryInfo } from '..'
- *
- * uni:['openLocation','chooseLocation']
- * import { openLocation, chooseLocation } from '..'
- *
- * uni:{
- * onUserCaptureScreen: "onCaptureScreen"
- * offUserCaptureScreen: "offCaptureScreen"
- * }
- *
- * uni.getBatteryInfo = getBatteryInfo
- * @param source
- * @param globalObject
- * @param define
- * @returns
- */
- function parseInjects(vite = true, platform, language, source, uniModuleRootDir, exports = {}) {
- if (platform === 'app-plus') {
- platform = 'app';
- }
- let rootDefines = {};
- Object.keys(exports).forEach((name) => {
- if (name.startsWith('uni')) {
- rootDefines[name] = exports[name];
- }
- });
- const injects = {};
- if (Object.keys(rootDefines).length) {
- const platformIndexFileName = path_1.default.resolve(uniModuleRootDir, 'utssdk', platform);
- const rootIndexFileName = path_1.default.resolve(uniModuleRootDir, 'utssdk', 'index.uts');
- let hasPlatformFile = uniModuleRootDir
- ? fs_extra_1.default.existsSync(rootIndexFileName) || fs_extra_1.default.existsSync(platformIndexFileName)
- : true;
- if (!hasPlatformFile) {
- if (platform === 'app') {
- hasPlatformFile =
- fs_extra_1.default.existsSync(path_1.default.resolve(uniModuleRootDir, 'utssdk', 'app-android')) ||
- fs_extra_1.default.existsSync(path_1.default.resolve(uniModuleRootDir, 'utssdk', 'app-ios'));
- }
- }
- // 其他平台修改source,直接指向目标文件,否则 uts2js 找不到类型信息
- if (platform !== 'app' &&
- platform !== 'app-android' &&
- platform !== 'app-ios') {
- if (fs_extra_1.default.existsSync(platformIndexFileName)) {
- source = `${source}/utssdk/${platform}/index.uts`;
- }
- else if (fs_extra_1.default.existsSync(rootIndexFileName)) {
- source = `${source}/utssdk/index.uts`;
- }
- }
- for (const key in rootDefines) {
- Object.assign(injects, parseInject(vite, platform, language, source, 'uni', rootDefines[key], hasPlatformFile));
- }
- }
- return injects;
- }
- exports.parseInjects = parseInjects;
- function parseInject(vite = true, platform, language, source, globalObject, define, hasPlatformFile) {
- const injects = {};
- if (define === false) {
- }
- else if (typeof define === 'string') {
- // {'uni.getBatteryInfo' : '@dcloudio/uni-getbatteryinfo'}
- if (hasPlatformFile) {
- injects[globalObject + '.' + define] = vite ? source : [source, 'default'];
- }
- }
- else if (Array.isArray(define)) {
- // {'uni.getBatteryInfo' : ['@dcloudio/uni-getbatteryinfo','getBatteryInfo]}
- if (hasPlatformFile) {
- define.forEach((d) => {
- injects[globalObject + '.' + d] = [source, d];
- });
- }
- }
- else {
- const keys = Object.keys(define);
- keys.forEach((d) => {
- if (typeof define[d] === 'string') {
- if (hasPlatformFile) {
- injects[globalObject + '.' + d] = [source, define[d]];
- }
- }
- else {
- const defineOptions = define[d];
- const p = platform === 'app-android' || platform === 'app-ios'
- ? 'app'
- : platform;
- if (!(p in defineOptions)) {
- if (hasPlatformFile) {
- injects[globalObject + '.' + d] = [source, defineOptions.name || d];
- }
- }
- else {
- if (defineOptions[p] !== false) {
- if (p === 'app') {
- const appOptions = defineOptions.app;
- if (isPlainObject(appOptions)) {
- if (language === 'javascript') {
- if (appOptions.js === false) {
- return;
- }
- }
- else if (language === 'kotlin') {
- if (appOptions.kotlin === false) {
- return;
- }
- }
- else if (language === 'swift') {
- if (appOptions.swift === false) {
- return;
- }
- }
- }
- injects[globalObject + '.' + d] = [
- source,
- defineOptions.name || d,
- defineOptions.app,
- ];
- }
- else {
- injects[globalObject + '.' + d] = [
- source,
- defineOptions.name || d,
- ];
- }
- }
- }
- }
- });
- }
- return injects;
- }
- const objectToString = Object.prototype.toString;
- const toTypeString = (value) => objectToString.call(value);
- function isPlainObject(val) {
- return toTypeString(val) === '[object Object]';
- }
|