Skip to content

Commit

Permalink
Merge branch 'master' of github.com:NervJS/taro
Browse files Browse the repository at this point in the history
  • Loading branch information
Pines-Cheng committed Mar 25, 2019
2 parents 5fb6fab + 3589621 commit 2448ae7
Show file tree
Hide file tree
Showing 25 changed files with 89 additions and 70 deletions.
5 changes: 4 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ const config = {
}
}
}
}
},
// 替换 JSX 中的属性名,参考:
// https://github.com/NervJS/taro/issues/2077
jsxAttributeNameReplace: {}
},
// H5 端专用配置
h5: {
Expand Down
18 changes: 10 additions & 8 deletions docs/event.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@ class Popper extends Component {

render () {
const name = 'test'
return <Button onClick={(e) => {
e.stopPropagation()
this.setState({
name
})
}}>
{this.state.name}
</Button>
return (
<Button onClick={(e) => {
e.stopPropagation()
this.setState({
name
})
}}>
{this.state.name}
</Button>
)
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/spec-for-taro.md
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ render () {
<MyComponent className='long body' foo='bar'>
<MyChild />
</MyComponent>
);
)
}

// good
Expand Down
2 changes: 1 addition & 1 deletion docs/taroize.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Taro 会将原始文件的生命周期钩子函数转换为 Taro 的生命周期
| onHide | componentDidHide |
| onReady | componentDidMount |
| onUnload | componentWillUnmount |
| onError | componentCatchError |
| onError | componentDidCatchError |
| App.onLaunch | componentWillMount |
| Component.created | componentWillMount |
| attached | componentDidMount |
Expand Down
8 changes: 4 additions & 4 deletions packages/taro-cli/__tests__/__snapshots__/rn.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let App = (_temp = _class = class App extends Component {
componentDidMount() {}
componentDidShow() {}
componentDidHide() {}
componentCatchError() {}
componentDidCatchError() {}
render() {
return <RootStack />;
}
Expand Down Expand Up @@ -79,7 +79,7 @@ let App = (_temp = _class = class App extends Component {
componentDidHide() {}
componentCatchError() {}
componentDidCatchError() {}
render() {
return <RootStack />;
Expand Down Expand Up @@ -161,7 +161,7 @@ let App = (_temp = _class = class App extends Component {
componentDidHide() {}
componentCatchError() {}
componentDidCatchError() {}
render() {
return <Provider store={store}>
Expand Down Expand Up @@ -217,7 +217,7 @@ let App = (_temp = _class = class App extends Component {
componentDidHide() {}
componentCatchError() {}
componentDidCatchError() {}
render() {
return <RootStack />;
Expand Down
6 changes: 3 additions & 3 deletions packages/taro-cli/__tests__/mocks/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class App extends Component {
componentDidHide () {}
componentCatchError () {}
componentDidCatchError () {}
render () {
return (
Expand Down Expand Up @@ -88,7 +88,7 @@ class App extends Component {
componentDidHide () {}
componentCatchError () {}
componentDidCatchError () {}
render () {
return (
Expand Down Expand Up @@ -133,7 +133,7 @@ class App extends Component {
componentDidHide () {}
componentCatchError () {}
componentDidCatchError () {}
render () {
return (
Expand Down
3 changes: 1 addition & 2 deletions packages/taro-cli/src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ const CONFIG = require('./config')

const appPath = process.cwd()


function build (args, buildConfig) {
const { type, watch } = buildConfig
const configDir = require(path.join(appPath, Util.PROJECT_CONFIG))(_.merge)
const outputPath = path.join(appPath, configDir.outputRoot || CONFIG.OUTPUT_DIR)
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath)
fs.ensureDirSync(outputPath)
} else {
if (type !== Util.BUILD_TYPES.H5) {
Util.emptyDirectory(outputPath)
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-cli/src/convertor.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Convertor {
if (fs.existsSync(this.convertRoot)) {
emptyDirectory(this.convertRoot, { excludes: ['node_modules'] })
} else {
fs.mkdirpSync(this.convertRoot)
fs.ensureDirSync(this.convertRoot)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/taro-cli/src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ exports.getRootPath = function () {
exports.getTaroPath = function () {
const taroPath = path.join(exports.homedir(), '.taro')
if (!fs.existsSync(taroPath)) {
fs.mkdirSync(taroPath)
fs.ensureDirSync(taroPath)
}
return taroPath
}
Expand Down
18 changes: 12 additions & 6 deletions packages/taro-cli/src/weapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,8 @@ function isFileToBeTaroComponent (code, sourcePath, outputPath) {
isNormal: true,
isTyped: Util.REG_TYPESCRIPT.test(sourcePath),
adapter: buildAdapter,
env: constantsReplaceList
env: constantsReplaceList,
jsxAttributeNameReplace: weappConf.jsxAttributeNameReplace
})
const { ast } = transformResult
let isTaroComponent = false
Expand Down Expand Up @@ -979,7 +980,8 @@ async function compileScriptFile (content, sourceFilePath, outputFilePath, adapt
isNormal: true,
isTyped: false,
adapter,
env: constantsReplaceList
env: constantsReplaceList,
jsxAttributeNameReplace: weappConf.jsxAttributeNameReplace
})
const res = parseAst(PARSE_AST_TYPE.NORMAL, transformResult.ast, [], sourceFilePath, outputFilePath)
return res.code
Expand Down Expand Up @@ -1102,7 +1104,8 @@ async function buildEntry () {
isApp: true,
isTyped: Util.REG_TYPESCRIPT.test(entryFilePath),
adapter: buildAdapter,
env: constantsReplaceList
env: constantsReplaceList,
jsxAttributeNameReplace: weappConf.jsxAttributeNameReplace
})
// app.js的template忽略
const res = parseAst(PARSE_AST_TYPE.ENTRY, transformResult.ast, [], entryFilePath, outputEntryFilePath)
Expand Down Expand Up @@ -1337,7 +1340,8 @@ async function buildSinglePage (page) {
isRoot: true,
isTyped: Util.REG_TYPESCRIPT.test(pageJs),
adapter: buildAdapter,
env: constantsReplaceList
env: constantsReplaceList,
jsxAttributeNameReplace: weappConf.jsxAttributeNameReplace
})
const pageDepComponents = transformResult.components
const compressTemplate = useCompileConf.compressTemplate
Expand Down Expand Up @@ -1774,7 +1778,8 @@ async function buildSingleComponent (componentObj, buildConfig = {}) {
isTyped: Util.REG_TYPESCRIPT.test(component),
isNormal: false,
adapter: buildAdapter,
env: constantsReplaceList
env: constantsReplaceList,
jsxAttributeNameReplace: weappConf.jsxAttributeNameReplace
})
const compressTemplate = useCompileConf.compressTemplate
const componentWXMLContent = (isProduction && compressTemplate) ? transformResult.compressedTemplate : transformResult.template
Expand Down Expand Up @@ -1926,7 +1931,8 @@ function compileDepScripts (scriptFiles) {
isNormal: true,
isTyped: Util.REG_TYPESCRIPT.test(item),
adapter: buildAdapter,
env: constantsReplaceList
env: constantsReplaceList,
jsxAttributeNameReplace: weappConf.jsxAttributeNameReplace
})
const ast = transformResult.ast
const res = parseAst(PARSE_AST_TYPE.NORMAL, ast, [], item, outputItem)
Expand Down
8 changes: 4 additions & 4 deletions packages/taro-cli/templates/default/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ module.exports = function (creater, params, helper, cb) {
}
const currentStyleExt = styleExtMap[css] || 'css'

fs.mkdirSync(projectPath)
fs.mkdirSync(sourceDir)
fs.mkdirSync(configDir)
fs.mkdirSync(path.join(sourceDir, 'pages'))
fs.ensureDirSync(projectPath)
fs.ensureDirSync(sourceDir)
fs.ensureDirSync(configDir)
fs.ensureDirSync(path.join(sourceDir, 'pages'))

creater.template(template, 'pkg', path.join(projectPath, 'package.json'), {
description,
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-cli/templates/default/indexhtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<meta name="format-detection" content="telephone=no,address=no">
<meta name="apple-mobile-web-app-status-bar-style" content="white">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" >
<title>Taro</title>
<title></title>
<script>
!function(x){function w(){var v,u,t,tes,s=x.document,r=s.documentElement,a=r.getBoundingClientRect().width;if(!v&&!u){var n=!!x.navigator.appVersion.match(/AppleWebKit.*Mobile.*/);v=x.devicePixelRatio;tes=x.devicePixelRatio;v=n?v:1,u=1/v}if(a>=640){r.style.fontSize="40px"}else{if(a<=320){r.style.fontSize="20px"}else{r.style.fontSize=a/320*20+"px"}}}x.addEventListener("resize",function(){w()});w()}(window);
</script>
Expand Down
8 changes: 4 additions & 4 deletions packages/taro-cli/templates/mobx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ module.exports = function (creater, params, helper, cb) {
}
const currentStyleExt = styleExtMap[css] || 'css'

fs.mkdirSync(projectPath)
fs.mkdirSync(sourceDir)
fs.mkdirSync(configDir)
fs.mkdirSync(path.join(sourceDir, 'pages'))
fs.ensureDirSync(projectPath)
fs.ensureDirSync(sourceDir)
fs.ensureDirSync(configDir)
fs.ensureDirSync(path.join(sourceDir, 'pages'))

creater.template(template, 'pkg', path.join(projectPath, 'package.json'), {
description,
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-cli/templates/mobx/indexhtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<meta name="format-detection" content="telephone=no,address=no">
<meta name="apple-mobile-web-app-status-bar-style" content="white">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" >
<title>Taro</title>
<title></title>
<script>
!function(x){function w(){var v,u,t,tes,s=x.document,r=s.documentElement,a=r.getBoundingClientRect().width;if(!v&&!u){var n=!!x.navigator.appVersion.match(/AppleWebKit.*Mobile.*/);v=x.devicePixelRatio;tes=x.devicePixelRatio;v=n?v:1,u=1/v}if(a>=640){r.style.fontSize="40px"}else{if(a<=320){r.style.fontSize="20px"}else{r.style.fontSize=a/320*20+"px"}}}x.addEventListener("resize",function(){w()});w()}(window);
</script>
Expand Down
2 changes: 0 additions & 2 deletions packages/taro-cli/templates/redux/appjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ class App extends Component {

componentDidHide () {}

componentCatchError () {}

componentDidCatchError () {}

// 在 App 类中的 render() 函数没有实际作用
Expand Down
16 changes: 8 additions & 8 deletions packages/taro-cli/templates/redux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ module.exports = function (creater, params, helper, cb) {
}
const currentStyleExt = styleExtMap[css] || 'css'

fs.mkdirSync(projectPath)
fs.mkdirSync(sourceDir)
fs.mkdirSync(configDir)
fs.mkdirSync(path.join(sourceDir, 'pages'))
fs.mkdirSync(constantsDir)
fs.mkdirSync(actionsDir)
fs.mkdirSync(reducersDir)
fs.mkdirSync(storeDir)
fs.ensureDirSync(projectPath)
fs.ensureDirSync(sourceDir)
fs.ensureDirSync(configDir)
fs.ensureDirSync(path.join(sourceDir, 'pages'))
fs.ensureDirSync(constantsDir)
fs.ensureDirSync(actionsDir)
fs.ensureDirSync(reducersDir)
fs.ensureDirSync(storeDir)

creater.template(template, 'pkg', path.join(projectPath, 'package.json'), {
description,
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-cli/templates/redux/indexhtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<meta name="format-detection" content="telephone=no,address=no">
<meta name="apple-mobile-web-app-status-bar-style" content="white">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" >
<title>Taro</title>
<title></title>
<script>
!function(x){function w(){var v,u,t,tes,s=x.document,r=s.documentElement,a=r.getBoundingClientRect().width;if(!v&&!u){var n=!!x.navigator.appVersion.match(/AppleWebKit.*Mobile.*/);v=x.devicePixelRatio;tes=x.devicePixelRatio;v=n?v:1,u=1/v}if(a>=640){r.style.fontSize="40px"}else{if(a<=320){r.style.fontSize="20px"}else{r.style.fontSize=a/320*20+"px"}}}x.addEventListener("resize",function(){w()});w()}(window);
</script>
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-cli/templates/wxcloud/client/indexhtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<meta name="format-detection" content="telephone=no,address=no">
<meta name="apple-mobile-web-app-status-bar-style" content="white">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" >
<title>Taro</title>
<title></title>
<script>
!function(x){function w(){var v,u,t,tes,s=x.document,r=s.documentElement,a=r.getBoundingClientRect().width;if(!v&&!u){var n=!!x.navigator.appVersion.match(/AppleWebKit.*Mobile.*/);v=x.devicePixelRatio;tes=x.devicePixelRatio;v=n?v:1,u=1/v}if(a>=640){r.style.fontSize="40px"}else{if(a<=320){r.style.fontSize="20px"}else{r.style.fontSize=a/320*20+"px"}}}x.addEventListener("resize",function(){w()});w()}(window);
</script>
Expand Down
12 changes: 6 additions & 6 deletions packages/taro-cli/templates/wxcloud/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ module.exports = function (creater, params, helper, cb) {
}
const currentStyleExt = styleExtMap[css] || 'css'

fs.mkdirSync(projectPath)
fs.mkdirSync(projectClientPath)
fs.mkdirSync(projectCloudPath)
fs.mkdirSync(sourceDir)
fs.mkdirSync(configDir)
fs.mkdirSync(path.join(sourceDir, 'pages'))
fs.ensureDirSync(projectPath)
fs.ensureDirSync(projectClientPath)
fs.ensureDirSync(projectCloudPath)
fs.ensureDirSync(sourceDir)
fs.ensureDirSync(configDir)
fs.ensureDirSync(path.join(sourceDir, 'pages'))

creater.template(template, path.join(clientDirName, 'pkg'), path.join(projectClientPath, 'package.json'), {
description,
Expand Down
12 changes: 12 additions & 0 deletions packages/taro-transformer-wx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,18 @@ export default function transform (options: Options): TransformResult {
},
JSXAttribute (path) {
const { name, value } = path.node

if (options.jsxAttributeNameReplace) {
for (const r in options.jsxAttributeNameReplace) {
if (options.jsxAttributeNameReplace.hasOwnProperty(r)) {
const element = options.jsxAttributeNameReplace[r]
if (t.isJSXIdentifier(name, { name: r })) {
path.node.name = t.jSXIdentifier(element)
}
}
}
}

if (!t.isJSXIdentifier(name) || value === null || t.isStringLiteral(value) || t.isJSXElement(value)) {
return
}
Expand Down
3 changes: 2 additions & 1 deletion packages/taro-transformer-wx/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export interface Options {
isTyped: boolean,
isNormal?: boolean,
env?: object,
adapter?: Adapters
adapter?: Adapters,
jsxAttributeNameReplace?: Object
}

export const transformOptions: Options = {} as any
Expand Down
6 changes: 4 additions & 2 deletions packages/taro-transformer-wx/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1440,8 +1440,10 @@ export class RenderParser {
JSXAttribute: !t.isIdentifier(indexParam) ? noop : (path: NodePath<t.JSXAttribute>) => {
const { value } = path.node
if (t.isJSXExpressionContainer(value) && t.isIdentifier(value.expression, { name: indexParam.name })) {
// tslint:disable-next-line:no-console
console.warn(codeFrameError(value.expression, '建议修改:使用循环的 index 变量作为 key 是一种反优化。参考:https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-array-index-key.md').message)
if (process.env.TERM_PROGRAM) { // 无法找到 cli 名称的工具(例如 idea/webstorm)显示这个报错可能会乱码
// tslint:disable-next-line:no-console
console.log(codeFrameError(value.expression, '建议修改:使用循环的 index 变量作为 key 是一种反优化。参考:https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-array-index-key.md').message)
}
}
},
JSXExpressionContainer: this.replaceIdWithTemplate(),
Expand Down
12 changes: 4 additions & 8 deletions packages/taro-webpack-runner/src/util/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,21 +375,17 @@ const getModule = ({
test: fontReg,
use: {
urlLoader: getUrlLoader([defaultFontUrlLoaderOption, {
options: {
name: `${staticDirectory}/fonts/[name].[ext]`,
...fontUrlLoaderOption
}
name: `${staticDirectory}/fonts/[name].[ext]`,
...fontUrlLoaderOption
}])
}
}
rule.image = {
test: imageReg,
use: {
urlLoader: getUrlLoader([defaultImageUrlLoaderOption, {
options: {
name: `${staticDirectory}/images/[name].[ext]`,
...imageUrlLoaderOption
}
name: `${staticDirectory}/images/[name].[ext]`,
...imageUrlLoaderOption
}])
}
}
Expand Down

0 comments on commit 2448ae7

Please sign in to comment.