-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(okam-build): add native swan component event handler adapter in …
…okam
- Loading branch information
Showing
7 changed files
with
213 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/okam-build/lib/build/init-native-swan-processor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @file Native swan support processor initialization | ||
* @author [email protected] | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const {registerProcessor} = require('../processor/type'); | ||
const adapterPlugin = require('../processor/js/plugins/babel-native-swan-plugin'); | ||
|
||
/** | ||
* Initialize native component js processor | ||
* | ||
* @inner | ||
* @param {Object=} opts the options to init | ||
* @param {string=} opts.processor the builtin processor name, by default `babel` | ||
* @param {Array=} opts.plugins the processor plugins | ||
*/ | ||
function initJsProcessor(opts) { | ||
let plugins = (opts && opts.plugins) || [adapterPlugin]; | ||
registerProcessor({ | ||
name: (opts && opts.processor) || 'babel', // override existed processor | ||
hook: { | ||
before(file, options) { | ||
if (file.isSwanCompScript && !adapterPlugin.isAdapterModule(file.path)) { | ||
options.plugins || (options.plugins = []); | ||
options.plugins.push.apply(options.plugins, plugins); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Init native transformation processors | ||
* | ||
* @param {Object=} options the initialization options | ||
* @param {Object=} options.js the component js processor init options | ||
*/ | ||
function initProcessor(options = {}) { | ||
let {js} = options; | ||
|
||
if (js !== false) { | ||
initJsProcessor(js); | ||
} | ||
} | ||
|
||
module.exports = initProcessor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/okam-build/lib/processor/js/plugins/babel-native-swan-plugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* @file Native swan component transformation plugin. | ||
* Fix component triggerEvent API to adapt the okam event handler. | ||
* @author [email protected] | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const {isVariableDefined, createImportDeclaration} = require('../transform/helper'); | ||
|
||
/** | ||
* The component polyfill module id | ||
* | ||
* @const | ||
* @type {string} | ||
*/ | ||
const COMPONENT_ADAPTER_MODULE_ID = 'okam-core/src/swan/adapter/component'; | ||
|
||
module.exports = exports = function ({types: t}) { | ||
return { | ||
visitor: { | ||
|
||
/** | ||
* Replace native component init | ||
* | ||
* @param {Object} path ast node path | ||
* @param {Object} state the processed node state | ||
*/ | ||
CallExpression(path, state) { | ||
if (path.isComponentAdapted) { | ||
return; | ||
} | ||
|
||
let {arguments: args, callee} = path.node; | ||
const calleeName = callee.name; | ||
if (!t.isIdentifier(callee) || args.length !== 1) { | ||
return; | ||
} | ||
|
||
if (calleeName === 'Component') { | ||
if (isVariableDefined(path, calleeName)) { | ||
return; | ||
} | ||
|
||
let rootPath = path.findParent(p => t.isProgram(p)); | ||
let bodyPath = rootPath.get('body.0'); | ||
|
||
// insert the component fix import statement | ||
const fixComponentFuncName = path.scope.generateUid('fixComponent'); | ||
bodyPath.insertBefore( | ||
createImportDeclaration( | ||
fixComponentFuncName, COMPONENT_ADAPTER_MODULE_ID, t | ||
) | ||
); | ||
|
||
path.replaceWith(t.callExpression( | ||
t.identifier(calleeName), | ||
[ | ||
t.callExpression( | ||
t.identifier(fixComponentFuncName), | ||
args | ||
) | ||
] | ||
)); | ||
|
||
// add flag to avoid repeat visiting | ||
path.isComponentAdapted = true; | ||
} | ||
}, | ||
|
||
/** | ||
* Replace `triggerEvent` API with custom `$emit` API | ||
* | ||
* @param {Object} path ast node path | ||
* @param {Object} state the processed node state | ||
*/ | ||
MemberExpression(path, state) { | ||
let {property} = path.node; | ||
if (t.isIdentifier(property) && property.name === 'triggerEvent') { | ||
property.name = '$emit'; | ||
return; | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
exports.adapterModuleId = COMPONENT_ADAPTER_MODULE_ID; | ||
|
||
exports.isAdapterModule = function (path) { | ||
return path.indexOf(COMPONENT_ADAPTER_MODULE_ID) !== -1; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters