Skip to content

Commit

Permalink
fix(compiler): component/element resolution at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
rigor789 committed Mar 24, 2020
1 parent 578c3ed commit 49b6f93
Show file tree
Hide file tree
Showing 10 changed files with 407 additions and 134 deletions.
6 changes: 5 additions & 1 deletion apps/test/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import {
onUnmounted,
onMounted,
ActionBar,
defineComponent
defineComponent,
compile
} from 'nativescript-vue'

const testComp = defineComponent({
template: `<StackLayout v-bind="$attrs">
<Button text="Hello Button" />
<Label text="Compiled label from template" />
<Label text="Another Compiled label from template" />
</StackLayout>`
})

console.log(compile('<StackLayout/>').toString())

function dumpViewTree(root) {
const mapNode = node => {
return {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@rollup/plugin-commonjs": "11.0.2",
"@types/jest": "^24.0.21",
"@types/puppeteer": "^2.0.0",
"@vue/compiler-sfc": "^3.0.0-alpha.9",
"@vue/compiler-sfc": "^3.0.0-alpha.10",
"brotli": "^1.3.2",
"chalk": "^2.4.2",
"conventional-changelog-cli": "^2.0.31",
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"homepage": "https://github.com/nativescript-vue/nativescript-vue/tree/dev/packages/compiler#readme",
"dependencies": {
"@vue/shared": "3.0.0-alpha.9",
"@vue/compiler-core": "3.0.0-alpha.9"
"@vue/shared": "3.0.0-alpha.10",
"@vue/compiler-core": "3.0.0-alpha.10"
}
}
9 changes: 1 addition & 8 deletions packages/compiler/src/parserOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ const isBuiltInComponent = (tag: string): symbol | undefined => {
export const parserOptions: ParserOptions = {
// We don't have void tags in NativeScript (<br>, <hr> etc.)
isVoidTag: tag => false,
isNativeTag: tag => {
if (isBuiltInComponent(tag)) {
return false
}
// in case we encounter an element that is not a built-in component
// we will assume it's a plugin provided element
return true
},
isNativeTag: tag => false,
isPreTag: tag => tag === 'pre',
isBuiltInComponent,
// todo: we might add support for different namespaces in the future
Expand Down
2 changes: 1 addition & 1 deletion packages/nativescript-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"homepage": "https://github.com/nativescript-vue/nativescript-vue/tree/dev/packages/nativescript-vue#readme",
"dependencies": {
"@vue/shared": "3.0.0-alpha.9",
"@vue/shared": "3.0.0-alpha.10",
"@nativescript-vue/compiler": "3.0.0-dev",
"@nativescript-vue/runtime": "3.0.0-dev"
}
Expand Down
4 changes: 4 additions & 0 deletions packages/nativescript-vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CompilerError
} from '@nativescript-vue/compiler'
import {
isKnownView,
registerRuntimeCompiler,
RenderFunction,
warn
Expand All @@ -31,6 +32,9 @@ function compileToFunction(
}

const { code } = compile(template, {
// at runtime - the registry should know about all elements, so we override the
// isNativeTag function from our standalone compiler
isNativeTag: tag => isKnownView(tag),
hoistStatic: true,
onError(err: CompilerError) {
// todo
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
"homepage": "https://github.com/nativescript-vue/nativescript-vue/tree/dev/packages/runtime#readme",
"dependencies": {
"@nativescript/core": "6.5.0",
"@vue/shared": "3.0.0-alpha.9",
"@vue/reactivity": "3.0.0-alpha.9",
"@vue/runtime-core": "3.0.0-alpha.9",
"@vue/shared": "3.0.0-alpha.10",
"@vue/reactivity": "3.0.0-alpha.10",
"@vue/runtime-core": "3.0.0-alpha.10",
"set-value": "3.0.1",
"unset-value": "1.0.0"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { Application } from '@nativescript/core'
import { nodeOps } from './nodeOps'
import { patchProp } from './patchProp'
import { INSVElement, INSVNode } from './nodes'
import { INSVElement } from './nodes'
import './registry'

const rendererOptions = {
Expand Down Expand Up @@ -39,7 +39,7 @@ function runApp(root: ComponentPublicInstance): ComponentPublicInstance {

export const render = ((...args) => {
ensureRenderer().render(...args)
}) as RootRenderFunction<INSVNode, INSVElement>
}) as RootRenderFunction<INSVElement>

export const createApp = ((...args) => {
const app = ensureRenderer().createApp(...args)
Expand All @@ -55,6 +55,7 @@ export const createApp = ((...args) => {
export * from './nodeOps'
export * from './runtimeHelpers'
export * from './registry'
export { resolveComponent } from './resolveAssets'

// Runtime components
export { ActionBar } from './components/ActionBar'
Expand Down
18 changes: 18 additions & 0 deletions packages/runtime/src/resolveAssets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
Component,
resolveComponent as _resolveComponent
} from '@vue/runtime-core'
import { isKnownView } from '.'

export function resolveComponent(name: string): Component | string | undefined {
// in the standalone compiler, everything is treated as a component because we don't
// know if certain tags are elements or not at runtime
// (they are only registered at runtime with registerElement)
// if we return a string here, vue will render them as normal elements
// with the default slot as children
if (isKnownView(name)) {
return name
}

return _resolveComponent(name)
}
Loading

0 comments on commit 49b6f93

Please sign in to comment.