From 137f457283fe3ccbef328d68e8819d14796b2d94 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Thu, 25 Mar 2021 17:06:03 +0800 Subject: [PATCH] feat: hmr --- packages/global.d.ts | 11 +- packages/shims-uni-app.d.ts | 26 ++- packages/uni-h5/dist/uni-h5.esm.js | 31 +--- .../src/framework/components/app/index.ts | 14 +- .../uni-h5/src/framework/plugin/components.ts | 18 -- packages/vite-plugin-uni/src/config/define.ts | 136 +-------------- .../src/configResolved/index.ts | 2 +- .../src/configResolved/plugins/appVue.ts | 2 +- .../src/configResolved/plugins/index.ts | 7 +- .../src/configResolved/plugins/pagesJson.ts | 146 +++------------- .../src/handleHotUpdate/index.ts | 38 ++++- packages/vite-plugin-uni/src/index.ts | 2 + packages/vite-plugin-uni/src/utils/define.ts | 160 ++++++++++++++++++ packages/vite-plugin-uni/src/utils/index.ts | 2 + .../vite-plugin-uni/src/utils/pagesJson.ts | 128 ++++++++++++++ 15 files changed, 399 insertions(+), 324 deletions(-) create mode 100644 packages/vite-plugin-uni/src/utils/define.ts create mode 100644 packages/vite-plugin-uni/src/utils/index.ts create mode 100644 packages/vite-plugin-uni/src/utils/pagesJson.ts diff --git a/packages/global.d.ts b/packages/global.d.ts index f55a21338a..35df557640 100644 --- a/packages/global.d.ts +++ b/packages/global.d.ts @@ -1,15 +1,7 @@ declare var tt: any declare var qa: any declare var swan: any -declare var __PLATFORM__: - | 'h5' - | 'app-plus' - | 'mp-alipay' - | 'mp-baidu' - | 'mp-qq' - | 'mp-toutiao' - | 'mp-weixin' - | 'quickapp-webview' +declare var __PLATFORM__: UniApp.PLATFORM declare var __PLATFORM_PREFIX__: 'wx' | 'qq' | 'my' | 'swan' | 'tt' | 'qa' declare var __GLOBAL__: Record @@ -22,6 +14,7 @@ declare var __VUE_OPTIONS_API__: boolean declare var __UNI_FEATURE_WX__: boolean declare var __UNI_FEATURE_WXS__: boolean +declare var __UNI_FEATURE_NVUE__: boolean declare var __UNI_FEATURE_PROMISE__: boolean declare var __UNI_FEATURE_LONGPRESS__: boolean declare var __UNI_FEATURE_ROUTER_MODE__: 'hash' | 'history' diff --git a/packages/shims-uni-app.d.ts b/packages/shims-uni-app.d.ts index 97e53650c9..fb9bf1b1bb 100644 --- a/packages/shims-uni-app.d.ts +++ b/packages/shims-uni-app.d.ts @@ -1,4 +1,13 @@ declare namespace UniApp { + type PLATFORM = + | 'h5' + | 'app-plus' + | 'mp-alipay' + | 'mp-baidu' + | 'mp-qq' + | 'mp-toutiao' + | 'mp-weixin' + | 'quickapp-webview' interface LayoutWindowOptions { matchMedia?: { minWidth?: number @@ -53,22 +62,35 @@ declare namespace UniApp { refreshOptions?: PageRefreshOptions } + interface PagesJsonPageStyle { + enablePullDownRefresh?: boolean + } + interface PagesJsonPageOptions { path: string - style?: Record + style?: PagesJsonPageStyle } interface PagesJsonSubpackagesOptions { root: string pages: PagesJsonPageOptions[] } + interface PagesJsonWindowOptions extends PagesJsonPageOptions { + matchMedia: { + minWidth: number + } + } + interface PagesJson { pages: PagesJsonPageOptions[] subpackages?: PagesJsonSubpackagesOptions[] subPackages?: PagesJsonSubpackagesOptions[] - globalStyle?: {} + globalStyle?: PagesJsonPageStyle tabBar?: { list: [] } + topWindow?: PagesJsonWindowOptions + leftWindow?: PagesJsonWindowOptions + rightWindow?: PagesJsonWindowOptions } } diff --git a/packages/uni-h5/dist/uni-h5.esm.js b/packages/uni-h5/dist/uni-h5.esm.js index d746fb6d72..743499c2b6 100644 --- a/packages/uni-h5/dist/uni-h5.esm.js +++ b/packages/uni-h5/dist/uni-h5.esm.js @@ -987,12 +987,12 @@ var AppComponent = defineComponent({ setup() { useCssVar(); useAppLifecycle(); - const {appClass, onLayoutChange} = useAppClass(); + const {clazz, onChange: onChange2} = useAppClass(); return () => (openBlock(), createBlock("uni-app", { - class: appClass.value + class: clazz.value }, [ createVNode(Layout, { - onChange: onLayoutChange + onChange: onChange2 }, null, 8, ["onChange"]) ], 2)); } @@ -1014,44 +1014,27 @@ function useAppLifecycle() { function useAppClass() { const showTabBar = ref(false); const showMaxWidth = ref(false); - function onLayoutChange(type, value) { + function onChange2(type, value) { if (type === "showTabBar") { showTabBar.value = value; } else if (type === "showMaxWidth") { showMaxWidth.value = value; } } - const appClass = computed(() => { + const clazz = computed(() => { return { "uni-app--showtabbar": showTabBar.value, "uni-app--maxwidth": showMaxWidth.value }; }); return { - appClass, - onLayoutChange + clazz, + onChange: onChange2 }; } -const _sfc_main$r = {}; -const _hoisted_1$d = /* @__PURE__ */ createVNode("uni-top-window", null, null, -1); -function _sfc_render$p(_ctx, _cache) { - const _component_router_view = resolveComponent("router-view"); - return openBlock(), createBlock("uni-layout", null, [ - _hoisted_1$d, - createVNode("uni-content", null, [ - createVNode("uni-main", null, [ - (openBlock(), createBlock(KeepAlive, null, [ - createVNode(_component_router_view) - ], 1024)) - ]) - ]) - ]); -} -_sfc_main$r.render = _sfc_render$p; function initSystemComponents(app) { AppComponent.name = COMPONENT_NAME_PREFIX + AppComponent.name; app.component(AppComponent.name, AppComponent); - app.component(_sfc_main$r.name, _sfc_main$r); } function initMixin(app) { app.mixin({ diff --git a/packages/uni-h5/src/framework/components/app/index.ts b/packages/uni-h5/src/framework/components/app/index.ts index 5d13f7b55f..dfcc43d503 100644 --- a/packages/uni-h5/src/framework/components/app/index.ts +++ b/packages/uni-h5/src/framework/components/app/index.ts @@ -25,20 +25,20 @@ export default defineComponent({ setup() { useCssVar() useAppLifecycle() - const { appClass, onLayoutChange } = useAppClass() + const { clazz, onChange } = useAppClass() return () => ( openBlock(), createBlock( 'uni-app', { - class: appClass.value, + class: clazz.value, }, [ createVNode( Layout, { - onChange: onLayoutChange, + onChange, }, null, 8 /* PROPS */, @@ -70,21 +70,21 @@ function useAppLifecycle() { function useAppClass() { const showTabBar = ref(false) const showMaxWidth = ref(false) - function onLayoutChange(type: string, value: boolean) { + function onChange(type: string, value: boolean) { if (type === 'showTabBar') { showTabBar.value = value } else if (type === 'showMaxWidth') { showMaxWidth.value = value } } - const appClass = computed(() => { + const clazz = computed(() => { return { 'uni-app--showtabbar': showTabBar.value, 'uni-app--maxwidth': showMaxWidth.value, } }) return { - appClass, - onLayoutChange, + clazz, + onChange, } } diff --git a/packages/uni-h5/src/framework/plugin/components.ts b/packages/uni-h5/src/framework/plugin/components.ts index 8fc92ac2db..340ccece60 100644 --- a/packages/uni-h5/src/framework/plugin/components.ts +++ b/packages/uni-h5/src/framework/plugin/components.ts @@ -3,27 +3,9 @@ import { App } from 'vue' import { COMPONENT_NAME_PREFIX } from '@dcloudio/uni-shared' import AppComponent from '../components/app/index' -// import PageComponent from '../components/page/index.vue' -// import AsyncErrorComponent from '../components/async-error/index.vue' -// import AsyncLoadingComponent from '../components/async-loading/index.vue' - -import LayoutComponent from '../components/app/test.vue' export function initSystemComponents(app: App) { // @ts-ignore AppComponent.name = COMPONENT_NAME_PREFIX + AppComponent.name - // @ts-ignore - // PageComponent.name = COMPONENT_NAME_PREFIX + PageComponent.name - // @ts-ignore - // AsyncErrorComponent.name = COMPONENT_NAME_PREFIX + AsyncErrorComponent.name - // @ts-ignore - // AsyncLoadingComponent.name = - // COMPONENT_NAME_PREFIX + AsyncLoadingComponent.name - app.component(AppComponent.name, AppComponent) - // app.component(PageComponent.name, PageComponent) - // app.component(AsyncErrorComponent.name, AsyncErrorComponent) - // app.component(AsyncLoadingComponent.name, AsyncLoadingComponent) - - app.component(LayoutComponent.name, LayoutComponent) } diff --git a/packages/vite-plugin-uni/src/config/define.ts b/packages/vite-plugin-uni/src/config/define.ts index 11ce1f3b7e..ecbdde4bfb 100644 --- a/packages/vite-plugin-uni/src/config/define.ts +++ b/packages/vite-plugin-uni/src/config/define.ts @@ -1,140 +1,10 @@ -import fs from 'fs' -import path from 'path' -import { parse } from 'jsonc-parser' import { ConfigEnv, UserConfig } from 'vite' import { VitePluginUniResolvedOptions } from '..' - -interface ProjectFeatures {} -interface PagesFeatures { - pages: boolean - tabBar: boolean - topWindow: boolean - leftWindow: boolean - rightWindow: boolean - pullDownRefresh: boolean -} -interface ManifestFeatures { - wx: boolean - wxs: boolean - promise: boolean - longpress: boolean - routerMode: '"hash"' | '"history"' -} - -function resolveProjectFeature(inputDir: string, { command }: ConfigEnv) { - const features: ProjectFeatures = {} - if (command === 'build') { - } - return features -} - -function resolvePagesFeature( - inputDir: string, - { command }: ConfigEnv -): PagesFeatures { - const features: PagesFeatures = { - pages: true, // 是否多页面 - tabBar: true, // 是否启用tabBar - topWindow: false, // 是否启用topWindow - leftWindow: false, // 是否启用leftWindow - rightWindow: false, // 是否启用rightWindow - pullDownRefresh: false, // 是否启用下拉刷新 - } - - const { - tabBar, - pages, - topWindow, - leftWindow, - rightWindow, - globalStyle, - } = parse(fs.readFileSync(path.join(inputDir, 'pages.json'), 'utf8')) - if (pages && pages.length === 1) { - features.pages = false - } - if (!(tabBar && tabBar.list && tabBar.list.length)) { - features.tabBar = false - } - if (topWindow && topWindow.path) { - features.topWindow = true - } - if (leftWindow && leftWindow.path) { - features.leftWindow = true - } - if (rightWindow && rightWindow.path) { - features.rightWindow = true - } - if (globalStyle && globalStyle.enablePullDownRefresh) { - features.pullDownRefresh = true - } else { - if ( - pages.find( - (page: { style: { enablePullDownRefresh: any } }) => - page.style && page.style.enablePullDownRefresh - ) - ) { - features.pullDownRefresh = true - } - } - - return features -} - -function resolveManifestFeature(inputDir: string): ManifestFeatures { - const features: ManifestFeatures = { - wx: true, // 是否启用小程序的组件实例 API,如:selectComponent 等(uni-core/src/service/plugin/appConfig) - wxs: true, // 是否启用 wxs 支持,如:getComponentDescriptor 等(uni-core/src/view/plugin/appConfig) - promise: false, // 是否启用旧版本的 promise 支持(即返回[err,res]的格式) - longpress: true, // 是否启用longpress - routerMode: '"hash"', // 启用的 router 类型(uni-h5/src/framework/plugin/router) - } - const manifest = parse( - fs.readFileSync(path.join(inputDir, 'manifest.json'), 'utf8') - ) - if ( - manifest.h5 && - manifest.h5.router && - manifest.h5.router.mode === 'history' - ) { - features.routerMode = '"history"' - } - // TODO manifest.json features - return features -} +import { getFeatures } from '../utils' export function createDefine( - { inputDir }: VitePluginUniResolvedOptions, + options: VitePluginUniResolvedOptions, env: ConfigEnv ): UserConfig['define'] { - const { - wx, - wxs, - pages, - tabBar, - promise, - longpress, - routerMode, - topWindow, - leftWindow, - rightWindow, - pullDownRefresh, - } = Object.assign( - resolveManifestFeature(inputDir), - resolvePagesFeature(inputDir, env), - resolveProjectFeature(inputDir, env) - ) - return { - __UNI_FEATURE_WX__: wx, - __UNI_FEATURE_WXS__: wxs, - __UNI_FEATURE_PROMISE__: promise, - __UNI_FEATURE_LONGPRESS__: longpress, - __UNI_FEATURE_ROUTER_MODE__: routerMode, - __UNI_FEATURE_PAGES__: pages, - __UNI_FEATURE_TABBAR__: tabBar, - __UNI_FEATURE_TOPWINDOW__: topWindow, - __UNI_FEATURE_LEFTWINDOW__: leftWindow, - __UNI_FEATURE_RIGHTWINDOW__: rightWindow, - __UNI_FEATURE_RESPONSIVE__: topWindow || leftWindow || rightWindow, - __UNI_FEATURE_PULL_DOWN_REFRESH__: pullDownRefresh, - } + return getFeatures(options, env.command) } diff --git a/packages/vite-plugin-uni/src/configResolved/index.ts b/packages/vite-plugin-uni/src/configResolved/index.ts index cea09b236e..36b24a3786 100644 --- a/packages/vite-plugin-uni/src/configResolved/index.ts +++ b/packages/vite-plugin-uni/src/configResolved/index.ts @@ -10,6 +10,6 @@ export function createConfigResolved(options: VitePluginUniResolvedOptions) { options.inputDir = path.resolve(config.root, 'src') options.assetsDir = config.build.assetsDir - resolvePlugins(config.command, config.plugins as Plugin[], options) + resolvePlugins(config, options) }) as Plugin['configResolved'] } diff --git a/packages/vite-plugin-uni/src/configResolved/plugins/appVue.ts b/packages/vite-plugin-uni/src/configResolved/plugins/appVue.ts index f9a87d6af0..213f6c0c7d 100644 --- a/packages/vite-plugin-uni/src/configResolved/plugins/appVue.ts +++ b/packages/vite-plugin-uni/src/configResolved/plugins/appVue.ts @@ -13,7 +13,7 @@ export function uniAppVuePlugin(options: VitePluginUniResolvedOptions): Plugin { //App.vue main request if (filename === appVuePath && !query.vue) { return { - code: `