提交 dc126681 编写于 作者: fxy060608's avatar fxy060608

wip(app): nvue

上级 68f44bf4
...@@ -18247,7 +18247,7 @@ function initPageOptions({ meta }) { ...@@ -18247,7 +18247,7 @@ function initPageOptions({ meta }) {
} }
function initNVuePage(id, nvuePageVm, pageInstance) { function initNVuePage(id, nvuePageVm, pageInstance) {
initPageVm(nvuePageVm, pageInstance); initPageVm(nvuePageVm, pageInstance);
addCurrentPage(nvuePageVm); addCurrentPage(initScope(id, nvuePageVm, pageInstance));
if (id === 1) { if (id === 1) {
// 首页是 nvue 时,在 registerPage 时,执行路由堆栈 // 首页是 nvue 时,在 registerPage 时,执行路由堆栈
if (__uniConfig.splashscreen && if (__uniConfig.splashscreen &&
......
...@@ -19,6 +19,7 @@ import { addCurrentPage, getAllPages } from './getCurrentPages' ...@@ -19,6 +19,7 @@ import { addCurrentPage, getAllPages } from './getCurrentPages'
import { getBaseSystemInfo } from '../../api/base/getBaseSystemInfo' import { getBaseSystemInfo } from '../../api/base/getBaseSystemInfo'
import { preloadWebviews, PreloadWebviewObject } from './preLoad' import { preloadWebviews, PreloadWebviewObject } from './preLoad'
import { navigateFinish } from '../../api/route/utils' import { navigateFinish } from '../../api/route/utils'
import { initScope } from './setup'
interface RegisterPageOptions { interface RegisterPageOptions {
url: string url: string
...@@ -154,7 +155,7 @@ function initNVuePage( ...@@ -154,7 +155,7 @@ function initNVuePage(
pageInstance: Page.PageInstance['$page'] pageInstance: Page.PageInstance['$page']
) { ) {
initPageVm(nvuePageVm, pageInstance) initPageVm(nvuePageVm, pageInstance)
addCurrentPage(nvuePageVm) addCurrentPage(initScope(id, nvuePageVm, pageInstance))
if (id === 1) { if (id === 1) {
// 首页是 nvue 时,在 registerPage 时,执行路由堆栈 // 首页是 nvue 时,在 registerPage 时,执行路由堆栈
if ( if (
......
...@@ -52,7 +52,7 @@ export function setupPage(component: VuePageComponent) { ...@@ -52,7 +52,7 @@ export function setupPage(component: VuePageComponent) {
return component return component
} }
function initScope( export function initScope(
pageId: number, pageId: number,
vm: ComponentPublicInstance, vm: ComponentPublicInstance,
pageInstance: Page.PageInstance['$page'] pageInstance: Page.PageInstance['$page']
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
"@dcloudio/uni-shared": "0.0.1-nvue3.3040020220221001", "@dcloudio/uni-shared": "0.0.1-nvue3.3040020220221001",
"@rollup/pluginutils": "^4.1.2", "@rollup/pluginutils": "^4.1.2",
"@vitejs/plugin-vue": "^2.2.2", "@vitejs/plugin-vue": "^2.2.2",
"@vue/compiler-dom": "3.2.31",
"@vue/compiler-sfc": "3.2.31", "@vue/compiler-sfc": "3.2.31",
"debug": "^4.3.3", "debug": "^4.3.3",
"fs-extra": "^10.0.0", "fs-extra": "^10.0.0",
......
...@@ -25,6 +25,7 @@ import { createConfigResolved } from '../../plugin/configResolved' ...@@ -25,6 +25,7 @@ import { createConfigResolved } from '../../plugin/configResolved'
import { defaultNVueRpx2Unit } from '@dcloudio/uni-shared' import { defaultNVueRpx2Unit } from '@dcloudio/uni-shared'
import { external, globals } from '../utils' import { external, globals } from '../utils'
import { transformRootNode } from './transforms/transformRootNode' import { transformRootNode } from './transforms/transformRootNode'
import { transformModel } from './transforms/vModel'
const uTags = { const uTags = {
text: 'u-text', text: 'u-text',
...@@ -48,6 +49,12 @@ export function initNVueNodeTransforms() { ...@@ -48,6 +49,12 @@ export function initNVueNodeTransforms() {
] ]
} }
export function initNVueDirectiveTransforms() {
return {
model: transformModel,
}
}
export function uniAppNVuePlugin({ export function uniAppNVuePlugin({
appService, appService,
}: { }: {
......
import {
transformModel as baseTransform,
DirectiveTransform,
ElementTypes,
findProp,
NodeTypes,
hasDynamicKeyVBind,
} from '@vue/compiler-core'
import { createDOMCompilerError, DOMErrorCodes } from '@vue/compiler-dom'
import { V_MODEL_TEXT, V_MODEL_DYNAMIC } from '@vue/compiler-dom'
export const transformModel: DirectiveTransform = (dir, node, context) => {
const baseResult = baseTransform(dir, node, context)
// base transform has errors OR component v-model (only need props)
if (!baseResult.props.length || node.tagType === ElementTypes.COMPONENT) {
return baseResult
}
if (dir.arg) {
context.onError(
createDOMCompilerError(
DOMErrorCodes.X_V_MODEL_ARG_ON_ELEMENT,
dir.arg.loc
)
)
}
function checkDuplicatedValue() {
const value = findProp(node, 'value')
if (value) {
context.onError(
createDOMCompilerError(
DOMErrorCodes.X_V_MODEL_UNNECESSARY_VALUE,
value.loc
)
)
}
}
const { tag } = node
const isCustomElement = context.isCustomElement(tag)
if (
tag === 'input' ||
tag === 'u-input' ||
tag === 'textarea' ||
tag === 'u-textarea' ||
isCustomElement
) {
let directiveToUse = V_MODEL_TEXT
let isInvalidType = false
if (tag === 'input' || tag === 'u-input' || isCustomElement) {
const type = findProp(node, `type`)
if (type) {
if (type.type === NodeTypes.DIRECTIVE) {
// :type="foo"
directiveToUse = V_MODEL_DYNAMIC
} else if (type.value) {
checkDuplicatedValue()
}
} else if (hasDynamicKeyVBind(node)) {
// element has bindings with dynamic keys, which can possibly contain
// "type".
directiveToUse = V_MODEL_DYNAMIC
} else {
// text type
checkDuplicatedValue()
}
} else {
// textarea
checkDuplicatedValue()
}
// inject runtime directive
// by returning the helper symbol via needRuntime
// the import will replaced a resolveDirective call.
if (!isInvalidType) {
baseResult.needRuntime = context.helper(directiveToUse)
}
} else {
context.onError(
createDOMCompilerError(
DOMErrorCodes.X_V_MODEL_ON_INVALID_ELEMENT,
dir.loc
)
)
}
// native vmodel doesn't need the `modelValue` props since they are also
// passed to the runtime as `binding.value`. removing it reduces code size.
baseResult.props = baseResult.props.filter(
(p) =>
!(
p.key.type === NodeTypes.SIMPLE_EXPRESSION &&
p.key.content === 'modelValue'
)
)
return baseResult
}
...@@ -109,7 +109,7 @@ const __pagePath = '${removeExt(filename)}' ...@@ -109,7 +109,7 @@ const __pagePath = '${removeExt(filename)}'
let __pageQuery = {} let __pageQuery = {}
try{ __pageQuery = JSON.parse(webview.__query__) }catch(e){} try{ __pageQuery = JSON.parse(webview.__query__) }catch(e){}
App.mpType = 'page' App.mpType = 'page'
const app = Vue.createPageApp(App,{$store:getApp().$store,__pageId,__pagePath,__pageQuery}) const app = Vue.createPageApp(App,{$store:getApp({allowDefault:true}).$store,__pageId,__pagePath,__pageQuery})
app.provide('__globalStyles', Vue.useCssStyles([...__uniConfig.styles, ...(App.styles||[])])) app.provide('__globalStyles', Vue.useCssStyles([...__uniConfig.styles, ...(App.styles||[])]))
app.mount('#root')`, app.mount('#root')`,
path.join(nvueOutDir(), 'main.js'), path.join(nvueOutDir(), 'main.js'),
......
...@@ -12,6 +12,7 @@ import { ...@@ -12,6 +12,7 @@ import {
} from '@dcloudio/uni-cli-shared' } from '@dcloudio/uni-cli-shared'
import { initNVueNodeTransforms } from '../../nvue' import { initNVueNodeTransforms } from '../../nvue'
import { initNVueDirectiveTransforms } from '../../nvue/plugin'
export function uniOptions(): UniVitePlugin['uni'] { export function uniOptions(): UniVitePlugin['uni'] {
const isNVueCompiler = process.env.UNI_COMPILER === 'nvue' const isNVueCompiler = process.env.UNI_COMPILER === 'nvue'
...@@ -53,6 +54,9 @@ export function uniOptions(): UniVitePlugin['uni'] { ...@@ -53,6 +54,9 @@ export function uniOptions(): UniVitePlugin['uni'] {
transformMatchMedia, transformMatchMedia,
transformPageHead, transformPageHead,
], ],
directiveTransforms: {
...(isNVueCompiler ? initNVueDirectiveTransforms() : {}),
},
}, },
} }
} }
...@@ -24,11 +24,11 @@ function createApp(code: string) { ...@@ -24,11 +24,11 @@ function createApp(code: string) {
return `${code.replace( return `${code.replace(
'createSSRApp', 'createSSRApp',
'createVueApp as createSSRApp' 'createVueApp as createSSRApp'
)};const {app:__app__,Vuex:__Vuex__,Pinia:__Pinia__}=createApp();uni.Vuex=__Vuex__;uni.Pinia=__Pinia__;__app__._component.mpType='app';__app__._component.render=()=>{};__app__.mount("#app");` )};const {app:__app__,Vuex:__Vuex__,Pinia:__Pinia__}=createApp();uni.Vuex=__Vuex__;uni.Pinia=__Pinia__;__app__.provide('__globalStyles', __uniConfig.styles);__app__._component.mpType='app';__app__._component.render=()=>{};__app__.mount("#app");`
} }
function createLegacyApp(code: string) { function createLegacyApp(code: string) {
return `function createApp(rootComponent,rootProps){rootComponent.mpTye='app';rootComponent.render=()=>{};const app=createVueApp(rootComponent,rootProps);const oldMount=app.mount;app.mount=(container)=>{const appVm=oldMount.call(app,container);return appVm;};return app;};${code.replace( return `function createApp(rootComponent,rootProps){rootComponent.mpTye='app';rootComponent.render=()=>{};const app=createVueApp(rootComponent,rootProps);app.provide('__globalStyles', __uniConfig.styles);const oldMount=app.mount;app.mount=(container)=>{const appVm=oldMount.call(app,container);return appVm;};return app;};${code.replace(
'createApp', 'createApp',
'createVueApp' 'createVueApp'
)}` )}`
......
...@@ -9433,6 +9433,11 @@ function parseStyleSheet(_ref22) { ...@@ -9433,6 +9433,11 @@ function parseStyleSheet(_ref22) {
var component = type; var component = type;
if (!component.__styles) { if (!component.__styles) {
// nvue 和 vue 混合开发时,__globalStyles注入的是未处理过的
if (appContext && isArray(appContext.provides.__globalStyles)) {
appContext.provides.__globalStyles = useCssStyles(appContext.provides.__globalStyles);
}
if (component.mpType === 'page' && appContext) { if (component.mpType === 'page' && appContext) {
// 如果是页面组件,则直接使用全局样式 // 如果是页面组件,则直接使用全局样式
component.__styles = appContext.provides.__globalStyles; component.__styles = appContext.provides.__globalStyles;
......
...@@ -7700,6 +7700,11 @@ function parseStyleSheet(_ref22) { ...@@ -7700,6 +7700,11 @@ function parseStyleSheet(_ref22) {
var component = type; var component = type;
if (!component.__styles) { if (!component.__styles) {
// nvue 和 vue 混合开发时,__globalStyles注入的是未处理过的
if (appContext && isArray(appContext.provides.__globalStyles)) {
appContext.provides.__globalStyles = useCssStyles(appContext.provides.__globalStyles);
}
if (component.mpType === 'page' && appContext) { if (component.mpType === 'page' && appContext) {
// 如果是页面组件,则直接使用全局样式 // 如果是页面组件,则直接使用全局样式
component.__styles = appContext.provides.__globalStyles; component.__styles = appContext.provides.__globalStyles;
......
...@@ -7892,6 +7892,10 @@ function parseClassList(classList, instance, el = null) { ...@@ -7892,6 +7892,10 @@ function parseClassList(classList, instance, el = null) {
function parseStyleSheet({ type, appContext }) { function parseStyleSheet({ type, appContext }) {
const component = type; const component = type;
if (!component.__styles) { if (!component.__styles) {
// nvue 和 vue 混合开发时,__globalStyles注入的是未处理过的
if (appContext && isArray(appContext.provides.__globalStyles)) {
appContext.provides.__globalStyles = useCssStyles(appContext.provides.__globalStyles);
}
if (component.mpType === 'page' && appContext) { if (component.mpType === 'page' && appContext) {
// 如果是页面组件,则直接使用全局样式 // 如果是页面组件,则直接使用全局样式
component.__styles = appContext.provides.__globalStyles; component.__styles = appContext.provides.__globalStyles;
......
...@@ -207,6 +207,7 @@ importers: ...@@ -207,6 +207,7 @@ importers:
'@types/fs-extra': ^9.0.13 '@types/fs-extra': ^9.0.13
'@vitejs/plugin-vue': ^2.2.2 '@vitejs/plugin-vue': ^2.2.2
'@vue/compiler-core': 3.2.31 '@vue/compiler-core': 3.2.31
'@vue/compiler-dom': 3.2.31
'@vue/compiler-sfc': 3.2.31 '@vue/compiler-sfc': 3.2.31
debug: ^4.3.3 debug: ^4.3.3
esbuild: ^0.14.14 esbuild: ^0.14.14
...@@ -221,6 +222,7 @@ importers: ...@@ -221,6 +222,7 @@ importers:
'@dcloudio/uni-shared': link:../uni-shared '@dcloudio/uni-shared': link:../uni-shared
'@rollup/pluginutils': 4.1.2 '@rollup/pluginutils': 4.1.2
'@vitejs/plugin-vue': 2.2.2_vite@2.8.4+vue@3.2.31 '@vitejs/plugin-vue': 2.2.2_vite@2.8.4+vue@3.2.31
'@vue/compiler-dom': 3.2.31
'@vue/compiler-sfc': 3.2.31 '@vue/compiler-sfc': 3.2.31
debug: 4.3.3 debug: 4.3.3
fs-extra: 10.0.0 fs-extra: 10.0.0
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册