Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
forked fromvuejs/vue

Commit2deda3d

Browse files
Hanks10100yyx990803
authored andcommitted
refactor(weex): Adjust the weex platform entry file to fit the new weex runtime (vuejs#6620)
* trim trailing whitespace* revert(weex): remove the new Function hack for V8, as Weex uses JSC nowRemove the `callFunctionNative` method and `compileBundle`, which is provided by modified V8. Inorder to maintain the consistency of the js engine, Weex also use JSC on Android. The legacy hackfor V8 engine should be removed.* refactor(weex): move module and component apis to weex runtimeModules and components should be registered in weex runtime, not the specific framework. The`registerModules`, `registerComponents`, `weex.supports` and `weex.requireModule` api is moved toweex runtime, which is in the "apache/incubator-weex" repo.* test(weex): refactor the test cases to fit the new weex-js-runtime* fix(weex): use document instead of renderer to create Element
1 parent4441e0f commit2deda3d

File tree

13 files changed

+525
-1318
lines changed

13 files changed

+525
-1318
lines changed

‎build/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const banner =
1717

1818
constweexFactoryPlugin={
1919
intro(){
20-
return'module.exports = function weexFactory (exports,renderer) {'
20+
return'module.exports = function weexFactory (exports,document) {'
2121
},
2222
outro(){
2323
return'}'

‎package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@
122122
"typescript":"^2.3.4",
123123
"uglify-js":"^3.0.15",
124124
"webpack":"^2.6.1",
125-
"weex-js-runtime":"^0.20.5",
126-
"weex-vdom-tester":"^0.2.0"
125+
"weex-js-runtime":"^0.22.0"
127126
},
128127
"config": {
129128
"commitizen": {

‎src/platforms/weex/entry-framework.js

Lines changed: 16 additions & 216 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,19 @@
1-
importTextNodefrom'weex/runtime/text-node'
2-
31
// this will be preserved during build
42
constVueFactory=require('./factory')
53

64
constinstances={}
7-
constmodules={}
8-
constcomponents={}
9-
10-
constrenderer={
11-
TextNode,
12-
instances,
13-
modules,
14-
components
15-
}
165

176
/**
18-
* Prepare framework config, basically about the virtual-DOM and JS bridge.
19-
*@param {object} cfg
7+
* Prepare framework config.
8+
*Nothing need to do actually, just an interface provided to weex runtime.
209
*/
21-
exportfunctioninit(cfg){
22-
renderer.Document=cfg.Document
23-
renderer.Element=cfg.Element
24-
renderer.Comment=cfg.Comment
25-
renderer.compileBundle=cfg.compileBundle
26-
}
10+
exportfunctioninit(){}
2711

2812
/**
2913
* Reset framework config and clear all registrations.
3014
*/
3115
exportfunctionreset(){
3216
clear(instances)
33-
clear(modules)
34-
clear(components)
35-
deleterenderer.Document
36-
deleterenderer.Element
37-
deleterenderer.Comment
38-
deleterenderer.compileBundle
3917
}
4018

4119
/**
@@ -63,47 +41,31 @@ export function createInstance (
6341
data,
6442
env={}
6543
){
66-
// Virtual-DOM object.
67-
constdocument=newrenderer.Document(instanceId,config.bundleUrl)
68-
44+
constweex=env.weex
45+
constdocument=weex.document
6946
constinstance=instances[instanceId]={
7047
instanceId, config, data,
7148
document
7249
}
7350

74-
// Prepare native module getter and HTML5 Timer APIs.
75-
constmoduleGetter=genModuleGetter(instanceId)
76-
consttimerAPIs=getInstanceTimer(instanceId,moduleGetter)
77-
78-
// Prepare `weex` instance variable.
79-
constweexInstanceVar={
80-
config,
81-
document,
82-
supports,
83-
requireModule:moduleGetter
84-
}
85-
Object.freeze(weexInstanceVar)
51+
consttimerAPIs=getInstanceTimer(instanceId,weex.requireModule)
8652

8753
// Each instance has a independent `Vue` module instance
88-
constVue=instance.Vue=createVueModuleInstance(instanceId,moduleGetter)
54+
constVue=instance.Vue=createVueModuleInstance(instanceId,weex)
8955

9056
// The function which create a closure the JS Bundle will run in.
9157
// It will declare some instance variables like `Vue`, HTML5 Timer APIs etc.
9258
constinstanceVars=Object.assign({
9359
Vue,
94-
weex:weexInstanceVar
60+
weex
9561
},timerAPIs,env.services)
9662

9763
appCode=`(function(global){ \n${appCode}\n })(Object.create(this))`
9864

99-
if(!callFunctionNative(instanceVars,appCode)){
100-
// If failed to compile functionBody on native side,
101-
// fallback to 'callFunction()'.
102-
callFunction(instanceVars,appCode)
103-
}
65+
callFunction(instanceVars,appCode)
10466

10567
// Send `createFinish` signal to native.
106-
instance.document.taskCenter.send('dom',{action:'createFinish'},[])
68+
document.taskCenter.send('dom',{action:'createFinish'},[])
10769

10870
returninstance
10971
}
@@ -118,6 +80,8 @@ export function destroyInstance (instanceId) {
11880
if(instance&&instance.appinstanceofinstance.Vue){
11981
instance.document.destroy()
12082
instance.app.$destroy()
83+
deleteinstance.document
84+
deleteinstance.app
12185
}
12286
deleteinstances[instanceId]
12387
}
@@ -200,91 +164,12 @@ export function receiveTasks (id, tasks) {
200164
returnnewError(`invalid instance id "${id}" or tasks`)
201165
}
202166

203-
/**
204-
* Register native modules information.
205-
*@param {object} newModules
206-
*/
207-
exportfunctionregisterModules(newModules){
208-
for(constnameinnewModules){
209-
if(!modules[name]){
210-
modules[name]={}
211-
}
212-
newModules[name].forEach(method=>{
213-
if(typeofmethod==='string'){
214-
modules[name][method]=true
215-
}else{
216-
modules[name][method.name]=method.args
217-
}
218-
})
219-
}
220-
}
221-
222-
/**
223-
* Check whether the module or the method has been registered.
224-
*@param {String} module name
225-
*@param {String} method name (optional)
226-
*/
227-
exportfunctionisRegisteredModule(name,method){
228-
if(typeofmethod==='string'){
229-
return!!(modules[name]&&modules[name][method])
230-
}
231-
return!!modules[name]
232-
}
233-
234-
/**
235-
* Register native components information.
236-
*@param {array} newComponents
237-
*/
238-
exportfunctionregisterComponents(newComponents){
239-
if(Array.isArray(newComponents)){
240-
newComponents.forEach(component=>{
241-
if(!component){
242-
return
243-
}
244-
if(typeofcomponent==='string'){
245-
components[component]=true
246-
}elseif(typeofcomponent==='object'&&typeofcomponent.type==='string'){
247-
components[component.type]=component
248-
}
249-
})
250-
}
251-
}
252-
253-
/**
254-
* Check whether the component has been registered.
255-
*@param {String} component name
256-
*/
257-
exportfunctionisRegisteredComponent(name){
258-
return!!components[name]
259-
}
260-
261-
/**
262-
* Detects whether Weex supports specific features.
263-
*@param {String} condition
264-
*/
265-
exportfunctionsupports(condition){
266-
if(typeofcondition!=='string')returnnull
267-
268-
constres=condition.match(/^@(\w+)\/(\w+)(\.(\w+))?$/i)
269-
if(res){
270-
consttype=res[1]
271-
constname=res[2]
272-
constmethod=res[4]
273-
switch(type){
274-
case'module':returnisRegisteredModule(name,method)
275-
case'component':returnisRegisteredComponent(name)
276-
}
277-
}
278-
279-
returnnull
280-
}
281-
282167
/**
283168
* Create a fresh instance of Vue for each Weex instance.
284169
*/
285-
functioncreateVueModuleInstance(instanceId,moduleGetter){
170+
functioncreateVueModuleInstance(instanceId,weex){
286171
constexports={}
287-
VueFactory(exports,renderer)
172+
VueFactory(exports,weex.document)
288173
constVue=exports.Vue
289174

290175
constinstance=instances[instanceId]
@@ -295,7 +180,7 @@ function createVueModuleInstance (instanceId, moduleGetter) {
295180
constisReservedTag=Vue.config.isReservedTag||(()=>false)
296181
constisRuntimeComponent=Vue.config.isRuntimeComponent||(()=>false)
297182
Vue.config.isReservedTag=name=>{
298-
return(!isRuntimeComponent(name)&&components[name])||
183+
return(!isRuntimeComponent(name)&&weex.supports(`@component/${name}`))||
299184
isReservedTag(name)||
300185
weexRegex.test(name)
301186
}
@@ -307,7 +192,7 @@ function createVueModuleInstance (instanceId, moduleGetter) {
307192

308193
// expose weex native module getter on subVue prototype so that
309194
// vdom runtime modules can access native modules via vnode.context
310-
Vue.prototype.$requireWeexModule=moduleGetter
195+
Vue.prototype.$requireWeexModule=weex.requireModule
311196

312197
// Hack `Vue` behavior to handle instance information and data
313198
// before root component created.
@@ -340,39 +225,6 @@ function createVueModuleInstance (instanceId, moduleGetter) {
340225
returnVue
341226
}
342227

343-
/**
344-
* Generate native module getter. Each native module has several
345-
* methods to call. And all the behaviors is instance-related. So
346-
* this getter will return a set of methods which additionally
347-
* send current instance id to native when called.
348-
*@param {string} instanceId
349-
*@return {function}
350-
*/
351-
functiongenModuleGetter(instanceId){
352-
constinstance=instances[instanceId]
353-
returnfunction(name){
354-
constnativeModule=modules[name]||[]
355-
constoutput={}
356-
for(constmethodNameinnativeModule){
357-
Object.defineProperty(output,methodName,{
358-
enumerable:true,
359-
configurable:true,
360-
get:functionproxyGetter(){
361-
return(...args)=>{
362-
returninstance.document.taskCenter.send('module',{module:name,method:methodName},args)
363-
}
364-
},
365-
set:functionproxySetter(val){
366-
if(typeofval==='function'){
367-
returninstance.document.taskCenter.send('module',{module:name,method:methodName},[val])
368-
}
369-
}
370-
})
371-
}
372-
returnoutput
373-
}
374-
}
375-
376228
/**
377229
* Generate HTML5 Timer APIs. An important point is that the callback
378230
* will be converted into callback id when sent to native. So the
@@ -430,55 +282,3 @@ function callFunction (globalObjects, body) {
430282
constresult=newFunction(...globalKeys)
431283
returnresult(...globalValues)
432284
}
433-
434-
/**
435-
* Call a new function generated on the V8 native side.
436-
*
437-
* This function helps speed up bundle compiling. Normally, the V8
438-
* engine needs to download, parse, and compile a bundle on every
439-
* visit. If 'compileBundle()' is available on native side,
440-
* the downloading, parsing, and compiling steps would be skipped.
441-
*@param {object} globalObjects
442-
*@param {string} body
443-
*@return {boolean}
444-
*/
445-
functioncallFunctionNative(globalObjects,body){
446-
if(typeofrenderer.compileBundle!=='function'){
447-
returnfalse
448-
}
449-
450-
letfn=void0
451-
letisNativeCompileOk=false
452-
letscript='(function ('
453-
constglobalKeys=[]
454-
constglobalValues=[]
455-
for(constkeyinglobalObjects){
456-
globalKeys.push(key)
457-
globalValues.push(globalObjects[key])
458-
}
459-
for(leti=0;i<globalKeys.length-1;++i){
460-
script+=globalKeys[i]
461-
script+=','
462-
}
463-
script+=globalKeys[globalKeys.length-1]
464-
script+=') {'
465-
script+=body
466-
script+='} )'
467-
468-
try{
469-
constweex=globalObjects.weex||{}
470-
constconfig=weex.config||{}
471-
fn=renderer.compileBundle(script,
472-
config.bundleUrl,
473-
config.bundleDigest,
474-
config.codeCachePath)
475-
if(fn&&typeoffn==='function'){
476-
fn(...globalValues)
477-
isNativeCompileOk=true
478-
}
479-
}catch(e){
480-
console.error(e)
481-
}
482-
483-
returnisNativeCompileOk
484-
}

‎src/platforms/weex/runtime/node-ops.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
/* globals renderer */
2-
// renderer is injected by weex factory wrapper
1+
/* globals document */
2+
// document is injected by weex factory wrapper
3+
4+
importTextNodefrom'weex/runtime/text-node'
35

46
exportconstnamespaceMap={}
57

68
exportfunctioncreateElement(tagName){
7-
returnnewrenderer.Element(tagName)
9+
returndocument.createElement(tagName)
810
}
911

1012
exportfunctioncreateElementNS(namespace,tagName){
11-
returnnewrenderer.Element(namespace+':'+tagName)
13+
returndocument.createElement(namespace+':'+tagName)
1214
}
1315

1416
exportfunctioncreateTextNode(text){
15-
returnnewrenderer.TextNode(text)
17+
returnnewTextNode(text)
1618
}
1719

1820
exportfunctioncreateComment(text){
19-
returnnewrenderer.Comment(text)
21+
returndocument.createComment(text)
2022
}
2123

2224
exportfunctioninsertBefore(node,target,before){

‎src/platforms/weex/util/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* globalsrenderer */
1+
/* globalsdocument */
22

33
import{makeMap}from'shared/util'
44

@@ -34,8 +34,8 @@ export function getTagNamespace () { /* console.log('getTagNamespace') */ }
3434
exportfunctionisUnknownElement(){/* console.log('isUnknownElement') */}
3535

3636
exportfunctionquery(el,document){
37-
//renderer is injected by weex factory wrapper
38-
constplaceholder=newrenderer.Comment('root')
37+
//document is injected by weex factory wrapper
38+
constplaceholder=document.createComment('root')
3939
placeholder.hasAttribute=placeholder.removeAttribute=function(){}// hack for patch
4040
document.documentElement.appendChild(placeholder)
4141
returnplaceholder

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp