-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: bump jsx2mp-loader version * feat: generating source maps (#1433) * refactor: generate inline source map in npm file * feat: add source map for rax code in watch mode * fix: add default sourceFileName to avoid test failure * refactor: use inline source map to support alipay IDE * fix: fix lint error * chore: remove useless import * feat: miniapp optimize build mode (#1434) * feat: clean dist dir before generating * feat: uglify runtime in build mode * fix: lint error * perf: minify js/css in build mode * feat: minify Code in build mode and do dce to all rax code * test: update compile result test case * fix: lint error * refactor: extract output logic to a single file * fix: copy path * fix: path resolve error in usingComponents * fix: path error in script when using node modules * fix: child component render * fix: child component render * chore: remove useless code * feat: support logical expression with jsx (#1424) * feat: support logical expression with jsx * fix: nested logical expression * chore: optimize warning * feat: support useRef * feat: add disable copy npm mode * chore: remove useless code * chore: add refs.length is 0 condition * feat: support ref is string * fix: fragment missing * refactor: extract transformCode to output function * fix: lint error * test: update test case of generating correct js code * feat: support build differentiated platform (#1442) * chore: opitimize code * feat: disable copy npm (#1441) * feat: add disable copy npm mode * refactor: extract transformCode to output function * fix: lint error * test: update test case of generating correct js code * chore: opitimize code * chore: bump version
- Loading branch information
1 parent
9f9a7bf
commit e2617a4
Showing
36 changed files
with
785 additions
and
326 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const t = require('@babel/types'); | ||
const { _transformAttribute } = require('../attribute'); | ||
const { parseExpression } = require('../../parser'); | ||
const adapter = require('../../adapter').ali; | ||
const genCode = require('../../codegen/genCode'); | ||
|
||
describe('Transform JSX Attribute', () => { | ||
it('should transform attribute name is key', () => { | ||
const code = '<View key={1}>test</View>'; | ||
const ast = parseExpression(code); | ||
_transformAttribute(ast, code, adapter); | ||
expect(genCode(ast).code).toEqual('<View a:key={1}>test</View>'); | ||
}); | ||
it('should transform attribute name is className', () => { | ||
const code = '<View className="box">test</View>'; | ||
const ast = parseExpression(code); | ||
_transformAttribute(ast, code, adapter); | ||
expect(genCode(ast).code).toEqual('<View class="box">test</View>'); | ||
}); | ||
it("should collect attribute name is ref and parse it's value as a string", () => { | ||
const code = '<View ref={scrollViewRef}>test</View>'; | ||
const ast = parseExpression(code); | ||
const refs = _transformAttribute(ast, code, adapter); | ||
expect(genCode(ast).code).toEqual('<View ref="scrollViewRef">test</View>'); | ||
expect(refs).toEqual([t.stringLiteral('scrollViewRef')]); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const t = require('@babel/types'); | ||
const traverse = require('../utils/traverseNodePath'); | ||
const genExpression = require('../codegen/genExpression'); | ||
const CodeError = require('../utils/CodeError'); | ||
|
||
function transformAttribute(ast, code, adapter) { | ||
const refs = []; | ||
traverse(ast, { | ||
JSXAttribute(path) { | ||
const { node } = path; | ||
const attrName = node.name.name; | ||
switch (attrName) { | ||
case 'key': | ||
node.name.name = adapter.key; | ||
break; | ||
case 'className': | ||
node.name.name = adapter.className; | ||
break; | ||
case 'ref': | ||
if (t.isJSXExpressionContainer(node.value)) { | ||
node.value = t.stringLiteral(genExpression(node.value.expression)); | ||
} | ||
if (t.isStringLiteral(node.value)) { | ||
refs.push(node.value); | ||
} else { | ||
throw new CodeError(code, node, path.loc, "Ref's type must be string or jsxExpressionContainer"); | ||
} | ||
break; | ||
default: | ||
path.stop(); | ||
} | ||
} | ||
}); | ||
return refs; | ||
} | ||
|
||
module.exports = { | ||
parse(parsed, code, options) { | ||
parsed.refs = transformAttribute(parsed.templateAST, code, options.adapter); | ||
}, | ||
|
||
// For test cases. | ||
_transformAttribute: transformAttribute, | ||
}; |
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
Oops, something went wrong.