You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
6.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initAutoImportOptions = void 0;
const uni_shared_1 = require("@dcloudio/uni-shared");
const uts_1 = require("../uts");
const uniLifeCyclePreset = {
from: '@dcloudio/uni-app',
imports: [
// ssr
'ssrRef',
'shallowSsrRef',
// uni-app lifecycle
// App and Page
'onShow',
'onHide',
// App
'onLaunch',
'onError',
'onThemeChange',
'onKeyboardHeightChange',
'onPageNotFound',
'onUnhandledRejection',
'onLastPageBackPress',
'onExit',
// Page
'onPageShow',
'onPageHide',
'onLoad',
'onReady',
'onUnload',
'onResize',
'onBackPress',
'onPageScroll',
'onTabItemTap',
'onReachBottom',
'onPullDownRefresh',
// 其他
'onShareTimeline',
'onShareAppMessage',
'onShareChat', // xhs-share
// 辅助
'renderComponentSlot',
],
};
const uniH5Preset = {
from: '@dcloudio/uni-h5',
imports: [
'onAppShow',
'onAppHide',
'offAppHide',
'offAppShow',
'UniElement',
'UniElementImpl',
'UniButtonElement',
'UniCanvasElement',
'UniCheckboxElement',
'UniCheckboxGroupElement',
'UniEditorElement',
'UniFormElement',
'UniIconElement',
'UniImageElement',
'UniInputElement',
'UniLabelElement',
'UniMovableAreaElement',
'UniMovableViewElement',
'UniNavigatorElement',
'UniPickerViewElement',
'UniPickerViewColumnElement',
'UniProgressElement',
'UniRadioElement',
'UniRadioGroupElement',
'UniRichTextElement',
'UniScrollViewElement',
'UniSliderElement',
'UniSwiperElement',
'UniSwiperItemElement',
'UniSwitchElement',
'UniTextElement',
'UniTextareaElement',
'UniViewElement',
'UniListViewElement',
'UniListItemElement',
'UniStickySectionElement',
'UniStickyHeaderElement',
'UniVideoElement',
'UniWebViewElement',
'UniMapElement',
'UniCoverViewElement',
'UniCoverImageElement',
'UniPickerElement',
],
};
const uniMiniProgramPreset = {
from: 'vue',
imports: ['UniElement', 'UniElementImpl'],
};
const cloudPreset = {
from: '@dcloudio/uni-cloud',
imports: ['uniCloud', 'UniCloudError'],
};
const uniAppLifeCyclePreset = {
from: 'vue',
imports: [
// ssr
'ssrRef',
'shallowSsrRef',
// uni-app lifecycle
// App and Page
'onShow',
'onHide',
// App
'onLaunch',
'onError',
'onThemeChange',
// onKeyboardHeightChange,
'onPageNotFound',
'onUnhandledRejection',
// onLastPageBackPress,
'onExit',
// Page
'onPageShow',
'onPageHide',
'onLoad',
'onReady',
'onUnload',
'onResize',
'onBackPress',
'onPageScroll',
'onTabItemTap',
'onReachBottom',
'onPullDownRefresh',
// 其他
'onShareTimeline',
'onShareAppMessage',
// onShareChat, // xhs-share
// 辅助用于自定义render函数时开发者可以调用此方法渲染组件的slot
'renderComponentSlot',
],
};
const vuePreset = {
from: 'vue',
imports: [
// vue lifecycle
'onActivated',
'onBeforeMount',
'onBeforeUnmount',
'onBeforeUpdate',
'onErrorCaptured',
'onDeactivated',
'onMounted',
'onServerPrefetch',
'onUnmounted',
'onUpdated',
// setup helpers
'useAttrs',
'useSlots',
// reactivity,
'computed',
'customRef',
'isReadonly',
'isRef',
'isProxy',
'isReactive',
'markRaw',
'reactive',
'readonly',
'ref',
'shallowReactive',
'shallowReadonly',
'shallowRef',
'triggerRef',
'toRaw',
'toRef',
'toRefs',
'toValue',
'unref',
'watch',
'watchEffect',
'watchPostEffect',
'watchSyncEffect',
// component
'defineComponent',
'defineAsyncComponent',
'getCurrentInstance',
'inject',
'nextTick',
'provide',
'useCssModule',
'createApp',
'hasInjectionContext',
// render
'h',
'mergeProps',
'cloneVNode',
'isVNode',
'resolveComponent',
'resolveDirective',
'withDirectives',
'withModifiers',
// effect scope
'effectScope',
'EffectScope',
'getCurrentScope',
'onScopeDispose',
// types 全部全局导入
// vapor
'getCurrentGenericInstance',
],
};
function initAutoImportOptions(platform, { imports = [], ...userOptions }) {
rewriteAutoImportOnce();
const autoImport = [vuePreset];
// 只有app-ios和app-harmony平台特殊处理
if (platform === 'app-ios' || platform === 'app-harmony') {
autoImport.push(uniAppLifeCyclePreset);
}
else {
autoImport.push(uniLifeCyclePreset);
}
// 内置框架编译时,不能注入这些内容
if (!process.env.UNI_COMPILE_EXT_API_TYPE) {
autoImport.push(cloudPreset);
}
if (platform === 'web') {
autoImport.push(uniH5Preset);
}
else if (platform.startsWith('mp-')) {
autoImport.push(uniMiniProgramPreset);
}
return {
...userOptions,
include: [/\.[u]?ts$/, /\.[u]?vue/],
exclude: [/[\\/]\.git[\\/]/],
imports: imports.concat(
// app-android 平台暂不注入其他
platform === 'app-android' ? [] : autoImport),
dts: false,
};
}
exports.initAutoImportOptions = initAutoImportOptions;
const rewriteAutoImportOnce = (0, uni_shared_1.once)(() => {
const unimport = require('unimport');
const originalCreateUnimport = unimport.createUnimport;
unimport.createUnimport = (opts) => {
const unimport_ = originalCreateUnimport(opts);
const originalScanImportsFromDir = unimport_.scanImportsFromDir;
unimport_.scanImportsFromDir = async (dir, options) => {
const exports_ = (await originalScanImportsFromDir(dir, options));
scanCustomElements(exports_);
return exports_;
};
return unimport_;
};
});
function scanCustomElements(exports_) {
const utsCustomElementsExports = (0, uts_1.getUTSCustomElementsExports)();
for (const [key, value] of utsCustomElementsExports.entries()) {
value.exports.forEach((export_) => {
exports_.push({
from: key,
name: export_[0],
});
});
}
}