Skip to content

Commit

Permalink
remove deprecated api mpx.remove/$remove & fix and reformat parser tr…
Browse files Browse the repository at this point in the history
…ans handler
  • Loading branch information
hiyuki committed Mar 14, 2022
1 parent 6079aab commit c44b8e7
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 128 deletions.
22 changes: 0 additions & 22 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,6 @@ if (__mpx_mode__ === 'web') {
watch = vm.$watch.bind(vm)
const set = Vue.set.bind(Vue)
const del = Vue.delete.bind(Vue)
const remove = function (...args) {
if (process.env.NODE_ENV !== 'production') {
error('$remove will be removed in next minor version, please use $delete instead!', this.$rawOptions && this.$rawOptions.mpxFileResource)
}
return del.apply(this, args)
}
// todo 补齐web必要api
APIs = {
createApp,
createPage,
Expand All @@ -124,34 +117,21 @@ if (__mpx_mode__ === 'web') {
watch,
use,
set,
remove,
delete: del,
getMixin,
implement
}

InstanceAPIs = {
$remove: remove
}
} else {
observable = function (obj) {
observe(obj)
return obj
}

const vm = {}

watch = function (expOrFn, cb, options) {
return watchWithVm(vm, expOrFn, cb, options)
}

const remove = function (...args) {
if (process.env.NODE_ENV !== 'production') {
error('$remove will be removed in next minor version, please use $delete instead!', this.$rawOptions && this.$rawOptions.mpxFileResource)
}
return del.apply(this, args)
}

APIs = {
createApp,
createPage,
Expand All @@ -165,15 +145,13 @@ if (__mpx_mode__ === 'web') {
watch,
use,
set,
remove,
delete: del,
getMixin,
implement
}

InstanceAPIs = {
$set: set,
$remove: remove,
$delete: del
}
}
Expand Down
201 changes: 95 additions & 106 deletions packages/webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,46 +969,6 @@ class MpxWebpackPlugin {
stage: -1000
}, (expr, calleeMembers, callExpr) => requireAsyncHandler(callExpr, calleeMembers))

const transHandler = (expr) => {
const module = parser.state.module
const current = parser.state.current
const { queryObj, resourcePath } = parseRequest(module.resource)
const localSrcMode = queryObj.mode
const globalSrcMode = mpx.srcMode
const srcMode = localSrcMode || globalSrcMode
const mode = mpx.mode

let target

if (expr.type === 'Identifier') {
target = expr
} else if (expr.type === 'MemberExpression') {
target = expr.object
}
if (!matchCondition(resourcePath, this.options.transMpxRules) || resourcePath.indexOf('@mpxjs') !== -1 || !target || mode === srcMode) {
return
}

const type = target.name

const name = type === 'wx' ? 'mpx' : 'createFactory'
const replaceContent = type === 'wx' ? 'mpx' : `createFactory(${JSON.stringify(type)})`

const dep = new ReplaceDependency(replaceContent, target.range)
current.addPresentationalDependency(dep)

let needInject = true
for (let dep of module.dependencies) {
if (dep instanceof CommonJsVariableDependency && dep.name === name) {
needInject = false
break
}
}
if (needInject) {
const dep = new CommonJsVariableDependency(`@mpxjs/core/src/runtime/${name}`, name)
module.addDependency(dep)
}
}
// hack babel polyfill global
parser.hooks.statementIf.tap('MpxWebpackPlugin', (expr) => {
if (/core-js.+microtask/.test(parser.state.module.resource)) {
Expand Down Expand Up @@ -1043,90 +1003,119 @@ class MpxWebpackPlugin {
}
})

// 处理跨平台转换
if (mpx.srcMode !== mpx.mode) {
// 全量替换未声明的wx identifier
parser.hooks.expression.for('wx').tap('MpxWebpackPlugin', transHandler)

// parser.hooks.evaluate.for('MemberExpression').tap('MpxWebpackPlugin', (expr) => {
// // Undeclared varible for wx[identifier]()
// // TODO Unable to handle wx[identifier]
// if (expr.object.name === 'wx' && !parser.scope.definitions.has('wx')) {
// transHandler(expr)
// }
// })
// // Trans for wx.xx, wx['xx'], wx.xx(), wx['xx']()
// parser.hooks.expressionMemberChain.for('wx').tap('MpxWebpackPlugin', transHandler)
// 处理跨平台全局对象转换
const transGlobalObject = (expr) => {
const module = parser.state.module
const current = parser.state.current
const { queryObj, resourcePath } = parseRequest(module.resource)
const localSrcMode = queryObj.mode
const globalSrcMode = mpx.srcMode
const srcMode = localSrcMode || globalSrcMode
const mode = mpx.mode

let target
if (expr.type === 'Identifier') {
target = expr
} else if (expr.type === 'MemberExpression') {
target = expr.object
}

if (!matchCondition(resourcePath, this.options.transMpxRules) || resourcePath.indexOf('@mpxjs') !== -1 || !target || mode === srcMode) return

const type = target.name
const name = type === 'wx' ? 'mpx' : 'createFactory'
const replaceContent = type === 'wx' ? 'mpx' : `createFactory(${JSON.stringify(type)})`

const dep = new ReplaceDependency(replaceContent, target.range)
current.addPresentationalDependency(dep)

let needInject = true
for (let dep of module.dependencies) {
if (dep instanceof CommonJsVariableDependency && dep.name === name) {
needInject = false
break
}
}
if (needInject) {
const dep = new CommonJsVariableDependency(`@mpxjs/core/src/runtime/${name}`, name)
module.addDependency(dep)
}
}

// 转换wx全局对象
parser.hooks.expression.for('wx').tap('MpxWebpackPlugin', transGlobalObject)
// Proxy ctor for transMode
if (!this.options.forceDisableProxyCtor) {
parser.hooks.call.for('Page').tap('MpxWebpackPlugin', (expr) => {
transHandler(expr.callee)
transGlobalObject(expr.callee)
})
parser.hooks.call.for('Component').tap('MpxWebpackPlugin', (expr) => {
transHandler(expr.callee)
transGlobalObject(expr.callee)
})
parser.hooks.call.for('App').tap('MpxWebpackPlugin', (expr) => {
transHandler(expr.callee)
transGlobalObject(expr.callee)
})
if (mpx.mode === 'ali' || mpx.mode === 'web') {
// 支付宝和web不支持Behaviors
parser.hooks.call.for('Behavior').tap('MpxWebpackPlugin', (expr) => {
transHandler(expr.callee)
transGlobalObject(expr.callee)
})
}
}
}

const apiBlackListMap = [
'createApp',
'createPage',
'createComponent',
'createStore',
'createStoreWithThis',
'mixin',
'injectMixins',
'toPureObject',
'observable',
'watch',
'use',
'set',
'remove',
'delete: del',
'setConvertRule',
'getMixin',
'getComputed',
'implement'
].reduce((map, api) => {
map[api] = true
return map
}, {})

const handler = (expr) => {
const callee = expr.callee
const args = expr.arguments
const name = callee.object.name
const { queryObj, resourcePath } = parseRequest(parser.state.module.resource)
const localSrcMode = queryObj.mode
const globalSrcMode = mpx.srcMode
const srcMode = localSrcMode || globalSrcMode

if (srcMode === globalSrcMode || apiBlackListMap[callee.property.name || callee.property.value] || (name !== 'mpx' && name !== 'wx') || (name === 'wx' && !matchCondition(resourcePath, this.options.transMpxRules))) {
return
}
// 为跨平台api调用注入srcMode参数指导api运行时转换
const apiBlackListMap = [
'createApp',
'createPage',
'createComponent',
'createStore',
'createStoreWithThis',
'mixin',
'injectMixins',
'toPureObject',
'observable',
'watch',
'use',
'set',
'remove',
'delete',
'setConvertRule',
'getMixin',
'getComputed',
'implement'
].reduce((map, api) => {
map[api] = true
return map
}, {})

const srcModeString = `__mpx_src_mode_${srcMode}__`
const dep = new InjectDependency({
content: args.length
? `, ${JSON.stringify(srcModeString)}`
: JSON.stringify(srcModeString),
index: expr.end - 1
})
parser.state.current.addPresentationalDependency(dep)
}
const injectSrcModeForTransApi = (expr, members) => {
// members为空数组时,callee并不是memberExpression
if (!members.length) return
const callee = expr.callee
const args = expr.arguments
const name = callee.object.name
const { queryObj, resourcePath } = parseRequest(parser.state.module.resource)
const localSrcMode = queryObj.mode
const globalSrcMode = mpx.srcMode
const srcMode = localSrcMode || globalSrcMode

if (srcMode === globalSrcMode || apiBlackListMap[callee.property.name || callee.property.value] || (name !== 'mpx' && name !== 'wx') || (name === 'wx' && !matchCondition(resourcePath, this.options.transMpxRules))) return

const srcModeString = `__mpx_src_mode_${srcMode}__`
const dep = new InjectDependency({
content: args.length
? `, ${JSON.stringify(srcModeString)}`
: JSON.stringify(srcModeString),
index: expr.end - 1
})
parser.state.current.addPresentationalDependency(dep)
}

if (mpx.srcMode !== mpx.mode) {
parser.hooks.callMemberChain.for(harmonySpecifierTag).tap('MpxWebpackPlugin', handler)
parser.hooks.callMemberChain.for('mpx').tap('MpxWebpackPlugin', handler)
parser.hooks.callMemberChain.for('wx').tap('MpxWebpackPlugin', handler)
parser.hooks.callMemberChain.for(harmonySpecifierTag).tap('MpxWebpackPlugin', injectSrcModeForTransApi)
parser.hooks.callMemberChain.for('mpx').tap('MpxWebpackPlugin', injectSrcModeForTransApi)
parser.hooks.callMemberChain.for('wx').tap('MpxWebpackPlugin', injectSrcModeForTransApi)
}
})

Expand Down

0 comments on commit c44b8e7

Please sign in to comment.