From e70b5bb57260dacb07557534729573fe800ee9ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E6=88=98?= Date: Mon, 27 Sep 2021 12:06:00 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E6=94=AF=E6=8C=81if=20else=E5=90=8E?= =?UTF-8?q?=E9=9D=A2=E5=8F=AA=E6=9C=89=E4=B8=80=E6=9D=A1=E8=AF=AD=E5=8F=A5?= =?UTF-8?q?=E3=80=81=E6=B2=A1=E6=9C=89=E5=A4=A7=E6=8B=AC=E5=8F=B7=E7=9A=84?= =?UTF-8?q?=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/jsx-compiler/src/modules/condition.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jsx-compiler/src/modules/condition.js b/packages/jsx-compiler/src/modules/condition.js index 807002c7..46fa7f5d 100644 --- a/packages/jsx-compiler/src/modules/condition.js +++ b/packages/jsx-compiler/src/modules/condition.js @@ -44,8 +44,8 @@ function transformRenderFunction(renderPath, adapter) { } if (!alternatePath.isIfStatement() && alternatePath.node) { - const alternateBodyPath = alternatePath.get('body'); - if (alternateBodyPath) { + if (alternatePath.isBlockStatement()) { + const alternateBodyPath = alternatePath.get('body'); alternateBodyPath.map((statementPath) => { handleAlternate( statementPath.get('expression'), @@ -322,7 +322,7 @@ function handleConsequent(path, expressionPath, templateMap, renderScope, adapte // Remove only if the expression contains JSX expressionPath.remove(); } - } + } else handleAlternate(expressionPath, templateMap, adapter) } } From 1f5473d7b85a99ed7f86a951b8ffc0249f68d45c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E6=88=98?= Date: Mon, 27 Sep 2021 16:42:18 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0test=20case?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/modules/__tests__/condition.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/packages/jsx-compiler/src/modules/__tests__/condition.js b/packages/jsx-compiler/src/modules/__tests__/condition.js index 7b182b68..aea5c24a 100644 --- a/packages/jsx-compiler/src/modules/__tests__/condition.js +++ b/packages/jsx-compiler/src/modules/__tests__/condition.js @@ -238,6 +238,36 @@ describe('Transiform condition render function', () => { setState(a); }, []); return {a}; +}`); + }); + + it('statement without square bracket in alternate', () => { + const ast = parseExpression(`(function render(props) { + const [loggined, setLoggined] = useState(false); + useEffect(() => { + const { isLogin } = app; + if (isLogin) { + setLoggined(true); + } else setLoggined(false); + }, []) + return {loggined}; + }) + `); + + const tmpVars = _transformRenderFunction(ast, adapter); + expect(genExpression(tmpVars.vdom)).toEqual(''); + expect(genExpression(ast)).toEqual(`function render(props) { + const [loggined, setLoggined] = useState(false); + useEffect(() => { + const { + isLogin + } = app; + + if (isLogin) { + setLoggined(true); + } else setLoggined(false); + }, []); + return {loggined}; }`); }); }); From b7af9dc092cab5afcd0f42d03886f55fe1bd3b35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E6=88=98?= Date: Wed, 24 Nov 2021 14:11:32 +0800 Subject: [PATCH 3/8] support native components event bind --- .../src/modules/__tests__/element.js | 30 +++++++++++++++++++ .../jsx-compiler/src/modules/components.js | 9 +++++- packages/jsx-compiler/src/modules/element.js | 3 +- .../jsx-compiler/src/utils/isBaseComponent.js | 10 +++++++ 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 packages/jsx-compiler/src/utils/isBaseComponent.js diff --git a/packages/jsx-compiler/src/modules/__tests__/element.js b/packages/jsx-compiler/src/modules/__tests__/element.js index 167a46fa..b8eb39d0 100644 --- a/packages/jsx-compiler/src/modules/__tests__/element.js +++ b/packages/jsx-compiler/src/modules/__tests__/element.js @@ -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(` + + `); + ast.openingElement.name.isCustom = true; + ast.openingElement.name.isNative = true; + _transform({ + templateAST: ast + }, wxAdapter); + expect(genInlineCode(ast).code).toEqual(''); + }); + + it('shouldn\'t transform events in non-native components (defined in package.json -> miniappConfig) in wechat miniprogram', () => { + const ast = parseExpression(` + + `); + ast.openingElement.name.isCustom = true; + ast.openingElement.name.isNative = false; + _transform({ + templateAST: ast + }, wxAdapter); + expect(genInlineCode(ast).code).toEqual(''); + }); }); diff --git a/packages/jsx-compiler/src/modules/components.js b/packages/jsx-compiler/src/modules/components.js index a7406c19..5087a363 100644 --- a/packages/jsx-compiler/src/modules/components.js +++ b/packages/jsx-compiler/src/modules/components.js @@ -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]) { // let tagId; @@ -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; + else node.isNative = false; } } } diff --git a/packages/jsx-compiler/src/modules/element.js b/packages/jsx-compiler/src/modules/element.js index c9def662..ce4c1698 100644 --- a/packages/jsx-compiler/src/modules/element.js +++ b/packages/jsx-compiler/src/modules/element.js @@ -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'); @@ -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 => { diff --git a/packages/jsx-compiler/src/utils/isBaseComponent.js b/packages/jsx-compiler/src/utils/isBaseComponent.js new file mode 100644 index 00000000..179f3d6a --- /dev/null +++ b/packages/jsx-compiler/src/utils/isBaseComponent.js @@ -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; +}; \ No newline at end of file From b6100f3313d610aa5df1f11908076e9ad2f0304f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E6=88=98?= Date: Wed, 24 Nov 2021 14:28:30 +0800 Subject: [PATCH 4/8] support native components event bind --- .../src/modules/__tests__/condition.js | 30 ------------------- .../jsx-compiler/src/modules/condition.js | 8 ++--- 2 files changed, 4 insertions(+), 34 deletions(-) diff --git a/packages/jsx-compiler/src/modules/__tests__/condition.js b/packages/jsx-compiler/src/modules/__tests__/condition.js index aea5c24a..7b182b68 100644 --- a/packages/jsx-compiler/src/modules/__tests__/condition.js +++ b/packages/jsx-compiler/src/modules/__tests__/condition.js @@ -238,36 +238,6 @@ describe('Transiform condition render function', () => { setState(a); }, []); return {a}; -}`); - }); - - it('statement without square bracket in alternate', () => { - const ast = parseExpression(`(function render(props) { - const [loggined, setLoggined] = useState(false); - useEffect(() => { - const { isLogin } = app; - if (isLogin) { - setLoggined(true); - } else setLoggined(false); - }, []) - return {loggined}; - }) - `); - - const tmpVars = _transformRenderFunction(ast, adapter); - expect(genExpression(tmpVars.vdom)).toEqual(''); - expect(genExpression(ast)).toEqual(`function render(props) { - const [loggined, setLoggined] = useState(false); - useEffect(() => { - const { - isLogin - } = app; - - if (isLogin) { - setLoggined(true); - } else setLoggined(false); - }, []); - return {loggined}; }`); }); }); diff --git a/packages/jsx-compiler/src/modules/condition.js b/packages/jsx-compiler/src/modules/condition.js index 46fa7f5d..8f071e64 100644 --- a/packages/jsx-compiler/src/modules/condition.js +++ b/packages/jsx-compiler/src/modules/condition.js @@ -44,8 +44,8 @@ function transformRenderFunction(renderPath, adapter) { } if (!alternatePath.isIfStatement() && alternatePath.node) { - if (alternatePath.isBlockStatement()) { - const alternateBodyPath = alternatePath.get('body'); + const alternateBodyPath = alternatePath.get('body'); + if (alternateBodyPath) { alternateBodyPath.map((statementPath) => { handleAlternate( statementPath.get('expression'), @@ -322,7 +322,7 @@ function handleConsequent(path, expressionPath, templateMap, renderScope, adapte // Remove only if the expression contains JSX expressionPath.remove(); } - } else handleAlternate(expressionPath, templateMap, adapter) + } } } @@ -377,4 +377,4 @@ function hasJSX(path) { } }); return exist; -} +} \ No newline at end of file From 96498df0642e414775bb370501250dbac1609954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E6=88=98?= Date: Wed, 24 Nov 2021 14:37:08 +0800 Subject: [PATCH 5/8] format space --- packages/jsx-compiler/src/modules/condition.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jsx-compiler/src/modules/condition.js b/packages/jsx-compiler/src/modules/condition.js index 8f071e64..807002c7 100644 --- a/packages/jsx-compiler/src/modules/condition.js +++ b/packages/jsx-compiler/src/modules/condition.js @@ -377,4 +377,4 @@ function hasJSX(path) { } }); return exist; -} \ No newline at end of file +} From 4bdb3e65349277c99037c597244616f57b3b567b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E6=88=98?= Date: Wed, 24 Nov 2021 14:38:47 +0800 Subject: [PATCH 6/8] format space --- packages/jsx-compiler/src/utils/isBaseComponent.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jsx-compiler/src/utils/isBaseComponent.js b/packages/jsx-compiler/src/utils/isBaseComponent.js index 179f3d6a..124bdf09 100644 --- a/packages/jsx-compiler/src/utils/isBaseComponent.js +++ b/packages/jsx-compiler/src/utils/isBaseComponent.js @@ -7,4 +7,4 @@ const baseComponents = require('../baseComponents'); 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; -}; \ No newline at end of file +}; From c48d7676b208fb70454448900dea705dc0d46d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E6=88=98?= Date: Wed, 24 Nov 2021 15:33:07 +0800 Subject: [PATCH 7/8] support native components event bind - update --- .../src/modules/__tests__/element.js | 1 + .../jsx-compiler/src/modules/components.js | 18 ++++++++++++++++-- packages/jsx-compiler/src/modules/element.js | 4 +--- .../jsx-compiler/src/utils/isBaseComponent.js | 10 ---------- 4 files changed, 18 insertions(+), 15 deletions(-) delete mode 100644 packages/jsx-compiler/src/utils/isBaseComponent.js diff --git a/packages/jsx-compiler/src/modules/__tests__/element.js b/packages/jsx-compiler/src/modules/__tests__/element.js index b8eb39d0..8c51319d 100644 --- a/packages/jsx-compiler/src/modules/__tests__/element.js +++ b/packages/jsx-compiler/src/modules/__tests__/element.js @@ -334,6 +334,7 @@ describe('Transform JSXElement', () => { /> `); ast.openingElement.name.isCustom = true; + ast.openingElement.name.isNative = true; _transform({ templateAST: ast }, wxAdapter); diff --git a/packages/jsx-compiler/src/modules/components.js b/packages/jsx-compiler/src/modules/components.js index 5087a363..f1b63955 100644 --- a/packages/jsx-compiler/src/modules/components.js +++ b/packages/jsx-compiler/src/modules/components.js @@ -35,6 +35,11 @@ function transformIdentifierComponentName(path, alias, dynamicValue, parsed, opt const aliasName = alias.name.replace(/@|\//g, '_'); const componentTag = alias.default ? aliasName : `${aliasName}-${alias.local.toLowerCase()}`; replaceComponentTagName(path, t.jsxIdentifier(componentTag)); + /** + * Judge whether the component is Custom Components. + * If it's Custom Component, the component should be defined in usingComponents property in .json, + * the tag's name of component should transform, and isCustomEl, isCustom should also be true. + */ node.isCustomEl = alias.isCustomEl; node.name.isCustom = true; @@ -153,9 +158,9 @@ function transformIdentifierComponentName(path, alias, dynamicValue, parsed, opt /** * Judge whether the component has native compiled component + * If it has, the property isNative should true, otherwise it would be false. */ - if (pkg && pkg.miniappConfig && pkg.miniappConfig[`main:${platform}`]) node.name.isNative = true; - else node.isNative = false; + node.name.isNative = !!getCustomComponentPath(pkg, platform) ? true : false; } } } @@ -475,3 +480,12 @@ function findParentsJSXListEl(path, parentList = []) { return parentList; } } + +function getCustomComponentPath(pkg, platform) { + /** + * Get the identifier in package.json -> miniappConfig + * In ali-miniApp, it will be 'main', otherwise it's `main:${platform}` + */ + const platformIdentifier = platform === 'ali' ? 'main' : `main:${platform}` + return pkg && pkg.miniappConfig && pkg.miniappConfig[platformIdentifier] || null +} diff --git a/packages/jsx-compiler/src/modules/element.js b/packages/jsx-compiler/src/modules/element.js index ce4c1698..aa61b0fe 100644 --- a/packages/jsx-compiler/src/modules/element.js +++ b/packages/jsx-compiler/src/modules/element.js @@ -6,14 +6,12 @@ const createJSXBinding = require('../utils/createJSXBinding'); const CodeError = require('../utils/CodeError'); const DynamicBinding = require('../utils/DynamicBinding'); const getCompiledComponents = require('../getCompiledComponents'); -const baseComponents = require('../baseComponents'); const replaceComponentTagName = require('../utils/replaceComponentTagName'); const { parseExpression } = require('../parser/index'); 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'); @@ -542,7 +540,7 @@ function transformTemplate( attr.name.name = attr.name.name.replace('on', 'bind').toLowerCase(); } }); - } else if (adapter.needTransformEvent && isBaseComponent(componentTagNode)) { + } else if (adapter.needTransformEvent && componentTagNode.isNative) { // Rax base component should add bind before onXXX // While events in custom component should not be changed node.attributes.forEach(attr => { diff --git a/packages/jsx-compiler/src/utils/isBaseComponent.js b/packages/jsx-compiler/src/utils/isBaseComponent.js deleted file mode 100644 index 124bdf09..00000000 --- a/packages/jsx-compiler/src/utils/isBaseComponent.js +++ /dev/null @@ -1,10 +0,0 @@ -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; -}; From 7ee1f167754283f54d116f6b99dd52cae5ec8e0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E6=88=98?= Date: Wed, 24 Nov 2021 16:35:06 +0800 Subject: [PATCH 8/8] support native components event bind - update - update --- packages/jsx-compiler/src/modules/components.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jsx-compiler/src/modules/components.js b/packages/jsx-compiler/src/modules/components.js index f1b63955..9146ea4b 100644 --- a/packages/jsx-compiler/src/modules/components.js +++ b/packages/jsx-compiler/src/modules/components.js @@ -160,7 +160,7 @@ function transformIdentifierComponentName(path, alias, dynamicValue, parsed, opt * Judge whether the component has native compiled component * If it has, the property isNative should true, otherwise it would be false. */ - node.name.isNative = !!getCustomComponentPath(pkg, platform) ? true : false; + node.name.isNative = !!getCustomComponentPath(pkg, platform); } } }