Skip to content

Commit ec2bcf8

Browse files
committed
weex rax interop prototype
1 parent bb80d59 commit ec2bcf8

File tree

4 files changed

+90
-4
lines changed

4 files changed

+90
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"nightwatch": "^0.9.9",
101101
"nightwatch-helpers": "^1.2.0",
102102
"phantomjs-prebuilt": "^2.1.1",
103+
"rax": "^0.1.2",
103104
"rollup": "^0.36.1",
104105
"rollup-plugin-alias": "^1.2.0",
105106
"rollup-plugin-babel": "^2.4.0",

src/platforms/weex/framework.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import Vue from 'weex/runtime/index'
2-
import renderer from 'weex/runtime/config'
1+
import Vue from './runtime/index'
2+
import renderer from './runtime/config'
3+
import genRaxRequire from './rax-interop/require'
4+
import genRaxWrapper from './rax-interop/wrap'
35

46
Vue.weexVersion = '__WEEX_VERSION__'
57
export { Vue }
@@ -107,11 +109,16 @@ export function createInstance (
107109
subVue[name] = Vue[name]
108110
})
109111

110-
// The function which create a closure the JS Bundle will run in.
111-
// It will declare some instance variables like `Vue`, HTML5 Timer APIs etc.
112+
// rax interop for require('rax')
113+
// NOTE: need to config rax as external in Webpack
114+
const raxRequire = genRaxRequire(document)
115+
// expose wrapRaxComponent
116+
subVue.wrapRaxComponent = genRaxWrapper(raxRequire('rax'))
117+
112118
const instanceVars = Object.assign({
113119
Vue: subVue,
114120
weex: weexInstanceVar,
121+
require: raxRequire,
115122
__weex_require_module__: weexInstanceVar.requireModule // deprecated
116123
}, timerAPIs)
117124
callFunction(instanceVars, appCode)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const raxFactory = require('rax/dist/rax.factory')
2+
3+
export default function genRaxRequire (document) {
4+
const context = {
5+
document,
6+
__weex_document__: document
7+
}
8+
9+
const modules = {
10+
rax: {
11+
factory: raxFactory.bind(context),
12+
module: { exports: {}},
13+
isInitialized: false
14+
}
15+
}
16+
17+
return function require (name) {
18+
var mod = modules[name]
19+
20+
if (mod && mod.isInitialized) {
21+
return mod.module.exports
22+
}
23+
24+
if (!mod) {
25+
throw new Error(
26+
'Requiring unknown module "' + name + '"'
27+
)
28+
}
29+
30+
if (mod.hasError) {
31+
throw new Error(
32+
'Requiring module "' + name + '" which threw an exception'
33+
)
34+
}
35+
36+
try {
37+
mod.isInitialized = true
38+
mod.factory(require, mod.module.exports, mod.module)
39+
} catch (e) {
40+
mod.hasError = true
41+
mod.isInitialized = false
42+
throw e
43+
}
44+
45+
return mod.module.exports
46+
}
47+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default function genRaxWrapper (Rax) {
2+
const { render, createElement, unmountComponentAtNode } = Rax
3+
4+
return function wrapRaxComponent (RaxComponent) {
5+
return {
6+
created () {
7+
const update = this._updateFromParent
8+
this._updateFromParent = function () {
9+
update.apply(this, arguments)
10+
this.renderRaxComponent()
11+
}
12+
},
13+
render (h) {
14+
return h('div')
15+
},
16+
mounted () {
17+
this.renderRaxComponent()
18+
},
19+
beforeDestroy () {
20+
unmountComponentAtNode(this.$el)
21+
},
22+
methods: {
23+
renderRaxComponent () {
24+
const data = this.$vnode.data
25+
const props = Object.assign({}, data.attrs, data.domProps, data.props)
26+
render(createElement(RaxComponent, props), this.$el)
27+
}
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)