Skip to content

Commit

Permalink
feat(postcss-pxtransform): 支持样式重制类的代码在 RN 端编译时通过块注释剔除
Browse files Browse the repository at this point in the history
使用 /*postcss-pxtransform rn eject enable*/ 与 /*postcss-pxtransform rn eject disable*/
  • Loading branch information
Pines-Cheng committed Aug 23, 2018
1 parent d9f7746 commit 3aa5ebf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/postcss-pxtransform/README.md
Expand Up @@ -166,4 +166,16 @@ Set the minimum pixel value to replace.
### 文件
对于头部包含注释`/*postcss-pxtransform disable*/` 的文件,插件不予处理。

## 剔除
`/*postcss-pxtransform rn eject enable*/``/*postcss-pxtransform rn eject disable*/` 中间的代码,
在编译成 RN 端的样式的时候,会被删除。建议将 RN 不支持的但 H5 端又必不可少的样式放到这里面。如:样式重制相关的代码。
```css
/*postcss-pxtransform rn eject enable*/

.test {
color: black;
}

/*postcss-pxtransform rn eject disable*/
```

18 changes: 18 additions & 0 deletions packages/postcss-pxtransform/index.js
Expand Up @@ -80,6 +80,24 @@ module.exports = postcss.plugin('postcss-pxtransform', function (options) {
}
}

// delete code between comment in RN
if (options.platform === 'rn') {
css.walkComments(comment => {
if (comment.text === 'postcss-pxtransform rn eject enable') {
let next = comment.next()
while (next) {
console.log(next.text)
if (next.type === 'comment' && next.text === 'postcss-pxtransform rn eject disable') {
break
}
const temp = next.next()
next.remove()
next = temp
}
}
})
}

css.walkDecls(function (decl, i) {
// This should be the fastest test and will remove most declarations
if (decl.value.indexOf('px') === -1) return
Expand Down
5 changes: 2 additions & 3 deletions packages/taro-cli/src/rn.js
Expand Up @@ -650,6 +650,8 @@ function buildTemp () {
}
resolve()
})
}).catch(e => {
throw e
})
}

Expand Down Expand Up @@ -708,9 +710,6 @@ function watchFiles () {
processFiles(filePath)
})
.on('error', error => console.log(`Watcher error: ${error}`))
.on('raw', (event, path, details) => {
console.log('Raw event info:', event, path, details)
})
}

async function build ({watch}) {
Expand Down

0 comments on commit 3aa5ebf

Please sign in to comment.