diff --git a/packages/shims-node.d.ts b/packages/shims-node.d.ts index 0bb241a8f0b6436b7e402a5eb09ac2cbd00389eb..2eb50dc3676920a7a3e6e45e41e82e376c6a4931 100644 --- a/packages/shims-node.d.ts +++ b/packages/shims-node.d.ts @@ -9,5 +9,8 @@ declare namespace NodeJS { UNI_CLI_CONTEXT: string UNI_COMPILER_VERSION: string UNI_HBUILDERX_PLUGINS: string + UNI_RENDERER?: 'native' + UNI_NVUE_COMPILER: 'uni-app' | 'weex' | 'vue' + UNI_NVUE_STYLE_COMPILER: 'uni-app' | 'weex' } } diff --git a/packages/shims-uni-app.d.ts b/packages/shims-uni-app.d.ts index 6d79b4026caad313c1d2932a3d024309b7adb35c..55b6ebf3e8535d765eb61712b2510fd90f96e4e4 100644 --- a/packages/shims-uni-app.d.ts +++ b/packages/shims-uni-app.d.ts @@ -67,6 +67,7 @@ declare namespace UniApp { alwaysShowBeforeRender: boolean autoclose: boolean } + onReady: (fn: Function) => void } interface UniRoute { @@ -160,6 +161,7 @@ declare namespace UniApp { } interface PagesJsonPageStyle extends PagesJsonPagePlatformStyle { + isNVue?: boolean disableScroll?: boolean enablePullDownRefresh?: boolean navigationBar: PageNavigationBar @@ -175,7 +177,6 @@ declare namespace UniApp { interface PageRouteMeta extends PagesJsonPageStyle { id?: number route: string - isNVue?: boolean isQuit?: boolean isEntry?: boolean isTabBar?: boolean diff --git a/packages/uni-app-plus/dist/uni-app-service.es.js b/packages/uni-app-plus/dist/uni-app-service.es.js index 3b8b9d9d85f53ad5b8941df50f28d36ba8249441..3d1aa30831e373e48fbc13e95a4e014e0cbac0d6 100644 --- a/packages/uni-app-plus/dist/uni-app-service.es.js +++ b/packages/uni-app-plus/dist/uni-app-service.es.js @@ -1866,7 +1866,9 @@ var serviceContext = (function (vue) { if (!vm) { return; } - const hooks = vm.$[name]; + // 兼容 nvue + const hooks = (vm._$weex ? vm.$options : vm.$)[name] + ; return hooks && invokeArrayFns(hooks, args); } @@ -10847,7 +10849,7 @@ var serviceContext = (function (vue) { return preloadWebviews[url]; } - function registerPage({ url, path, query, openType, webview, vm, }) { + function registerPage({ url, path, query, openType, webview, }) { // fast 模式,nvue 首页时,会在nvue中主动调用registerPage并传入首页webview,此时初始化一下首页(因为此时可能还未调用registerApp) if (webview) { initEntry(); @@ -10893,15 +10895,15 @@ var serviceContext = (function (vue) { const route = path.substr(1); webview.__uniapp_route = route; const pageInstance = initPageInternalInstance(openType, url, query, routeOptions.meta); - if (!webview.nvue) { - createPage(parseInt(webview.id), route, query, pageInstance, initPageOptions(routeOptions)); + initNVueEntryPage(webview); + if (webview.nvue) { + // nvue 时,先启用一个占位 vm + const fakeNVueVm = createNVueVm(webview, pageInstance); + initPageVm(fakeNVueVm, pageInstance); + addCurrentPage(fakeNVueVm); } else { - initPageVm(vm, pageInstance); - addCurrentPage(vm); - if (webview.__preload__) { - webview.__page__ = vm; - } + createPage(parseInt(webview.id), route, query, pageInstance, initPageOptions(routeOptions)); } return webview; } @@ -10926,6 +10928,40 @@ var serviceContext = (function (vue) { windowTop: meta.navigationBar.type === 'float' ? statusbarHeight + NAVBAR_HEIGHT : 0, windowBottom: tabBar$1.indexOf(meta.route) >= 0 && tabBar$1.cover ? tabBar$1.height : 0, }; + } + function initNVueEntryPage(webview) { + const isLaunchNVuePage = webview.id === '1' && webview.nvue; + // 首页是 nvue 时,在 registerPage 时,执行路由堆栈 + if (isLaunchNVuePage) { + if (__uniConfig.splashscreen && + __uniConfig.splashscreen.autoclose && + !__uniConfig.splashscreen.alwaysShowBeforeRender) { + plus.navigator.closeSplashscreen(); + } + __uniConfig.onReady(function () { + navigateFinish(); + }); + } + } + function createNVueVm(webview, pageInstance) { + return { + $: {}, + onNVuePageCreated(vm, curNVuePage) { + // 替换真实的 nvue 的 vm + initPageVm(vm, pageInstance); + const pages = getAllPages(); + const index = pages.findIndex((p) => p === curNVuePage); + if (index > -1) { + pages.splice(index, 1, vm); + } + if (webview.__preload__) { + webview.__page__ = vm; + } + }, + $getAppWebview() { + return webview; + }, + }; } const navigateTo = defineAsyncApi(API_NAVIGATE_TO, (args, { resolve, reject }) => { diff --git a/packages/uni-app-plus/src/service/framework/page/register.ts b/packages/uni-app-plus/src/service/framework/page/register.ts index 675e49f5667f617b9ab38425a3613eadcfe69978..d1541fcf640b26be44165fc77a022858ac132095 100644 --- a/packages/uni-app-plus/src/service/framework/page/register.ts +++ b/packages/uni-app-plus/src/service/framework/page/register.ts @@ -14,9 +14,10 @@ import { createWebview, initWebview } from '../webview' import { createPage } from './define' import { getStatusbarHeight } from '../../../helpers/statusBar' import tabBar from '../app/tabBar' -import { addCurrentPage } from './getCurrentPages' +import { addCurrentPage, getAllPages } from './getCurrentPages' import { getBaseSystemInfo } from '../../api/base/getBaseSystemInfo' import { preloadWebviews, PreloadWebviewObject } from './preLoad' +import { navigateFinish } from '../../api/route/utils' interface RegisterPageOptions { url: string @@ -24,7 +25,6 @@ interface RegisterPageOptions { query: Record openType: UniApp.OpenType webview?: PlusWebviewWebviewObject - vm?: ComponentPublicInstance // nvue vm instance // eventChannel: unknown } @@ -34,7 +34,6 @@ export function registerPage({ query, openType, webview, - vm, }: RegisterPageOptions) { // fast 模式,nvue 首页时,会在nvue中主动调用registerPage并传入首页webview,此时初始化一下首页(因为此时可能还未调用registerApp) if (webview) { @@ -96,7 +95,14 @@ export function registerPage({ routeOptions.meta ) - if (!(webview as any).nvue) { + initNVueEntryPage(webview) + + if ((webview as any).nvue) { + // nvue 时,先启用一个占位 vm + const fakeNVueVm = createNVueVm(webview, pageInstance) + initPageVm(fakeNVueVm, pageInstance) + addCurrentPage(fakeNVueVm) + } else { createPage( parseInt(webview.id!), route, @@ -104,13 +110,6 @@ export function registerPage({ pageInstance, initPageOptions(routeOptions) ) - } else { - initPageVm(vm!, pageInstance) - addCurrentPage(vm!) - - if ((webview as any).__preload__) { - ;(webview as any).__page__ = vm - } } return webview } @@ -139,3 +138,44 @@ function initPageOptions({ meta }: UniApp.UniRoute): PageNodeOptions { tabBar.indexOf(meta.route) >= 0 && tabBar.cover ? tabBar.height : 0, } } + +function initNVueEntryPage(webview: PlusWebviewWebviewObject) { + const isLaunchNVuePage = webview.id === '1' && (webview as any).nvue + // 首页是 nvue 时,在 registerPage 时,执行路由堆栈 + if (isLaunchNVuePage) { + if ( + __uniConfig.splashscreen && + __uniConfig.splashscreen.autoclose && + !__uniConfig.splashscreen.alwaysShowBeforeRender + ) { + plus.navigator.closeSplashscreen() + } + __uniConfig.onReady(function () { + navigateFinish() + }) + } +} + +function createNVueVm( + webview: PlusWebviewWebviewObject, + pageInstance: Page.PageInstance['$page'] +) { + return { + $: {}, // navigateBack 时,invokeHook 会调用 $ + onNVuePageCreated(vm: ComponentPublicInstance, curNVuePage: unknown) { + // 替换真实的 nvue 的 vm + initPageVm(vm, pageInstance) + const pages = getAllPages() + const index = pages.findIndex((p) => p === curNVuePage) + if (index > -1) { + pages.splice(index, 1, vm) + } + if ((webview as any).__preload__) { + ;(webview as any).__page__ = vm + } + }, + $getAppWebview() { + return webview + }, + } as unknown as ComponentPublicInstance +} diff --git a/packages/uni-cli-nvue/dist/utils/env.js b/packages/uni-cli-nvue/dist/utils/env.js deleted file mode 100644 index c72cac70ad10555ea8e8d184dea5ff3efd55c9d8..0000000000000000000000000000000000000000 --- a/packages/uni-cli-nvue/dist/utils/env.js +++ /dev/null @@ -1,9 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.initEnv = void 0; -function initEnv(options) { - if (options.styleCompiler === 'uni-app') { - process.env.UNI_NVUE_STYLE_COMPILER = 'uni-app'; - } -} -exports.initEnv = initEnv; diff --git a/packages/uni-cli-nvue/dist/webpack/config/entry.js b/packages/uni-cli-nvue/dist/webpack/config/entry.js new file mode 100644 index 0000000000000000000000000000000000000000..62ded8155072bf406340a3f74b2ab7f69857299e --- /dev/null +++ b/packages/uni-cli-nvue/dist/webpack/config/entry.js @@ -0,0 +1,7 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.createEntry = void 0; +function createEntry() { + return process.UNI_NVUE_ENTRY; +} +exports.createEntry = createEntry; diff --git a/packages/uni-cli-nvue/dist/webpack/config/index.js b/packages/uni-cli-nvue/dist/webpack/config/index.js index d0583be08a6453235e8fcb4c7d380f2dcb1bd848..ff4665283dcdd86d589155d493bd0e0737cfa264 100644 --- a/packages/uni-cli-nvue/dist/webpack/config/index.js +++ b/packages/uni-cli-nvue/dist/webpack/config/index.js @@ -6,14 +6,13 @@ const output_1 = require("./output"); const module_1 = require("./module"); const plugins_1 = require("./plugins"); const resolve_1 = require("./resolve"); +const entry_1 = require("./entry"); function createConfig(mode, options) { return { mode: mode, devtool: false, watch: mode === 'development', - entry() { - return process.UNI_NVUE_ENTRY; - }, + entry: entry_1.createEntry(), externals: { vue: 'Vue', }, diff --git a/packages/uni-cli-nvue/dist/webpack/config/module/rules/vueLoader/compilerOptions/modules/index.js b/packages/uni-cli-nvue/dist/webpack/config/module/rules/vueLoader/compilerOptions/modules/index.js index e8e083993c7a7d23e9dff6814811f65edd4c14b5..8c10726cf1f0622ed3d2ffc7c31c001b0a630f15 100644 --- a/packages/uni-cli-nvue/dist/webpack/config/module/rules/vueLoader/compilerOptions/modules/index.js +++ b/packages/uni-cli-nvue/dist/webpack/config/module/rules/vueLoader/compilerOptions/modules/index.js @@ -6,10 +6,10 @@ const boolAttr_1 = require("./boolAttr"); const easycom_1 = require("./easycom"); const renderWhole_1 = require("./renderWhole"); const tags_1 = require("./tags"); -function createModules(options) { +function createModules(_) { // 先处理 easycom const modules = [easycom_1.createEasycomModule(), renderWhole_1.createRenderWholeModule()]; - if (options.compiler === 'uni-app') { + if (process.env.UNI_NVUE_COMPILER === 'uni-app') { modules.push(tags_1.createTagsModule()); } modules.push(assetUrl_1.createAssetUrlModule()); diff --git a/packages/uni-cli-nvue/dist/webpack/index.js b/packages/uni-cli-nvue/dist/webpack/index.js index 4a2a8781dc3a75bae3601c85e3c2431ca9e534c2..8389653068668f0fa8202a88312548d53661d56d 100644 --- a/packages/uni-cli-nvue/dist/webpack/index.js +++ b/packages/uni-cli-nvue/dist/webpack/index.js @@ -8,10 +8,8 @@ const webpack_1 = __importDefault(require("webpack")); const uni_shared_1 = require("@dcloudio/uni-shared"); const config_1 = require("./config"); const alias_1 = require("./alias"); -const env_1 = require("../utils/env"); const initModuleAliasOnce = uni_shared_1.once(alias_1.initModuleAlias); function runWebpack(mode, options) { - env_1.initEnv(options); initModuleAliasOnce(); return new Promise((resolve, reject) => { webpack_1.default(config_1.createConfig(mode, options), (err, stats) => { diff --git a/packages/uni-cli-nvue/src/types.ts b/packages/uni-cli-nvue/src/types.ts index 0522b6239a51896f4aa2052fd7dbbca34a2c52d0..7f585953b074e835ed3cf251b5912fa900af72b4 100644 --- a/packages/uni-cli-nvue/src/types.ts +++ b/packages/uni-cli-nvue/src/types.ts @@ -1,4 +1 @@ -interface NVueCompilerOptions { - compiler: 'uni-app' | 'weex' - styleCompiler: 'uni-app' | 'weex' -} +interface NVueCompilerOptions {} diff --git a/packages/uni-cli-nvue/src/utils/env.ts b/packages/uni-cli-nvue/src/utils/env.ts deleted file mode 100644 index f9fc7bc9f3855c2523683dddd1e324d65f522491..0000000000000000000000000000000000000000 --- a/packages/uni-cli-nvue/src/utils/env.ts +++ /dev/null @@ -1,5 +0,0 @@ -export function initEnv(options: NVueCompilerOptions) { - if (options.styleCompiler === 'uni-app') { - process.env.UNI_NVUE_STYLE_COMPILER = 'uni-app' - } -} diff --git a/packages/uni-cli-nvue/src/webpack/config/entry.ts b/packages/uni-cli-nvue/src/webpack/config/entry.ts new file mode 100644 index 0000000000000000000000000000000000000000..bc8ecaa198d1c2407fe33dd04195526fec815e8b --- /dev/null +++ b/packages/uni-cli-nvue/src/webpack/config/entry.ts @@ -0,0 +1,3 @@ +export function createEntry() { + return process.UNI_NVUE_ENTRY +} diff --git a/packages/uni-cli-nvue/src/webpack/config/index.ts b/packages/uni-cli-nvue/src/webpack/config/index.ts index cec78b6197ab8298b5103d209ff4e2f0c0d90aa8..cbc7230e52d71d43d5d613ab34e10b16335f96e7 100644 --- a/packages/uni-cli-nvue/src/webpack/config/index.ts +++ b/packages/uni-cli-nvue/src/webpack/config/index.ts @@ -4,6 +4,7 @@ import { createOutput } from './output' import { createModule } from './module' import { createPlugins } from './plugins' import { createResolve } from './resolve' +import { createEntry } from './entry' export function createConfig( mode: 'production' | 'development', options: NVueCompilerOptions @@ -12,9 +13,7 @@ export function createConfig( mode: mode, devtool: false, watch: mode === 'development', - entry() { - return process.UNI_NVUE_ENTRY - }, + entry: createEntry(), externals: { vue: 'Vue', }, diff --git a/packages/uni-cli-nvue/src/webpack/config/module/rules/vueLoader/compilerOptions/modules/index.ts b/packages/uni-cli-nvue/src/webpack/config/module/rules/vueLoader/compilerOptions/modules/index.ts index dd2854b7150e14aaa8417c4ae94541c90f9170c2..af11740aca657f05e49e83ac33bae99b6a3dcdd9 100644 --- a/packages/uni-cli-nvue/src/webpack/config/module/rules/vueLoader/compilerOptions/modules/index.ts +++ b/packages/uni-cli-nvue/src/webpack/config/module/rules/vueLoader/compilerOptions/modules/index.ts @@ -4,10 +4,10 @@ import { createBoolAttrModule } from './boolAttr' import { createEasycomModule } from './easycom' import { createRenderWholeModule } from './renderWhole' import { createTagsModule } from './tags' -export function createModules(options: NVueCompilerOptions): ModuleOptions[] { +export function createModules(_: NVueCompilerOptions): ModuleOptions[] { // 先处理 easycom const modules = [createEasycomModule(), createRenderWholeModule()] - if (options.compiler === 'uni-app') { + if (process.env.UNI_NVUE_COMPILER === 'uni-app') { modules.push(createTagsModule()) } diff --git a/packages/uni-cli-nvue/src/webpack/index.ts b/packages/uni-cli-nvue/src/webpack/index.ts index b991893da67ea540c47bf08108187a9605e554a3..9654f811f95097b170e4c795f6edddeb0c77583c 100644 --- a/packages/uni-cli-nvue/src/webpack/index.ts +++ b/packages/uni-cli-nvue/src/webpack/index.ts @@ -3,7 +3,6 @@ import { once } from '@dcloudio/uni-shared' import { createConfig } from './config' import { initModuleAlias } from './alias' -import { initEnv } from '../utils/env' const initModuleAliasOnce = once(initModuleAlias) @@ -11,7 +10,6 @@ function runWebpack( mode: 'production' | 'development', options: NVueCompilerOptions ) { - initEnv(options) initModuleAliasOnce() return new Promise((resolve, reject) => { webpack(createConfig(mode, options), (err, stats) => { diff --git a/packages/uni-cli-shared/src/json/app/manifest/launchwebview.ts b/packages/uni-cli-shared/src/json/app/manifest/launchwebview.ts index af47b3a483e9892fcab59d137ffbadf584b8f598..2b8eb74f75ce437d93b1280bdba143af39df9d4a 100644 --- a/packages/uni-cli-shared/src/json/app/manifest/launchwebview.ts +++ b/packages/uni-cli-shared/src/json/app/manifest/launchwebview.ts @@ -17,13 +17,17 @@ export function initLaunchwebview( process.env.UNI_ENTRY_PAGE_PATH = entryPagePath manifestJson.plus.useragent.value = 'uni-app' - manifestJson.launch_path = '__uniappview.html' extend(manifestJson.plus.launchwebview, { id: '1', kernel: 'WKWebview', }) - // TODO 纯原生渲染 - // TODO 首页为nvue - // manifestJson.plus.launchwebview.uniNView = {path:'.js'} - // manifestJson.plus.launchwebview.id = '2' + + // 首页为nvue + const entryPage = pagesJson.pages.find((p) => p.path === entryPagePath) + if (entryPage?.style.isNVue) { + manifestJson.plus.launchwebview.uniNView = { path: entryPagePath + '.js' } + manifestJson.plus.launchwebview.id = '2' + } else { + manifestJson.launch_path = '__uniappview.html' + } } diff --git a/packages/uni-cli-shared/src/json/app/manifest/nvue.ts b/packages/uni-cli-shared/src/json/app/manifest/nvue.ts index 54fb74040bb8918a69588cb17e28db9e03a751c1..4284b3cc52cb85176950dad0254a5b156c30d942 100644 --- a/packages/uni-cli-shared/src/json/app/manifest/nvue.ts +++ b/packages/uni-cli-shared/src/json/app/manifest/nvue.ts @@ -3,10 +3,24 @@ export function initNVue( pagesJson: UniApp.PagesJson ) {} +export function getRenderer(manifestJson: Record) { + const platformOptions = manifestJson['app-plus'] + if (platformOptions && platformOptions.renderer === 'native') { + return 'native' + } + return '' +} + export function getNVueCompiler(manifestJson: Record) { const platformOptions = manifestJson['app-plus'] - if (platformOptions && platformOptions.nvueCompiler === 'weex') { - return 'weex' + if (platformOptions) { + const { nvueCompiler } = platformOptions + if (nvueCompiler === 'weex') { + return 'weex' + } + if (nvueCompiler === 'vue') { + return 'vue' + } } return 'uni-app' } diff --git a/packages/uni-cli-shared/src/json/app/pages/definePage.ts b/packages/uni-cli-shared/src/json/app/pages/definePage.ts index 392da63240b9a09565e16129e7895864f538b56c..ad9953fed7d2b5402aaa808427eefa455f0269c6 100644 --- a/packages/uni-cli-shared/src/json/app/pages/definePage.ts +++ b/packages/uni-cli-shared/src/json/app/pages/definePage.ts @@ -3,7 +3,10 @@ import { normalizeIdentifier, normalizePagePath } from '../../../utils' export function definePageCode(pagesJson: Record) { const importPagesCode: string[] = [] const definePagesCode: string[] = [] - pagesJson.pages.forEach((page: UniApp.UniRoute) => { + pagesJson.pages.forEach((page: UniApp.PagesJsonPageOptions) => { + if (page.style.isNVue) { + return + } const pagePath = page.path const pageIdentifier = normalizeIdentifier(pagePath) const pagePathWithExtname = normalizePagePath(pagePath, 'app') diff --git a/packages/uni-cli-shared/src/json/app/pages/index.ts b/packages/uni-cli-shared/src/json/app/pages/index.ts index 97239fddea6e1560940606551d0c0fc81c0dcc94..7ec8843700c7317a6749b07e5a284bcf2ab90060 100644 --- a/packages/uni-cli-shared/src/json/app/pages/index.ts +++ b/packages/uni-cli-shared/src/json/app/pages/index.ts @@ -3,6 +3,8 @@ import { definePageCode } from './definePage' import { normalizeAppUniConfig } from './uniConfig' import { normalizeAppUniRoutes } from './uniRoutes' +export * from './nvue' + export function normalizeAppPagesJson(pagesJson: Record) { return polyfillCode + restoreGlobalCode + definePageCode(pagesJson) } diff --git a/packages/uni-cli-shared/src/json/app/pages/nvue.ts b/packages/uni-cli-shared/src/json/app/pages/nvue.ts new file mode 100644 index 0000000000000000000000000000000000000000..bdecd1d4fa0523ef024b65762178098bf08aa411 --- /dev/null +++ b/packages/uni-cli-shared/src/json/app/pages/nvue.ts @@ -0,0 +1,42 @@ +import path from 'path' +import { normalizePath } from '../../../utils' + +export function initWebpackNVueEntry(pages: UniApp.PagesJsonPageOptions[]) { + process.UNI_NVUE_ENTRY = {} + pages.forEach((page) => { + if (page.style.isNVue) { + process.UNI_NVUE_ENTRY[page.path] = genWebpackBase64Code(page.path) + } + }) +} + +function genWebpackBase64Code(route: string) { + return `data:text/javascript;base64,${Buffer.from( + genNVueEntryCode(route) + ).toString('base64')}` +} + +function genNVueEntryCode(route: string) { + return `import App from '${normalizePath( + path.resolve(process.env.UNI_INPUT_DIR, route) + )}.nvue?mpType=page' +if (typeof Promise !== 'undefined' && !Promise.prototype.finally) { + Promise.prototype.finally = function(callback) { + var promise = this.constructor + return this.then(function(value) { + return promise.resolve(callback()).then(function() { + return value + }) + }, function(reason) { + return promise.resolve(callback()).then(function() { + throw reason + }) + }) + } +} +App.mpType = 'page' +App.route = '${route}' +App.el = '#root' +new Vue(App) +` +} diff --git a/packages/uni-cli-shared/src/json/app/pages/uniConfig.ts b/packages/uni-cli-shared/src/json/app/pages/uniConfig.ts index b39a032f5d868dec152ce7feec387daed030da99..62a69a0e6b0895eb750e4f9d27235272c513ee57 100644 --- a/packages/uni-cli-shared/src/json/app/pages/uniConfig.ts +++ b/packages/uni-cli-shared/src/json/app/pages/uniConfig.ts @@ -9,7 +9,7 @@ interface AppUniConfig { pages: string[] globalStyle: UniApp.PagesJsonPageStyle nvue: { - compiler: 'uni-app' | 'weex' + compiler: 'uni-app' | 'weex' | 'vue' styleCompiler: 'weex' | 'uni-app' 'flex-direction': 'row' | 'row-reverse' | 'column' | 'column-reverse' } diff --git a/packages/uni-cli-shared/src/json/pages.ts b/packages/uni-cli-shared/src/json/pages.ts index 5288c7f4b24205dd02f0a879022eeeb056d2ad0d..3e4c3774a80f88fd0bcddaa226eb7d30ab3b13e3 100644 --- a/packages/uni-cli-shared/src/json/pages.ts +++ b/packages/uni-cli-shared/src/json/pages.ts @@ -4,6 +4,7 @@ import { extend, hasOwn, isArray, isPlainObject } from '@vue/shared' import { once, TABBAR_HEIGHT } from '@dcloudio/uni-shared' import { normalizePath } from '../utils' import { parseJson } from './json' +import { initWebpackNVueEntry } from './app/pages' export const parsePagesJson = ( inputDir: string, @@ -40,8 +41,17 @@ export function normalizePagesJson(jsonStr: string, platform: UniApp.PLATFORM) { ) // pageStyle normalizePages(pagesJson.pages, platform) + + if (platform === 'app' && process.env.UNI_NVUE_COMPILER !== 'vue') { + initWebpackNVueEntry(pagesJson.pages) + } + // globalStyle - pagesJson.globalStyle = normalizePageStyle(pagesJson.globalStyle!, platform) + pagesJson.globalStyle = normalizePageStyle( + null, + pagesJson.globalStyle!, + platform + ) // tabBar if (pagesJson.tabBar) { const tabBar = normalizeTabBar(pagesJson.tabBar!) @@ -70,7 +80,7 @@ function normalizePages( platform: UniApp.PLATFORM ) { return pages.filter((page) => { - page.style = normalizePageStyle(page.style!, platform) + page.style = normalizePageStyle(page.path, page.style!, platform) return true }) } @@ -93,9 +103,17 @@ function normalizeSubpackages( } function normalizePageStyle( + pagePath: string | null, pageStyle: UniApp.PagesJsonPageStyle, platform: UniApp.PLATFORM ) { + const isNVue = + pagePath && + process.env.UNI_NVUE_COMPILER !== 'vue' && + fs.existsSync(path.join(process.env.UNI_INPUT_DIR, pagePath + '.nvue')) + ? true + : undefined + if (pageStyle) { if (platform === 'h5') { extend(pageStyle, pageStyle['app'] || pageStyle['app-plus']) @@ -112,9 +130,10 @@ function normalizePageStyle( pageStyle.pullToRefresh = normalizePullToRefresh(pageStyle) } } + pageStyle.isNVue = isNVue return removePlatformStyle(pageStyle) } - return { navigationBar: {} } + return { navigationBar: {}, isNVue } } const navigationBarMaps = { @@ -323,13 +342,9 @@ export function normalizePagesRoute( (tabBarPage: { pagePath: string }) => tabBarPage.pagePath === pagePath ) const isTabBar = tabBarIndex !== -1 ? true : undefined - const isNVue = fs.existsSync( - path.join(process.env.UNI_INPUT_DIR, pagePath + '.nvue') - ) let windowTop = 0 const meta = extend( { - isNVue: isNVue || undefined, isQuit: isEntry || isTabBar ? true : undefined, isEntry: isEntry || undefined, isTabBar: isTabBar || undefined, diff --git a/packages/uni-core/src/helpers/hook.ts b/packages/uni-core/src/helpers/hook.ts index 20baa75568d6a7d9f90cdb06d2050807008f9722..96613503c52825d4c423eb988f4e2ded6d54e828 100644 --- a/packages/uni-core/src/helpers/hook.ts +++ b/packages/uni-core/src/helpers/hook.ts @@ -30,7 +30,12 @@ export function invokeHook( if (!vm) { return } - const hooks = vm.$[name as string] + // 兼容 nvue + + const hooks = + __PLATFORM__ === 'app' + ? ((vm as any)._$weex ? vm.$options : vm.$)[name as string] + : vm.$[name as string] return hooks && invokeArrayFns(hooks, args) } diff --git a/packages/vite-plugin-uni/src/cli/nvue.ts b/packages/vite-plugin-uni/src/cli/nvue.ts index 6a2c227a87933d24d2c8e9814b15c9eecaaedc8d..fbef4e91524056eca80dd092fcd14428c088b311 100644 --- a/packages/vite-plugin-uni/src/cli/nvue.ts +++ b/packages/vite-plugin-uni/src/cli/nvue.ts @@ -3,20 +3,22 @@ import { getNVueCompiler, getNVueStyleCompiler, } from '@dcloudio/uni-cli-shared' +import { getRenderer } from '../../../uni-cli-shared/dist/json/app/manifest/nvue' -function initNVueCompilerOptions() { +export function initNVueEnv() { const manifestJson = parseManifestJsonOnce(process.env.UNI_INPUT_DIR) - const nvueCompilerOptions = { - compiler: 'uni-app', - styleCompiler: 'weex', + if (getRenderer(manifestJson) === 'native') { + process.env.UNI_RENDERER = 'native' } - if (getNVueCompiler(manifestJson) === 'uni-app') { - nvueCompilerOptions.compiler = 'uni-app' + const nvueCompiler = getNVueCompiler(manifestJson) + if (nvueCompiler === 'uni-app') { + process.env.UNI_NVUE_COMPILER = 'uni-app' + } else if (nvueCompiler === 'vue') { + process.env.UNI_NVUE_COMPILER = 'vue' } if (getNVueStyleCompiler(manifestJson) === 'uni-app') { - nvueCompilerOptions.styleCompiler = 'uni-app' + process.env.UNI_NVUE_STYLE_COMPILER = 'uni-app' } - return nvueCompilerOptions } export async function runNVue(mode: 'prod' | 'dev') { @@ -38,10 +40,12 @@ export async function runNVue(mode: 'prod' | 'dev') { if (!nvue) { return } - const options = initNVueCompilerOptions() + if (process.env.UNI_NVUE_COMPILER === 'vue') { + return + } if (mode === 'prod') { - await nvue.runWebpackBuild(options) + await nvue.runWebpackBuild() } else { - await nvue.runWebpackDev(options) + await nvue.runWebpackDev() } } diff --git a/packages/vite-plugin-uni/src/cli/utils.ts b/packages/vite-plugin-uni/src/cli/utils.ts index 596229663be6bb5c50bfb6af18c9bc025bac806e..87ee5a6a8a39df1eaae28d2e4b244ce23fe8f498 100644 --- a/packages/vite-plugin-uni/src/cli/utils.ts +++ b/packages/vite-plugin-uni/src/cli/utils.ts @@ -5,6 +5,7 @@ import { BuildOptions, InlineConfig } from 'vite' import { isInHBuilderX } from '@dcloudio/uni-cli-shared' import { CliOptions } from '.' +import { initNVueEnv } from './nvue' export const PLATFORMS = [ 'app', @@ -82,6 +83,9 @@ export function initEnv(type: 'dev' | 'build', options: CliOptions) { process.exit(1) ) } + if (process.env.UNI_PLATFORM === 'app') { + initNVueEnv() + } } export function cleanOptions(options: CliOptions) {