Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/jsx-compiler/src/modules/__tests__/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,4 +526,34 @@ describe('Transform JSXElement', () => {
}).toThrowError();
});
});

it('should transform events in native components (defined in package.json -> miniappConfig) in wechat miniprogram', () => {
const ast = parseExpression(`
<custom-element
onClick={onClick}
onChange={onChange}
/>
`);
ast.openingElement.name.isCustom = true;
ast.openingElement.name.isNative = true;
_transform({
templateAST: ast
}, wxAdapter);
expect(genInlineCode(ast).code).toEqual('<custom-element bindonClick="_e0" bindonChange="_e1" />');
});

it('shouldn\'t transform events in non-native components (defined in package.json -> miniappConfig) in wechat miniprogram', () => {
const ast = parseExpression(`
<custom-element
onClick={onClick}
onChange={onChange}
/>
`);
ast.openingElement.name.isCustom = true;
ast.openingElement.name.isNative = false;
_transform({
templateAST: ast
}, wxAdapter);
expect(genInlineCode(ast).code).toEqual('<custom-element onClick="_e0" onChange="_e1" />');
});
});
9 changes: 8 additions & 1 deletion packages/jsx-compiler/src/modules/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ function transformIdentifierComponentName(path, alias, dynamicValue, parsed, opt
node.isCustomEl = alias.isCustomEl;
node.name.isCustom = true;

if (!getCompiledComponents(options.adapter.platform)[componentTag]) {
const platform = options.adapter.platform;
if (!getCompiledComponents(platform)[componentTag]) {
// <tag __tagId="tagId" />

let tagId;
Expand Down Expand Up @@ -149,6 +150,12 @@ function transformIdentifierComponentName(path, alias, dynamicValue, parsed, opt
importedComponent.isFromComponentLibrary = true;
});
}

/**
* Judge whether the component has native compiled component
*/
if (pkg && pkg.miniappConfig && pkg.miniappConfig[`main:${platform}`]) node.name.isNative = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

支付宝小程序的字段应该直接是 main

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外这句是不是直接写成赋值语句就好了

else node.isNative = false;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/jsx-compiler/src/modules/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const isSlotScopeNode = require('../utils/isSlotScopeNode');
const { isDirectiveAttr, isEventHandlerAttr, isRenderPropsAttr, BINDING_REG } = require('../utils/checkAttr');
const handleValidIdentifier = require('../utils/handleValidIdentifier');
const isNativeComponent = require('../utils/isNativeComponent');
const isBaseComponent = require('../utils/isBaseComponent');
const isDerivedFromProps = require('../utils/isDerivedFromProps');
const { componentCommonProps } = require('../adapter');

Expand Down Expand Up @@ -541,7 +542,7 @@ function transformTemplate(
attr.name.name = attr.name.name.replace('on', 'bind').toLowerCase();
}
});
} else if (adapter.needTransformEvent && baseComponents.indexOf(name) > -1) {
} else if (adapter.needTransformEvent && isBaseComponent(componentTagNode)) {
// Rax base component should add bind before onXXX
// While events in custom component should not be changed
node.attributes.forEach(attr => {
Expand Down
10 changes: 10 additions & 0 deletions packages/jsx-compiler/src/utils/isBaseComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const baseComponents = require('../baseComponents');

/**
* Judge whether node is base component
* @param {Object} node
*/
module.exports = function isBaseComponent(node) {
// Rax base components and native base components are recognized as base components
return baseComponents.indexOf(node.name) > -1 || node && node.isNative;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

前面已经用到 node.name 了,这里不需要 node && node.isNative

};